jiro4989 / nimjson

nimjson generates nim object definitions from json documents.
https://jiro4989.github.io/nimjson/nimjson.html
MIT License
52 stars 5 forks source link

support json schema #30

Closed enthus1ast closed 1 year ago

enthus1ast commented 1 year ago

Hey @jiro4989

this is a great tool already, it would be even better if it would also eat json schemas https://json-schema.org/ Any chance you could implement this? :)

enthus1ast commented 1 year ago

An example what this could buy is something like this. These days applications often describe their protocol in json schema, this is a file where they describe all valid json elems with their type and description etc:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://nats.io/schemas/jetstream/advisory/v1/nak.json",
  "description": "Advisory published when a message was naked using a AckNak acknowledgement",
  "title": "io.nats.jetstream.advisory.v1.nak",
  "type": "object",
  "required": [
    "type",
    "id",
    "timestamp",
    "stream",
    "consumer",
    "consumer_seq",
    "stream_seq",
    "deliveries"
  ],
  "additionalProperties": false,
  "properties": {
    "type": {
      "type": "string",
      "const": "io.nats.jetstream.advisory.v1.nak"
    },
    "id": {
      "type": "string",
      "description": "Unique correlation ID for this event"
    },
    "timestamp": {
      "type": "string",
      "description": "The time this event was created in RFC3339 format"
    },
    "stream": {
      "type": "string",
      "description": "The name of the stream where the message is stored"
    },
    "consumer": {
      "type": "string",
      "description": "The name of the consumer where the message was naked"
    },
    "consumer_seq": {
      "type": "string",
      "minimum": 1,
      "description": "The sequence of the message in the consumer that was naked"
    },
    "stream_seq": {
      "type": "string",
      "minimum": 1,
      "description": "The sequence of the message in the stream that was naked"
    },
    "deliveries": {
      "type": "integer",
      "minimum": 1,
      "description": "The number of deliveries that were attempted"
    },
    "domain": {
      "type": "string",
      "minimum": 1,
      "description": "The domain of the JetStreamServer"
    }
  }
}
jiro4989 commented 1 year ago

@enthus1ast Thanks. I didn't know json schemas. Json schemas looks good to me. I will add it to my schedule of new features😄

pietroppeter commented 1 year ago

I am also interested, I think support for jsonschema would be great! Non required stuff could be wrapped in an Optional and we could use variant types when the schema allows for alternatives.

Note that there is already @PMunch's https://github.com/PMunch/jsonschema but that has a different approach to jsonschema (all types are distinct JsonNode).

One possible use case of this would be to provide vega-lite functionalities for nimib: all the specs there are published as jsonschema and the python port altair is created taking advantage of those (using some manual meta-programming for python). I think Nim could shine in doing that work (this has been a "secret" side project of mine, called deneb, for obvious reasons). Using this library and jsony for serialization could be a way to go forward.

edit: a short article on using vegalite in nimib: https://pietroppeter.github.io/nblog/drafts/vegalite_example.html

PMunch commented 1 year ago

My jsonschema is actually built around typescript definitions and is being used in NimLSP. The rationale for everything being distinct JsonNode was simply to avoid copying data from potentially large data structures. The way it works is essentially just to verify the structures and types, and then cast it as a distinct with only the specified fields as accessible through getters. This makes the whole thing a bit safer to use, while still not having to reserialize the data.

pietroppeter commented 1 year ago

My jsonschema is actually built around typescript definitions and is being used in NimLSP. ...

Ah, did not know about this use case. Make sense!

jiro4989 commented 1 year ago

@enthus1ast nimjson supported JSON Schema. (v3.0.0)

https://github.com/jiro4989/nimjson/releases

However, it is probably not perfectly supported. If there are any deficiencies, please create the issues.