From 5f5be513659c2e6b81d1528cc3a95b2a0f3bb792 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Mon, 31 May 2010 02:36:16 -0700 Subject: [PATCH] modified jsx_parser to reject bare json values, fixed typo --- examples/jsx_parser.erl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/jsx_parser.erl b/examples/jsx_parser.erl index 44ccea6..3d1001a 100644 --- a/examples/jsx_parser.erl +++ b/examples/jsx_parser.erl @@ -37,7 +37,7 @@ decode(JSON) -> {incomplete, _} -> {error, badjson} ; {error, badjson} -> - {error, badjosn} + {error, badjson} ; {Result, _} -> Result end. @@ -46,7 +46,7 @@ decode(JSON) -> %% 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 %% 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) -> [dict:new()] ++ Stack; @@ -72,6 +72,11 @@ event(end_array, [Array]) -> event({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) %% as a function. so naming our formatting functions string, number and literal will %% allow the following shortcut