Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a json to erlang data structure function #7

Open
alinbsp opened this issue May 11, 2020 · 2 comments
Open

Implement a json to erlang data structure function #7

alinbsp opened this issue May 11, 2020 · 2 comments
Assignees

Comments

@alinbsp
Copy link
Owner

alinbsp commented May 11, 2020

No description provided.

@alinbsp alinbsp changed the title Implement a json to record function Implement a json to erland data structure function May 13, 2020
@alinbsp
Copy link
Owner Author

alinbsp commented May 13, 2020

@alinbsp alinbsp changed the title Implement a json to erland data structure function Implement a json to erlang data structure function May 13, 2020
@bogdanbph
Copy link
Collaborator

printrecrec(#accountDetails{name=Name, balance=Balance, pin=Pin}) ->
"name= " ++ Name ++ ",\nbalance= " ++ integer_to_list(Balance) ++ ",\npin= " ++ integer_to_list(Pin) ++ "\n}".

printrec(#account{id=Id, details=Details}) ->
"account{\nid= " ++ integer_to_list(Id) ++ ",\n" ++ printrecrec(Details).

json_to_record({struct, Json}) ->
NewList = lists:map(fun({K, V}) when K =:= <<"name">> -> {binary_to_list(K), binary_to_list(V)};
({K, V}) -> {binary_to_list(K), V}
end, Json),
Id = proplists:get_value("id", NewList, -1),
Name = proplists:get_value("name", NewList, ""),
Balance = proplists:get_value("balance", NewList, "wd"),
Pin = proplists:get_value("pin", NewList, -1),
Acc=#account{id = Id, details=#accountDetails{name = Name, balance = Balance, pin = Pin}}.

loop() {
..
"create_json" ->
Body = Req:recv_body(),
Json = mochijson2:decode(Body), %%RETURNS A TUPLE
Recordul = json_to_record(Json),
Req:respond({200, [{"Content-Type", "text/plain"}], printrec(Recordul)});
}

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

No branches or pull requests

2 participants