Replies: 1 comment 1 reply
-
Is the Options here for something JSON5 support? like: |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Beta Was this translation helpful? Give feedback.
All reactions