0
votes

I am using Play framework's to convert between a case class and Json.

How can I extract the schema of the Json corresponding to the case class?

Edit: If the class is case class Foo(string:Option[String], int:Option[Int])

Schema should be (approximately):

{
 "$schema": "http://json-schema.org/draft-07/schema#",
 "$id": "http://example.com/product.schema.json",
 "title": "Foo",
 "type": "object",
 "properties": {
   "string": {
     "type": "string"
   },
   "int": {
     "type": "int"
   }
 },
 "required": [ ]
}
1
You cannot. JSON codecs are just functions and you cannot extract from function how it's made, you can only call it. You would have to construct schema in parallel to codecs keeping them in sync or use some structure that could be used to construct both codecs and schema. - Mateusz Kubuszok
Do you mean extract data from JSON corresponding to the case class by any chance? - Amit Singh
@TomerShetah added an example - Jus12

1 Answers

1
votes

Use scala-jsonschema for that and sponsor the author of this great library.

The library supports also spray-json, circe and some other JSON parsers for Scala.