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

add attempt_atom flag #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ The options for decode are:
will ensure that the parsed object only contains a single entry
containing the last value seen. This mirrors the parsing beahvior
of virtually every other JSON parser.
* `attempt_atom` - Attempts to convert keys to existing atoms. Will
return keys as binaries if the atom does not exists.
* `copy_strings` - Normally, when strings are decoded, they are
created as sub-binaries of the input data. With some workloads, this
leads to an undesirable bloating of memory: Strings in the decode
Expand Down Expand Up @@ -91,8 +93,8 @@ The options for encode are:
Data Format
-----------

Erlang JSON Erlang
==========================================================================
Erlang JSON Erlang Notes
======================================================================================

null -> null -> null
true -> true -> true
Expand All @@ -107,11 +109,17 @@ Data Format
{[]} -> {} -> {[]}
{[{foo, bar}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
{[{<<"foo">>, <<"bar">>}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
#{<<"foo">> => <<"bar">>} -> {"foo": "bar"} -> #{<<"foo">> => <<"bar">>}
#{<<"foo">> => <<"bar">>} -> {"foo": "bar"} -> #{<<"foo">> => <<"bar">>} (1)
{[{<<"foo">>, <<"bar">>}]} -> {"foo": "bar"} -> {[{foo, <<"bar">>}]} (2)
#{<<"foo">> => <<"bar">>} -> {"foo": "bar"} -> #{foo => <<"bar">>} (1, 2)

Note 1: This entry is only valid for VM's that support the `maps` data type
(i.e., 17.0 and newer) and client code must pass the `return_maps`
option to `jiffy:decode/2`.

Note 2: This entry is only valid if the atom existed before and the client code must
pass the `attempt_atom` option to `jiffy:decode/2`.

N.B. The last entry in this table is only valid for VM's that support
the `maps` data type (i.e., 17.0 and newer) and client code must pass
the `return_maps` option to `jiffy:decode/2`.

Improvements over EEP0018
-------------------------
Expand Down
6 changes: 5 additions & 1 deletion c_src/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ typedef struct {
int return_trailer;
int dedupe_keys;
int copy_strings;
int attempt_atom;
ERL_NIF_TERM null_term;

unsigned char* p;
Expand Down Expand Up @@ -85,6 +86,7 @@ dec_new(ErlNifEnv* env)
d->return_trailer = 0;
d->dedupe_keys = 0;
d->copy_strings = 0;
d->attempt_atom = 0;
d->null_term = d->atoms->atom_null;

d->p = NULL;
Expand Down Expand Up @@ -695,6 +697,8 @@ decode_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
d->null_term = d->atoms->atom_nil;
} else if(get_null_term(env, val, &(d->null_term))) {
continue;
} else if(enif_is_identical(val, d->atoms->atom_attempt_atom)) {
d->attempt_atom = 1;
} else {
return enif_make_badarg(env);
}
Expand Down Expand Up @@ -980,7 +984,7 @@ decode_iter(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
}
dec_pop_assert(d, st_value);
if(!make_object(env, curr, &val,
d->return_maps, d->dedupe_keys)) {
d->return_maps, d->dedupe_keys, d->attempt_atom)) {
ret = dec_error(d, "internal_object_error");
goto done;
}
Expand Down
1 change: 1 addition & 0 deletions c_src/jiffy.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
st->atom_escape_forward_slashes = make_atom(env, "escape_forward_slashes");
st->atom_dedupe_keys = make_atom(env, "dedupe_keys");
st->atom_copy_strings = make_atom(env, "copy_strings");
st->atom_attempt_atom = make_atom(env, "attempt_atom");

// Markers used in encoding
st->ref_object = make_atom(env, "$object_ref$");
Expand Down
3 changes: 2 additions & 1 deletion c_src/jiffy.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef struct {
ERL_NIF_TERM atom_escape_forward_slashes;
ERL_NIF_TERM atom_dedupe_keys;
ERL_NIF_TERM atom_copy_strings;
ERL_NIF_TERM atom_attempt_atom;

ERL_NIF_TERM ref_object;
ERL_NIF_TERM ref_array;
Expand Down Expand Up @@ -72,7 +73,7 @@ void dec_destroy(ErlNifEnv* env, void* obj);
void enc_destroy(ErlNifEnv* env, void* obj);

int make_object(ErlNifEnv* env, ERL_NIF_TERM pairs, ERL_NIF_TERM* out,
int ret_map, int dedupe_keys);
int ret_map, int dedupe_keys, int attempt_atom);

int int_from_hex(const unsigned char* p);
int int_to_hex(int val, unsigned char* p);
Expand Down
22 changes: 21 additions & 1 deletion c_src/objects.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,23 @@

BEGIN_C

static ERL_NIF_TERM
key_attempt_atom(ErlNifEnv* env, ERL_NIF_TERM key) {
ERL_NIF_TERM keyatom;
ErlNifBinary keybin;

if(enif_inspect_binary(env, key, &keybin) && keybin.size < 256) {
if(enif_make_existing_atom_len(env, (char *)keybin.data, keybin.size,
&keyatom, ERL_NIF_UTF8)) {
return keyatom;
}
}
return key;
}

int
make_object(ErlNifEnv* env, ERL_NIF_TERM pairs, ERL_NIF_TERM* out,
int ret_map, int dedupe_keys)
int ret_map, int dedupe_keys, int attempt_atom)
{
ERL_NIF_TERM ret;
ERL_NIF_TERM key;
Expand All @@ -37,6 +51,9 @@ make_object(ErlNifEnv* env, ERL_NIF_TERM pairs, ERL_NIF_TERM* out,
if(!enif_get_list_cell(env, pairs, &key, &pairs)) {
assert(0 == 1 && "Unbalanced object pairs.");
}
if(attempt_atom) {
key = key_attempt_atom(env, key);
}
if(!enif_get_map_value(env, ret, key, &old_val)) {
if(!enif_make_map_put(env, ret, key, val, &ret)) {
return 0;
Expand All @@ -53,6 +70,9 @@ make_object(ErlNifEnv* env, ERL_NIF_TERM pairs, ERL_NIF_TERM* out,
if(!enif_get_list_cell(env, pairs, &key, &pairs)) {
assert(0 == 1 && "Unbalanced object pairs.");
}
if(attempt_atom) {
key = key_attempt_atom(env, key);
}
if(dedupe_keys) {
ErlNifBinary bin;
if(!enif_inspect_binary(env, key, &bin)) {
Expand Down
1 change: 1 addition & 0 deletions src/jiffy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
| return_trailer
| dedupe_keys
| copy_strings
| attempt_atom
| {null_term, any()}
| {bytes_per_iter, non_neg_integer()}
| {bytes_per_red, non_neg_integer()}.
Expand Down
32 changes: 32 additions & 0 deletions test/jiffy_16_attempt_atom_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
% This file is part of Jiffy released under the MIT license.
% See the LICENSE file for more information.

-module(jiffy_16_attempt_atom_tests).

-include_lib("eunit/include/eunit.hrl").

attempt_atom_test_() ->
Opts = [attempt_atom],
_ = key_is_atom,
Cases = [
{<<"{\"key_no_atom\":1}">>, {[{<<"key_no_atom">>, 1}]}},
{<<"{\"key_is_atom\":1}">>, {[{key_is_atom, 1}]}}
],
{"Test attempt_atom", lists:map(fun({Data, Result}) ->
?_assertEqual(Result, jiffy:decode(Data, Opts))
end, Cases)}.

-ifndef(JIFFY_NO_MAPS).

attempt_atom_map_test_() ->
Opts = [attempt_atom, return_maps],
_ = key_is_atom,
Cases = [
{<<"{\"key_no_atom\":1}">>, #{<<"key_no_atom">> => 1}},
{<<"{\"key_is_atom\":1}">>, #{key_is_atom => 1}}
],
{"Test attempt_atom_map", lists:map(fun({Data, Result}) ->
?_assertEqual(Result, jiffy:decode(Data, Opts))
end, Cases)}.

-endif.