tink_json icon indicating copy to clipboard operation
tink_json copied to clipboard

intermediate typedef

Open francescoagati opened this issue 6 years ago • 19 comments

hi, in a case like this can be possible have the typedef intermedie generated for the json definition?

enum A {
	A1(b:B);
}

enum B {
	B1(c:C);
}

enum C {
	C1(s:String);
}

typedef X = {
	a:A
}

 class Main {


	static function main() {
		var x:X = {
			a: A1(B1(C1("test")))
		};
		var s = tink.Json.stringify(x);
		trace(s);
		var x2:X = tink.Json.parse(s);
		var rt = switch (x2.a) {
			case A1(B1(C1(s))): s;
		};
		trace(rt);
	}
}


francescoagati avatar Feb 05 '19 01:02 francescoagati

What is the issue here?

kevinresol avatar Feb 05 '19 05:02 kevinresol

I want generale a schema for the JSON generated.

francescoagati avatar Feb 05 '19 07:02 francescoagati

Isn't X the schema? Or you mean you want to export X as JSON schema?

back2dos avatar Feb 05 '19 08:02 back2dos

I wanted to generate a JSON schema. But Can be right also only have a new typedef forse the JSON generated

francescoagati avatar Feb 05 '19 08:02 francescoagati

I don't understand the second part. Isn't X already the typedef?

back2dos avatar Feb 05 '19 08:02 back2dos

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

francescoagati avatar Feb 05 '19 09:02 francescoagati

Ah, ok ... I understand now. Well, this could be interesting, in particular because we now allow the type to vary depending on constructor :D

back2dos avatar Feb 05 '19 10:02 back2dos

yes, for example using a generic build for generate the intermediate typedef for the json.

francescoagati avatar Feb 05 '19 10:02 francescoagati

this can be related https://github.com/HaxeFoundation/haxe/issues/4630

francescoagati avatar Feb 10 '19 20:02 francescoagati

@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 avatar Mar 26 '19 16:03 kevinresol

@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

francescoagati avatar Mar 27 '19 08:03 francescoagati

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?)

kevinresol avatar May 28 '20 03:05 kevinresol

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.

back2dos avatar May 28 '20 10:05 back2dos

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

francescoagati avatar May 28 '20 11:05 francescoagati

What's the problem with json-schema-to-typescript?

back2dos avatar May 28 '20 11:05 back2dos

yes is only for haven't a two steps passage

francescoagati avatar May 28 '20 12:05 francescoagati

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}}}

kevinresol avatar Feb 22 '21 03:02 kevinresol

Json2Object support json schema

francescoagati avatar Feb 22 '21 09:02 francescoagati

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)

kevinresol avatar Feb 22 '21 10:02 kevinresol