Skip to content
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

Pre-encoded JSON support, take 2 #140

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

dhull
Copy link
Contributor

@dhull dhull commented Mar 16, 2017

This pull request is a rewrite of #139 that uses @davisp's approach to including pre-encoded JSON in the input.

It adds a new function jiffy:partial_encode/2 that takes an array or object and returns a term that can later be included as part of the input in a future jiffy:encode call.

PartialArray = jiffy:partial_encode([ 1, 2 ], []).
PartialObject = jiffy:partial_encode({[ {one, 1} ]}, []).
iolist_to_binary(jiffy:encode([ [ PartialArray, 3 ], {[ PartialObject, {two, 2} ]} ])).
%% => <<"[[1,2,3],{\"one\":1,\"two\":2}]">>

It uses the atoms $partial_array$ and $partial_object$ to mark these partially-encoded values. For the partially-encoded array we could technically use json but I added $partial_array$ because the partially-encoded array is not well-formed JSON while the JSON in {json, Json} is, and I thought it was worth distinguishing the two.

I did not test my code with native Erlang maps but I believe it will work if the tuple {Key, Value} returned by jiffy_partial_encode is added to the map by map:put(Key, Value, Map).

Copy link

@facekoob facekoob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iPad icloud bypass

This change allows users to insert pre-encoded JSON `iodata()` anywhere
there can be a valid `json_value()` (i.e., anywhere except object keys).
This approach occurred to me looking at PR davisp#139 from @dhull. The
technical difference being that this approach does not copy the given
`iodata()` into the output and instead re-uses the same input term as
part of the output `iodata()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants