json reader: correctly handle empty objects in objects

Attempting to parse { "foo" : {} } previously resulted in an error.
(close-hash) expected to consolidate an object into the values of the
containing object (in the first line of code).  While there is none in
the case of the empty hash, the structure of the accumulator looked like
it contained an unconsolidated object, so it folded the values into the
keys and got very confused.
Alec Berryman 2009-12-24 17:48:16 -06:00 committed by Slava Pestov
parent 92393c7df7
commit 43a7facd09
2 changed files with 15 additions and 12 deletions

View File

@ -1,4 +1,4 @@
USING: arrays json.reader kernel strings tools.test
USING: assocs arrays json.reader kernel strings tools.test
hashtables json ;
IN: json.reader.tests
@ -58,3 +58,6 @@ IN: json.reader.tests
{ 0 } [ " 0" json> ] unit-test
{ 0 } [ "0 " json> ] unit-test
{ 0 } [ " 0 " json> ] unit-test
! empty objects are allowed as values in objects
{ H{ { "foo" H{ } } } } [ "{ \"foo\" : {}}" json> ] unit-test

View File

@ -68,12 +68,12 @@ DEFER: j-string
dup pop >array over push ;
: (close-hash) ( accum -- accum' )
dup length 3 >= [ v-over-push ] when
dup dup [ pop ] dip pop swap
dup [ length 3 >= ] [ last V{ } = not ] bi@ and [ v-over-push ] when
dup dup [ pop ] bi@ swap
zip H{ } assoc-clone-like over push ;
: scan ( accum char -- accum )
! 2dup . . ! Great for debug...
! 2dup 1string swap . . ! Great for debug...
[
{
{ CHAR: \" [ j-string over push ] }