hjson / hjson-js

Hjson for JavaScript
https://hjson.github.io/
MIT License
416 stars 49 forks source link

Allow serializing DSF types to JSON #24

Closed ehmicky closed 7 years ago

ehmicky commented 7 years ago

This is a feature suggestion.

At the moment, dsf.parse() is only called on quoteless strings. Should it be fired on any value of any type instead?

The reason is: when DSF types are serialized to JSON, they will be converted to any type, e.g. dsf.math will convert JavaScript number Infinity to JSON string "Inf". JSON does not allow quoteless strings, so those values cannot be parsed back to JavaScript by dsf.parse().

For example example.hjson:

{
  a: Inf
}

would be converted by JSON.stringify(HJSON.parse(fileContent)) (not using dsf.math() here) to:

{
  "a": "Inf"
}

which would be parsed by HJSON.parse(fileContent) as:

{
  a: "Inf"
}

In a nutshell, I think allowing dsf.parse() to be called on any type would allow custom DSF types to be serializable to JSON without information/type loss. It could also create ambiguity, e.g. the parser would assume that JSON "Inf" is JavaScript Infinity, where it could also be a JavaScript string "Inf". There might also be performance penalties. But, providing it's on an optional basis, maybe the benefits would outweigh the drawbacks. At the moment, I cannot really find any mainstream project that allow custom types to be serialized to standard JSON without losing type information.

laktak commented 7 years ago

Thanks for the input but this sounds like a problem of JSON, not Hjson. If JSON loses the type information we can hardly get it back by guessing it in Hjson.

I see that this would be a nice workaround for your issue but it has too many drawbacks to implement it here.

If you simply want a workaround you could just walk your object tree yourself and reuse DSF from Hjson to parse any string values:

// init
var runDsf = dsf.loadDsf(dsfDef, "parse");
// parse:
var dsfValue = runDsf(value);
ehmicky commented 7 years ago

Thanks for looking this up.

I don't think that's a problem with JSON. JSON was designed as a portable and minimalist data-interchange format, i.e. to be simple and have few types. But this does not mean it cannot be augmented with type information, while still keeping JSON syntax. Providing both the decoder and the encoder agree on some way to decode/encode type information, it is possible. An example of a high profile project that does exactly this is transit-format.

The outcome is that Hjson could be serialized to and parsed from JSON without losing DSL type information. This would allow Hjson to use DSL in a similar way YAML uses tags.

If this still does not convince you, you can close this issue, I understand if you think this might not fit in Hjson. :smiley:

laktak commented 7 years ago

I'm sorry but this not the purpose of Hjson. You are welcome to fork or reuse the DSF parts though.