Open francescoagati opened 5 years ago
What is the issue here?
I want generale a schema for the JSON generated.
Isn't X
the schema? Or you mean you want to export X
as JSON schema?
I wanted to generate a JSON schema. But Can be right also only have a new typedef forse the JSON generated
I don't understand the second part. Isn't X
already the typedef?
Imagine this enum
enum Color {
Rgb(a:Int, b:Int, c:Int);
Hsv(hsv:{ hue:Float, saturation:Float, value:Float });//notice the single argument with name equal to the constructor
Hsl(value:{ hue:Float, saturation:Float, lightness:Float });
White;//no constructor
}
i would like to have this typedef
{ Rgb: { "a": Int, "b": Int, "c": Int}}
{ Hsv: { "hue": Int, "saturation": Int, "value": Int }}
{ Hsl: { "value: { "hue": Int, "saturation": Int, "lightness": Int } }}
in this mode i can generate also the schema for example for typescript or jsonschema
Ah, ok ... I understand now. Well, this could be interesting, in particular because we now allow the type to vary depending on constructor :D
yes, for example using a generic build for generate the intermediate typedef for the json.
this can be related https://github.com/HaxeFoundation/haxe/issues/4630
@francescoagati your linked issue is probably not related. Because tink_json has its own way to serialize haxe enums which is independent of the enum's runtime representation.
@kevinresol yes i know. now i am working on a library for generate typefed compatible with tink_json for the generation of the enum. when i have finish i will publish this on github
we now allow the type to vary depending on constructor :D
I have strong belief in TypeScript that it can handle this with something like:
"White" | {"Rgb": {a:number, b:number, c:number}}
But I don't think it worth the trouble creating Haxe typedefs (or is it even possible to create a useful one?)
Yeah, in Haxe this will probably be super awkward. What might be interesting is the ability to generate json schema, although that will also hit a wall when it comes to custom parsers/serializers and such.
for example jhttps://lib.haxe.org/p/json2object/ json2Object has a JsonSchemaWriter for generate a jsonschema but for use it with typescript i need to do two steps for convert the jsonschema in typescript. i would like to use a intermediate typedef because in this mode i can expose this typed in typescript using hxtsdgen
What's the problem with json-schema-to-typescript?
yes is only for haven't a two steps passage
So I just tried JSON Schema and I think it is expressive enough to describe tink_json's output.
enum Color {
White;
Hsl(value:{ hue:Float, saturation:Float, lightness:Float });
Hsv(hsv:{ hue:Float, saturation:Float, value:Float });
}
Schema:
{
"oneOf": [
{ "type": "string", "const": "White" },
{
"type": "object",
"required": ["Hsl"],
"properties": {
"Hsl": {
"type": "object",
"required": ["value"],
"properties": {
"value": {
"type": "object",
"required": ["hue", "lightness", "saturation"],
"properties": {
"hue": {
"type": "number"
},
"lightness": {
"type": "number"
},
"saturation": {
"type": "number"
}
}
}
}
}
}
},
{
"type": "object",
"required": ["Hsv"],
"properties": {
"Hsv": {
"type": "object",
"required": ["hue", "saturation", "value"],
"properties": {
"hue": {
"type": "number"
},
"saturation": {
"type": "number"
},
"value": {
"type": "number"
}
}
}
}
}
]
}
Examples:
"White"
{"Hsv":{"hue":1,"saturation":2,"value":3}}
{"Hsl":{"value":{"hue":1,"lightness":3,"saturation":2}}}
Json2Object support json schema
I made a branch with initial support of json schema: https://github.com/haxetink/tink_json/tree/schema
but further work is still needed to support custom rules (@:json
/@:jsonStringify
/@:jsonParse
)
hi, in a case like this can be possible have the typedef intermedie generated for the json definition?