curiosity-ai / h5

🚀 The next generation C# to JavaScript compiler
https://github.com/curiosity-ai/h5
Apache License 2.0
210 stars 30 forks source link

H5 does not respect CamelCasePropertyNamesContractResolver #81

Closed ghost closed 1 year ago

ghost commented 1 year ago

Bridge applies camelcase but not H5.

I do not know the reason yet. So just created it here to track the problem.

ghost commented 1 year ago

It looks like here it does not use camelCase variable to set key.

if (nometa) {
    if (obj.toJSON) {
        raw = obj.toJSON();
    } else {
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                raw[key] = Newtonsoft.Json.JsonConvert.SerializeObject(obj[key], formatting, settings, true);
            }
        }
    }
}

Using raw[key.charAt(0).toLowerCase() + key.substr(1)] for tests generates JSON with camelCase property name.

Strange thing that it works in Bridge. I have tested it and it says: nometa - false.

So in H5 - nometa is true, in Bridge is is false. Obviously above source code does not validate camelCase. So it has bug.

But it is not clear anything yet about H5.getMetadata(type).

ghost commented 1 year ago

In H5 t.$getMetadata is undefined because it is Object, but in Bridge it is "$$fullname": "$AnonymousType$4".

ghost commented 1 year ago

If H5 does not emit anonymous types metadata and type is always object then possible fix probably should look like this:

if (obj.toJSON) {
    raw = obj.toJSON();
} else {
    var camelCase = settings && H5.is(settings.ContractResolver, Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver);

    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            var name = camelCase ? key.charAt(0).toLowerCase() + key.substr(1) : key;
            raw[name] = Newtonsoft.Json.JsonConvert.SerializeObject(obj[key], formatting, settings, true);
        }
    }
}
ghost commented 1 year ago

In H5 t.$getMetadata is undefined because it is Object, but in Bridge it is "$$fullname": "$AnonymousType$4".

I think this one is related to default behaviour set to [Rules(AnonymousType = AnonymousTypeRule.Plain)] which is not so in Bridge.

theolivenbaum commented 1 year ago

@hardhub changing the rule to the Bridge default fixed the issue for you?

ghost commented 1 year ago

@hardhub changing the rule to the Bridge default fixed the issue for you?

It is workaround to emulate Bridge behaviour. But in general it should have the fix:

image

I have attached full version from my fork: JsonConvert.zip

theolivenbaum commented 1 year ago

Thanks @hardhub, merged the change back to the json package now.