modified jsx_parser to reject bare json values, fixed typo

This commit is contained in:
alisdair sullivan 2010-05-31 02:36:16 -07:00
parent 2cf20cf809
commit 5f5be51365

View file

@ -37,7 +37,7 @@ decode(JSON) ->
{incomplete, _} -> {incomplete, _} ->
{error, badjson} {error, badjson}
; {error, badjson} -> ; {error, badjson} ->
{error, badjosn} {error, badjson}
; {Result, _} -> ; {Result, _} ->
Result Result
end. end.
@ -46,7 +46,7 @@ decode(JSON) ->
%% erlang representation is dicts for objects and lists for arrays. these are pushed %% erlang representation is dicts for objects and lists for arrays. these are pushed
%% onto a stack, the top of which is our current level, deeper levels represent parent %% onto a stack, the top of which is our current level, deeper levels represent parent
%% and grandparent levels in the json structure. keys are also stored on top of the array %% and grandparent levels in the json structure. keys are also stored on top of the array
%% during parsing of their associated values. %% during parsing of their associated values.
event(start_object, Stack) -> event(start_object, Stack) ->
[dict:new()] ++ Stack; [dict:new()] ++ Stack;
@ -72,6 +72,11 @@ event(end_array, [Array]) ->
event({key, Key}, [Object|Stack]) -> event({key, Key}, [Object|Stack]) ->
[{key, Key}] ++ [Object] ++ Stack; [{key, Key}] ++ [Object] ++ Stack;
%% reject values that aren't wrapped by an array or object
event({_Type, _Value}, []) ->
erlang:error(badjson);
%% this is kind of a dirty hack, but erlang will interpret atoms when applied to (Args) %% this is kind of a dirty hack, but erlang will interpret atoms when applied to (Args)
%% as a function. so naming our formatting functions string, number and literal will %% as a function. so naming our formatting functions string, number and literal will
%% allow the following shortcut %% allow the following shortcut