Skip to content

Commit e4a9590

Browse files
davispKuroneer
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 e39e525 commit e4a9590

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
@@ -173,6 +173,8 @@ finish_encode([<<_/binary>>=B | Rest], Acc) ->
173173
finish_encode([Val | Rest], Acc) when is_integer(Val) ->
174174
Bin = list_to_binary(integer_to_list(Val)),
175175
finish_encode(Rest, [Bin | Acc]);
176+
finish_encode([{json, Json} | Rest], Acc) ->
177+
finish_encode(Rest, [Json | Acc]);
176178
finish_encode([InvalidEjson | _], _) ->
177179
error({invalid_ejson, InvalidEjson});
178180
finish_encode(_, _) ->

0 commit comments

Comments
 (0)