Closed ehmicky closed 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);
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:
I'm sorry but this not the purpose of Hjson. You are welcome to fork or reuse the DSF parts though.
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 numberInfinity
to JSON string"Inf"
. JSON does not allow quoteless strings, so those values cannot be parsed back to JavaScript bydsf.parse()
.For example
example.hjson
:would be converted by
JSON.stringify(HJSON.parse(fileContent))
(not usingdsf.math()
here) to:which would be parsed by
HJSON.parse(fileContent)
as: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 JavaScriptInfinity
, 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.