Skip to content

Commit 7ba1005

Browse files
davispDavid Hull
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 ee8b48c commit 7ba1005

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
@@ -781,8 +781,11 @@ encode_iter(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
781781
}
782782
} else if(enif_get_tuple(env, curr, &arity, &tuple)) {
783783
if(arity != 1) {
784-
ret = enc_obj_error(e, "invalid_ejson", curr);
785-
goto done;
784+
if(!enc_unknown(e, curr)) {
785+
ret = enc_error(e, "internal_error");
786+
goto done;
787+
}
788+
continue;
786789
}
787790
if(!enif_is_list(env, tuple[0])) {
788791
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
@@ -160,6 +160,8 @@ finish_encode([<<_/binary>>=B | Rest], Acc) ->
160160
finish_encode([Val | Rest], Acc) when is_integer(Val) ->
161161
Bin = list_to_binary(integer_to_list(Val)),
162162
finish_encode(Rest, [Bin | Acc]);
163+
finish_encode([{json, Json} | Rest], Acc) ->
164+
finish_encode(Rest, [Json | Acc]);
163165
finish_encode([InvalidEjson | _], _) ->
164166
throw({error, {invalid_ejson, InvalidEjson}});
165167
finish_encode(_, _) ->

0 commit comments

Comments
 (0)