Skip to content

Commit f6e8d43

Browse files
davispKenan Gillet
authored andcommitted
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 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()`.
1 parent 9ea1b35 commit f6e8d43

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

c_src/encoder.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,11 @@ encode_iter(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
859859
}
860860
} else if(enif_get_tuple(env, curr, &arity, &tuple)) {
861861
if(arity != 1) {
862-
ret = enc_obj_error(e, "invalid_ejson", curr);
863-
goto done;
862+
if(!enc_unknown(e, curr)) {
863+
ret = enc_error(e, "internal_error");
864+
goto done;
865+
}
866+
continue;
864867
}
865868
if(!enif_is_list(env, tuple[0])) {
866869
ret = enc_obj_error(e, "invalid_object", curr);

src/jiffy.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ finish_encode([<<_/binary>>=B | Rest], Acc) ->
174174
finish_encode([Val | Rest], Acc) when is_integer(Val) ->
175175
Bin = list_to_binary(integer_to_list(Val)),
176176
finish_encode(Rest, [Bin | Acc]);
177+
finish_encode([{json, Json} | Rest], Acc) ->
178+
finish_encode(Rest, [Json | Acc]);
177179
finish_encode([InvalidEjson | _], _) ->
178180
error({invalid_ejson, InvalidEjson});
179181
finish_encode(_, _) ->

0 commit comments

Comments
 (0)