andyglow / scala-jsonschema

Scala JSON Schema
Other
123 stars 38 forks source link

Question: Get Json schema from case class #350

Closed agamjainhpe closed 7 months ago

agamjainhpe commented 7 months ago

Hi,

I have a use case in which I want to create a json-schema for case classes I tried running the below code val personSchema: json.Schema[Person] = Json.schema[Person]

the output it's giving is object(name: string: ReadWrite /R, age: integer: ReadWrite /R, address: string: ReadWrite /R,)

How do i get string something like mentioned in the Readme?

agamjainhpe commented 7 months ago

@andyglow

andyglow commented 7 months ago

hello, @agamjainhpe, sorry for the late reply.

Right, what you get from Json.schema macro is an instance of Schema[T] You can use it as is, it has all the info you needed, but is not so much helpful if you need a json representation of the schema. For that there is an abstraction called As* as well as Version As* is a bridge between the schema and it's representation in the json world that your app support. Basically there is a bunch of adapters for different json libraries: Circe, Play, Spray, etc..

So, please check tests for the details.

There is also AsValue that allows schema representation in internal json language. Which lets you do something like this:

println(JsonFormatter.format(AsValue.schema(schema)))`