Repository navigation
Replies: 2 comments 1 reply
Is the Options here for something JSON5 support? like: |
|
Looking at this again. I think we are missing another object in this chain: structured data -> serialize -> policy -> JSONFormatter -> output iopipe We want to have the logic of outputting JSON[5] be separate from the formatting. Note that the parser does not care at all about the formatting of the data (spacing). So that object is missing in the parsing chain. Going to explore something like this: structured data -> serialize -> policy -> JSONFormatter -> JSONOutputter -> output iopipe To the policy, the JSONFormatter will encompass all the functionality, but the JSONOutputter will be more of a "low level" type which just outputs valid JSON[5]. So validation happens at the outputter level, and formatting happens at the formatter level. Will work on this. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Thinking more about serialization, based on discussion in #64
We currently have the following model for deserialization:
input iopipe -> Tokenizer -> deserialize -> policy -> structured data
The policy uses the tokenizer as a tool to deal at the token level. The Tokenizer enforces the input validity, and provides mechanisms (through UFCS functions and other things) to help transform the tokens into data (such as strings and numbers which might have escapes, etc.).
Now, what we have for serialization currently is a less complex system:
structured data -> serialize -> char[] delegate -> output iopipe
Adding in the policy, it looks like this:
structured data -> serialize -> policy -> char[] delegate -> output iopipe
This looks very similar, even symmetrical, to the chain for deserialization. However, notice the API for output is the simplest thing it can be -- a
char[]accepting delegate.In fact, the iopipe behind the delegate is not even important here. You could technically substitute any output range.
But we want options for customization. People want different formatting, they want customized handling of types. They want nice tools to enforce or help write valid JSON and JSON5.
Not only that, but we are an iopipe-library! the point of iopipe is to give buffer access to users. Hiding everything behind a
char[]delegate defeats that purpose!So I propose for serialization a new chain of control:
structured data -> serialize -> policy -> JSONFormatter -> output iopipe
The JSONFormatter object will assist like the Tokenizer, in generating valid JSON. It will have methods or UFCS functions like
addMemberName(key), and automatically add the name and colon, expecting a value.The API might look like this:
Each serialization implementation function will be given a JSONFormatter, a Policy, and a Value to serialize, just like the deserializer gets that triple.
The policy can implement defaults for each type, similar to the deserializer. It has the additional chore of implementing formatting (like indentation).
Similar callbacks for outputting objects or arrays like
serializeObject, orserializeArray.We also might want to give a mechanism to directly write to the output iopipe. Maybe that's how the string data is written without validation? EDIT: did so.
A good test for whether we got the API right is if we can reimplement the formatjson example program with it.
ping @gulugulubing @benjones @Inkrementator @ahmetsait
All reactions