Typesafe Dynamic Variable

Hi

In rust I was using rust-json json::JsonValue - Rust, and I’ve found an interesting style of programming.
Together with rusts Result and Option types, its possible to work with variables that will have their type known only at runtime, but still being confident that the code will not break.

Basically, when you parse a JSON string you get a Value type, which is stringifyable, plus indexable, and itirable, plus it has methods to make it to an int, float, bool where all the pluses returns options, that must be unwrapped (provide a default value, or default action, or explicitly crashed) in case the value was not indexable, or the key doesn’t exists, or couldn’t be parsed.

I am trying to use haxe to create libraries that I could use from both PHP and JavaScript, the caller must be able to pass over free JavaScript objects and PHP arrays, I do not want to use Dynamic or Any, because then I am losing all type safety (and inference), and I would be more then happy to be able to verify the input I am receiving at runtime (something that haxe type safety couldn’t assure, if the code is used from outside).

I could just go ahead and write such a library, but I would like to hear earlier from experienced haxe devs, what is already out there, what is their opinion on this issue, and how such a thing should be implemented.

@nadako made a pretty similar library here: GitHub - nadako/hxjsonast: Parse JSON into position-aware AST with Haxe!

Excerpt from hxjsonast/Json.hx at master · nadako/hxjsonast · GitHub :

enum JsonValue {
    /**
        Quoted JSON string.
    **/
    JString(s:String);

    /**
        Number as defined in the JSON spec.
        Represented as `String`, because JSON spec doesn't specify minimum and maximum values,
        so it's up to the user to parse this string to a number.
    **/
    JNumber(s:String);

    /**
        JSON object.
        The fields array is ordered as defined in the JSON file.
    **/
    JObject(fields:Array<JObjectField>);

    /**
        Array of JSON values.
    **/
    JArray(values:Array<Json>);

    /**
        Boolean value.
    **/
    JBool(b:Bool);

    /**
        Null value.
    **/
    JNull;
}

That library looks very interesting for parsing. But I don’t get how generating gson with it works. Are you supposed to pass the value positions when generating json? How can that be an easy way of doing it?

perhaps GitHub - haxetink/tink_json: Macro powered JSON.?

@danielo515 Hxjsonast is primarily for parsing JSON at runtime while providing some semblance of type information, it is not ideal for generating new json. Instead look into using haxe.Json.stringify for JSON generation. Encoding JSON - Haxe - The Cross-platform Toolkit.
By passing -D haxeJson you can then encode maps as well making it verify easy to do dynamic runtime JSON generation.
Implementation details (Standard Library - Json) - Haxe - The Cross-platform Toolkit

@serjek tink_json won’t work since they’re is dealing with runtime parsing. Tink JSON is quite powerful though and has a pretty robust API, so if you have a defined structure to your JSON it’s worth checking out.

tink_json does come with a dynamic parser as described

You just need to instruct the parser to parse into the Value type e.g.

tink.Json.parse(("{}":Value))

I read the api documentation and indeed I get to the conclusion that it has a nice api for type safe runtime parsing.
However, the Readme also claims that generating json is also very easy, and reading the api it looks to me like a tedious and error prone process, and that is what I am asking about.

Why the way, what is the main difference between the two libraries? Why would you choose one over the other?