Parcourir la source

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()`.
preencode-sketch
Paul J. Davis il y a 8 ans
Parent
révision
4613640394
2 fichiers modifiés avec 7 ajouts et 2 suppressions
  1. +5
    -2
      c_src/encoder.c
  2. +2
    -0
      src/jiffy.erl

+ 5
- 2
c_src/encoder.c Voir le fichier

@ -784,8 +784,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);

+ 2
- 0
src/jiffy.erl Voir le fichier

@ -158,6 +158,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 | _], _) ->
throw({error, {invalid_ejson, InvalidEjson}});
finish_encode(_, _) ->

Chargement…
Annuler
Enregistrer