-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support other formats than JSON #34
Comments
I would be in favour of the intermediate format. I think the other option would make addig another format quite a lot of work (almost as much as just creating creating a whole separate library to support that format). I half remember reading about a general Var type in dlang that can be used for any type (and also that JSONValue was derived from that). Such a type could be used for an intermediate format. Another question is whether an intermediate format should support specific data structures. For example json supports arrays/ranges and maps. That seems quite a sensible basic set. |
Is this what you are thinking of? https://github.com/adamdruppe/arsd/blob/master/jsvar.d or http://dlang.org/phobos/std_variant.html |
I meant the variant indeed: http://dlang.org/phobos/std_variant.html In many ways the ultimate specialization is to keep the types as they are, so we do need to make some kind of generalization. For example representing a struct/class with an associative array seems a natural generalization to me. |
That seems reasonable, and is the way JavaScript does it. But we should add as much metadata as possible to allow the format to do what it needs. std.variant could be a good fit if we use the Algebraic type (A closed discriminated union with a limited type universe). It would reduce some boilerplate code for the base types (and allow for using switch on the type using typeid?).
|
After our discussion about how similar jsonizer and painlessjson are, I got interested in this idea too. I've been messing around with an experiment that attempts work directly with whatever data it is given.
Support for data types (json, xml, yaml, ect.) could be provided as plugins that implement the above functions for a given data type. For example, a plugin based on std.json might provide:
I haven't gotten very far but I thought I'd throw the concept out there. |
Actually, now that I describe that it sounds needlessly complicated. Maybe just using |
What you described is the strategy pattern I talked about in the first comment. It made me think and I realized that there are goals that are at conflict: Simple and unified interface vs CTFE. We might be able to satisfy both goals. The current state of painlessJSON is that we can generate code that is tailored for a specific type. We have the possibility of generating code which is as fast as if it was handwritten. The intermediate format as propsed removes some information from the compile time and forces it into run time (I haven't tested this so please prove me wrong). This gives the implementer of the format flexibility and ease of use:
The strategy pattern instead keeps information at compile time which enables optimizations, but it forces a hierarchical structure and a coding style. painlessjson/source/painlessjson/painlessjson.d Lines 261 to 307 in 4de0fca
Extraction of nested types from the intermediate format has to be done somehow too. I would like to give total freedom, while also offering an intermediate format walker that would enable you to do the implementation as if we were using the strategy pattern. This could be done as a mixin or as a function/class that takes a format-class with the right methods as a template parameter. |
To add support for other formats we will have to separate the conversion code from the JSON-specific things. I see two potentially viable ways of doing it: An intermediate format or the strategy pattern.
The intermediate format would be good because the converter code would have a common target and the format code could do whatever it want as long as it provides a valid intermediate format object. The problem would be coming up with the format and avoiding creating boilerplate code.
The strategy pattern would be replacing every call to the JSON object and instead call to an object that knows how to extract/insert data from/to the format. This will probably not be as flexible as the intermediate format, but would be easier to implement.
The text was updated successfully, but these errors were encountered: