0

I have a json-schema (https://json-schema.org) with recursive fields, and I would like to programmatically parse json that adheres to the schema in Scala.

One option is to use Argus (https://github.com/aishfenton/Argus), but the only issue is that it uses Scala macros, so a solution that uses this library isn't supported by IntelliJ.

What's the recommended way to perform a task like this in Scala, preferably something that plays well with IntelliJ?

3 Answers3

0

Have you look at https://github.com/circe/circe , it is pretty good to parse Json with typed format.

Wonay
  • 1,160
  • 13
  • 35
0

I don't know what you mean with recursive fields. But there's lots of different libraries for parsing json. You could use lift-json
https://github.com/lift/framework/tree/master/core/json

Which seems popular, at least from what I've seen here on Stackoverflow. But I personally am very comfortable with and prefer play.json
https://www.playframework.com/documentation/2.6.x/ScalaJson#Json
(Also, I use IntelliJ and work in the Play-framework)

If you really don't want to use any special libraries, someone tried to do that here How to parse JSON in Scala using standard Scala classes?

GamingFelix
  • 239
  • 2
  • 10
0

Circe is a great library for working with JSON. The following example uses semi automatic decoding. Circe also has guides for automatic decoding and for using custom codecs.

import io.circe.Decoder
import io.circe.parser.decode
import io.circe.generic.semiauto.deriveDecoder

object Example {

  case class MyClass(name: String, code: Int, sub: MySubClass)
  case class MySubClass(value: Int)
  implicit val myClassDecoder:    Decoder[MyClass]    = deriveDecoder
  implicit val mySubClassDecoder: Decoder[MySubClass] = deriveDecoder

  def main(args: Array[String]): Unit = {
    val input = """{"name": "Bob", "code": 200, "sub": {"value": 42}}"""
    println(decode[MyClass](input).fold(_ => "parse failed", _.toString))
  }

}
codenoodle
  • 982
  • 6
  • 19