Skip to content

Commit

Permalink
Allow for pre-encoded JSON during encoding
Browse files Browse the repository at this point in the history
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()`.
  • Loading branch information
davisp authored and Kuroneer committed Mar 24, 2020
1 parent 4523170 commit bbd1b7c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions c_src/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,11 @@ encode_iter(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
}
} else if(enif_get_tuple(env, curr, &arity, &tuple)) {
if(arity != 1) {
ret = enc_obj_error(e, "invalid_ejson", curr);
goto done;
if(!enc_unknown(e, curr)) {
ret = enc_error(e, "internal_error");
goto done;
}
continue;
}
if(!enif_is_list(env, tuple[0])) {
ret = enc_obj_error(e, "invalid_object", curr);
Expand Down
2 changes: 2 additions & 0 deletions src/jiffy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ finish_encode([<<_/binary>>=B | Rest], Acc) ->
finish_encode([Val | Rest], Acc) when is_integer(Val) ->
Bin = list_to_binary(integer_to_list(Val)),
finish_encode(Rest, [Bin | Acc]);
finish_encode([{json, Json} | Rest], Acc) ->
finish_encode(Rest, [Json | Acc]);
finish_encode([InvalidEjson | _], _) ->
error({invalid_ejson, InvalidEjson});
finish_encode(_, _) ->
Expand Down

0 comments on commit bbd1b7c

Please sign in to comment.