Fable: Util.ofJson in fable-core fails on JSON inputs containing null

Created on 28 Jul 2016  路  6Comments  路  Source: fable-compiler/Fable

Util.ofJson doesn't work when input contains a null value. This JS will reproduce:

var fableCore = require('fable-core');

var input = '{"nullTest": null}';
var output = fableCore.Util.ofJson(input);

TypeError: Cannot read property '__type' of null

Looks like easy fix. Last line below (262 of fable-core.js) is missing a null-check before the __type test:

Util.ofJson = function ofJson(json) {
    return JSON.parse(json, function (k, v) {
        if ((typeof v === "undefined" ? "undefined" : _typeof(v)) == "object" && v.__type) {
bug

All 6 comments

typeof null (as well as _typeof(null) here) returns "object" in JS instead of something more sensible. This great JS feature strikes again! 馃槃

Thanks a lot for providing both the issue report and the solution! Yeah, JS equality and "typing" rules can be really funny :wink: I've fixed this for master but it won't be released until v0.5.0. If you need it now I can also release an amend to the current fable-core.

Thanks for the fix! No need to amend fable-core -- I already made the change in my local copy, so I can continue development for now. Looking forward to v0.5.0!

Also, I noticed a similar issue in the toJson function. Not sure if still a problem in master.

Great! I'll close the issue then :+1:

Thanks for pointing out about toJson, I fixed it too. There'll be several changes with JSON serialization in v0.5.0 to make it easier to interact with Json.NET on the server side and also to allow serialization of maps, sets and list. I hope you like them!

Good to hear! Will the serialization format be changing at all in the new version? I just finished getting my custom Json.NET serializer set up so that it reads/writes the format used currently by Fable (including __type annotations). Pretty ugly code but it seems to do the job for me for now! Hoping I don't have to rework too much after upgrading.

Either way, I'm definitely looking forward to being able to serialize the other collection types. For now I've been diligent about only using arrays in types that get sent to and from the server. Will be nice to be able to use the other types too now!

The main change is that, instead of __type, $type will be used to add the full type name, the same way Json.NET does when you use the TypeNameHandling.All setting. So hopefully you should be able adapt your converter easily (it'll probably still be necessary at least for union types for which Json.NET is not adding the type info even when using TypeNameHandling.All). Fable still doesn't use the same format in $type as Json.NET (generic arguments and assembly name are missing) but it can read type info from Json.NET anyway (just by stripping the unneeded parts). More info in the updated docs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SCullman picture SCullman  路  49Comments

dbrattli picture dbrattli  路  54Comments

alfonsogarciacaro picture alfonsogarciacaro  路  37Comments

alfonsogarciacaro picture alfonsogarciacaro  路  30Comments

alfonsogarciacaro picture alfonsogarciacaro  路  26Comments