Last active
April 8, 2022 04:03
-
-
Save matfournier/cea92260d15ab68cebcd1902b12731b7 to your computer and use it in GitHub Desktop.
json parsing tutorial scala setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.effect.{IO, IOApp} | |
import derevo.circe.{decoder, encoder} | |
import derevo.derive | |
import io.circe.syntax._ | |
import io.circe.parser._ | |
import cats.effect.implicits._ | |
import cats.implicits._ | |
import io.circe.Json | |
import io.circe.Json.JNumber | |
import io.circe.optics.JsonPath._ | |
@derive (encoder, decoder) | |
case class User(age: Int, name: String) | |
@derive (encoder, decoder) | |
case class Wrapper(xs: List[User]) | |
//object Blah extends IOApp.Simple { | |
// override def run: IO[Unit] = { | |
// val m = User(1794, "Mat").asJson.noSpaces | |
// for { | |
// m2 <- parse(m).liftTo[IO] | |
// revMat <- someStuff(m2).liftTo[IO] | |
// revMatC <- revMat.as[User].liftTo[IO] | |
// _ <- IO.println(s"m2: $m2 revMat: ${revMat.noSpaces} revMatC: $revMatC") | |
// } yield() | |
// } | |
// | |
// def someStuff(json: Json): Either[Throwable, Json] = { | |
// json.hcursor.downField("name").withFocus(j => j.withNumber(s => JNumber(s))).top.toRight(new Throwable("borp")) | |
// } | |
//} | |
object WrappedBlah extends IOApp.Simple { | |
val json: String = Wrapper(List( | |
User(18, "mat"), | |
User(17, "joe"), | |
User(12, "max"), | |
User(11, "max2"), | |
User(10, "max3"), | |
User(9, "max4"), | |
)).asJson.noSpaces | |
val parsed: Json = parse(json).getOrElse(throw new Exception("hack")) | |
override def run: IO[Unit] = { | |
// extract down N fields | |
IO.println(parsed.hcursor.downField("xs").downN(2).downField("age").as[Int]) | |
val elements = parsed.hcursor.downField("xs").focus.flatMap(_.asArray).getOrElse(Vector.empty) | |
val ages = elements.flatMap(js => js.hcursor.get[Int]("age").toOption) | |
IO.println(ages) | |
// easier -> | |
val ages2: List[Int] = root.xs.each.age.int.getAll(parsed) | |
IO.println(ages2) | |
} | |
} | |
object tt extends App { | |
trait Thing { | |
implicit val t: Long = System.currentTimeMillis() | |
} | |
case class One(s: String) extends Thing | |
case class Two(s: String) extends Thing | |
val one = One("one") | |
Thread.sleep(5000) | |
val two = Two("two") | |
println(s"one: ${one.t}, two: ${two.t}") | |
println(s"${one.t == two.t}") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ThisBuild / scalaVersion := "2.13.8" | |
ThisBuild / version := "0.1.0" | |
ThisBuild / organization := "com.mf" | |
ThisBuild / organizationName := "mf" | |
lazy val root = (project in file(".")) | |
.settings( | |
name := "scratch" | |
) | |
scalacOptions ++= Seq( | |
"-Ymacro-annotations", | |
"-language:higherKinds", | |
"-language:existentials" | |
) | |
val circeVersion = "0.14.1" | |
libraryDependencies ++= Seq( | |
"io.circe" %% "circe-core", | |
"io.circe" %% "circe-generic", | |
"io.circe" %% "circe-parser", | |
"io.circe" %% "circe-optics" | |
).map(_ % circeVersion) | |
libraryDependencies += "org.typelevel" %% "cats-core" % "2.3.0" | |
libraryDependencies += "org.typelevel" %% "cats-effect" % "3.3.9" | |
libraryDependencies += "io.estatico" %% "newtype" % "0.4.4" | |
libraryDependencies += "tf.tofu" %% "derevo-circe" % "0.13.0" | |
libraryDependencies += "tf.tofu" %% "derevo-cats" % "0.13.0" | |
// available for 2.12, 2.13, 3.0 | |
libraryDependencies += "co.fs2" %% "fs2-core" % "3.2.7" | |
// optional I/O library | |
libraryDependencies += "co.fs2" %% "fs2-io" % "3.2.7" | |
libraryDependencies ++= Seq( | |
"dev.optics" %% "monocle-core" % "3.1.0", | |
"dev.optics" %% "monocle-macro" % "3.1.0" | |
) | |
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.11" % "test" | |
addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.1") | |
addCompilerPlugin("org.typelevel" % "kind-projector" % "0.13.2" cross CrossVersion.full) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment