Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

What if it is a array of strings? #51

Open
AbhishekLYK opened this issue Sep 19, 2019 · 1 comment
Open

What if it is a array of strings? #51

AbhishekLYK opened this issue Sep 19, 2019 · 1 comment

Comments

@AbhishekLYK
Copy link

AbhishekLYK commented Sep 19, 2019

I have an array of strings in my json like following...
{"userId":1, "rooms":["4","2","3]}

And I declared json like
-json({user, {string, "userId"},{list,"rooms",[{default,[ ]]}}})

but it is giving me error when trying to use to_json method

[Reason : {{case_clause,{error,"rooma",throw,{invalid_list_item,<<"4">>}}},[{ejson_encode,convert,5,[{file,"src/ejson_encode.erl"},{line,80}]},{ejson_encode,encode1,3,[{file,"src/ejson_encode.erl"},{line,53}]},{ejson_encode,encode,3,[{file,"src/ejson_encode.erl"},{line,32}]},{ejson,to_json,3,[{file,"src/ejson.erl"},{line,82}]}, .............

@AbhishekLYK
Copy link
Author

If you change ejson_enocode module's function like below

mixed_list_rule(AttrName, Value, Rules, Opts) when is_list(Value) ->
try lists:map(
fun(N) when is_number(N) ->
N;
(B) when is_boolean(B) ->
B;
(L) when is_binary(L) ->
L;

(T) when is_tuple(T) ->
Rec = element(1, T),
case encode1(T, Rules, Opts) of
{error, R} ->
throw(R);
AttrList ->
add_rec_type(Rec, AttrList)
end;
(E) ->
throw({invalid_list_item, E})
end, Value) of
List ->
{ok, {AttrName, List}}
catch
E:R ->
{error, AttrName, E, R}
end;

It works correctly. Can you update?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant