Skip to content

Instantly share code, notes, and snippets.

@sinkovsky
Created October 10, 2009 12:03
Show Gist options
  • Save sinkovsky/206808 to your computer and use it in GitHub Desktop.
Save sinkovsky/206808 to your computer and use it in GitHub Desktop.
tick_list_from_json(Series_id, Json) ->
case probix_series:series(Series_id) of
not_found ->
{error, not_found};
_Series ->
case decode_tick_json(Series_id, Json) of
bad_json -> {error, bad_json};
List -> List
end
end.
json_struct_to_tick(Series_id, {struct, Struct}) ->
#tick{
id = {Series_id, proplists:get_value(<<"timestamp">>, Struct)},
value = proplists:get_value(<<"value">>, Struct)
}.
decode_tick_json(Series_Id, Json) ->
try mochijson2:decode(Json) of
List when is_list(List) ->
[ json_struct_to_tick(Series_Id, P) || P <- List ];
Struct when is_tuple(Struct) ->
[ json_struct_to_tick(Series_Id, Struct) ]
catch
_Any ->
bad_json
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment