Skip to content

Instantly share code, notes, and snippets.

@StVolodymyr
Created May 31, 2013 11:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StVolodymyr/5684429 to your computer and use it in GitHub Desktop.
Save StVolodymyr/5684429 to your computer and use it in GitHub Desktop.
-module(json_parser).
-export([decode_api/1]).
-record(api, {
status,
message,
timeout
}).
-record(api1, {
status
}).
-record(api2, {
status,
message
}).
-record(api3, {
status,
timeout
}).
decoder() ->
jsonx:decoder([
{api, record_info(fields, api)},
{api1, record_info(fields, api1)},
{api2, record_info(fields, api2)},
{api3, record_info(fields, api3)}
]).
decode_api(Json) ->
Decoder = decoder(),
case Decoder(Json) of
#api{} = Result ->
Result;
#api1{status = Status1} ->
#api{status = Status1, message = <<"">>, timeout = 0};
#api2{status = Status2, message = Message2} ->
#api{status = Status2, message = Message2, timeout = 0};
#api3{status = Status3, timeout = Timeout3} ->
#api{status = Status3, message = <<"">>, timeout = Timeout3};
Other ->
Other
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment