From 52ce9fc99e72ecc36ad83d943e0b73680480220d Mon Sep 17 00:00:00 2001 From: "Paul J. Davis" Date: Wed, 15 Mar 2017 12:12:08 -0500 Subject: [PATCH] Allow for pre-encoded JSON during encoding 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 #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()`. --- c_src/encoder.c | 7 +++++-- src/jiffy.erl | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/c_src/encoder.c b/c_src/encoder.c index 4cfb3533..be371e47 100644 --- a/c_src/encoder.c +++ b/c_src/encoder.c @@ -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); diff --git a/src/jiffy.erl b/src/jiffy.erl index 7639b7d1..342d10f2 100644 --- a/src/jiffy.erl +++ b/src/jiffy.erl @@ -173,6 +173,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(_, _) ->