lists.squeak.org
Sign In
Sign Up
Sign In
Sign Up
Manage this list
×
Keyboard Shortcuts
Thread View
j
: Next unread message
k
: Previous unread message
j a
: Jump to all threads
j l
: Jump to MailingList overview
2024
May
April
March
February
January
2023
December
November
October
September
August
July
June
May
April
March
February
January
2022
December
November
October
September
August
July
June
May
April
March
February
January
2021
December
November
October
September
August
July
June
May
April
March
February
January
2020
December
November
October
September
August
July
June
May
April
March
February
January
2019
December
November
October
September
August
July
June
May
April
March
February
January
2018
December
November
October
September
August
July
June
May
April
March
February
January
2017
December
November
October
September
August
July
June
May
April
March
February
January
2016
December
November
October
September
August
July
June
May
April
March
February
January
2015
December
November
October
September
August
July
June
May
April
March
February
January
2014
December
November
October
September
August
July
June
May
April
March
February
January
2013
December
November
October
September
August
July
June
May
April
March
February
January
2012
December
November
October
September
August
July
June
May
April
March
February
January
2011
December
November
October
September
August
July
June
May
April
March
February
January
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008
December
November
October
September
August
July
June
May
April
March
February
January
2007
December
November
October
September
August
July
June
May
April
March
February
January
2006
December
November
October
September
August
July
June
May
April
March
February
January
2005
December
November
October
September
August
July
June
May
April
March
February
January
2004
December
November
October
September
August
July
June
May
April
March
February
January
2003
December
November
October
September
August
July
June
May
April
March
February
January
2002
December
November
October
September
August
July
June
May
April
March
February
January
2001
December
November
October
September
August
July
June
May
April
March
February
January
2000
December
November
October
September
August
July
June
May
April
March
February
January
1999
December
November
October
September
August
July
June
May
April
March
February
January
1998
December
November
October
September
August
July
June
May
April
March
February
January
1997
December
November
October
September
August
July
June
May
April
March
February
January
1996
December
November
October
September
August
July
June
May
April
March
February
January
1995
December
November
October
September
August
July
June
May
April
March
February
January
1994
December
November
October
September
August
July
June
May
April
March
February
January
1993
December
November
October
September
August
July
June
May
April
March
February
January
1992
December
November
October
September
August
July
June
May
April
March
February
January
1991
December
November
October
September
August
July
June
May
April
March
February
January
1990
December
November
October
September
August
July
June
May
April
March
February
January
1989
December
November
October
September
August
July
June
May
April
March
February
January
1988
December
November
October
September
August
July
June
May
April
March
February
January
1987
December
November
October
September
August
July
June
May
April
March
February
January
1986
December
November
October
September
August
July
June
May
April
March
February
January
1985
December
November
October
September
August
July
June
May
April
March
February
January
1984
December
November
October
September
August
July
June
May
April
March
February
January
1983
December
November
October
September
August
July
June
May
April
March
February
January
1982
December
November
October
September
August
July
June
May
April
March
February
January
1981
December
November
October
September
August
July
June
May
April
March
February
January
1980
December
November
October
September
August
July
June
May
April
March
February
January
1979
December
November
October
September
August
July
June
May
April
March
February
January
1978
December
November
October
September
August
July
June
May
April
March
February
January
1977
December
November
October
September
August
July
June
May
April
March
February
January
1976
December
November
October
September
August
July
June
May
April
March
February
January
1975
December
November
October
September
August
July
June
May
April
March
February
January
1974
December
November
October
September
August
July
June
May
April
March
February
January
1973
December
November
October
September
August
July
June
May
April
March
February
January
1972
December
November
October
September
August
July
June
May
April
March
February
January
1971
December
November
October
September
August
July
June
May
April
March
February
January
1970
December
November
October
September
August
July
June
May
April
March
February
January
List overview
Download
Squeak-dev
September 2022
----- 2024 -----
May 2024
April 2024
March 2024
February 2024
January 2024
----- 2023 -----
December 2023
November 2023
October 2023
September 2023
August 2023
July 2023
June 2023
May 2023
April 2023
March 2023
February 2023
January 2023
----- 2022 -----
December 2022
November 2022
October 2022
September 2022
August 2022
July 2022
June 2022
May 2022
April 2022
March 2022
February 2022
January 2022
----- 2021 -----
December 2021
November 2021
October 2021
September 2021
August 2021
July 2021
June 2021
May 2021
April 2021
March 2021
February 2021
January 2021
----- 2020 -----
December 2020
November 2020
October 2020
September 2020
August 2020
July 2020
June 2020
May 2020
April 2020
March 2020
February 2020
January 2020
----- 2019 -----
December 2019
November 2019
October 2019
September 2019
August 2019
July 2019
June 2019
May 2019
April 2019
March 2019
February 2019
January 2019
----- 2018 -----
December 2018
November 2018
October 2018
September 2018
August 2018
July 2018
June 2018
May 2018
April 2018
March 2018
February 2018
January 2018
----- 2017 -----
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
February 2017
January 2017
----- 2016 -----
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
----- 2015 -----
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
----- 2014 -----
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
----- 2013 -----
December 2013
November 2013
October 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
----- 2012 -----
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
March 2012
February 2012
January 2012
----- 2011 -----
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
----- 2010 -----
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
----- 2009 -----
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
----- 2008 -----
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
----- 2007 -----
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
----- 2006 -----
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
----- 2005 -----
December 2005
November 2005
October 2005
September 2005
August 2005
July 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
----- 2004 -----
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
January 2004
----- 2003 -----
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003
----- 2002 -----
December 2002
November 2002
October 2002
September 2002
August 2002
July 2002
June 2002
May 2002
April 2002
March 2002
February 2002
January 2002
----- 2001 -----
December 2001
November 2001
October 2001
September 2001
August 2001
July 2001
June 2001
May 2001
April 2001
March 2001
February 2001
January 2001
----- 2000 -----
December 2000
November 2000
October 2000
September 2000
August 2000
July 2000
June 2000
May 2000
April 2000
March 2000
February 2000
January 2000
----- 1999 -----
December 1999
November 1999
October 1999
September 1999
August 1999
July 1999
June 1999
May 1999
April 1999
March 1999
February 1999
January 1999
----- 1998 -----
December 1998
November 1998
October 1998
September 1998
August 1998
July 1998
June 1998
May 1998
April 1998
March 1998
February 1998
January 1998
----- 1997 -----
December 1997
November 1997
October 1997
September 1997
August 1997
July 1997
June 1997
May 1997
April 1997
March 1997
February 1997
January 1997
----- 1996 -----
December 1996
November 1996
October 1996
September 1996
August 1996
July 1996
June 1996
May 1996
April 1996
March 1996
February 1996
January 1996
----- 1995 -----
December 1995
November 1995
October 1995
September 1995
August 1995
July 1995
June 1995
May 1995
April 1995
March 1995
February 1995
January 1995
----- 1994 -----
December 1994
November 1994
October 1994
September 1994
August 1994
July 1994
June 1994
May 1994
April 1994
March 1994
February 1994
January 1994
----- 1993 -----
December 1993
November 1993
October 1993
September 1993
August 1993
July 1993
June 1993
May 1993
April 1993
March 1993
February 1993
January 1993
----- 1992 -----
December 1992
November 1992
October 1992
September 1992
August 1992
July 1992
June 1992
May 1992
April 1992
March 1992
February 1992
January 1992
----- 1991 -----
December 1991
November 1991
October 1991
September 1991
August 1991
July 1991
June 1991
May 1991
April 1991
March 1991
February 1991
January 1991
----- 1990 -----
December 1990
November 1990
October 1990
September 1990
August 1990
July 1990
June 1990
May 1990
April 1990
March 1990
February 1990
January 1990
----- 1989 -----
December 1989
November 1989
October 1989
September 1989
August 1989
July 1989
June 1989
May 1989
April 1989
March 1989
February 1989
January 1989
----- 1988 -----
December 1988
November 1988
October 1988
September 1988
August 1988
July 1988
June 1988
May 1988
April 1988
March 1988
February 1988
January 1988
----- 1987 -----
December 1987
November 1987
October 1987
September 1987
August 1987
July 1987
June 1987
May 1987
April 1987
March 1987
February 1987
January 1987
----- 1986 -----
December 1986
November 1986
October 1986
September 1986
August 1986
July 1986
June 1986
May 1986
April 1986
March 1986
February 1986
January 1986
----- 1985 -----
December 1985
November 1985
October 1985
September 1985
August 1985
July 1985
June 1985
May 1985
April 1985
March 1985
February 1985
January 1985
----- 1984 -----
December 1984
November 1984
October 1984
September 1984
August 1984
July 1984
June 1984
May 1984
April 1984
March 1984
February 1984
January 1984
----- 1983 -----
December 1983
November 1983
October 1983
September 1983
August 1983
July 1983
June 1983
May 1983
April 1983
March 1983
February 1983
January 1983
----- 1982 -----
December 1982
November 1982
October 1982
September 1982
August 1982
July 1982
June 1982
May 1982
April 1982
March 1982
February 1982
January 1982
----- 1981 -----
December 1981
November 1981
October 1981
September 1981
August 1981
July 1981
June 1981
May 1981
April 1981
March 1981
February 1981
January 1981
----- 1980 -----
December 1980
November 1980
October 1980
September 1980
August 1980
July 1980
June 1980
May 1980
April 1980
March 1980
February 1980
January 1980
----- 1979 -----
December 1979
November 1979
October 1979
September 1979
August 1979
July 1979
June 1979
May 1979
April 1979
March 1979
February 1979
January 1979
----- 1978 -----
December 1978
November 1978
October 1978
September 1978
August 1978
July 1978
June 1978
May 1978
April 1978
March 1978
February 1978
January 1978
----- 1977 -----
December 1977
November 1977
October 1977
September 1977
August 1977
July 1977
June 1977
May 1977
April 1977
March 1977
February 1977
January 1977
----- 1976 -----
December 1976
November 1976
October 1976
September 1976
August 1976
July 1976
June 1976
May 1976
April 1976
March 1976
February 1976
January 1976
----- 1975 -----
December 1975
November 1975
October 1975
September 1975
August 1975
July 1975
June 1975
May 1975
April 1975
March 1975
February 1975
January 1975
----- 1974 -----
December 1974
November 1974
October 1974
September 1974
August 1974
July 1974
June 1974
May 1974
April 1974
March 1974
February 1974
January 1974
----- 1973 -----
December 1973
November 1973
October 1973
September 1973
August 1973
July 1973
June 1973
May 1973
April 1973
March 1973
February 1973
January 1973
----- 1972 -----
December 1972
November 1972
October 1972
September 1972
August 1972
July 1972
June 1972
May 1972
April 1972
March 1972
February 1972
January 1972
----- 1971 -----
December 1971
November 1971
October 1971
September 1971
August 1971
July 1971
June 1971
May 1971
April 1971
March 1971
February 1971
January 1971
----- 1970 -----
December 1970
November 1970
October 1970
September 1970
August 1970
July 1970
June 1970
May 1970
April 1970
March 1970
February 1970
January 1970
squeak-dev@lists.squeakfoundation.org
29 participants
157 discussions
Start a n
N
ew thread
The Trunk: JSON-klub.46.mcz
by commits@source.squeak.org
08 Sep '22
08 Sep '22
Christoph Thiede uploaded a new version of JSON to project The Trunk:
http://source.squeak.org/trunk/JSON-klub.46.mcz
==================== Summary ==================== Name: JSON-klub.46 Author: klub Time: 14 January 2018, 5:55:56.563627 pm UUID: 7e8086c5-b2b1-46ec-83d0-94e1daee207b Ancestors: JSON-ul.45 Added OrderedJsonObject which is the same as JsonObject, but it inherits from OrderedDictionary, so the order of the entries is preserved. ==================== Snapshot ==================== SystemOrganization addCategory: #JSON! ----- Method: Integer>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream ^ self printOn: aWriteStream base: 10 ! ----- Method: Association>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream self key asString jsonWriteOn: aStream. aStream nextPut: $:; space. self value jsonWriteOn: aStream.! ----- Method: False>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'false'! ----- Method: Number>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream self printOn: aWriteStream base: 10! OrderedDictionary subclass: #OrderedJsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: OrderedJsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: OrderedJsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: OrderedJsonObject>>doesNotUnderstand: (in category 'as yet unclassified') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: OrderedJsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: OrderedJsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: Time>>jsonWriteOn: (in category '*JSON') ----- jsonWriteOn: stream stream nextPut: $". self print24: true showSeconds: true on: stream. stream nextPut: $"! ----- Method: WriteStream>>jsonPrint: (in category '*json-printing') ----- jsonPrint: anObject anObject jsonWriteOn: self! ----- Method: Text>>jsonWriteOn: (in category '*json-printing') ----- jsonWriteOn: aStream self string jsonWriteOn: aStream! ----- Method: UndefinedObject>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream aWriteStream nextPutAll: 'null'! Error subclass: #JsonSyntaxError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonSyntaxError commentStamp: '<historical>' prior: 0! Class Json signals instances of me when an input stream contains invalid JSON input.! ----- Method: Dictionary>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | needComma | needComma := false. aStream nextPut: ${. self associationsDo: [ :assoc | needComma ifTrue: [ aStream nextPut: $, ] ifFalse: [ needComma := true ]. assoc key jsonWriteOn: aStream. aStream nextPut: $:. assoc value jsonWriteOn: aStream ]. aStream nextPut: $}.! Dictionary subclass: #JsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: JsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: JsonObject>>doesNotUnderstand: (in category 'as yet unclassified') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: JsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: JsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: True>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'true'! NumberParser subclass: #JsonNumberParser instanceVariableNames: 'fraction' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonNumberParser>>allowPlusSign (in category 'accessing') ----- allowPlusSign ^false! ----- Method: JsonNumberParser>>allowPlusSignInExponent (in category 'accessing') ----- allowPlusSignInExponent ^true! ----- Method: JsonNumberParser>>error: (in category 'error') ----- error: aString JsonSyntaxError signal: aString! ----- Method: JsonNumberParser>>exponentLetters (in category 'accessing') ----- exponentLetters ^'eE'! ----- Method: JsonNumberParser>>initialize (in category 'initialize-release') ----- initialize super initialize. base := 10. fraction := Fraction numerator: 0 denominator: 1! ----- Method: JsonNumberParser>>makeFloatFromMantissa:exponent: (in category 'parsing-private') ----- makeFloatFromMantissa: mantissa exponent: exponent exponent = 0 ifTrue: [ ^mantissa asFloat ]. exponent >= 16 ifTrue: [ ^(mantissa * (self tenRaisedTo: exponent)) asFloat ]. (exponent >= -15 and: [ mantissa highBitOfMagnitude <= 54 ]) ifTrue: [ exponent >= 1 ifTrue: [ ^mantissa asFloat * (self tenRaisedTo: exponent) asFloat ]. ^mantissa asFloat / (self tenRaisedTo: exponent negated) asFloat ]. ^(fraction setNumerator: mantissa denominator: (self tenRaisedTo: exponent negated)) asFloat! ----- Method: JsonNumberParser>>nextElementaryLargeInteger (in category 'parsing-large int') ----- nextElementaryLargeInteger "Form an unsigned integer with incoming digits from sourceStream. Return this integer, or zero if no digits found. Stop reading if end of digits or if a LargeInteger is formed. Count the number of digits and the position of lastNonZero digit and store them in instVar." | digit value | value := 0. nDigits := 0. lastNonZero := 0. [ value isLarge ifTrue: [ ^value ]. ((digit := (sourceStream next ifNil: [ ^value ]) asInteger - 48 "$0 asInteger") < 0 or: [ digit >= 10 ]) ifTrue: [ sourceStream skip: -1. ^value ]. nDigits := nDigits + 1. digit = 0 ifFalse: [ lastNonZero := nDigits. value := value * 10 + digit ] ifTrue: [ value := value * 10 ] ] repeat! ----- Method: JsonNumberParser>>nextNumber: (in category 'parsing-public') ----- nextNumber: negative | numberOfTrailingZeroInIntegerPart numberOfTrailingZeroInFractionPart numberOfNonZeroFractionDigits mantissa value | integerPart := self nextUnsignedIntegerOrNil ifNil: [ ^self error: 'Missing integer part!!' ]. numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. (sourceStream peekFor: $.) ifFalse: [ "No fraction part" negative ifTrue: [ integerPart := integerPart negated ]. self readExponent ifFalse: [ ^integerPart ]. exponent > 0 ifTrue: [ ^integerPart * (self tenRaisedTo: exponent) ]. ^(Fraction numerator: integerPart denominator: (self tenRaisedTo: exponent negated)) asFloat ]. fractionPart := self nextUnsignedIntegerOrNil ifNil: [ self error: 'Missing fraction part!!' ]. fractionPart isZero ifTrue: [ self readExponent ifFalse: [ negative ifTrue: [ integerPart isZero ifTrue: [ ^Float negativeZero ]. ^integerPart negated ]. ^integerPart ]. exponent >= 0 ifTrue: [ negative ifTrue: [ integerPart := integerPart negated ]. ^integerPart * (self tenRaisedTo: exponent) ]. value := self makeFloatFromMantissa: integerPart exponent: exponent. negative ifTrue: [ ^value negated ]. ^value ]. numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. numberOfNonZeroFractionDigits := lastNonZero. self readExponent. exponent := exponent - numberOfNonZeroFractionDigits. mantissa := integerPart * (10 raisedToInteger: numberOfNonZeroFractionDigits) + (fractionPart // (10 raisedToInteger: numberOfTrailingZeroInFractionPart)). value := self makeFloatFromMantissa: mantissa exponent: exponent. negative ifTrue: [ ^value negated ]. ^value! ----- Method: JsonNumberParser>>nextUnsignedIntegerOrNil (in category 'parsing-public') ----- nextUnsignedIntegerOrNil "Form an unsigned integer with incoming digits from sourceStream. Answer this integer, or nil if no digit found. Count the number of digits and the position of lastNonZero digit and store them in instVar" | nPackets high nDigitsHigh lastNonZeroHigh low | "read no more digits than one elementary LargeInteger" high := self nextElementaryLargeInteger. nDigits = 0 ifTrue: [^nil]. "Not enough digits to form a LargeInteger, stop iteration" high isLarge ifFalse: [^high]. "We now have to engage arithmetic with LargeInteger Decompose the integer in a high and low packets of growing size:" nPackets := 1. nDigitsHigh := nDigits. lastNonZeroHigh := lastNonZero. [ low := self nextLargeIntegerBase: 10 nPackets: nPackets . high := high * (10 raisedToInteger: nDigits) + low. lastNonZero = 0 ifFalse: [lastNonZeroHigh := lastNonZero + nDigitsHigh]. nDigitsHigh := nDigitsHigh + nDigits. low isLarge] whileTrue: [nPackets := nPackets * 2]. nDigits := nDigitsHigh. lastNonZero := lastNonZeroHigh. ^high! ----- Method: JsonNumberParser>>tenRaisedTo: (in category 'parsing-private') ----- tenRaisedTo: anInteger anInteger >= 0 ifFalse: [ ^10 raisedToInteger: anInteger ]. anInteger >= 16 ifTrue: [ ^10 raisedToInteger: anInteger ]. ^#(1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000 100000000000 1000000000000 10000000000000 100000000000000 1000000000000000) at: anInteger + 1! ----- Method: ScaledDecimal>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: stream self printFractionAsDecimalOn: stream! ----- Method: Collection>>jsonWriteOn: (in category '*json') ----- jsonWriteOn: aStream "By default, use array braces " aStream nextPut: $[. self do: [:each | each jsonWriteOn: aStream ] separatedBy: [ aStream nextPut: $, ]. aStream nextPut: $]! TestCase subclass: #JsonTests instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonTests commentStamp: '<historical>' prior: 0! I provide a number of test cases for class Json.! ----- Method: JsonTests>>json:equals: (in category 'as yet unclassified') ----- json: aString equals: aValue | readValue | readValue := self readFrom: aString. self assert: readValue = aValue.! ----- Method: JsonTests>>readFrom: (in category 'as yet unclassified') ----- readFrom: aString ^ (Json newWithConstructors: {JsonDummyTestObject.}) readFrom: aString readStream ! ----- Method: JsonTests>>render:equals: (in category 'as yet unclassified') ----- render: anObject equals: aString self assert: (Json render: anObject) = aString! ----- Method: JsonTests>>simpleDummyObject (in category 'as yet unclassified') ----- simpleDummyObject ^ JsonDummyTestObject new a: 1; b: 2; c: 3; yourself! ----- Method: JsonTests>>testArray (in category 'as yet unclassified') ----- testArray self json: '[]' equals: #(). self json: '[[]]' equals: #(#()). self json: '[[], []]' equals: #(#() #()). self json: '["hi", "there"]' equals: #('hi' 'there'). self json: '[["a", "b", null]]' equals: #(('a' 'b' nil)).! ----- Method: JsonTests>>testAtomFalse (in category 'as yet unclassified') ----- testAtomFalse self json: 'false' equals: false. self json: ' false' equals: false. self json: 'false ' equals: false. self json: ' false ' equals: false. ! ----- Method: JsonTests>>testAtomNull (in category 'as yet unclassified') ----- testAtomNull self json: 'null' equals: nil. self json: ' null' equals: nil. self json: 'null ' equals: nil. self json: ' null ' equals: nil. ! ----- Method: JsonTests>>testAtomNumber (in category 'as yet unclassified') ----- testAtomNumber self json: '1' equals: 1. self json: '0123' equals: 123. self json: '1.23e2' equals: 123. self json: '-1' equals: -1. self json: '-0' equals: 0. self json: '[-1.2]' equals: #(-1.2).! ----- Method: JsonTests>>testAtomString (in category 'as yet unclassified') ----- testAtomString self json: '"hi"' equals: 'hi'. self json: '"\""' equals: '"'. self json: '"\\"' equals: '\'. self json: '""' equals: ''. self json: '"a\u0004b"' equals: ({$a. Character value: 4. $b} as: String). self json: '"a\nb"' equals: ({$a. Character lf. $b} as: String).! ----- Method: JsonTests>>testAtomTrue (in category 'as yet unclassified') ----- testAtomTrue self json: 'true' equals: true. self json: ' true' equals: true. self json: 'true ' equals: true. self json: ' true ' equals: true. ! ----- Method: JsonTests>>testCtor (in category 'as yet unclassified') ----- testCtor self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}' equals: self simpleDummyObject. self json: (Json render: self simpleDummyObject) equals: self simpleDummyObject.! ----- Method: JsonTests>>testDictionary (in category 'as yet unclassified') ----- testDictionary self json: '{}' equals: (JsonObject new). self json: '{"a": "a"}' equals: (JsonObject new at: 'a' put: 'a'; yourself). self json: '{"a": [[]]}' equals: (JsonObject new at: 'a' put: #(#()); yourself). self json: '{"a":"b", "b":"a"}' equals: (JsonObject new add: 'a'->'b'; add: 'b'->'a';yourself).! ----- Method: JsonTests>>testMissingCtor (in category 'as yet unclassified') ----- testMissingCtor self should: [self readFrom: '@Missing[]'] raise: JsonSyntaxError ! ----- Method: JsonTests>>testMissingCtorNoMap (in category 'as yet unclassified') ----- testMissingCtorNoMap self should: [Json new readFrom: '@Missing[]' readStream] raise: JsonSyntaxError! ----- Method: JsonTests>>testStringWithUnicode (in category 'as yet unclassified') ----- testStringWithUnicode | unicodeString | unicodeString := (Unicode value: 16r263A) asString. self json: '"\u263A"' equals:unicodeString. self json: '"', unicodeString, '"' equals: unicodeString. self render: unicodeString equals: '"', unicodeString, '"'.! ----- Method: JsonTests>>testWriteAssociation (in category 'as yet unclassified') ----- testWriteAssociation self render: 'key' -> 'value' equals: '"key": "value"'. self render: 'key' -> 2 equals: '"key": 2'. "keys should be strings" self render: 42 -> 2 equals: '"42": 2'. "try to do _something_ for more complex keys" self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'. ! ----- Method: JsonTests>>testWriteString (in category 'as yet unclassified') ----- testWriteString self render: '"' equals: '"\""'. self render: '\' equals: '"\\"'. self render: 'hi' equals: '"hi"'. self render: ({$a. Character lf. $b} as: String) equals: '"a\nb"'. self render: ({$a. Character value: 4. $b} as: String) equals: '"a\u0004b"'.! ----- Method: String>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | index start | aStream nextPut: $". start := 1. [ (index := self indexOfAnyOf: Json escapeSet startingAt: start) = 0 ] whileFalse: [ aStream next: index - start putAll: self startingAt: start. aStream nextPutAll: (Json escapeForCharacter: (self at: index)). start := index + 1 ]. start <= self size ifTrue: [ aStream next: self size + 1 - start putAll: self startingAt: start ]. aStream nextPut: $". ! Object subclass: #Json instanceVariableNames: 'stream currentCharacter arrayBufferStream stringBufferStream numberParser ctorMap' classVariableNames: '' poolDictionaries: '' category: 'JSON'! Json class instanceVariableNames: 'escapeArray escapeSet'! !Json commentStamp: 'ul 3/29/2016 04:38' prior: 0! I support reading and writing JSON (see
http://json.org
) formatted data - strings, numbers, boolean, nil, arrays and dictionaries. The implementation is mainly based on RFC 7159 (
https://www.ietf.org/rfc/rfc7159.txt
). It has been extended with syntax for invoking a prearranged list of constructors on read objects. Character encoding is not handled here. That's something you have to do before reading or after writing. Mixed processing is possible, but it's entirely your responsibility to keep things correct. Instance Variables stream: <PositionableStream> currentCharacter: <Character> arrayBufferStream: <WriteStream> stringBufferStream: <ReadWriteStream> numberParser: <ExtendedNumberParser> ctorMap: <Dictionary> For parsing, stream has to be initialized by sending #readFrom: to my instance. The parser will pre-read one character, which will be stored in currentCharacter, so the stream is expected to support position manipulation, hence it should be a PositionableStream. arrayBufferStream is WriteStream on an Array, which holds the values during array parsing (see #readArray). When there's a recursive call, this stream is used as a stack to separate the elements parsed in the previous invocation from the current one. stringBufferStream is a ReadWriteStream on a String, which holds the currently parsed String (see #readString), the currently parsed number (see #readNumber) or the name of the currently parsed constructor (see #readConstructor). Recursion has no effect on this. numberParser is a cached instance of ExtendedNumberParser. It's initialized on stringBufferStream, so it can quickly parse the number stored in it. ctorMap is the Dictionary holding the constructor classes by name. arrayBufferStream, stringBufferStream and numberParser are initialized lazily, and should not be accessed externally. Since these are shared objects, along with stream and currentCharacter, holding state, therefore a single instance of me shouldn't be used to parse multiple streams at the same time. For writing, you can use the methods in the class-side rendering category. To convert individual objects to a string, you can use #asJsonString, to serialize it on a stream, you can use #jsonWriteOn:. ! Json class instanceVariableNames: 'escapeArray escapeSet'! ----- Method: Json class>>escapeForCharacter: (in category 'accessing') ----- escapeForCharacter: c | asciiValue | (asciiValue := c asciiValue) < 128 ifTrue: [ ^escapeArray at: asciiValue + 1 ]. ^nil! ----- Method: Json class>>escapeSet (in category 'accessing') ----- escapeSet ^escapeSet! ----- Method: Json class>>initialize (in category 'class initialization') ----- initialize "Json initialize." | newEscapeArray newEscapeSet | newEscapeArray := Array new: 128. newEscapeSet := CharacterSet new. (0 to: 31), #(127) do: [ :each | newEscapeArray at: each + 1 put: '\u', (each printStringHex padded: #left to: 4 with: $0). newEscapeSet add: (Character value: each) ]. { $" -> '\"'. $\ -> '\\'. $/ -> '\/'. "A hack, so the generated JSON will always be HTML+Javascript compatible, because the generated JSON will not contain the seqence </script>." Character backspace -> '\b'. Character lf -> '\n'. Character newPage -> '\f'. Character cr -> '\r'. Character tab -> '\t'. } do: [ :each | newEscapeArray at: each key asciiValue + 1 put: each value. newEscapeSet add: each key ]. escapeArray := newEscapeArray. escapeSet := newEscapeSet! ----- Method: Json class>>mimeType (in category 'accessing') ----- mimeType ^ 'application/x-json'! ----- Method: Json class>>newWithConstructors: (in category 'instance creation') ----- newWithConstructors: aCollection | m | m := Dictionary new. aCollection do: [:each | (each isKindOf: Association) ifTrue: [m add: each] ifFalse: [m at: each name asString put: each]]. ^ self new ctorMap: m; yourself.! ----- Method: Json class>>readFrom: (in category 'instance creation') ----- readFrom: aStream ^ self new readFrom: aStream.! ----- Method: Json class>>render: (in category 'rendering') ----- render: anObject ^String streamContents: [ :stream | anObject jsonWriteOn: stream ]! ----- Method: Json class>>render:withConstructor:on: (in category 'rendering') ----- render: anObject withConstructor: aConstructorName on: aStream aStream nextPutAll: '@', aConstructorName. anObject jsonWriteOn: aStream. ! ----- Method: Json class>>renderInstanceVariables:of:on: (in category 'rendering') ----- renderInstanceVariables: aCollection of: anObject on: aStream | map | map := Dictionary new. aCollection do: [:ivarName | map at: ivarName put: (anObject instVarNamed: ivarName)]. self render: map withConstructor: anObject class name asString on: aStream! ----- Method: Json>>arrayBufferStream (in category 'private') ----- arrayBufferStream ^arrayBufferStream ifNil: [ arrayBufferStream := (Array new: 10) writeStream ]! ----- Method: Json>>consume: (in category 'private') ----- consume: aString 1 to: aString size do: [ :index | stream next == (aString at: index) ifFalse: [ self error: 'Expected ', (aString at: index) printString ] ]. currentCharacter := stream next! ----- Method: Json>>ctorMap (in category 'accessing') ----- ctorMap ^ ctorMap! ----- Method: Json>>ctorMap: (in category 'accessing') ----- ctorMap: m ctorMap := m! ----- Method: Json>>error: (in category 'error handling') ----- error: aString JsonSyntaxError signal: aString! ----- Method: Json>>interpretStringEscape: (in category 'private') ----- interpretStringEscape: aCharacter aCharacter == $b ifTrue: [ ^Character backspace ]. aCharacter == $n ifTrue: [ ^Character lf ]. aCharacter == $f ifTrue: [ ^Character newPage ]. aCharacter == $r ifTrue: [ ^Character cr ]. aCharacter == $t ifTrue: [ ^Character tab ]. aCharacter == $u ifTrue: [ ^self unescapeUnicode ]. aCharacter == $" ifTrue: [ ^aCharacter ]. aCharacter == $\ ifTrue: [ ^aCharacter ]. aCharacter == $/ ifTrue: [ ^aCharacter ]. self error: 'Unexpected escaped character: ', aCharacter asString ! ----- Method: Json>>readAny (in category 'private') ----- readAny self skipWhitespace. currentCharacter == ${ ifTrue: [ ^self readDictionary ]. currentCharacter == $[ ifTrue: [ ^self readArray ]. currentCharacter == $" ifTrue: [ ^self readString ]. currentCharacter == $t ifTrue: [ self consume: 'rue'. ^true ]. currentCharacter == $f ifTrue: [ self consume: 'alse'. ^false ]. currentCharacter == $n ifTrue: [ self consume: 'ull'. ^nil ]. currentCharacter == $- ifTrue: [ ^self readNumber: true ]. (#($0 $1 $2 $3 $4 $5 $6 $7 $8 $9) instVarsInclude: currentCharacter) ifTrue: [ ^self readNumber: false ]. currentCharacter == $@ ifTrue: [ ^self readConstructor ]. ^self error: 'Unknown Json input'! ----- Method: Json>>readArray (in category 'private') ----- readArray | initialPosition | currentCharacter := stream next. self skipWhitespace. currentCharacter == $] ifTrue: [ currentCharacter := stream next. ^#() ]. initialPosition := self arrayBufferStream position. [ arrayBufferStream nextPut: self readAny. self skipWhitespace. currentCharacter == $] ifTrue: [ | result | result := arrayBufferStream originalContents copyFrom: initialPosition + 1 to: arrayBufferStream position. arrayBufferStream position: initialPosition. currentCharacter := stream next. ^result ]. currentCharacter == $, ifFalse: [ self error: 'Unexpected character: ', currentCharacter asString ]. currentCharacter := stream next ] repeat. ! ----- Method: Json>>readConstructor (in category 'private') ----- readConstructor ctorMap ifNil: [ ^self error: 'No constructors were declared.' ]. self stringBufferStream resetToStart. [ (currentCharacter := stream next) == $. or: [ currentCharacter isLetter ] ] whileTrue: [ stringBufferStream nextPut: currentCharacter ]. (ctorMap at: stringBufferStream contents ifAbsent: nil) ifNotNil: [ :constructor | ^constructor constructFromJson: self readAny ]. self error: 'Unknown constructor: ', stringBufferStream contents! ----- Method: Json>>readDictionary (in category 'private') ----- readDictionary | result key commaNeeded | result := JsonObject new. commaNeeded := false. currentCharacter := stream next. [ self skipWhitespace. currentCharacter == $} ifTrue: [ currentCharacter := stream next. ^result ]. commaNeeded ifFalse: [ commaNeeded := true ] ifTrue: [ currentCharacter == $, ifFalse: [ self error: 'Missing comma' ]. currentCharacter := stream next. self skipWhitespace ]. currentCharacter == $" ifFalse: [ self error: 'Key in dictionary must be string' ]. key := self readString. self skipWhitespace. currentCharacter == $: ifFalse: [ self error: 'Missing colon' ]. currentCharacter := stream next. result at: key put: self readAny ] repeat. ! ----- Method: Json>>readFrom: (in category 'parsing') ----- readFrom: aStream | result | stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next. result := self readAny. stream atEnd ifFalse: [ stream skip: -1 ]. "Undo prereading." ^result! ----- Method: Json>>readNumber: (in category 'private') ----- readNumber: negative | result | negative ifFalse: [ stream skip: -1 ]. result := (numberParser ifNil: [ numberParser := JsonNumberParser new on: stream; failBlock: [ self error: 'Invalid number.' ]; yourself ]) nextNumber: negative. currentCharacter := stream next. ^result ! ----- Method: Json>>readString (in category 'private') ----- readString self stringBufferStream resetToStart. [ (currentCharacter := stream next) == $\ ifTrue: [ stringBufferStream nextPut: (self interpretStringEscape: stream next) ] ifFalse: [ currentCharacter == $" ifTrue: [ currentCharacter := stream next. ^stringBufferStream contents ]. stringBufferStream nextPut: currentCharacter ] ] repeat! ----- Method: Json>>skipWhitespace (in category 'private') ----- skipWhitespace [ currentCharacter isSeparator ] whileTrue: [ currentCharacter := stream next ]! ----- Method: Json>>stream (in category 'accessing') ----- stream "Answer the value of stream" ^ stream! ----- Method: Json>>stringBufferStream (in category 'private') ----- stringBufferStream ^stringBufferStream ifNil: [ stringBufferStream := ReadWriteStream on: (String new: 64) ]! ----- Method: Json>>unescapeUnicode (in category 'private') ----- unescapeUnicode | code digitValue | code := 0. 1 to: 4 do: [ :index | (digitValue := stream next digitValue) < 0 ifTrue: [ self error: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ code := code * 16 + digitValue ] ifFalse: [ self error: 'Invalid hexadecimal digit' ] ]. ^Character value: code! Object subclass: #JsonDummyTestObject instanceVariableNames: 'a b c' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonDummyTestObject class>>constructFromJson: (in category 'as yet unclassified') ----- constructFromJson: j ^ self new a: (j at: 'a'); b: (j at: 'b'); c: (j at: 'c'); yourself! ----- Method: JsonDummyTestObject>>= (in category 'as yet unclassified') ----- = other ^ other class == self class and: [ a = other a and: [ b = other b and: [ c = other c]]]! ----- Method: JsonDummyTestObject>>a (in category 'accessing') ----- a "Answer the value of a" ^ a! ----- Method: JsonDummyTestObject>>a: (in category 'accessing') ----- a: anObject "Set the value of a" a := anObject! ----- Method: JsonDummyTestObject>>b (in category 'accessing') ----- b "Answer the value of b" ^ b! ----- Method: JsonDummyTestObject>>b: (in category 'accessing') ----- b: anObject "Set the value of b" b := anObject! ----- Method: JsonDummyTestObject>>c (in category 'accessing') ----- c "Answer the value of c" ^ c! ----- Method: JsonDummyTestObject>>c: (in category 'accessing') ----- c: anObject "Set the value of c" c := anObject! ----- Method: JsonDummyTestObject>>jsonWriteOn: (in category 'as yet unclassified') ----- jsonWriteOn: s Json renderInstanceVariables: {#a. #b. #c} of: self on: s ! ----- Method: Object>>asJsonString (in category '*JSON') ----- asJsonString ^ String streamContents: [:str | self jsonWriteOn: str ]!
1
0
0
0
The Trunk: JSON-ul.47.mcz
by commits@source.squeak.org
08 Sep '22
08 Sep '22
Christoph Thiede uploaded a new version of JSON to project The Trunk:
http://source.squeak.org/trunk/JSON-ul.47.mcz
==================== Summary ==================== Name: JSON-ul.47 Author: ul Time: 22 January 2018, 12:32:52.716166 am UUID: f2d44ea2-c2a1-4da0-9dec-5a3b1823ab3b Ancestors: JSON-ul.46, JSON-klub.46 - merged with JSON-klub.46 - ScaledDecimal>>jsonWriteOn: rounds the number instead of truncating it ==================== Snapshot ==================== SystemOrganization addCategory: #JSON! ----- Method: Integer>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream ^ self printOn: aWriteStream base: 10 ! ----- Method: Association>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream self key asString jsonWriteOn: aStream. aStream nextPut: $:; space. self value jsonWriteOn: aStream.! ----- Method: False>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'false'! ----- Method: Number>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream self printOn: aWriteStream base: 10! OrderedDictionary subclass: #OrderedJsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: OrderedJsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: OrderedJsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: OrderedJsonObject>>doesNotUnderstand: (in category 'as yet unclassified') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: OrderedJsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: OrderedJsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: Time>>jsonWriteOn: (in category '*JSON') ----- jsonWriteOn: stream stream nextPut: $". self print24: true showSeconds: true on: stream. stream nextPut: $"! ----- Method: WriteStream>>jsonPrint: (in category '*json-printing') ----- jsonPrint: anObject anObject jsonWriteOn: self! ----- Method: Text>>jsonWriteOn: (in category '*json-printing') ----- jsonWriteOn: aStream self string jsonWriteOn: aStream! ----- Method: UndefinedObject>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream aWriteStream nextPutAll: 'null'! Error subclass: #JsonSyntaxError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonSyntaxError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! Class Json signals (possibly-indirect) instances of me when reading a JSON value from an input stream fails.! JsonSyntaxError subclass: #JsonIncompleteError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonIncompleteError commentStamp: 'tonyg 4/29/2016 11:37' prior: 0! I signal that reading a JSON value failed because more input is required, but that the input seen so far was not incorrect. Compare to JsonInvalidError. Be warned that reading numbers directly out of a stream can be ambiguous!! Consider reading from '1234'. Is the result intended to be 1234, or is there missing input, and the next character will be '5', making the result (possibly) 12345?! JsonSyntaxError subclass: #JsonInvalidError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonInvalidError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! I signal that reading a JSON value failed because the input didn't match the (extended)JSON grammar implemented by class Json.! ----- Method: Dictionary>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | needComma | needComma := false. aStream nextPut: ${. self associationsDo: [ :assoc | needComma ifTrue: [ aStream nextPut: $, ] ifFalse: [ needComma := true ]. assoc key jsonWriteOn: aStream. aStream nextPut: $:. assoc value jsonWriteOn: aStream ]. aStream nextPut: $}.! Dictionary subclass: #JsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: JsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: JsonObject>>doesNotUnderstand: (in category 'as yet unclassified') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: JsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: JsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: True>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'true'! NumberParser subclass: #JsonNumberParser instanceVariableNames: 'fraction' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonNumberParser>>allowPlusSign (in category 'accessing') ----- allowPlusSign ^false! ----- Method: JsonNumberParser>>allowPlusSignInExponent (in category 'accessing') ----- allowPlusSignInExponent ^true! ----- Method: JsonNumberParser>>error: (in category 'error') ----- error: aString JsonSyntaxError signal: aString! ----- Method: JsonNumberParser>>exponentLetters (in category 'accessing') ----- exponentLetters ^'eE'! ----- Method: JsonNumberParser>>initialize (in category 'initialize-release') ----- initialize super initialize. base := 10. fraction := Fraction numerator: 0 denominator: 1! ----- Method: JsonNumberParser>>makeFloatFromMantissa:exponent: (in category 'parsing-private') ----- makeFloatFromMantissa: mantissa exponent: exponent exponent = 0 ifTrue: [ ^mantissa asFloat ]. exponent >= 16 ifTrue: [ ^(mantissa * (self tenRaisedTo: exponent)) asFloat ]. (exponent >= -15 and: [ mantissa highBitOfMagnitude <= 54 ]) ifTrue: [ exponent >= 1 ifTrue: [ ^mantissa asFloat * (self tenRaisedTo: exponent) asFloat ]. ^mantissa asFloat / (self tenRaisedTo: exponent negated) asFloat ]. ^(fraction setNumerator: mantissa denominator: (self tenRaisedTo: exponent negated)) asFloat! ----- Method: JsonNumberParser>>nextElementaryLargeInteger (in category 'parsing-large int') ----- nextElementaryLargeInteger "Form an unsigned integer with incoming digits from sourceStream. Return this integer, or zero if no digits found. Stop reading if end of digits or if a LargeInteger is formed. Count the number of digits and the position of lastNonZero digit and store them in instVar." | digit value | value := 0. nDigits := 0. lastNonZero := 0. [ value isLarge ifTrue: [ ^value ]. ((digit := (sourceStream next ifNil: [ ^value ]) asInteger - 48 "$0 asInteger") < 0 or: [ digit >= 10 ]) ifTrue: [ sourceStream skip: -1. ^value ]. nDigits := nDigits + 1. digit = 0 ifFalse: [ lastNonZero := nDigits. value := value * 10 + digit ] ifTrue: [ value := value * 10 ] ] repeat! ----- Method: JsonNumberParser>>nextNumber: (in category 'parsing-public') ----- nextNumber: negative | numberOfTrailingZeroInIntegerPart numberOfTrailingZeroInFractionPart numberOfNonZeroFractionDigits mantissa value | integerPart := self nextUnsignedIntegerOrNil ifNil: [ ^self error: 'Missing integer part!!' ]. numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. (sourceStream peekFor: $.) ifFalse: [ "No fraction part" negative ifTrue: [ integerPart := integerPart negated ]. self readExponent ifFalse: [ ^integerPart ]. exponent > 0 ifTrue: [ ^integerPart * (self tenRaisedTo: exponent) ]. ^(Fraction numerator: integerPart denominator: (self tenRaisedTo: exponent negated)) asFloat ]. fractionPart := self nextUnsignedIntegerOrNil ifNil: [ self error: 'Missing fraction part!!' ]. fractionPart isZero ifTrue: [ self readExponent ifFalse: [ negative ifTrue: [ integerPart isZero ifTrue: [ ^Float negativeZero ]. ^integerPart negated ]. ^integerPart ]. exponent >= 0 ifTrue: [ negative ifTrue: [ integerPart := integerPart negated ]. ^integerPart * (self tenRaisedTo: exponent) ]. value := self makeFloatFromMantissa: integerPart exponent: exponent. negative ifTrue: [ ^value negated ]. ^value ]. numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. numberOfNonZeroFractionDigits := lastNonZero. self readExponent. exponent := exponent - numberOfNonZeroFractionDigits. mantissa := integerPart * (self tenRaisedTo: numberOfNonZeroFractionDigits) + (fractionPart // (self tenRaisedTo: numberOfTrailingZeroInFractionPart)). value := self makeFloatFromMantissa: mantissa exponent: exponent. negative ifTrue: [ ^value negated ]. ^value! ----- Method: JsonNumberParser>>nextUnsignedIntegerOrNil (in category 'parsing-public') ----- nextUnsignedIntegerOrNil "Form an unsigned integer with incoming digits from sourceStream. Answer this integer, or nil if no digit found. Count the number of digits and the position of lastNonZero digit and store them in instVar" | nPackets high nDigitsHigh lastNonZeroHigh low | "read no more digits than one elementary LargeInteger" high := self nextElementaryLargeInteger. nDigits = 0 ifTrue: [^nil]. "Not enough digits to form a LargeInteger, stop iteration" high isLarge ifFalse: [^high]. "We now have to engage arithmetic with LargeInteger Decompose the integer in a high and low packets of growing size:" nPackets := 1. nDigitsHigh := nDigits. lastNonZeroHigh := lastNonZero. [ low := self nextLargeIntegerBase: 10 nPackets: nPackets . high := high * (self tenRaisedTo: nDigits) + low. lastNonZero = 0 ifFalse: [lastNonZeroHigh := lastNonZero + nDigitsHigh]. nDigitsHigh := nDigitsHigh + nDigits. low isLarge] whileTrue: [nPackets := nPackets * 2]. nDigits := nDigitsHigh. lastNonZero := lastNonZeroHigh. ^high! ----- Method: JsonNumberParser>>tenRaisedTo: (in category 'parsing-private') ----- tenRaisedTo: anInteger anInteger >= 0 ifFalse: [ ^10 raisedToInteger: anInteger ]. anInteger >= 16 ifTrue: [ ^10 raisedToInteger: anInteger ]. ^#(1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000 100000000000 1000000000000 10000000000000 100000000000000 1000000000000000) at: anInteger + 1! ----- Method: ScaledDecimal>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: stream self printOn: stream showingDecimalPlaces: scale! ----- Method: Collection>>jsonWriteOn: (in category '*json') ----- jsonWriteOn: aStream "By default, use array braces " aStream nextPut: $[. self do: [:each | each jsonWriteOn: aStream ] separatedBy: [ aStream nextPut: $, ]. aStream nextPut: $]! TestCase subclass: #JsonTests instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonTests commentStamp: '<historical>' prior: 0! I provide a number of test cases for class Json.! ----- Method: JsonTests>>assertIncompleteJson: (in category 'as yet unclassified') ----- assertIncompleteJson: aString self should: [self readFrom: aString] raise: JsonIncompleteError! ----- Method: JsonTests>>assertInvalidJson: (in category 'as yet unclassified') ----- assertInvalidJson: aString self should: [self readFrom: aString] raise: JsonInvalidError! ----- Method: JsonTests>>json:equals: (in category 'as yet unclassified') ----- json: aString equals: aValue | readValue | readValue := self readFrom: aString. self assert: readValue = aValue.! ----- Method: JsonTests>>readFrom: (in category 'as yet unclassified') ----- readFrom: aString ^ (Json newWithConstructors: {JsonDummyTestObject.}) readFrom: aString readStream ! ----- Method: JsonTests>>render:equals: (in category 'as yet unclassified') ----- render: anObject equals: aString self assert: (Json render: anObject) = aString! ----- Method: JsonTests>>simpleDummyObject (in category 'as yet unclassified') ----- simpleDummyObject ^ JsonDummyTestObject new a: 1; b: 2; c: 3; yourself! ----- Method: JsonTests>>testArray (in category 'as yet unclassified') ----- testArray self json: '[]' equals: #(). self json: '[[]]' equals: #(#()). self json: '[[], []]' equals: #(#() #()). self json: '["hi", "there"]' equals: #('hi' 'there'). self json: '[["a", "b", null]]' equals: #(('a' 'b' nil)).! ----- Method: JsonTests>>testAtomFalse (in category 'as yet unclassified') ----- testAtomFalse self json: 'false' equals: false. self json: ' false' equals: false. self json: 'false ' equals: false. self json: ' false ' equals: false. ! ----- Method: JsonTests>>testAtomNull (in category 'as yet unclassified') ----- testAtomNull self json: 'null' equals: nil. self json: ' null' equals: nil. self json: 'null ' equals: nil. self json: ' null ' equals: nil. ! ----- Method: JsonTests>>testAtomNumber (in category 'as yet unclassified') ----- testAtomNumber self json: '1' equals: 1. self json: '0123' equals: 123. self json: '1.23e2' equals: 123. self json: '-1' equals: -1. self json: '-0' equals: 0. self json: '[-1.2]' equals: #(-1.2).! ----- Method: JsonTests>>testAtomString (in category 'as yet unclassified') ----- testAtomString self json: '"hi"' equals: 'hi'. self json: '"\""' equals: '"'. self json: '"\\"' equals: '\'. self json: '""' equals: ''. self json: '"a\u0004b"' equals: ({$a. Character value: 4. $b} as: String). self json: '"a\nb"' equals: ({$a. Character lf. $b} as: String).! ----- Method: JsonTests>>testAtomTrue (in category 'as yet unclassified') ----- testAtomTrue self json: 'true' equals: true. self json: ' true' equals: true. self json: 'true ' equals: true. self json: ' true ' equals: true. ! ----- Method: JsonTests>>testCtor (in category 'as yet unclassified') ----- testCtor self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}' equals: self simpleDummyObject. self json: (Json render: self simpleDummyObject) equals: self simpleDummyObject.! ----- Method: JsonTests>>testDictionary (in category 'as yet unclassified') ----- testDictionary self json: '{}' equals: (JsonObject new). self json: '{"a": "a"}' equals: (JsonObject new at: 'a' put: 'a'; yourself). self json: '{"a": [[]]}' equals: (JsonObject new at: 'a' put: #(#()); yourself). self json: '{"a":"b", "b":"a"}' equals: (JsonObject new add: 'a'->'b'; add: 'b'->'a';yourself).! ----- Method: JsonTests>>testIncomplete (in category 'as yet unclassified') ----- testIncomplete self assertIncompleteJson: ''. self assertIncompleteJson: ' '. self assertIncompleteJson: ' tr'. self assertIncompleteJson: 'tru'. self assertIncompleteJson: '['. self assertIncompleteJson: '[true'. self assertIncompleteJson: '[true,'. self assertIncompleteJson: '{'. self assertIncompleteJson: '{"hi"'. self assertIncompleteJson: '{"hi":'. self assertIncompleteJson: '{"hi":true'. self assertIncompleteJson: '{"hi":true,'. self assertIncompleteJson: '{"hi":true,"'. self assertIncompleteJson: '"hello'. self assertIncompleteJson: '"hello '. self assertIncompleteJson: '"hello\'. self assertIncompleteJson: '"\u26'.! ----- Method: JsonTests>>testInvalid (in category 'as yet unclassified') ----- testInvalid self assertInvalidJson: 'x'. self assertInvalidJson: ' x'. self assertInvalidJson: ' trx'. self assertInvalidJson: 'trux'. self assertInvalidJson: '.'. self assertInvalidJson: ':'. self assertInvalidJson: ','. self assertInvalidJson: ']'. self assertInvalidJson: '}'. self assertInvalidJson: '[x'. self assertInvalidJson: '[true t'. self assertInvalidJson: '[true,]'. self assertInvalidJson: '{]'. self assertInvalidJson: '{,'. self assertInvalidJson: '{"hi",'. self assertInvalidJson: '{"hi":x'. self assertInvalidJson: '{"hi":,'. self assertInvalidJson: '{"hi":true "'. self assertInvalidJson: '{"hi":true,}'. self assertInvalidJson: '\u263A'.! ----- Method: JsonTests>>testInvalidUnicodeEscapes (in category 'as yet unclassified') ----- testInvalidUnicodeEscapes self assertInvalidJson: '"\u26"'. "Note that naively reading four chars runs off the end here, so we might see JsonIncompleteError, which would be wrong." self assertInvalidJson: '"\u2Z"'. self assertInvalidJson: '"\u2Z44"'.! ----- Method: JsonTests>>testMissingCtor (in category 'as yet unclassified') ----- testMissingCtor self assertInvalidJson: '@Missing[]'! ----- Method: JsonTests>>testMissingCtorNoMap (in category 'as yet unclassified') ----- testMissingCtorNoMap self should: [Json new readFrom: '@Missing[]' readStream] raise: JsonInvalidError! ----- Method: JsonTests>>testStreaming (in category 'as yet unclassified') ----- testStreaming | j | j := Json new stream: 'truefalsetrue[]{}1.234 5.678"A""B"nullnull' readStream. self assert: j readAny equals: true. self assert: j readAny equals: false. self assert: j readAny equals: true. self assert: j readAny equals: #(). self assert: j readAny equals: JsonObject new. self assert: j readAny equals: 1.234. self assert: j readAny equals: 5.678. self assert: j readAny equals: 'A'. self assert: j readAny equals: 'B'. self assert: j readAny equals: nil. self assert: j readAny equals: nil.! ----- Method: JsonTests>>testStringWithUnicode (in category 'as yet unclassified') ----- testStringWithUnicode | unicodeString | unicodeString := (Unicode value: 16r263A) asString. self json: '"\u263A"' equals:unicodeString. self json: '"\u263a"' equals:unicodeString. self json: '"', unicodeString, '"' equals: unicodeString. self render: unicodeString equals: '"', unicodeString, '"'.! ----- Method: JsonTests>>testWriteAssociation (in category 'as yet unclassified') ----- testWriteAssociation self render: 'key' -> 'value' equals: '"key": "value"'. self render: 'key' -> 2 equals: '"key": 2'. "keys should be strings" self render: 42 -> 2 equals: '"42": 2'. "try to do _something_ for more complex keys" self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'. ! ----- Method: JsonTests>>testWriteString (in category 'as yet unclassified') ----- testWriteString self render: '"' equals: '"\""'. self render: '\' equals: '"\\"'. self render: 'hi' equals: '"hi"'. self render: ({$a. Character lf. $b} as: String) equals: '"a\nb"'. self render: ({$a. Character value: 4. $b} as: String) equals: '"a\u0004b"'.! ----- Method: String>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | index start | aStream nextPut: $". start := 1. [ (index := self indexOfAnyOf: Json escapeSet startingAt: start) = 0 ] whileFalse: [ aStream next: index - start putAll: self startingAt: start. aStream nextPutAll: (Json escapeForCharacter: (self at: index)). start := index + 1 ]. start <= self size ifTrue: [ aStream next: self size + 1 - start putAll: self startingAt: start ]. aStream nextPut: $". ! Object subclass: #Json instanceVariableNames: 'stream currentCharacter arrayBufferStream stringBufferStream numberParser ctorMap' classVariableNames: '' poolDictionaries: '' category: 'JSON'! Json class instanceVariableNames: 'escapeArray escapeSet'! !Json commentStamp: 'ul 3/29/2016 04:38' prior: 0! I support reading and writing JSON (see
http://json.org
) formatted data - strings, numbers, boolean, nil, arrays and dictionaries. The implementation is mainly based on RFC 7159 (
https://www.ietf.org/rfc/rfc7159.txt
). It has been extended with syntax for invoking a prearranged list of constructors on read objects. Character encoding is not handled here. That's something you have to do before reading or after writing. Mixed processing is possible, but it's entirely your responsibility to keep things correct. Instance Variables stream: <PositionableStream> currentCharacter: <Character> arrayBufferStream: <WriteStream> stringBufferStream: <ReadWriteStream> numberParser: <ExtendedNumberParser> ctorMap: <Dictionary> For parsing, stream has to be initialized by sending #readFrom: to my instance. The parser will pre-read one character, which will be stored in currentCharacter, so the stream is expected to support position manipulation, hence it should be a PositionableStream. arrayBufferStream is WriteStream on an Array, which holds the values during array parsing (see #readArray). When there's a recursive call, this stream is used as a stack to separate the elements parsed in the previous invocation from the current one. stringBufferStream is a ReadWriteStream on a String, which holds the currently parsed String (see #readString), the currently parsed number (see #readNumber) or the name of the currently parsed constructor (see #readConstructor). Recursion has no effect on this. numberParser is a cached instance of ExtendedNumberParser. It's initialized on stringBufferStream, so it can quickly parse the number stored in it. ctorMap is the Dictionary holding the constructor classes by name. arrayBufferStream, stringBufferStream and numberParser are initialized lazily, and should not be accessed externally. Since these are shared objects, along with stream and currentCharacter, holding state, therefore a single instance of me shouldn't be used to parse multiple streams at the same time. For writing, you can use the methods in the class-side rendering category. To convert individual objects to a string, you can use #asJsonString, to serialize it on a stream, you can use #jsonWriteOn:. ! Json class instanceVariableNames: 'escapeArray escapeSet'! ----- Method: Json class>>escapeForCharacter: (in category 'accessing') ----- escapeForCharacter: c | asciiValue | (asciiValue := c asciiValue) < 128 ifTrue: [ ^escapeArray at: asciiValue + 1 ]. ^nil! ----- Method: Json class>>escapeSet (in category 'accessing') ----- escapeSet ^escapeSet! ----- Method: Json class>>initialize (in category 'class initialization') ----- initialize "Json initialize." | newEscapeArray newEscapeSet | newEscapeArray := Array new: 128. newEscapeSet := CharacterSet new. (0 to: 31), #(127) do: [ :each | newEscapeArray at: each + 1 put: '\u', (each printStringHex padded: #left to: 4 with: $0). newEscapeSet add: (Character value: each) ]. { $" -> '\"'. $\ -> '\\'. $/ -> '\/'. "A hack, so the generated JSON will always be HTML+Javascript compatible, because the generated JSON will not contain the seqence </script>." Character backspace -> '\b'. Character lf -> '\n'. Character newPage -> '\f'. Character cr -> '\r'. Character tab -> '\t'. } do: [ :each | newEscapeArray at: each key asciiValue + 1 put: each value. newEscapeSet add: each key ]. escapeArray := newEscapeArray. escapeSet := newEscapeSet! ----- Method: Json class>>mimeType (in category 'accessing') ----- mimeType ^ 'application/x-json'! ----- Method: Json class>>newWithConstructors: (in category 'instance creation') ----- newWithConstructors: aCollection | m | m := Dictionary new. aCollection do: [:each | (each isKindOf: Association) ifTrue: [m add: each] ifFalse: [m at: each name asString put: each]]. ^ self new ctorMap: m; yourself.! ----- Method: Json class>>readFrom: (in category 'instance creation') ----- readFrom: aStream ^ self new readFrom: aStream.! ----- Method: Json class>>render: (in category 'rendering') ----- render: anObject ^String streamContents: [ :stream | anObject jsonWriteOn: stream ]! ----- Method: Json class>>render:withConstructor:on: (in category 'rendering') ----- render: anObject withConstructor: aConstructorName on: aStream aStream nextPutAll: '@', aConstructorName. anObject jsonWriteOn: aStream. ! ----- Method: Json class>>renderInstanceVariables:of:on: (in category 'rendering') ----- renderInstanceVariables: aCollection of: anObject on: aStream | map | map := Dictionary new. aCollection do: [:ivarName | map at: ivarName put: (anObject instVarNamed: ivarName)]. self render: map withConstructor: anObject class name asString on: aStream! ----- Method: Json>>arrayBufferStream (in category 'private') ----- arrayBufferStream ^arrayBufferStream ifNil: [ arrayBufferStream := (Array new: 10) writeStream ]! ----- Method: Json>>consume: (in category 'private') ----- consume: aString | message | 1 to: aString size do: [ :index | (currentCharacter := stream next) == (aString at: index) ifFalse: [ message := 'Expected ', (aString at: index) printString. currentCharacter ifNil: [ self incomplete: message ] ifNotNil: [ self invalid: message ] ] ]. currentCharacter := stream next! ----- Method: Json>>ctorMap (in category 'accessing') ----- ctorMap ^ ctorMap! ----- Method: Json>>ctorMap: (in category 'accessing') ----- ctorMap: m ctorMap := m! ----- Method: Json>>error: (in category 'error handling') ----- error: aString (currentCharacter isNil and: [ stream atEnd ]) ifTrue: [ self incomplete: aString ] ifFalse: [ self invalid: aString ]! ----- Method: Json>>incomplete: (in category 'error handling') ----- incomplete: aString JsonIncompleteError signal: aString! ----- Method: Json>>interpretStringEscape: (in category 'private') ----- interpretStringEscape: aCharacter aCharacter == $b ifTrue: [ ^Character backspace ]. aCharacter == $n ifTrue: [ ^Character lf ]. aCharacter == $f ifTrue: [ ^Character newPage ]. aCharacter == $r ifTrue: [ ^Character cr ]. aCharacter == $t ifTrue: [ ^Character tab ]. aCharacter == $u ifTrue: [ ^self unescapeUnicode ]. aCharacter == $" ifTrue: [ ^aCharacter ]. aCharacter == $\ ifTrue: [ ^aCharacter ]. aCharacter == $/ ifTrue: [ ^aCharacter ]. self error: 'Unexpected escaped character: ', aCharacter asString ! ----- Method: Json>>invalid: (in category 'error handling') ----- invalid: aString JsonInvalidError signal: aString! ----- Method: Json>>readAny (in category 'private') ----- readAny self skipWhitespace. currentCharacter == ${ ifTrue: [ ^self readDictionary ]. currentCharacter == $[ ifTrue: [ ^self readArray ]. currentCharacter == $" ifTrue: [ ^self readString ]. currentCharacter == $t ifTrue: [ self consume: 'rue'. ^true ]. currentCharacter == $f ifTrue: [ self consume: 'alse'. ^false ]. currentCharacter == $n ifTrue: [ self consume: 'ull'. ^nil ]. currentCharacter == $- ifTrue: [ ^self readNumber: true ]. (#($0 $1 $2 $3 $4 $5 $6 $7 $8 $9) instVarsInclude: currentCharacter) ifTrue: [ ^self readNumber: false ]. currentCharacter == $@ ifTrue: [ ^self readConstructor ]. self invalid: 'Unknown Json input'! ----- Method: Json>>readArray (in category 'private') ----- readArray | initialPosition | currentCharacter := stream next. self skipWhitespace. currentCharacter == $] ifTrue: [ currentCharacter := stream next. ^#() ]. initialPosition := self arrayBufferStream position. [ arrayBufferStream nextPut: self readAny. self skipWhitespace. currentCharacter == $] ifTrue: [ | result | result := arrayBufferStream originalContents copyFrom: initialPosition + 1 to: arrayBufferStream position. arrayBufferStream position: initialPosition. currentCharacter := stream next. ^result ]. currentCharacter == $, ifFalse: [ self error: 'Unexpected character: ', currentCharacter asString ]. currentCharacter := stream next ] repeat. ! ----- Method: Json>>readConstructor (in category 'private') ----- readConstructor ctorMap ifNil: [ ^self error: 'No constructors were declared.' ]. self resetStringBufferStream. [ (currentCharacter := stream next) == $. or: [ currentCharacter isLetter ] ] whileTrue: [ stringBufferStream nextPut: currentCharacter ]. (ctorMap at: stringBufferStream contents ifAbsent: nil) ifNotNil: [ :constructor | ^constructor constructFromJson: self readAny ]. self error: 'Unknown constructor: ', stringBufferStream contents! ----- Method: Json>>readDictionary (in category 'private') ----- readDictionary | result key commaNeeded | result := JsonObject new. commaNeeded := false. currentCharacter := stream next. [ self skipWhitespace. currentCharacter == $} ifTrue: [ currentCharacter := stream next. ^result ]. commaNeeded ifFalse: [ commaNeeded := true ] ifTrue: [ currentCharacter == $, ifFalse: [ self error: 'Missing comma' ]. currentCharacter := stream next. self skipWhitespace ]. currentCharacter == $" ifFalse: [ self error: 'Key in dictionary must be string' ]. key := self readString. self skipWhitespace. currentCharacter == $: ifFalse: [ self error: 'Missing colon' ]. currentCharacter := stream next. result at: key put: self readAny ] repeat. ! ----- Method: Json>>readFrom: (in category 'parsing') ----- readFrom: aStream | result | stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next. result := self readAny. stream atEnd ifFalse: [ stream skip: -1 ]. "Undo prereading." ^result! ----- Method: Json>>readNumber: (in category 'private') ----- readNumber: negative | result | negative ifFalse: [ stream skip: -1 ]. result := (numberParser ifNil: [ numberParser := JsonNumberParser new on: stream; failBlock: [ self error: 'Invalid number.' ]; yourself ]) nextNumber: negative. currentCharacter := stream next. ^result ! ----- Method: Json>>readString (in category 'private') ----- readString self resetStringBufferStream. [ currentCharacter := stream next ifNil: [self incomplete: 'Unexpected end of string' ]. currentCharacter == $" ifTrue: [ currentCharacter := stream next. ^stringBufferStream contents ]. currentCharacter == $\ ifTrue: [ stringBufferStream nextPut: (self interpretStringEscape: (stream next ifNil: [ self incomplete: 'Unexpected end of string' ])) ] ifFalse: [ stringBufferStream nextPut: currentCharacter ] ] repeat! ----- Method: Json>>resetStringBufferStream (in category 'private') ----- resetStringBufferStream stringBufferStream ifNil: [ stringBufferStream := ReadWriteStream on: (String new: 64) ] ifNotNil: [ stringBufferStream resetToStart ]! ----- Method: Json>>skipWhitespace (in category 'private') ----- skipWhitespace currentCharacter ifNil: [ self incomplete: 'Input stream is empty' ]. [ currentCharacter isSeparator ] whileTrue: [ currentCharacter := stream next ifNil: [ self incomplete: 'Input stream is empty' ]. ]! ----- Method: Json>>stream (in category 'accessing') ----- stream "Answer the value of stream" ^ stream! ----- Method: Json>>stream: (in category 'private') ----- stream: aStream "For testing purposes only" stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next! ----- Method: Json>>unescapeUnicode (in category 'private') ----- unescapeUnicode | code digitValue | code := 0. 1 to: 4 do: [ :index | (digitValue := (stream next ifNil: [ self incomplete: 'Unexpected end of stream' ]) digitValue) < 0 ifTrue: [ self invalid: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ code := code * 16 + digitValue ] ifFalse: [ self invalid: 'Invalid hexadecimal digit' ] ]. ^Character value: code! Object subclass: #JsonDummyTestObject instanceVariableNames: 'a b c' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonDummyTestObject class>>constructFromJson: (in category 'as yet unclassified') ----- constructFromJson: j ^ self new a: (j at: 'a'); b: (j at: 'b'); c: (j at: 'c'); yourself! ----- Method: JsonDummyTestObject>>= (in category 'as yet unclassified') ----- = other ^ other class == self class and: [ a = other a and: [ b = other b and: [ c = other c]]]! ----- Method: JsonDummyTestObject>>a (in category 'accessing') ----- a "Answer the value of a" ^ a! ----- Method: JsonDummyTestObject>>a: (in category 'accessing') ----- a: anObject "Set the value of a" a := anObject! ----- Method: JsonDummyTestObject>>b (in category 'accessing') ----- b "Answer the value of b" ^ b! ----- Method: JsonDummyTestObject>>b: (in category 'accessing') ----- b: anObject "Set the value of b" b := anObject! ----- Method: JsonDummyTestObject>>c (in category 'accessing') ----- c "Answer the value of c" ^ c! ----- Method: JsonDummyTestObject>>c: (in category 'accessing') ----- c: anObject "Set the value of c" c := anObject! ----- Method: JsonDummyTestObject>>jsonWriteOn: (in category 'as yet unclassified') ----- jsonWriteOn: s Json renderInstanceVariables: {#a. #b. #c} of: self on: s ! ----- Method: Object>>asJsonString (in category '*JSON') ----- asJsonString ^ String streamContents: [:str | self jsonWriteOn: str ]!
1
0
0
0
The Trunk: JSON-ul.48.mcz
by commits@source.squeak.org
08 Sep '22
08 Sep '22
Christoph Thiede uploaded a new version of JSON to project The Trunk:
http://source.squeak.org/trunk/JSON-ul.48.mcz
==================== Summary ==================== Name: JSON-ul.48 Author: ul Time: 16 July 2018, 4:00:43.631343 pm UUID: 0f8f75fe-fa5f-4326-9ff3-c308d74c77e8 Ancestors: JSON-ul.47 JsonObject & OrderedJsonObject - #at:put: verifies that the key is a String - make #at:put: return self instead of the value stored. This allows one to omit the #yourself send when an object is scripted, which sometimes happens and results in hard to debug issues. - allow one to change the default dictionary class before parsing via Json >> #dictionaryClass:. This makes OrderedJsonObject more useful. - updated the tests and categorized the test methods ==================== Snapshot ==================== SystemOrganization addCategory: #JSON! ----- Method: Integer>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream ^ self printOn: aWriteStream base: 10 ! ----- Method: Association>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream self key asString jsonWriteOn: aStream. aStream nextPut: $:; space. self value jsonWriteOn: aStream.! ----- Method: False>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'false'! ----- Method: Number>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream self printOn: aWriteStream base: 10! OrderedDictionary subclass: #OrderedJsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: OrderedJsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: OrderedJsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: OrderedJsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: OrderedJsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: OrderedJsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: OrderedJsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: Time>>jsonWriteOn: (in category '*JSON') ----- jsonWriteOn: stream stream nextPut: $". self print24: true showSeconds: true on: stream. stream nextPut: $"! ----- Method: WriteStream>>jsonPrint: (in category '*json-printing') ----- jsonPrint: anObject anObject jsonWriteOn: self! ----- Method: Text>>jsonWriteOn: (in category '*json-printing') ----- jsonWriteOn: aStream self string jsonWriteOn: aStream! ----- Method: UndefinedObject>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream aWriteStream nextPutAll: 'null'! Error subclass: #JsonSyntaxError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonSyntaxError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! Class Json signals (possibly-indirect) instances of me when reading a JSON value from an input stream fails.! JsonSyntaxError subclass: #JsonIncompleteError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonIncompleteError commentStamp: 'tonyg 4/29/2016 11:37' prior: 0! I signal that reading a JSON value failed because more input is required, but that the input seen so far was not incorrect. Compare to JsonInvalidError. Be warned that reading numbers directly out of a stream can be ambiguous!! Consider reading from '1234'. Is the result intended to be 1234, or is there missing input, and the next character will be '5', making the result (possibly) 12345?! JsonSyntaxError subclass: #JsonInvalidError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonInvalidError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! I signal that reading a JSON value failed because the input didn't match the (extended)JSON grammar implemented by class Json.! ----- Method: Dictionary>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | needComma | needComma := false. aStream nextPut: ${. self associationsDo: [ :assoc | needComma ifTrue: [ aStream nextPut: $, ] ifFalse: [ needComma := true ]. assoc key jsonWriteOn: aStream. aStream nextPut: $:. assoc value jsonWriteOn: aStream ]. aStream nextPut: $}.! Dictionary subclass: #JsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: JsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: JsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: JsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: JsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: JsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: True>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'true'! NumberParser subclass: #JsonNumberParser instanceVariableNames: 'fraction' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonNumberParser>>allowPlusSign (in category 'accessing') ----- allowPlusSign ^false! ----- Method: JsonNumberParser>>allowPlusSignInExponent (in category 'accessing') ----- allowPlusSignInExponent ^true! ----- Method: JsonNumberParser>>error: (in category 'error') ----- error: aString JsonSyntaxError signal: aString! ----- Method: JsonNumberParser>>exponentLetters (in category 'accessing') ----- exponentLetters ^'eE'! ----- Method: JsonNumberParser>>initialize (in category 'initialize-release') ----- initialize super initialize. base := 10. fraction := Fraction numerator: 0 denominator: 1! ----- Method: JsonNumberParser>>makeFloatFromMantissa:exponent: (in category 'parsing-private') ----- makeFloatFromMantissa: mantissa exponent: exponent exponent = 0 ifTrue: [ ^mantissa asFloat ]. exponent >= 16 ifTrue: [ ^(mantissa * (self tenRaisedTo: exponent)) asFloat ]. (exponent >= -15 and: [ mantissa highBitOfMagnitude <= 54 ]) ifTrue: [ exponent >= 1 ifTrue: [ ^mantissa asFloat * (self tenRaisedTo: exponent) asFloat ]. ^mantissa asFloat / (self tenRaisedTo: exponent negated) asFloat ]. ^(fraction setNumerator: mantissa denominator: (self tenRaisedTo: exponent negated)) asFloat! ----- Method: JsonNumberParser>>nextElementaryLargeInteger (in category 'parsing-large int') ----- nextElementaryLargeInteger "Form an unsigned integer with incoming digits from sourceStream. Return this integer, or zero if no digits found. Stop reading if end of digits or if a LargeInteger is formed. Count the number of digits and the position of lastNonZero digit and store them in instVar." | digit value | value := 0. nDigits := 0. lastNonZero := 0. [ value isLarge ifTrue: [ ^value ]. ((digit := (sourceStream next ifNil: [ ^value ]) asInteger - 48 "$0 asInteger") < 0 or: [ digit >= 10 ]) ifTrue: [ sourceStream skip: -1. ^value ]. nDigits := nDigits + 1. digit = 0 ifFalse: [ lastNonZero := nDigits. value := value * 10 + digit ] ifTrue: [ value := value * 10 ] ] repeat! ----- Method: JsonNumberParser>>nextNumber: (in category 'parsing-public') ----- nextNumber: negative | numberOfTrailingZeroInIntegerPart numberOfTrailingZeroInFractionPart numberOfNonZeroFractionDigits mantissa value | integerPart := self nextUnsignedIntegerOrNil ifNil: [ ^self error: 'Missing integer part!!' ]. numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. (sourceStream peekFor: $.) ifFalse: [ "No fraction part" negative ifTrue: [ integerPart := integerPart negated ]. self readExponent ifFalse: [ ^integerPart ]. exponent > 0 ifTrue: [ ^integerPart * (self tenRaisedTo: exponent) ]. ^(Fraction numerator: integerPart denominator: (self tenRaisedTo: exponent negated)) asFloat ]. fractionPart := self nextUnsignedIntegerOrNil ifNil: [ self error: 'Missing fraction part!!' ]. fractionPart isZero ifTrue: [ self readExponent ifFalse: [ negative ifTrue: [ integerPart isZero ifTrue: [ ^Float negativeZero ]. ^integerPart negated ]. ^integerPart ]. exponent >= 0 ifTrue: [ negative ifTrue: [ integerPart := integerPart negated ]. ^integerPart * (self tenRaisedTo: exponent) ]. value := self makeFloatFromMantissa: integerPart exponent: exponent. negative ifTrue: [ ^value negated ]. ^value ]. numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. numberOfNonZeroFractionDigits := lastNonZero. self readExponent. exponent := exponent - numberOfNonZeroFractionDigits. mantissa := integerPart * (self tenRaisedTo: numberOfNonZeroFractionDigits) + (fractionPart // (self tenRaisedTo: numberOfTrailingZeroInFractionPart)). value := self makeFloatFromMantissa: mantissa exponent: exponent. negative ifTrue: [ ^value negated ]. ^value! ----- Method: JsonNumberParser>>nextUnsignedIntegerOrNil (in category 'parsing-public') ----- nextUnsignedIntegerOrNil "Form an unsigned integer with incoming digits from sourceStream. Answer this integer, or nil if no digit found. Count the number of digits and the position of lastNonZero digit and store them in instVar" | nPackets high nDigitsHigh lastNonZeroHigh low | "read no more digits than one elementary LargeInteger" high := self nextElementaryLargeInteger. nDigits = 0 ifTrue: [^nil]. "Not enough digits to form a LargeInteger, stop iteration" high isLarge ifFalse: [^high]. "We now have to engage arithmetic with LargeInteger Decompose the integer in a high and low packets of growing size:" nPackets := 1. nDigitsHigh := nDigits. lastNonZeroHigh := lastNonZero. [ low := self nextLargeIntegerBase: 10 nPackets: nPackets . high := high * (self tenRaisedTo: nDigits) + low. lastNonZero = 0 ifFalse: [lastNonZeroHigh := lastNonZero + nDigitsHigh]. nDigitsHigh := nDigitsHigh + nDigits. low isLarge] whileTrue: [nPackets := nPackets * 2]. nDigits := nDigitsHigh. lastNonZero := lastNonZeroHigh. ^high! ----- Method: JsonNumberParser>>tenRaisedTo: (in category 'parsing-private') ----- tenRaisedTo: anInteger anInteger >= 0 ifFalse: [ ^10 raisedToInteger: anInteger ]. anInteger >= 16 ifTrue: [ ^10 raisedToInteger: anInteger ]. ^#(1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000 100000000000 1000000000000 10000000000000 100000000000000 1000000000000000) at: anInteger + 1! ----- Method: ScaledDecimal>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: stream self printOn: stream showingDecimalPlaces: scale! ----- Method: Collection>>jsonWriteOn: (in category '*json') ----- jsonWriteOn: aStream "By default, use array braces " aStream nextPut: $[. self do: [:each | each jsonWriteOn: aStream ] separatedBy: [ aStream nextPut: $, ]. aStream nextPut: $]! TestCase subclass: #JsonTests instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonTests commentStamp: '<historical>' prior: 0! I provide a number of test cases for class Json.! ----- Method: JsonTests>>assertIncompleteJson: (in category 'helpers') ----- assertIncompleteJson: aString self should: [self readFrom: aString] raise: JsonIncompleteError! ----- Method: JsonTests>>assertInvalidJson: (in category 'helpers') ----- assertInvalidJson: aString self should: [self readFrom: aString] raise: JsonInvalidError! ----- Method: JsonTests>>json:equals: (in category 'helpers') ----- json: aString equals: aValue | readValue | readValue := self readFrom: aString. self assert: readValue = aValue.! ----- Method: JsonTests>>readFrom: (in category 'helpers') ----- readFrom: aString ^ (Json newWithConstructors: {JsonDummyTestObject.}) readFrom: aString readStream ! ----- Method: JsonTests>>render:equals: (in category 'helpers') ----- render: anObject equals: aString self assert: (Json render: anObject) = aString! ----- Method: JsonTests>>simpleDummyObject (in category 'helpers') ----- simpleDummyObject ^ JsonDummyTestObject new a: 1; b: 2; c: 3; yourself! ----- Method: JsonTests>>testArray (in category 'tests') ----- testArray self json: '[]' equals: #(). self json: '[[]]' equals: #(#()). self json: '[[], []]' equals: #(#() #()). self json: '["hi", "there"]' equals: #('hi' 'there'). self json: '[["a", "b", null]]' equals: #(('a' 'b' nil)).! ----- Method: JsonTests>>testAtomFalse (in category 'tests') ----- testAtomFalse self json: 'false' equals: false. self json: ' false' equals: false. self json: 'false ' equals: false. self json: ' false ' equals: false. ! ----- Method: JsonTests>>testAtomNull (in category 'tests') ----- testAtomNull self json: 'null' equals: nil. self json: ' null' equals: nil. self json: 'null ' equals: nil. self json: ' null ' equals: nil. ! ----- Method: JsonTests>>testAtomNumber (in category 'tests') ----- testAtomNumber self json: '1' equals: 1. self json: '0123' equals: 123. self json: '1.23e2' equals: 123. self json: '-1' equals: -1. self json: '-0' equals: 0. self json: '[-1.2]' equals: #(-1.2).! ----- Method: JsonTests>>testAtomString (in category 'tests') ----- testAtomString self json: '"hi"' equals: 'hi'. self json: '"\""' equals: '"'. self json: '"\\"' equals: '\'. self json: '""' equals: ''. self json: '"a\u0004b"' equals: ({$a. Character value: 4. $b} as: String). self json: '"a\nb"' equals: ({$a. Character lf. $b} as: String).! ----- Method: JsonTests>>testAtomTrue (in category 'tests') ----- testAtomTrue self json: 'true' equals: true. self json: ' true' equals: true. self json: 'true ' equals: true. self json: ' true ' equals: true. ! ----- Method: JsonTests>>testCtor (in category 'tests') ----- testCtor self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}' equals: self simpleDummyObject. self json: (Json render: self simpleDummyObject) equals: self simpleDummyObject.! ----- Method: JsonTests>>testDictionary (in category 'tests') ----- testDictionary self json: '{}' equals: (JsonObject new). self json: '{"a": "a"}' equals: (JsonObject new at: 'a' put: 'a'; yourself). self json: '{"a": [[]]}' equals: (JsonObject new at: 'a' put: #(#()); yourself). self json: '{"a":"b", "b":"a"}' equals: (JsonObject new add: 'a'->'b'; add: 'b'->'a';yourself).! ----- Method: JsonTests>>testDictionaryClass (in category 'tests') ----- testDictionaryClass | parser | parser := Json new. self assert: (parser readFrom: '{}' readStream) class == JsonObject. parser dictionaryClass: OrderedJsonObject. self assert: (parser readFrom: '{}' readStream) class == OrderedJsonObject. self assert: (parser readFrom: '{"x":{"y":{}}}' readStream) x y class == OrderedJsonObject! ----- Method: JsonTests>>testIncomplete (in category 'tests') ----- testIncomplete self assertIncompleteJson: ''. self assertIncompleteJson: ' '. self assertIncompleteJson: ' tr'. self assertIncompleteJson: 'tru'. self assertIncompleteJson: '['. self assertIncompleteJson: '[true'. self assertIncompleteJson: '[true,'. self assertIncompleteJson: '{'. self assertIncompleteJson: '{"hi"'. self assertIncompleteJson: '{"hi":'. self assertIncompleteJson: '{"hi":true'. self assertIncompleteJson: '{"hi":true,'. self assertIncompleteJson: '{"hi":true,"'. self assertIncompleteJson: '"hello'. self assertIncompleteJson: '"hello '. self assertIncompleteJson: '"hello\'. self assertIncompleteJson: '"\u26'.! ----- Method: JsonTests>>testInvalid (in category 'tests') ----- testInvalid self assertInvalidJson: 'x'. self assertInvalidJson: ' x'. self assertInvalidJson: ' trx'. self assertInvalidJson: 'trux'. self assertInvalidJson: '.'. self assertInvalidJson: ':'. self assertInvalidJson: ','. self assertInvalidJson: ']'. self assertInvalidJson: '}'. self assertInvalidJson: '[x'. self assertInvalidJson: '[true t'. self assertInvalidJson: '[true,]'. self assertInvalidJson: '{]'. self assertInvalidJson: '{,'. self assertInvalidJson: '{"hi",'. self assertInvalidJson: '{"hi":x'. self assertInvalidJson: '{"hi":,'. self assertInvalidJson: '{"hi":true "'. self assertInvalidJson: '{"hi":true,}'. self assertInvalidJson: '\u263A'.! ----- Method: JsonTests>>testInvalidUnicodeEscapes (in category 'tests') ----- testInvalidUnicodeEscapes self assertInvalidJson: '"\u26"'. "Note that naively reading four chars runs off the end here, so we might see JsonIncompleteError, which would be wrong." self assertInvalidJson: '"\u2Z"'. self assertInvalidJson: '"\u2Z44"'.! ----- Method: JsonTests>>testJsonObjectAtPutReturnsTheObject (in category 'tests') ----- testJsonObjectAtPutReturnsTheObject { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self assert: (json at: 'foo' put: 1) == json. self assert: (json foo: 1) == json ]! ----- Method: JsonTests>>testMissingCtor (in category 'tests') ----- testMissingCtor self assertInvalidJson: '@Missing[]'! ----- Method: JsonTests>>testMissingCtorNoMap (in category 'tests') ----- testMissingCtorNoMap self should: [Json new readFrom: '@Missing[]' readStream] raise: JsonInvalidError! ----- Method: JsonTests>>testNonStringKeysRaiseError (in category 'tests') ----- testNonStringKeysRaiseError { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self should: [ json at: 1 put: 1 ] raise: Error. self assert: json isEmpty. json at: '1' put: 1. self assert: 1 equals: (json at: '1') ]! ----- Method: JsonTests>>testStreaming (in category 'tests') ----- testStreaming | j | j := Json new stream: 'truefalsetrue[]{}1.234 5.678"A""B"nullnull' readStream. self assert: j readAny equals: true. self assert: j readAny equals: false. self assert: j readAny equals: true. self assert: j readAny equals: #(). self assert: j readAny equals: JsonObject new. self assert: j readAny equals: 1.234. self assert: j readAny equals: 5.678. self assert: j readAny equals: 'A'. self assert: j readAny equals: 'B'. self assert: j readAny equals: nil. self assert: j readAny equals: nil.! ----- Method: JsonTests>>testStringWithUnicode (in category 'tests') ----- testStringWithUnicode | unicodeString | unicodeString := (Unicode value: 16r263A) asString. self json: '"\u263A"' equals:unicodeString. self json: '"\u263a"' equals:unicodeString. self json: '"', unicodeString, '"' equals: unicodeString. self render: unicodeString equals: '"', unicodeString, '"'.! ----- Method: JsonTests>>testWriteAssociation (in category 'tests') ----- testWriteAssociation self render: 'key' -> 'value' equals: '"key": "value"'. self render: 'key' -> 2 equals: '"key": 2'. "keys should be strings" self render: 42 -> 2 equals: '"42": 2'. "try to do _something_ for more complex keys" self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'. ! ----- Method: JsonTests>>testWriteString (in category 'tests') ----- testWriteString self render: '"' equals: '"\""'. self render: '\' equals: '"\\"'. self render: 'hi' equals: '"hi"'. self render: ({$a. Character lf. $b} as: String) equals: '"a\nb"'. self render: ({$a. Character value: 4. $b} as: String) equals: '"a\u0004b"'.! ----- Method: String>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | index start | aStream nextPut: $". start := 1. [ (index := self indexOfAnyOf: Json escapeSet startingAt: start) = 0 ] whileFalse: [ aStream next: index - start putAll: self startingAt: start. aStream nextPutAll: (Json escapeForCharacter: (self at: index)). start := index + 1 ]. start <= self size ifTrue: [ aStream next: self size + 1 - start putAll: self startingAt: start ]. aStream nextPut: $". ! Object subclass: #Json instanceVariableNames: 'stream currentCharacter arrayBufferStream stringBufferStream numberParser ctorMap dictionaryClass' classVariableNames: '' poolDictionaries: '' category: 'JSON'! Json class instanceVariableNames: 'escapeArray escapeSet'! !Json commentStamp: 'ul 3/29/2016 04:38' prior: 0! I support reading and writing JSON (see
http://json.org
) formatted data - strings, numbers, boolean, nil, arrays and dictionaries. The implementation is mainly based on RFC 7159 (
https://www.ietf.org/rfc/rfc7159.txt
). It has been extended with syntax for invoking a prearranged list of constructors on read objects. Character encoding is not handled here. That's something you have to do before reading or after writing. Mixed processing is possible, but it's entirely your responsibility to keep things correct. Instance Variables stream: <PositionableStream> currentCharacter: <Character> arrayBufferStream: <WriteStream> stringBufferStream: <ReadWriteStream> numberParser: <ExtendedNumberParser> ctorMap: <Dictionary> For parsing, stream has to be initialized by sending #readFrom: to my instance. The parser will pre-read one character, which will be stored in currentCharacter, so the stream is expected to support position manipulation, hence it should be a PositionableStream. arrayBufferStream is WriteStream on an Array, which holds the values during array parsing (see #readArray). When there's a recursive call, this stream is used as a stack to separate the elements parsed in the previous invocation from the current one. stringBufferStream is a ReadWriteStream on a String, which holds the currently parsed String (see #readString), the currently parsed number (see #readNumber) or the name of the currently parsed constructor (see #readConstructor). Recursion has no effect on this. numberParser is a cached instance of ExtendedNumberParser. It's initialized on stringBufferStream, so it can quickly parse the number stored in it. ctorMap is the Dictionary holding the constructor classes by name. arrayBufferStream, stringBufferStream and numberParser are initialized lazily, and should not be accessed externally. Since these are shared objects, along with stream and currentCharacter, holding state, therefore a single instance of me shouldn't be used to parse multiple streams at the same time. For writing, you can use the methods in the class-side rendering category. To convert individual objects to a string, you can use #asJsonString, to serialize it on a stream, you can use #jsonWriteOn:. ! Json class instanceVariableNames: 'escapeArray escapeSet'! ----- Method: Json class>>escapeForCharacter: (in category 'accessing') ----- escapeForCharacter: c | asciiValue | (asciiValue := c asciiValue) < 128 ifTrue: [ ^escapeArray at: asciiValue + 1 ]. ^nil! ----- Method: Json class>>escapeSet (in category 'accessing') ----- escapeSet ^escapeSet! ----- Method: Json class>>initialize (in category 'class initialization') ----- initialize "Json initialize." | newEscapeArray newEscapeSet | newEscapeArray := Array new: 128. newEscapeSet := CharacterSet new. (0 to: 31), #(127) do: [ :each | newEscapeArray at: each + 1 put: '\u', (each printStringHex padded: #left to: 4 with: $0). newEscapeSet add: (Character value: each) ]. { $" -> '\"'. $\ -> '\\'. $/ -> '\/'. "A hack, so the generated JSON will always be HTML+Javascript compatible, because the generated JSON will not contain the seqence </script>." Character backspace -> '\b'. Character lf -> '\n'. Character newPage -> '\f'. Character cr -> '\r'. Character tab -> '\t'. } do: [ :each | newEscapeArray at: each key asciiValue + 1 put: each value. newEscapeSet add: each key ]. escapeArray := newEscapeArray. escapeSet := newEscapeSet! ----- Method: Json class>>mimeType (in category 'accessing') ----- mimeType ^ 'application/x-json'! ----- Method: Json class>>newWithConstructors: (in category 'instance creation') ----- newWithConstructors: aCollection | m | m := Dictionary new. aCollection do: [:each | (each isKindOf: Association) ifTrue: [m add: each] ifFalse: [m at: each name asString put: each]]. ^ self new ctorMap: m; yourself.! ----- Method: Json class>>readFrom: (in category 'instance creation') ----- readFrom: aStream ^ self new readFrom: aStream.! ----- Method: Json class>>render: (in category 'rendering') ----- render: anObject ^String streamContents: [ :stream | anObject jsonWriteOn: stream ]! ----- Method: Json class>>render:withConstructor:on: (in category 'rendering') ----- render: anObject withConstructor: aConstructorName on: aStream aStream nextPutAll: '@', aConstructorName. anObject jsonWriteOn: aStream. ! ----- Method: Json class>>renderInstanceVariables:of:on: (in category 'rendering') ----- renderInstanceVariables: aCollection of: anObject on: aStream | map | map := Dictionary new. aCollection do: [:ivarName | map at: ivarName put: (anObject instVarNamed: ivarName)]. self render: map withConstructor: anObject class name asString on: aStream! ----- Method: Json>>arrayBufferStream (in category 'private') ----- arrayBufferStream ^arrayBufferStream ifNil: [ arrayBufferStream := (Array new: 10) writeStream ]! ----- Method: Json>>consume: (in category 'private') ----- consume: aString | message | 1 to: aString size do: [ :index | (currentCharacter := stream next) == (aString at: index) ifFalse: [ message := 'Expected ', (aString at: index) printString. currentCharacter ifNil: [ self incomplete: message ] ifNotNil: [ self invalid: message ] ] ]. currentCharacter := stream next! ----- Method: Json>>ctorMap (in category 'accessing') ----- ctorMap ^ ctorMap! ----- Method: Json>>ctorMap: (in category 'accessing') ----- ctorMap: m ctorMap := m! ----- Method: Json>>dictionaryClass: (in category 'accessing') ----- dictionaryClass: aClass "This method allows you to override the default dictionary class to be created while parsing a stream. Instances of aClass are expected to understand #at:put:." dictionaryClass := aClass! ----- Method: Json>>error: (in category 'error handling') ----- error: aString (currentCharacter isNil and: [ stream atEnd ]) ifTrue: [ self incomplete: aString ] ifFalse: [ self invalid: aString ]! ----- Method: Json>>incomplete: (in category 'error handling') ----- incomplete: aString JsonIncompleteError signal: aString! ----- Method: Json>>initialize (in category 'initialize-release') ----- initialize dictionaryClass := JsonObject ! ----- Method: Json>>interpretStringEscape: (in category 'private') ----- interpretStringEscape: aCharacter aCharacter == $b ifTrue: [ ^Character backspace ]. aCharacter == $n ifTrue: [ ^Character lf ]. aCharacter == $f ifTrue: [ ^Character newPage ]. aCharacter == $r ifTrue: [ ^Character cr ]. aCharacter == $t ifTrue: [ ^Character tab ]. aCharacter == $u ifTrue: [ ^self unescapeUnicode ]. aCharacter == $" ifTrue: [ ^aCharacter ]. aCharacter == $\ ifTrue: [ ^aCharacter ]. aCharacter == $/ ifTrue: [ ^aCharacter ]. self error: 'Unexpected escaped character: ', aCharacter asString ! ----- Method: Json>>invalid: (in category 'error handling') ----- invalid: aString JsonInvalidError signal: aString! ----- Method: Json>>readAny (in category 'private') ----- readAny self skipWhitespace. currentCharacter == ${ ifTrue: [ ^self readDictionary ]. currentCharacter == $[ ifTrue: [ ^self readArray ]. currentCharacter == $" ifTrue: [ ^self readString ]. currentCharacter == $t ifTrue: [ self consume: 'rue'. ^true ]. currentCharacter == $f ifTrue: [ self consume: 'alse'. ^false ]. currentCharacter == $n ifTrue: [ self consume: 'ull'. ^nil ]. currentCharacter == $- ifTrue: [ ^self readNumber: true ]. (#($0 $1 $2 $3 $4 $5 $6 $7 $8 $9) instVarsInclude: currentCharacter) ifTrue: [ ^self readNumber: false ]. currentCharacter == $@ ifTrue: [ ^self readConstructor ]. self invalid: 'Unknown Json input'! ----- Method: Json>>readArray (in category 'private') ----- readArray | initialPosition | currentCharacter := stream next. self skipWhitespace. currentCharacter == $] ifTrue: [ currentCharacter := stream next. ^#() ]. initialPosition := self arrayBufferStream position. [ arrayBufferStream nextPut: self readAny. self skipWhitespace. currentCharacter == $] ifTrue: [ | result | result := arrayBufferStream originalContents copyFrom: initialPosition + 1 to: arrayBufferStream position. arrayBufferStream position: initialPosition. currentCharacter := stream next. ^result ]. currentCharacter == $, ifFalse: [ self error: 'Unexpected character: ', currentCharacter asString ]. currentCharacter := stream next ] repeat. ! ----- Method: Json>>readConstructor (in category 'private') ----- readConstructor ctorMap ifNil: [ ^self error: 'No constructors were declared.' ]. self resetStringBufferStream. [ (currentCharacter := stream next) == $. or: [ currentCharacter isLetter ] ] whileTrue: [ stringBufferStream nextPut: currentCharacter ]. (ctorMap at: stringBufferStream contents ifAbsent: nil) ifNotNil: [ :constructor | ^constructor constructFromJson: self readAny ]. self error: 'Unknown constructor: ', stringBufferStream contents! ----- Method: Json>>readDictionary (in category 'private') ----- readDictionary | result key commaNeeded | result := dictionaryClass new. commaNeeded := false. currentCharacter := stream next. [ self skipWhitespace. currentCharacter == $} ifTrue: [ currentCharacter := stream next. ^result ]. commaNeeded ifFalse: [ commaNeeded := true ] ifTrue: [ currentCharacter == $, ifFalse: [ self error: 'Missing comma' ]. currentCharacter := stream next. self skipWhitespace ]. currentCharacter == $" ifFalse: [ self error: 'Key in dictionary must be string' ]. key := self readString. self skipWhitespace. currentCharacter == $: ifFalse: [ self error: 'Missing colon' ]. currentCharacter := stream next. result at: key put: self readAny ] repeat! ----- Method: Json>>readFrom: (in category 'parsing') ----- readFrom: aStream | result | stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next. result := self readAny. stream atEnd ifFalse: [ stream skip: -1 ]. "Undo prereading." ^result! ----- Method: Json>>readNumber: (in category 'private') ----- readNumber: negative | result | negative ifFalse: [ stream skip: -1 ]. result := (numberParser ifNil: [ numberParser := JsonNumberParser new on: stream; failBlock: [ self error: 'Invalid number.' ]; yourself ]) nextNumber: negative. currentCharacter := stream next. ^result ! ----- Method: Json>>readString (in category 'private') ----- readString self resetStringBufferStream. [ currentCharacter := stream next ifNil: [self incomplete: 'Unexpected end of string' ]. currentCharacter == $" ifTrue: [ currentCharacter := stream next. ^stringBufferStream contents ]. currentCharacter == $\ ifTrue: [ stringBufferStream nextPut: (self interpretStringEscape: (stream next ifNil: [ self incomplete: 'Unexpected end of string' ])) ] ifFalse: [ stringBufferStream nextPut: currentCharacter ] ] repeat! ----- Method: Json>>resetStringBufferStream (in category 'private') ----- resetStringBufferStream stringBufferStream ifNil: [ stringBufferStream := ReadWriteStream on: (String new: 64) ] ifNotNil: [ stringBufferStream resetToStart ]! ----- Method: Json>>skipWhitespace (in category 'private') ----- skipWhitespace currentCharacter ifNil: [ self incomplete: 'Input stream is empty' ]. [ currentCharacter isSeparator ] whileTrue: [ currentCharacter := stream next ifNil: [ self incomplete: 'Input stream is empty' ]. ]! ----- Method: Json>>stream (in category 'accessing') ----- stream "Answer the value of stream" ^ stream! ----- Method: Json>>stream: (in category 'private') ----- stream: aStream "For testing purposes only" stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next! ----- Method: Json>>unescapeUnicode (in category 'private') ----- unescapeUnicode | code digitValue | code := 0. 1 to: 4 do: [ :index | (digitValue := (stream next ifNil: [ self incomplete: 'Unexpected end of stream' ]) digitValue) < 0 ifTrue: [ self invalid: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ code := code * 16 + digitValue ] ifFalse: [ self invalid: 'Invalid hexadecimal digit' ] ]. ^Character value: code! Object subclass: #JsonDummyTestObject instanceVariableNames: 'a b c' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonDummyTestObject class>>constructFromJson: (in category 'as yet unclassified') ----- constructFromJson: j ^ self new a: (j at: 'a'); b: (j at: 'b'); c: (j at: 'c'); yourself! ----- Method: JsonDummyTestObject>>= (in category 'as yet unclassified') ----- = other ^ other class == self class and: [ a = other a and: [ b = other b and: [ c = other c]]]! ----- Method: JsonDummyTestObject>>a (in category 'accessing') ----- a "Answer the value of a" ^ a! ----- Method: JsonDummyTestObject>>a: (in category 'accessing') ----- a: anObject "Set the value of a" a := anObject! ----- Method: JsonDummyTestObject>>b (in category 'accessing') ----- b "Answer the value of b" ^ b! ----- Method: JsonDummyTestObject>>b: (in category 'accessing') ----- b: anObject "Set the value of b" b := anObject! ----- Method: JsonDummyTestObject>>c (in category 'accessing') ----- c "Answer the value of c" ^ c! ----- Method: JsonDummyTestObject>>c: (in category 'accessing') ----- c: anObject "Set the value of c" c := anObject! ----- Method: JsonDummyTestObject>>jsonWriteOn: (in category 'as yet unclassified') ----- jsonWriteOn: s Json renderInstanceVariables: {#a. #b. #c} of: self on: s ! ----- Method: Object>>asJsonString (in category '*JSON') ----- asJsonString ^ String streamContents: [:str | self jsonWriteOn: str ]!
1
0
0
0
The Trunk: JSON-klub.49.mcz
by commits@source.squeak.org
08 Sep '22
08 Sep '22
Christoph Thiede uploaded a new version of JSON to project The Trunk:
http://source.squeak.org/trunk/JSON-klub.49.mcz
==================== Summary ==================== Name: JSON-klub.49 Author: klub Time: 8 November 2018, 1:54:36.404002 pm UUID: 3fd83ffb-e4b6-4e55-82ac-f7a526bf2601 Ancestors: JSON-ul.48 - added convenience methods #parseAsJson and #parseAsOrderedJson to String to make it easier to parse a String ==================== Snapshot ==================== SystemOrganization addCategory: #JSON! ----- Method: Integer>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream ^ self printOn: aWriteStream base: 10 ! ----- Method: Association>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream self key asString jsonWriteOn: aStream. aStream nextPut: $:; space. self value jsonWriteOn: aStream.! ----- Method: False>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'false'! ----- Method: Number>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream self printOn: aWriteStream base: 10! OrderedDictionary subclass: #OrderedJsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: OrderedJsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: OrderedJsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: OrderedJsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: OrderedJsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: OrderedJsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: OrderedJsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: Time>>jsonWriteOn: (in category '*JSON') ----- jsonWriteOn: stream stream nextPut: $". self print24: true showSeconds: true on: stream. stream nextPut: $"! ----- Method: WriteStream>>jsonPrint: (in category '*json-printing') ----- jsonPrint: anObject anObject jsonWriteOn: self! ----- Method: Text>>jsonWriteOn: (in category '*json-printing') ----- jsonWriteOn: aStream self string jsonWriteOn: aStream! ----- Method: UndefinedObject>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream aWriteStream nextPutAll: 'null'! Error subclass: #JsonSyntaxError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonSyntaxError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! Class Json signals (possibly-indirect) instances of me when reading a JSON value from an input stream fails.! JsonSyntaxError subclass: #JsonIncompleteError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonIncompleteError commentStamp: 'tonyg 4/29/2016 11:37' prior: 0! I signal that reading a JSON value failed because more input is required, but that the input seen so far was not incorrect. Compare to JsonInvalidError. Be warned that reading numbers directly out of a stream can be ambiguous!! Consider reading from '1234'. Is the result intended to be 1234, or is there missing input, and the next character will be '5', making the result (possibly) 12345?! JsonSyntaxError subclass: #JsonInvalidError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonInvalidError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! I signal that reading a JSON value failed because the input didn't match the (extended)JSON grammar implemented by class Json.! ----- Method: Dictionary>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | needComma | needComma := false. aStream nextPut: ${. self associationsDo: [ :assoc | needComma ifTrue: [ aStream nextPut: $, ] ifFalse: [ needComma := true ]. assoc key jsonWriteOn: aStream. aStream nextPut: $:. assoc value jsonWriteOn: aStream ]. aStream nextPut: $}.! Dictionary subclass: #JsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: JsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: JsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: JsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: JsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: JsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: True>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'true'! NumberParser subclass: #JsonNumberParser instanceVariableNames: 'fraction' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonNumberParser>>allowPlusSign (in category 'accessing') ----- allowPlusSign ^false! ----- Method: JsonNumberParser>>allowPlusSignInExponent (in category 'accessing') ----- allowPlusSignInExponent ^true! ----- Method: JsonNumberParser>>error: (in category 'error') ----- error: aString JsonSyntaxError signal: aString! ----- Method: JsonNumberParser>>exponentLetters (in category 'accessing') ----- exponentLetters ^'eE'! ----- Method: JsonNumberParser>>initialize (in category 'initialize-release') ----- initialize super initialize. base := 10. fraction := Fraction numerator: 0 denominator: 1! ----- Method: JsonNumberParser>>makeFloatFromMantissa:exponent: (in category 'parsing-private') ----- makeFloatFromMantissa: mantissa exponent: exponent exponent = 0 ifTrue: [ ^mantissa asFloat ]. exponent >= 16 ifTrue: [ ^(mantissa * (self tenRaisedTo: exponent)) asFloat ]. (exponent >= -15 and: [ mantissa highBitOfMagnitude <= 54 ]) ifTrue: [ exponent >= 1 ifTrue: [ ^mantissa asFloat * (self tenRaisedTo: exponent) asFloat ]. ^mantissa asFloat / (self tenRaisedTo: exponent negated) asFloat ]. ^(fraction setNumerator: mantissa denominator: (self tenRaisedTo: exponent negated)) asFloat! ----- Method: JsonNumberParser>>nextElementaryLargeInteger (in category 'parsing-large int') ----- nextElementaryLargeInteger "Form an unsigned integer with incoming digits from sourceStream. Return this integer, or zero if no digits found. Stop reading if end of digits or if a LargeInteger is formed. Count the number of digits and the position of lastNonZero digit and store them in instVar." | digit value | value := 0. nDigits := 0. lastNonZero := 0. [ value isLarge ifTrue: [ ^value ]. ((digit := (sourceStream next ifNil: [ ^value ]) asInteger - 48 "$0 asInteger") < 0 or: [ digit >= 10 ]) ifTrue: [ sourceStream skip: -1. ^value ]. nDigits := nDigits + 1. digit = 0 ifFalse: [ lastNonZero := nDigits. value := value * 10 + digit ] ifTrue: [ value := value * 10 ] ] repeat! ----- Method: JsonNumberParser>>nextNumber: (in category 'parsing-public') ----- nextNumber: negative | numberOfTrailingZeroInIntegerPart numberOfTrailingZeroInFractionPart numberOfNonZeroFractionDigits mantissa value | integerPart := self nextUnsignedIntegerOrNil ifNil: [ ^self error: 'Missing integer part!!' ]. numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. (sourceStream peekFor: $.) ifFalse: [ "No fraction part" negative ifTrue: [ integerPart := integerPart negated ]. self readExponent ifFalse: [ ^integerPart ]. exponent > 0 ifTrue: [ ^integerPart * (self tenRaisedTo: exponent) ]. ^(Fraction numerator: integerPart denominator: (self tenRaisedTo: exponent negated)) asFloat ]. fractionPart := self nextUnsignedIntegerOrNil ifNil: [ self error: 'Missing fraction part!!' ]. fractionPart isZero ifTrue: [ self readExponent ifFalse: [ negative ifTrue: [ integerPart isZero ifTrue: [ ^Float negativeZero ]. ^integerPart negated ]. ^integerPart ]. exponent >= 0 ifTrue: [ negative ifTrue: [ integerPart := integerPart negated ]. ^integerPart * (self tenRaisedTo: exponent) ]. value := self makeFloatFromMantissa: integerPart exponent: exponent. negative ifTrue: [ ^value negated ]. ^value ]. numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. numberOfNonZeroFractionDigits := lastNonZero. self readExponent. exponent := exponent - numberOfNonZeroFractionDigits. mantissa := integerPart * (self tenRaisedTo: numberOfNonZeroFractionDigits) + (fractionPart // (self tenRaisedTo: numberOfTrailingZeroInFractionPart)). value := self makeFloatFromMantissa: mantissa exponent: exponent. negative ifTrue: [ ^value negated ]. ^value! ----- Method: JsonNumberParser>>nextUnsignedIntegerOrNil (in category 'parsing-public') ----- nextUnsignedIntegerOrNil "Form an unsigned integer with incoming digits from sourceStream. Answer this integer, or nil if no digit found. Count the number of digits and the position of lastNonZero digit and store them in instVar" | nPackets high nDigitsHigh lastNonZeroHigh low | "read no more digits than one elementary LargeInteger" high := self nextElementaryLargeInteger. nDigits = 0 ifTrue: [^nil]. "Not enough digits to form a LargeInteger, stop iteration" high isLarge ifFalse: [^high]. "We now have to engage arithmetic with LargeInteger Decompose the integer in a high and low packets of growing size:" nPackets := 1. nDigitsHigh := nDigits. lastNonZeroHigh := lastNonZero. [ low := self nextLargeIntegerBase: 10 nPackets: nPackets . high := high * (self tenRaisedTo: nDigits) + low. lastNonZero = 0 ifFalse: [lastNonZeroHigh := lastNonZero + nDigitsHigh]. nDigitsHigh := nDigitsHigh + nDigits. low isLarge] whileTrue: [nPackets := nPackets * 2]. nDigits := nDigitsHigh. lastNonZero := lastNonZeroHigh. ^high! ----- Method: JsonNumberParser>>tenRaisedTo: (in category 'parsing-private') ----- tenRaisedTo: anInteger anInteger >= 0 ifFalse: [ ^10 raisedToInteger: anInteger ]. anInteger >= 16 ifTrue: [ ^10 raisedToInteger: anInteger ]. ^#(1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000 100000000000 1000000000000 10000000000000 100000000000000 1000000000000000) at: anInteger + 1! ----- Method: ScaledDecimal>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: stream self printOn: stream showingDecimalPlaces: scale! ----- Method: Collection>>jsonWriteOn: (in category '*json') ----- jsonWriteOn: aStream "By default, use array braces " aStream nextPut: $[. self do: [:each | each jsonWriteOn: aStream ] separatedBy: [ aStream nextPut: $, ]. aStream nextPut: $]! TestCase subclass: #JsonTests instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonTests commentStamp: '<historical>' prior: 0! I provide a number of test cases for class Json.! ----- Method: JsonTests>>assertIncompleteJson: (in category 'helpers') ----- assertIncompleteJson: aString self should: [self readFrom: aString] raise: JsonIncompleteError! ----- Method: JsonTests>>assertInvalidJson: (in category 'helpers') ----- assertInvalidJson: aString self should: [self readFrom: aString] raise: JsonInvalidError! ----- Method: JsonTests>>json:equals: (in category 'helpers') ----- json: aString equals: aValue | readValue | readValue := self readFrom: aString. self assert: readValue = aValue.! ----- Method: JsonTests>>readFrom: (in category 'helpers') ----- readFrom: aString ^ (Json newWithConstructors: {JsonDummyTestObject.}) readFrom: aString readStream ! ----- Method: JsonTests>>render:equals: (in category 'helpers') ----- render: anObject equals: aString self assert: (Json render: anObject) = aString! ----- Method: JsonTests>>simpleDummyObject (in category 'helpers') ----- simpleDummyObject ^ JsonDummyTestObject new a: 1; b: 2; c: 3; yourself! ----- Method: JsonTests>>testArray (in category 'tests') ----- testArray self json: '[]' equals: #(). self json: '[[]]' equals: #(#()). self json: '[[], []]' equals: #(#() #()). self json: '["hi", "there"]' equals: #('hi' 'there'). self json: '[["a", "b", null]]' equals: #(('a' 'b' nil)).! ----- Method: JsonTests>>testAtomFalse (in category 'tests') ----- testAtomFalse self json: 'false' equals: false. self json: ' false' equals: false. self json: 'false ' equals: false. self json: ' false ' equals: false. ! ----- Method: JsonTests>>testAtomNull (in category 'tests') ----- testAtomNull self json: 'null' equals: nil. self json: ' null' equals: nil. self json: 'null ' equals: nil. self json: ' null ' equals: nil. ! ----- Method: JsonTests>>testAtomNumber (in category 'tests') ----- testAtomNumber self json: '1' equals: 1. self json: '0123' equals: 123. self json: '1.23e2' equals: 123. self json: '-1' equals: -1. self json: '-0' equals: 0. self json: '[-1.2]' equals: #(-1.2).! ----- Method: JsonTests>>testAtomString (in category 'tests') ----- testAtomString self json: '"hi"' equals: 'hi'. self json: '"\""' equals: '"'. self json: '"\\"' equals: '\'. self json: '""' equals: ''. self json: '"a\u0004b"' equals: ({$a. Character value: 4. $b} as: String). self json: '"a\nb"' equals: ({$a. Character lf. $b} as: String).! ----- Method: JsonTests>>testAtomTrue (in category 'tests') ----- testAtomTrue self json: 'true' equals: true. self json: ' true' equals: true. self json: 'true ' equals: true. self json: ' true ' equals: true. ! ----- Method: JsonTests>>testCtor (in category 'tests') ----- testCtor self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}' equals: self simpleDummyObject. self json: (Json render: self simpleDummyObject) equals: self simpleDummyObject.! ----- Method: JsonTests>>testDictionary (in category 'tests') ----- testDictionary self json: '{}' equals: (JsonObject new). self json: '{"a": "a"}' equals: (JsonObject new at: 'a' put: 'a'; yourself). self json: '{"a": [[]]}' equals: (JsonObject new at: 'a' put: #(#()); yourself). self json: '{"a":"b", "b":"a"}' equals: (JsonObject new add: 'a'->'b'; add: 'b'->'a';yourself).! ----- Method: JsonTests>>testDictionaryClass (in category 'tests') ----- testDictionaryClass | parser | parser := Json new. self assert: (parser readFrom: '{}' readStream) class == JsonObject. parser dictionaryClass: OrderedJsonObject. self assert: (parser readFrom: '{}' readStream) class == OrderedJsonObject. self assert: (parser readFrom: '{"x":{"y":{}}}' readStream) x y class == OrderedJsonObject! ----- Method: JsonTests>>testIncomplete (in category 'tests') ----- testIncomplete self assertIncompleteJson: ''. self assertIncompleteJson: ' '. self assertIncompleteJson: ' tr'. self assertIncompleteJson: 'tru'. self assertIncompleteJson: '['. self assertIncompleteJson: '[true'. self assertIncompleteJson: '[true,'. self assertIncompleteJson: '{'. self assertIncompleteJson: '{"hi"'. self assertIncompleteJson: '{"hi":'. self assertIncompleteJson: '{"hi":true'. self assertIncompleteJson: '{"hi":true,'. self assertIncompleteJson: '{"hi":true,"'. self assertIncompleteJson: '"hello'. self assertIncompleteJson: '"hello '. self assertIncompleteJson: '"hello\'. self assertIncompleteJson: '"\u26'.! ----- Method: JsonTests>>testInvalid (in category 'tests') ----- testInvalid self assertInvalidJson: 'x'. self assertInvalidJson: ' x'. self assertInvalidJson: ' trx'. self assertInvalidJson: 'trux'. self assertInvalidJson: '.'. self assertInvalidJson: ':'. self assertInvalidJson: ','. self assertInvalidJson: ']'. self assertInvalidJson: '}'. self assertInvalidJson: '[x'. self assertInvalidJson: '[true t'. self assertInvalidJson: '[true,]'. self assertInvalidJson: '{]'. self assertInvalidJson: '{,'. self assertInvalidJson: '{"hi",'. self assertInvalidJson: '{"hi":x'. self assertInvalidJson: '{"hi":,'. self assertInvalidJson: '{"hi":true "'. self assertInvalidJson: '{"hi":true,}'. self assertInvalidJson: '\u263A'.! ----- Method: JsonTests>>testInvalidUnicodeEscapes (in category 'tests') ----- testInvalidUnicodeEscapes self assertInvalidJson: '"\u26"'. "Note that naively reading four chars runs off the end here, so we might see JsonIncompleteError, which would be wrong." self assertInvalidJson: '"\u2Z"'. self assertInvalidJson: '"\u2Z44"'.! ----- Method: JsonTests>>testJsonObjectAtPutReturnsTheObject (in category 'tests') ----- testJsonObjectAtPutReturnsTheObject { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self assert: (json at: 'foo' put: 1) == json. self assert: (json foo: 1) == json ]! ----- Method: JsonTests>>testMissingCtor (in category 'tests') ----- testMissingCtor self assertInvalidJson: '@Missing[]'! ----- Method: JsonTests>>testMissingCtorNoMap (in category 'tests') ----- testMissingCtorNoMap self should: [Json new readFrom: '@Missing[]' readStream] raise: JsonInvalidError! ----- Method: JsonTests>>testNonStringKeysRaiseError (in category 'tests') ----- testNonStringKeysRaiseError { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self should: [ json at: 1 put: 1 ] raise: Error. self assert: json isEmpty. json at: '1' put: 1. self assert: 1 equals: (json at: '1') ]! ----- Method: JsonTests>>testStreaming (in category 'tests') ----- testStreaming | j | j := Json new stream: 'truefalsetrue[]{}1.234 5.678"A""B"nullnull' readStream. self assert: j readAny equals: true. self assert: j readAny equals: false. self assert: j readAny equals: true. self assert: j readAny equals: #(). self assert: j readAny equals: JsonObject new. self assert: j readAny equals: 1.234. self assert: j readAny equals: 5.678. self assert: j readAny equals: 'A'. self assert: j readAny equals: 'B'. self assert: j readAny equals: nil. self assert: j readAny equals: nil.! ----- Method: JsonTests>>testStringWithUnicode (in category 'tests') ----- testStringWithUnicode | unicodeString | unicodeString := (Unicode value: 16r263A) asString. self json: '"\u263A"' equals:unicodeString. self json: '"\u263a"' equals:unicodeString. self json: '"', unicodeString, '"' equals: unicodeString. self render: unicodeString equals: '"', unicodeString, '"'.! ----- Method: JsonTests>>testWriteAssociation (in category 'tests') ----- testWriteAssociation self render: 'key' -> 'value' equals: '"key": "value"'. self render: 'key' -> 2 equals: '"key": 2'. "keys should be strings" self render: 42 -> 2 equals: '"42": 2'. "try to do _something_ for more complex keys" self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'. ! ----- Method: JsonTests>>testWriteString (in category 'tests') ----- testWriteString self render: '"' equals: '"\""'. self render: '\' equals: '"\\"'. self render: 'hi' equals: '"hi"'. self render: ({$a. Character lf. $b} as: String) equals: '"a\nb"'. self render: ({$a. Character value: 4. $b} as: String) equals: '"a\u0004b"'.! ----- Method: String>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | index start | aStream nextPut: $". start := 1. [ (index := self indexOfAnyOf: Json escapeSet startingAt: start) = 0 ] whileFalse: [ aStream next: index - start putAll: self startingAt: start. aStream nextPutAll: (Json escapeForCharacter: (self at: index)). start := index + 1 ]. start <= self size ifTrue: [ aStream next: self size + 1 - start putAll: self startingAt: start ]. aStream nextPut: $". ! ----- Method: String>>parseAsJson (in category '*JSON') ----- parseAsJson "Convenience" ^Json readFrom: self readStream! ----- Method: String>>parseAsOrderedJson (in category '*JSON') ----- parseAsOrderedJson "Convenience" ^Json new dictionaryClass: OrderedJsonObject; readFrom: self readStream! Object subclass: #Json instanceVariableNames: 'stream currentCharacter arrayBufferStream stringBufferStream numberParser ctorMap dictionaryClass' classVariableNames: '' poolDictionaries: '' category: 'JSON'! Json class instanceVariableNames: 'escapeArray escapeSet'! !Json commentStamp: 'ul 3/29/2016 04:38' prior: 0! I support reading and writing JSON (see
http://json.org
) formatted data - strings, numbers, boolean, nil, arrays and dictionaries. The implementation is mainly based on RFC 7159 (
https://www.ietf.org/rfc/rfc7159.txt
). It has been extended with syntax for invoking a prearranged list of constructors on read objects. Character encoding is not handled here. That's something you have to do before reading or after writing. Mixed processing is possible, but it's entirely your responsibility to keep things correct. Instance Variables stream: <PositionableStream> currentCharacter: <Character> arrayBufferStream: <WriteStream> stringBufferStream: <ReadWriteStream> numberParser: <ExtendedNumberParser> ctorMap: <Dictionary> For parsing, stream has to be initialized by sending #readFrom: to my instance. The parser will pre-read one character, which will be stored in currentCharacter, so the stream is expected to support position manipulation, hence it should be a PositionableStream. arrayBufferStream is WriteStream on an Array, which holds the values during array parsing (see #readArray). When there's a recursive call, this stream is used as a stack to separate the elements parsed in the previous invocation from the current one. stringBufferStream is a ReadWriteStream on a String, which holds the currently parsed String (see #readString), the currently parsed number (see #readNumber) or the name of the currently parsed constructor (see #readConstructor). Recursion has no effect on this. numberParser is a cached instance of ExtendedNumberParser. It's initialized on stringBufferStream, so it can quickly parse the number stored in it. ctorMap is the Dictionary holding the constructor classes by name. arrayBufferStream, stringBufferStream and numberParser are initialized lazily, and should not be accessed externally. Since these are shared objects, along with stream and currentCharacter, holding state, therefore a single instance of me shouldn't be used to parse multiple streams at the same time. For writing, you can use the methods in the class-side rendering category. To convert individual objects to a string, you can use #asJsonString, to serialize it on a stream, you can use #jsonWriteOn:. ! Json class instanceVariableNames: 'escapeArray escapeSet'! ----- Method: Json class>>escapeForCharacter: (in category 'accessing') ----- escapeForCharacter: c | asciiValue | (asciiValue := c asciiValue) < 128 ifTrue: [ ^escapeArray at: asciiValue + 1 ]. ^nil! ----- Method: Json class>>escapeSet (in category 'accessing') ----- escapeSet ^escapeSet! ----- Method: Json class>>initialize (in category 'class initialization') ----- initialize "Json initialize." | newEscapeArray newEscapeSet | newEscapeArray := Array new: 128. newEscapeSet := CharacterSet new. (0 to: 31), #(127) do: [ :each | newEscapeArray at: each + 1 put: '\u', (each printStringHex padded: #left to: 4 with: $0). newEscapeSet add: (Character value: each) ]. { $" -> '\"'. $\ -> '\\'. $/ -> '\/'. "A hack, so the generated JSON will always be HTML+Javascript compatible, because the generated JSON will not contain the seqence </script>." Character backspace -> '\b'. Character lf -> '\n'. Character newPage -> '\f'. Character cr -> '\r'. Character tab -> '\t'. } do: [ :each | newEscapeArray at: each key asciiValue + 1 put: each value. newEscapeSet add: each key ]. escapeArray := newEscapeArray. escapeSet := newEscapeSet! ----- Method: Json class>>mimeType (in category 'accessing') ----- mimeType ^ 'application/x-json'! ----- Method: Json class>>newWithConstructors: (in category 'instance creation') ----- newWithConstructors: aCollection | m | m := Dictionary new. aCollection do: [:each | (each isKindOf: Association) ifTrue: [m add: each] ifFalse: [m at: each name asString put: each]]. ^ self new ctorMap: m; yourself.! ----- Method: Json class>>readFrom: (in category 'instance creation') ----- readFrom: aStream ^ self new readFrom: aStream.! ----- Method: Json class>>render: (in category 'rendering') ----- render: anObject ^String streamContents: [ :stream | anObject jsonWriteOn: stream ]! ----- Method: Json class>>render:withConstructor:on: (in category 'rendering') ----- render: anObject withConstructor: aConstructorName on: aStream aStream nextPutAll: '@', aConstructorName. anObject jsonWriteOn: aStream. ! ----- Method: Json class>>renderInstanceVariables:of:on: (in category 'rendering') ----- renderInstanceVariables: aCollection of: anObject on: aStream | map | map := Dictionary new. aCollection do: [:ivarName | map at: ivarName put: (anObject instVarNamed: ivarName)]. self render: map withConstructor: anObject class name asString on: aStream! ----- Method: Json>>arrayBufferStream (in category 'private') ----- arrayBufferStream ^arrayBufferStream ifNil: [ arrayBufferStream := (Array new: 10) writeStream ]! ----- Method: Json>>consume: (in category 'private') ----- consume: aString | message | 1 to: aString size do: [ :index | (currentCharacter := stream next) == (aString at: index) ifFalse: [ message := 'Expected ', (aString at: index) printString. currentCharacter ifNil: [ self incomplete: message ] ifNotNil: [ self invalid: message ] ] ]. currentCharacter := stream next! ----- Method: Json>>ctorMap (in category 'accessing') ----- ctorMap ^ ctorMap! ----- Method: Json>>ctorMap: (in category 'accessing') ----- ctorMap: m ctorMap := m! ----- Method: Json>>dictionaryClass: (in category 'accessing') ----- dictionaryClass: aClass "This method allows you to override the default dictionary class to be created while parsing a stream. Instances of aClass are expected to understand #at:put:." dictionaryClass := aClass! ----- Method: Json>>error: (in category 'error handling') ----- error: aString (currentCharacter isNil and: [ stream atEnd ]) ifTrue: [ self incomplete: aString ] ifFalse: [ self invalid: aString ]! ----- Method: Json>>incomplete: (in category 'error handling') ----- incomplete: aString JsonIncompleteError signal: aString! ----- Method: Json>>initialize (in category 'initialize-release') ----- initialize dictionaryClass := JsonObject ! ----- Method: Json>>interpretStringEscape: (in category 'private') ----- interpretStringEscape: aCharacter aCharacter == $b ifTrue: [ ^Character backspace ]. aCharacter == $n ifTrue: [ ^Character lf ]. aCharacter == $f ifTrue: [ ^Character newPage ]. aCharacter == $r ifTrue: [ ^Character cr ]. aCharacter == $t ifTrue: [ ^Character tab ]. aCharacter == $u ifTrue: [ ^self unescapeUnicode ]. aCharacter == $" ifTrue: [ ^aCharacter ]. aCharacter == $\ ifTrue: [ ^aCharacter ]. aCharacter == $/ ifTrue: [ ^aCharacter ]. self error: 'Unexpected escaped character: ', aCharacter asString ! ----- Method: Json>>invalid: (in category 'error handling') ----- invalid: aString JsonInvalidError signal: aString! ----- Method: Json>>readAny (in category 'private') ----- readAny self skipWhitespace. currentCharacter == ${ ifTrue: [ ^self readDictionary ]. currentCharacter == $[ ifTrue: [ ^self readArray ]. currentCharacter == $" ifTrue: [ ^self readString ]. currentCharacter == $t ifTrue: [ self consume: 'rue'. ^true ]. currentCharacter == $f ifTrue: [ self consume: 'alse'. ^false ]. currentCharacter == $n ifTrue: [ self consume: 'ull'. ^nil ]. currentCharacter == $- ifTrue: [ ^self readNumber: true ]. (#($0 $1 $2 $3 $4 $5 $6 $7 $8 $9) instVarsInclude: currentCharacter) ifTrue: [ ^self readNumber: false ]. currentCharacter == $@ ifTrue: [ ^self readConstructor ]. self invalid: 'Unknown Json input'! ----- Method: Json>>readArray (in category 'private') ----- readArray | initialPosition | currentCharacter := stream next. self skipWhitespace. currentCharacter == $] ifTrue: [ currentCharacter := stream next. ^#() ]. initialPosition := self arrayBufferStream position. [ arrayBufferStream nextPut: self readAny. self skipWhitespace. currentCharacter == $] ifTrue: [ | result | result := arrayBufferStream originalContents copyFrom: initialPosition + 1 to: arrayBufferStream position. arrayBufferStream position: initialPosition. currentCharacter := stream next. ^result ]. currentCharacter == $, ifFalse: [ self error: 'Unexpected character: ', currentCharacter asString ]. currentCharacter := stream next ] repeat. ! ----- Method: Json>>readConstructor (in category 'private') ----- readConstructor ctorMap ifNil: [ ^self error: 'No constructors were declared.' ]. self resetStringBufferStream. [ (currentCharacter := stream next) == $. or: [ currentCharacter isLetter ] ] whileTrue: [ stringBufferStream nextPut: currentCharacter ]. (ctorMap at: stringBufferStream contents ifAbsent: nil) ifNotNil: [ :constructor | ^constructor constructFromJson: self readAny ]. self error: 'Unknown constructor: ', stringBufferStream contents! ----- Method: Json>>readDictionary (in category 'private') ----- readDictionary | result key commaNeeded | result := dictionaryClass new. commaNeeded := false. currentCharacter := stream next. [ self skipWhitespace. currentCharacter == $} ifTrue: [ currentCharacter := stream next. ^result ]. commaNeeded ifFalse: [ commaNeeded := true ] ifTrue: [ currentCharacter == $, ifFalse: [ self error: 'Missing comma' ]. currentCharacter := stream next. self skipWhitespace ]. currentCharacter == $" ifFalse: [ self error: 'Key in dictionary must be string' ]. key := self readString. self skipWhitespace. currentCharacter == $: ifFalse: [ self error: 'Missing colon' ]. currentCharacter := stream next. result at: key put: self readAny ] repeat! ----- Method: Json>>readFrom: (in category 'parsing') ----- readFrom: aStream | result | stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next. result := self readAny. stream atEnd ifFalse: [ stream skip: -1 ]. "Undo prereading." ^result! ----- Method: Json>>readNumber: (in category 'private') ----- readNumber: negative | result | negative ifFalse: [ stream skip: -1 ]. result := (numberParser ifNil: [ numberParser := JsonNumberParser new on: stream; failBlock: [ self error: 'Invalid number.' ]; yourself ]) nextNumber: negative. currentCharacter := stream next. ^result ! ----- Method: Json>>readString (in category 'private') ----- readString self resetStringBufferStream. [ currentCharacter := stream next ifNil: [self incomplete: 'Unexpected end of string' ]. currentCharacter == $" ifTrue: [ currentCharacter := stream next. ^stringBufferStream contents ]. currentCharacter == $\ ifTrue: [ stringBufferStream nextPut: (self interpretStringEscape: (stream next ifNil: [ self incomplete: 'Unexpected end of string' ])) ] ifFalse: [ stringBufferStream nextPut: currentCharacter ] ] repeat! ----- Method: Json>>resetStringBufferStream (in category 'private') ----- resetStringBufferStream stringBufferStream ifNil: [ stringBufferStream := ReadWriteStream on: (String new: 64) ] ifNotNil: [ stringBufferStream resetToStart ]! ----- Method: Json>>skipWhitespace (in category 'private') ----- skipWhitespace currentCharacter ifNil: [ self incomplete: 'Input stream is empty' ]. [ currentCharacter isSeparator ] whileTrue: [ currentCharacter := stream next ifNil: [ self incomplete: 'Input stream is empty' ]. ]! ----- Method: Json>>stream (in category 'accessing') ----- stream "Answer the value of stream" ^ stream! ----- Method: Json>>stream: (in category 'private') ----- stream: aStream "For testing purposes only" stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next! ----- Method: Json>>unescapeUnicode (in category 'private') ----- unescapeUnicode | code digitValue | code := 0. 1 to: 4 do: [ :index | (digitValue := (stream next ifNil: [ self incomplete: 'Unexpected end of stream' ]) digitValue) < 0 ifTrue: [ self invalid: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ code := code * 16 + digitValue ] ifFalse: [ self invalid: 'Invalid hexadecimal digit' ] ]. ^Character value: code! Object subclass: #JsonDummyTestObject instanceVariableNames: 'a b c' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonDummyTestObject class>>constructFromJson: (in category 'as yet unclassified') ----- constructFromJson: j ^ self new a: (j at: 'a'); b: (j at: 'b'); c: (j at: 'c'); yourself! ----- Method: JsonDummyTestObject>>= (in category 'as yet unclassified') ----- = other ^ other class == self class and: [ a = other a and: [ b = other b and: [ c = other c]]]! ----- Method: JsonDummyTestObject>>a (in category 'accessing') ----- a "Answer the value of a" ^ a! ----- Method: JsonDummyTestObject>>a: (in category 'accessing') ----- a: anObject "Set the value of a" a := anObject! ----- Method: JsonDummyTestObject>>b (in category 'accessing') ----- b "Answer the value of b" ^ b! ----- Method: JsonDummyTestObject>>b: (in category 'accessing') ----- b: anObject "Set the value of b" b := anObject! ----- Method: JsonDummyTestObject>>c (in category 'accessing') ----- c "Answer the value of c" ^ c! ----- Method: JsonDummyTestObject>>c: (in category 'accessing') ----- c: anObject "Set the value of c" c := anObject! ----- Method: JsonDummyTestObject>>jsonWriteOn: (in category 'as yet unclassified') ----- jsonWriteOn: s Json renderInstanceVariables: {#a. #b. #c} of: self on: s ! ----- Method: Object>>asJsonString (in category '*JSON') ----- asJsonString ^ String streamContents: [:str | self jsonWriteOn: str ]!
1
0
0
0
The Trunk: JSON-klub.50.mcz
by commits@source.squeak.org
08 Sep '22
08 Sep '22
Christoph Thiede uploaded a new version of JSON to project The Trunk:
http://source.squeak.org/trunk/JSON-klub.50.mcz
==================== Summary ==================== Name: JSON-klub.50 Author: klub Time: 24 April 2019, 11:02:02.046155 am UUID: 7a41cfff-0f5a-4997-b10f-98d63ee967b4 Ancestors: JSON-klub.49 - Added SerializedJson and String >> #asSerializedJson which can be used to build and serialize json objects with parts that have already been serialized. ==================== Snapshot ==================== SystemOrganization addCategory: #JSON! ----- Method: Integer>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream ^ self printOn: aWriteStream base: 10 ! ----- Method: Association>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream self key asString jsonWriteOn: aStream. aStream nextPut: $:; space. self value jsonWriteOn: aStream.! ----- Method: False>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'false'! ----- Method: Number>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream self printOn: aWriteStream base: 10! OrderedDictionary subclass: #OrderedJsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: OrderedJsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: OrderedJsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: OrderedJsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: OrderedJsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: OrderedJsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: OrderedJsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: Time>>jsonWriteOn: (in category '*JSON') ----- jsonWriteOn: stream stream nextPut: $". self print24: true showSeconds: true on: stream. stream nextPut: $"! ----- Method: WriteStream>>jsonPrint: (in category '*json-printing') ----- jsonPrint: anObject anObject jsonWriteOn: self! ----- Method: Text>>jsonWriteOn: (in category '*json-printing') ----- jsonWriteOn: aStream self string jsonWriteOn: aStream! ----- Method: UndefinedObject>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream aWriteStream nextPutAll: 'null'! Error subclass: #JsonSyntaxError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonSyntaxError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! Class Json signals (possibly-indirect) instances of me when reading a JSON value from an input stream fails.! JsonSyntaxError subclass: #JsonIncompleteError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonIncompleteError commentStamp: 'tonyg 4/29/2016 11:37' prior: 0! I signal that reading a JSON value failed because more input is required, but that the input seen so far was not incorrect. Compare to JsonInvalidError. Be warned that reading numbers directly out of a stream can be ambiguous!! Consider reading from '1234'. Is the result intended to be 1234, or is there missing input, and the next character will be '5', making the result (possibly) 12345?! JsonSyntaxError subclass: #JsonInvalidError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonInvalidError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! I signal that reading a JSON value failed because the input didn't match the (extended)JSON grammar implemented by class Json.! ----- Method: Dictionary>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | needComma | needComma := false. aStream nextPut: ${. self associationsDo: [ :assoc | needComma ifTrue: [ aStream nextPut: $, ] ifFalse: [ needComma := true ]. assoc key jsonWriteOn: aStream. aStream nextPut: $:. assoc value jsonWriteOn: aStream ]. aStream nextPut: $}.! Dictionary subclass: #JsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: JsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: JsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: JsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: JsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: JsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: True>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'true'! NumberParser subclass: #JsonNumberParser instanceVariableNames: 'fraction' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonNumberParser>>allowPlusSign (in category 'accessing') ----- allowPlusSign ^false! ----- Method: JsonNumberParser>>allowPlusSignInExponent (in category 'accessing') ----- allowPlusSignInExponent ^true! ----- Method: JsonNumberParser>>error: (in category 'error') ----- error: aString JsonSyntaxError signal: aString! ----- Method: JsonNumberParser>>exponentLetters (in category 'accessing') ----- exponentLetters ^'eE'! ----- Method: JsonNumberParser>>initialize (in category 'initialize-release') ----- initialize super initialize. base := 10. fraction := Fraction numerator: 0 denominator: 1! ----- Method: JsonNumberParser>>makeFloatFromMantissa:exponent: (in category 'parsing-private') ----- makeFloatFromMantissa: mantissa exponent: exponent exponent = 0 ifTrue: [ ^mantissa asFloat ]. exponent >= 16 ifTrue: [ ^(mantissa * (self tenRaisedTo: exponent)) asFloat ]. (exponent >= -15 and: [ mantissa highBitOfMagnitude <= 54 ]) ifTrue: [ exponent >= 1 ifTrue: [ ^mantissa asFloat * (self tenRaisedTo: exponent) asFloat ]. ^mantissa asFloat / (self tenRaisedTo: exponent negated) asFloat ]. ^(fraction setNumerator: mantissa denominator: (self tenRaisedTo: exponent negated)) asFloat! ----- Method: JsonNumberParser>>nextElementaryLargeInteger (in category 'parsing-large int') ----- nextElementaryLargeInteger "Form an unsigned integer with incoming digits from sourceStream. Return this integer, or zero if no digits found. Stop reading if end of digits or if a LargeInteger is formed. Count the number of digits and the position of lastNonZero digit and store them in instVar." | digit value | value := 0. nDigits := 0. lastNonZero := 0. [ value isLarge ifTrue: [ ^value ]. ((digit := (sourceStream next ifNil: [ ^value ]) asInteger - 48 "$0 asInteger") < 0 or: [ digit >= 10 ]) ifTrue: [ sourceStream skip: -1. ^value ]. nDigits := nDigits + 1. digit = 0 ifFalse: [ lastNonZero := nDigits. value := value * 10 + digit ] ifTrue: [ value := value * 10 ] ] repeat! ----- Method: JsonNumberParser>>nextNumber: (in category 'parsing-public') ----- nextNumber: negative | numberOfTrailingZeroInIntegerPart numberOfTrailingZeroInFractionPart numberOfNonZeroFractionDigits mantissa value | integerPart := self nextUnsignedIntegerOrNil ifNil: [ ^self error: 'Missing integer part!!' ]. numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. (sourceStream peekFor: $.) ifFalse: [ "No fraction part" negative ifTrue: [ integerPart := integerPart negated ]. self readExponent ifFalse: [ ^integerPart ]. exponent > 0 ifTrue: [ ^integerPart * (self tenRaisedTo: exponent) ]. ^(Fraction numerator: integerPart denominator: (self tenRaisedTo: exponent negated)) asFloat ]. fractionPart := self nextUnsignedIntegerOrNil ifNil: [ self error: 'Missing fraction part!!' ]. fractionPart isZero ifTrue: [ self readExponent ifFalse: [ negative ifTrue: [ integerPart isZero ifTrue: [ ^Float negativeZero ]. ^integerPart negated ]. ^integerPart ]. exponent >= 0 ifTrue: [ negative ifTrue: [ integerPart := integerPart negated ]. ^integerPart * (self tenRaisedTo: exponent) ]. value := self makeFloatFromMantissa: integerPart exponent: exponent. negative ifTrue: [ ^value negated ]. ^value ]. numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. numberOfNonZeroFractionDigits := lastNonZero. self readExponent. exponent := exponent - numberOfNonZeroFractionDigits. mantissa := integerPart * (self tenRaisedTo: numberOfNonZeroFractionDigits) + (fractionPart // (self tenRaisedTo: numberOfTrailingZeroInFractionPart)). value := self makeFloatFromMantissa: mantissa exponent: exponent. negative ifTrue: [ ^value negated ]. ^value! ----- Method: JsonNumberParser>>nextUnsignedIntegerOrNil (in category 'parsing-public') ----- nextUnsignedIntegerOrNil "Form an unsigned integer with incoming digits from sourceStream. Answer this integer, or nil if no digit found. Count the number of digits and the position of lastNonZero digit and store them in instVar" | nPackets high nDigitsHigh lastNonZeroHigh low | "read no more digits than one elementary LargeInteger" high := self nextElementaryLargeInteger. nDigits = 0 ifTrue: [^nil]. "Not enough digits to form a LargeInteger, stop iteration" high isLarge ifFalse: [^high]. "We now have to engage arithmetic with LargeInteger Decompose the integer in a high and low packets of growing size:" nPackets := 1. nDigitsHigh := nDigits. lastNonZeroHigh := lastNonZero. [ low := self nextLargeIntegerBase: 10 nPackets: nPackets . high := high * (self tenRaisedTo: nDigits) + low. lastNonZero = 0 ifFalse: [lastNonZeroHigh := lastNonZero + nDigitsHigh]. nDigitsHigh := nDigitsHigh + nDigits. low isLarge] whileTrue: [nPackets := nPackets * 2]. nDigits := nDigitsHigh. lastNonZero := lastNonZeroHigh. ^high! ----- Method: JsonNumberParser>>tenRaisedTo: (in category 'parsing-private') ----- tenRaisedTo: anInteger anInteger >= 0 ifFalse: [ ^10 raisedToInteger: anInteger ]. anInteger >= 16 ifTrue: [ ^10 raisedToInteger: anInteger ]. ^#(1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000 100000000000 1000000000000 10000000000000 100000000000000 1000000000000000) at: anInteger + 1! ----- Method: ScaledDecimal>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: stream self printOn: stream showingDecimalPlaces: scale! ----- Method: Collection>>jsonWriteOn: (in category '*json') ----- jsonWriteOn: aStream "By default, use array braces " aStream nextPut: $[. self do: [:each | each jsonWriteOn: aStream ] separatedBy: [ aStream nextPut: $, ]. aStream nextPut: $]! TestCase subclass: #JsonTests instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonTests commentStamp: '<historical>' prior: 0! I provide a number of test cases for class Json.! ----- Method: JsonTests>>assertIncompleteJson: (in category 'helpers') ----- assertIncompleteJson: aString self should: [self readFrom: aString] raise: JsonIncompleteError! ----- Method: JsonTests>>assertInvalidJson: (in category 'helpers') ----- assertInvalidJson: aString self should: [self readFrom: aString] raise: JsonInvalidError! ----- Method: JsonTests>>json:equals: (in category 'helpers') ----- json: aString equals: aValue | readValue | readValue := self readFrom: aString. self assert: readValue = aValue.! ----- Method: JsonTests>>readFrom: (in category 'helpers') ----- readFrom: aString ^ (Json newWithConstructors: {JsonDummyTestObject.}) readFrom: aString readStream ! ----- Method: JsonTests>>render:equals: (in category 'helpers') ----- render: anObject equals: aString self assert: (Json render: anObject) = aString! ----- Method: JsonTests>>simpleDummyObject (in category 'helpers') ----- simpleDummyObject ^ JsonDummyTestObject new a: 1; b: 2; c: 3; yourself! ----- Method: JsonTests>>testArray (in category 'tests') ----- testArray self json: '[]' equals: #(). self json: '[[]]' equals: #(#()). self json: '[[], []]' equals: #(#() #()). self json: '["hi", "there"]' equals: #('hi' 'there'). self json: '[["a", "b", null]]' equals: #(('a' 'b' nil)).! ----- Method: JsonTests>>testAtomFalse (in category 'tests') ----- testAtomFalse self json: 'false' equals: false. self json: ' false' equals: false. self json: 'false ' equals: false. self json: ' false ' equals: false. ! ----- Method: JsonTests>>testAtomNull (in category 'tests') ----- testAtomNull self json: 'null' equals: nil. self json: ' null' equals: nil. self json: 'null ' equals: nil. self json: ' null ' equals: nil. ! ----- Method: JsonTests>>testAtomNumber (in category 'tests') ----- testAtomNumber self json: '1' equals: 1. self json: '0123' equals: 123. self json: '1.23e2' equals: 123. self json: '-1' equals: -1. self json: '-0' equals: 0. self json: '[-1.2]' equals: #(-1.2).! ----- Method: JsonTests>>testAtomString (in category 'tests') ----- testAtomString self json: '"hi"' equals: 'hi'. self json: '"\""' equals: '"'. self json: '"\\"' equals: '\'. self json: '""' equals: ''. self json: '"a\u0004b"' equals: ({$a. Character value: 4. $b} as: String). self json: '"a\nb"' equals: ({$a. Character lf. $b} as: String).! ----- Method: JsonTests>>testAtomTrue (in category 'tests') ----- testAtomTrue self json: 'true' equals: true. self json: ' true' equals: true. self json: 'true ' equals: true. self json: ' true ' equals: true. ! ----- Method: JsonTests>>testCtor (in category 'tests') ----- testCtor self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}' equals: self simpleDummyObject. self json: (Json render: self simpleDummyObject) equals: self simpleDummyObject.! ----- Method: JsonTests>>testDictionary (in category 'tests') ----- testDictionary self json: '{}' equals: (JsonObject new). self json: '{"a": "a"}' equals: (JsonObject new at: 'a' put: 'a'; yourself). self json: '{"a": [[]]}' equals: (JsonObject new at: 'a' put: #(#()); yourself). self json: '{"a":"b", "b":"a"}' equals: (JsonObject new add: 'a'->'b'; add: 'b'->'a';yourself).! ----- Method: JsonTests>>testDictionaryClass (in category 'tests') ----- testDictionaryClass | parser | parser := Json new. self assert: (parser readFrom: '{}' readStream) class == JsonObject. parser dictionaryClass: OrderedJsonObject. self assert: (parser readFrom: '{}' readStream) class == OrderedJsonObject. self assert: (parser readFrom: '{"x":{"y":{}}}' readStream) x y class == OrderedJsonObject! ----- Method: JsonTests>>testIncomplete (in category 'tests') ----- testIncomplete self assertIncompleteJson: ''. self assertIncompleteJson: ' '. self assertIncompleteJson: ' tr'. self assertIncompleteJson: 'tru'. self assertIncompleteJson: '['. self assertIncompleteJson: '[true'. self assertIncompleteJson: '[true,'. self assertIncompleteJson: '{'. self assertIncompleteJson: '{"hi"'. self assertIncompleteJson: '{"hi":'. self assertIncompleteJson: '{"hi":true'. self assertIncompleteJson: '{"hi":true,'. self assertIncompleteJson: '{"hi":true,"'. self assertIncompleteJson: '"hello'. self assertIncompleteJson: '"hello '. self assertIncompleteJson: '"hello\'. self assertIncompleteJson: '"\u26'.! ----- Method: JsonTests>>testInvalid (in category 'tests') ----- testInvalid self assertInvalidJson: 'x'. self assertInvalidJson: ' x'. self assertInvalidJson: ' trx'. self assertInvalidJson: 'trux'. self assertInvalidJson: '.'. self assertInvalidJson: ':'. self assertInvalidJson: ','. self assertInvalidJson: ']'. self assertInvalidJson: '}'. self assertInvalidJson: '[x'. self assertInvalidJson: '[true t'. self assertInvalidJson: '[true,]'. self assertInvalidJson: '{]'. self assertInvalidJson: '{,'. self assertInvalidJson: '{"hi",'. self assertInvalidJson: '{"hi":x'. self assertInvalidJson: '{"hi":,'. self assertInvalidJson: '{"hi":true "'. self assertInvalidJson: '{"hi":true,}'. self assertInvalidJson: '\u263A'.! ----- Method: JsonTests>>testInvalidUnicodeEscapes (in category 'tests') ----- testInvalidUnicodeEscapes self assertInvalidJson: '"\u26"'. "Note that naively reading four chars runs off the end here, so we might see JsonIncompleteError, which would be wrong." self assertInvalidJson: '"\u2Z"'. self assertInvalidJson: '"\u2Z44"'.! ----- Method: JsonTests>>testJsonObjectAtPutReturnsTheObject (in category 'tests') ----- testJsonObjectAtPutReturnsTheObject { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self assert: (json at: 'foo' put: 1) == json. self assert: (json foo: 1) == json ]! ----- Method: JsonTests>>testMissingCtor (in category 'tests') ----- testMissingCtor self assertInvalidJson: '@Missing[]'! ----- Method: JsonTests>>testMissingCtorNoMap (in category 'tests') ----- testMissingCtorNoMap self should: [Json new readFrom: '@Missing[]' readStream] raise: JsonInvalidError! ----- Method: JsonTests>>testNonStringKeysRaiseError (in category 'tests') ----- testNonStringKeysRaiseError { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self should: [ json at: 1 put: 1 ] raise: Error. self assert: json isEmpty. json at: '1' put: 1. self assert: 1 equals: (json at: '1') ]! ----- Method: JsonTests>>testStreaming (in category 'tests') ----- testStreaming | j | j := Json new stream: 'truefalsetrue[]{}1.234 5.678"A""B"nullnull' readStream. self assert: j readAny equals: true. self assert: j readAny equals: false. self assert: j readAny equals: true. self assert: j readAny equals: #(). self assert: j readAny equals: JsonObject new. self assert: j readAny equals: 1.234. self assert: j readAny equals: 5.678. self assert: j readAny equals: 'A'. self assert: j readAny equals: 'B'. self assert: j readAny equals: nil. self assert: j readAny equals: nil.! ----- Method: JsonTests>>testStringWithUnicode (in category 'tests') ----- testStringWithUnicode | unicodeString | unicodeString := (Unicode value: 16r263A) asString. self json: '"\u263A"' equals:unicodeString. self json: '"\u263a"' equals:unicodeString. self json: '"', unicodeString, '"' equals: unicodeString. self render: unicodeString equals: '"', unicodeString, '"'.! ----- Method: JsonTests>>testWriteAssociation (in category 'tests') ----- testWriteAssociation self render: 'key' -> 'value' equals: '"key": "value"'. self render: 'key' -> 2 equals: '"key": 2'. "keys should be strings" self render: 42 -> 2 equals: '"42": 2'. "try to do _something_ for more complex keys" self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'. ! ----- Method: JsonTests>>testWriteString (in category 'tests') ----- testWriteString self render: '"' equals: '"\""'. self render: '\' equals: '"\\"'. self render: 'hi' equals: '"hi"'. self render: ({$a. Character lf. $b} as: String) equals: '"a\nb"'. self render: ({$a. Character value: 4. $b} as: String) equals: '"a\u0004b"'.! ----- Method: String>>asSerializedJson (in category '*JSON') ----- asSerializedJson " Assume that the receiver is a valid serialized json string. Return an object that understands #jsonWriteOn: and will write this string on its argument. " ^SerializedJson on: self! ----- Method: String>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | index start | aStream nextPut: $". start := 1. [ (index := self indexOfAnyOf: Json escapeSet startingAt: start) = 0 ] whileFalse: [ aStream next: index - start putAll: self startingAt: start. aStream nextPutAll: (Json escapeForCharacter: (self at: index)). start := index + 1 ]. start <= self size ifTrue: [ aStream next: self size + 1 - start putAll: self startingAt: start ]. aStream nextPut: $". ! ----- Method: String>>parseAsJson (in category '*JSON') ----- parseAsJson "Convenience" ^Json readFrom: self readStream! ----- Method: String>>parseAsOrderedJson (in category '*JSON') ----- parseAsOrderedJson "Convenience" ^Json new dictionaryClass: OrderedJsonObject; readFrom: self readStream! Object subclass: #Json instanceVariableNames: 'stream currentCharacter arrayBufferStream stringBufferStream numberParser ctorMap dictionaryClass' classVariableNames: '' poolDictionaries: '' category: 'JSON'! Json class instanceVariableNames: 'escapeArray escapeSet'! !Json commentStamp: 'ul 3/29/2016 04:38' prior: 0! I support reading and writing JSON (see
http://json.org
) formatted data - strings, numbers, boolean, nil, arrays and dictionaries. The implementation is mainly based on RFC 7159 (
https://www.ietf.org/rfc/rfc7159.txt
). It has been extended with syntax for invoking a prearranged list of constructors on read objects. Character encoding is not handled here. That's something you have to do before reading or after writing. Mixed processing is possible, but it's entirely your responsibility to keep things correct. Instance Variables stream: <PositionableStream> currentCharacter: <Character> arrayBufferStream: <WriteStream> stringBufferStream: <ReadWriteStream> numberParser: <ExtendedNumberParser> ctorMap: <Dictionary> For parsing, stream has to be initialized by sending #readFrom: to my instance. The parser will pre-read one character, which will be stored in currentCharacter, so the stream is expected to support position manipulation, hence it should be a PositionableStream. arrayBufferStream is WriteStream on an Array, which holds the values during array parsing (see #readArray). When there's a recursive call, this stream is used as a stack to separate the elements parsed in the previous invocation from the current one. stringBufferStream is a ReadWriteStream on a String, which holds the currently parsed String (see #readString), the currently parsed number (see #readNumber) or the name of the currently parsed constructor (see #readConstructor). Recursion has no effect on this. numberParser is a cached instance of ExtendedNumberParser. It's initialized on stringBufferStream, so it can quickly parse the number stored in it. ctorMap is the Dictionary holding the constructor classes by name. arrayBufferStream, stringBufferStream and numberParser are initialized lazily, and should not be accessed externally. Since these are shared objects, along with stream and currentCharacter, holding state, therefore a single instance of me shouldn't be used to parse multiple streams at the same time. For writing, you can use the methods in the class-side rendering category. To convert individual objects to a string, you can use #asJsonString, to serialize it on a stream, you can use #jsonWriteOn:. ! Json class instanceVariableNames: 'escapeArray escapeSet'! ----- Method: Json class>>escapeForCharacter: (in category 'accessing') ----- escapeForCharacter: c | asciiValue | (asciiValue := c asciiValue) < 128 ifTrue: [ ^escapeArray at: asciiValue + 1 ]. ^nil! ----- Method: Json class>>escapeSet (in category 'accessing') ----- escapeSet ^escapeSet! ----- Method: Json class>>initialize (in category 'class initialization') ----- initialize "Json initialize." | newEscapeArray newEscapeSet | newEscapeArray := Array new: 128. newEscapeSet := CharacterSet new. (0 to: 31), #(127) do: [ :each | newEscapeArray at: each + 1 put: '\u', (each printStringHex padded: #left to: 4 with: $0). newEscapeSet add: (Character value: each) ]. { $" -> '\"'. $\ -> '\\'. $/ -> '\/'. "A hack, so the generated JSON will always be HTML+Javascript compatible, because the generated JSON will not contain the seqence </script>." Character backspace -> '\b'. Character lf -> '\n'. Character newPage -> '\f'. Character cr -> '\r'. Character tab -> '\t'. } do: [ :each | newEscapeArray at: each key asciiValue + 1 put: each value. newEscapeSet add: each key ]. escapeArray := newEscapeArray. escapeSet := newEscapeSet! ----- Method: Json class>>mimeType (in category 'accessing') ----- mimeType ^ 'application/x-json'! ----- Method: Json class>>newWithConstructors: (in category 'instance creation') ----- newWithConstructors: aCollection | m | m := Dictionary new. aCollection do: [:each | (each isKindOf: Association) ifTrue: [m add: each] ifFalse: [m at: each name asString put: each]]. ^ self new ctorMap: m; yourself.! ----- Method: Json class>>readFrom: (in category 'instance creation') ----- readFrom: aStream ^ self new readFrom: aStream.! ----- Method: Json class>>render: (in category 'rendering') ----- render: anObject ^String streamContents: [ :stream | anObject jsonWriteOn: stream ]! ----- Method: Json class>>render:withConstructor:on: (in category 'rendering') ----- render: anObject withConstructor: aConstructorName on: aStream aStream nextPutAll: '@', aConstructorName. anObject jsonWriteOn: aStream. ! ----- Method: Json class>>renderInstanceVariables:of:on: (in category 'rendering') ----- renderInstanceVariables: aCollection of: anObject on: aStream | map | map := Dictionary new. aCollection do: [:ivarName | map at: ivarName put: (anObject instVarNamed: ivarName)]. self render: map withConstructor: anObject class name asString on: aStream! ----- Method: Json>>arrayBufferStream (in category 'private') ----- arrayBufferStream ^arrayBufferStream ifNil: [ arrayBufferStream := (Array new: 10) writeStream ]! ----- Method: Json>>consume: (in category 'private') ----- consume: aString | message | 1 to: aString size do: [ :index | (currentCharacter := stream next) == (aString at: index) ifFalse: [ message := 'Expected ', (aString at: index) printString. currentCharacter ifNil: [ self incomplete: message ] ifNotNil: [ self invalid: message ] ] ]. currentCharacter := stream next! ----- Method: Json>>ctorMap (in category 'accessing') ----- ctorMap ^ ctorMap! ----- Method: Json>>ctorMap: (in category 'accessing') ----- ctorMap: m ctorMap := m! ----- Method: Json>>dictionaryClass: (in category 'accessing') ----- dictionaryClass: aClass "This method allows you to override the default dictionary class to be created while parsing a stream. Instances of aClass are expected to understand #at:put:." dictionaryClass := aClass! ----- Method: Json>>error: (in category 'error handling') ----- error: aString (currentCharacter isNil and: [ stream atEnd ]) ifTrue: [ self incomplete: aString ] ifFalse: [ self invalid: aString ]! ----- Method: Json>>incomplete: (in category 'error handling') ----- incomplete: aString JsonIncompleteError signal: aString! ----- Method: Json>>initialize (in category 'initialize-release') ----- initialize dictionaryClass := JsonObject ! ----- Method: Json>>interpretStringEscape: (in category 'private') ----- interpretStringEscape: aCharacter aCharacter == $b ifTrue: [ ^Character backspace ]. aCharacter == $n ifTrue: [ ^Character lf ]. aCharacter == $f ifTrue: [ ^Character newPage ]. aCharacter == $r ifTrue: [ ^Character cr ]. aCharacter == $t ifTrue: [ ^Character tab ]. aCharacter == $u ifTrue: [ ^self unescapeUnicode ]. aCharacter == $" ifTrue: [ ^aCharacter ]. aCharacter == $\ ifTrue: [ ^aCharacter ]. aCharacter == $/ ifTrue: [ ^aCharacter ]. self error: 'Unexpected escaped character: ', aCharacter asString ! ----- Method: Json>>invalid: (in category 'error handling') ----- invalid: aString JsonInvalidError signal: aString! ----- Method: Json>>readAny (in category 'private') ----- readAny self skipWhitespace. currentCharacter == ${ ifTrue: [ ^self readDictionary ]. currentCharacter == $[ ifTrue: [ ^self readArray ]. currentCharacter == $" ifTrue: [ ^self readString ]. currentCharacter == $t ifTrue: [ self consume: 'rue'. ^true ]. currentCharacter == $f ifTrue: [ self consume: 'alse'. ^false ]. currentCharacter == $n ifTrue: [ self consume: 'ull'. ^nil ]. currentCharacter == $- ifTrue: [ ^self readNumber: true ]. (#($0 $1 $2 $3 $4 $5 $6 $7 $8 $9) instVarsInclude: currentCharacter) ifTrue: [ ^self readNumber: false ]. currentCharacter == $@ ifTrue: [ ^self readConstructor ]. self invalid: 'Unknown Json input'! ----- Method: Json>>readArray (in category 'private') ----- readArray | initialPosition | currentCharacter := stream next. self skipWhitespace. currentCharacter == $] ifTrue: [ currentCharacter := stream next. ^#() ]. initialPosition := self arrayBufferStream position. [ arrayBufferStream nextPut: self readAny. self skipWhitespace. currentCharacter == $] ifTrue: [ | result | result := arrayBufferStream originalContents copyFrom: initialPosition + 1 to: arrayBufferStream position. arrayBufferStream position: initialPosition. currentCharacter := stream next. ^result ]. currentCharacter == $, ifFalse: [ self error: 'Unexpected character: ', currentCharacter asString ]. currentCharacter := stream next ] repeat. ! ----- Method: Json>>readConstructor (in category 'private') ----- readConstructor ctorMap ifNil: [ ^self error: 'No constructors were declared.' ]. self resetStringBufferStream. [ (currentCharacter := stream next) == $. or: [ currentCharacter isLetter ] ] whileTrue: [ stringBufferStream nextPut: currentCharacter ]. (ctorMap at: stringBufferStream contents ifAbsent: nil) ifNotNil: [ :constructor | ^constructor constructFromJson: self readAny ]. self error: 'Unknown constructor: ', stringBufferStream contents! ----- Method: Json>>readDictionary (in category 'private') ----- readDictionary | result key commaNeeded | result := dictionaryClass new. commaNeeded := false. currentCharacter := stream next. [ self skipWhitespace. currentCharacter == $} ifTrue: [ currentCharacter := stream next. ^result ]. commaNeeded ifFalse: [ commaNeeded := true ] ifTrue: [ currentCharacter == $, ifFalse: [ self error: 'Missing comma' ]. currentCharacter := stream next. self skipWhitespace ]. currentCharacter == $" ifFalse: [ self error: 'Key in dictionary must be string' ]. key := self readString. self skipWhitespace. currentCharacter == $: ifFalse: [ self error: 'Missing colon' ]. currentCharacter := stream next. result at: key put: self readAny ] repeat! ----- Method: Json>>readFrom: (in category 'parsing') ----- readFrom: aStream | result | stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next. result := self readAny. stream atEnd ifFalse: [ stream skip: -1 ]. "Undo prereading." ^result! ----- Method: Json>>readNumber: (in category 'private') ----- readNumber: negative | result | negative ifFalse: [ stream skip: -1 ]. result := (numberParser ifNil: [ numberParser := JsonNumberParser new on: stream; failBlock: [ self error: 'Invalid number.' ]; yourself ]) nextNumber: negative. currentCharacter := stream next. ^result ! ----- Method: Json>>readString (in category 'private') ----- readString self resetStringBufferStream. [ currentCharacter := stream next ifNil: [self incomplete: 'Unexpected end of string' ]. currentCharacter == $" ifTrue: [ currentCharacter := stream next. ^stringBufferStream contents ]. currentCharacter == $\ ifTrue: [ stringBufferStream nextPut: (self interpretStringEscape: (stream next ifNil: [ self incomplete: 'Unexpected end of string' ])) ] ifFalse: [ stringBufferStream nextPut: currentCharacter ] ] repeat! ----- Method: Json>>resetStringBufferStream (in category 'private') ----- resetStringBufferStream stringBufferStream ifNil: [ stringBufferStream := ReadWriteStream on: (String new: 64) ] ifNotNil: [ stringBufferStream resetToStart ]! ----- Method: Json>>skipWhitespace (in category 'private') ----- skipWhitespace currentCharacter ifNil: [ self incomplete: 'Input stream is empty' ]. [ currentCharacter isSeparator ] whileTrue: [ currentCharacter := stream next ifNil: [ self incomplete: 'Input stream is empty' ]. ]! ----- Method: Json>>stream (in category 'accessing') ----- stream "Answer the value of stream" ^ stream! ----- Method: Json>>stream: (in category 'private') ----- stream: aStream "For testing purposes only" stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next! ----- Method: Json>>unescapeUnicode (in category 'private') ----- unescapeUnicode | code digitValue | code := 0. 1 to: 4 do: [ :index | (digitValue := (stream next ifNil: [ self incomplete: 'Unexpected end of stream' ]) digitValue) < 0 ifTrue: [ self invalid: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ code := code * 16 + digitValue ] ifFalse: [ self invalid: 'Invalid hexadecimal digit' ] ]. ^Character value: code! Object subclass: #JsonDummyTestObject instanceVariableNames: 'a b c' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonDummyTestObject class>>constructFromJson: (in category 'as yet unclassified') ----- constructFromJson: j ^ self new a: (j at: 'a'); b: (j at: 'b'); c: (j at: 'c'); yourself! ----- Method: JsonDummyTestObject>>= (in category 'as yet unclassified') ----- = other ^ other class == self class and: [ a = other a and: [ b = other b and: [ c = other c]]]! ----- Method: JsonDummyTestObject>>a (in category 'accessing') ----- a "Answer the value of a" ^ a! ----- Method: JsonDummyTestObject>>a: (in category 'accessing') ----- a: anObject "Set the value of a" a := anObject! ----- Method: JsonDummyTestObject>>b (in category 'accessing') ----- b "Answer the value of b" ^ b! ----- Method: JsonDummyTestObject>>b: (in category 'accessing') ----- b: anObject "Set the value of b" b := anObject! ----- Method: JsonDummyTestObject>>c (in category 'accessing') ----- c "Answer the value of c" ^ c! ----- Method: JsonDummyTestObject>>c: (in category 'accessing') ----- c: anObject "Set the value of c" c := anObject! ----- Method: JsonDummyTestObject>>jsonWriteOn: (in category 'as yet unclassified') ----- jsonWriteOn: s Json renderInstanceVariables: {#a. #b. #c} of: self on: s ! ----- Method: Object>>asJsonString (in category '*JSON') ----- asJsonString ^ String streamContents: [:str | self jsonWriteOn: str ]! Object subclass: #SerializedJson instanceVariableNames: 'jsonString' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: SerializedJson class>>on: (in category 'instance creation') ----- on: aString ^self new jsonString: aString; yourself! ----- Method: SerializedJson>>jsonString (in category 'accessing') ----- jsonString ^jsonString ! ----- Method: SerializedJson>>jsonString: (in category 'accessing') ----- jsonString: aString jsonString := aString ! ----- Method: SerializedJson>>jsonWriteOn: (in category 'json-writing') ----- jsonWriteOn: stream stream nextPutAll: jsonString!
1
0
0
0
The Trunk: JSON-ul.51.mcz
by commits@source.squeak.org
08 Sep '22
08 Sep '22
Christoph Thiede uploaded a new version of JSON to project The Trunk:
http://source.squeak.org/trunk/JSON-ul.51.mcz
==================== Summary ==================== Name: JSON-ul.51 Author: ul Time: 28 March 2020, 4:56:38.588541 am UUID: 63bf1eab-8112-4eca-91e4-530066259253 Ancestors: JSON-klub.50 - fix: only undo prereading in Json >> #readFrom: when there was a character read from the stream - fix: avoid long parsing when exponent is larger than what Floats can represent - fix: raise JsonSyntaxError when a number atom has leading zeroes ==================== Snapshot ==================== SystemOrganization addCategory: #JSON! ----- Method: Integer>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream ^ self printOn: aWriteStream base: 10 ! ----- Method: Association>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream self key asString jsonWriteOn: aStream. aStream nextPut: $:; space. self value jsonWriteOn: aStream.! ----- Method: False>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'false'! ----- Method: Number>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream self printOn: aWriteStream base: 10! OrderedDictionary subclass: #OrderedJsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: OrderedJsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: OrderedJsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: OrderedJsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: OrderedJsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: OrderedJsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: OrderedJsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: Time>>jsonWriteOn: (in category '*JSON') ----- jsonWriteOn: stream stream nextPut: $". self print24: true showSeconds: true on: stream. stream nextPut: $"! ----- Method: WriteStream>>jsonPrint: (in category '*json-printing') ----- jsonPrint: anObject anObject jsonWriteOn: self! ----- Method: Text>>jsonWriteOn: (in category '*json-printing') ----- jsonWriteOn: aStream self string jsonWriteOn: aStream! ----- Method: UndefinedObject>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream aWriteStream nextPutAll: 'null'! Error subclass: #JsonSyntaxError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonSyntaxError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! Class Json signals (possibly-indirect) instances of me when reading a JSON value from an input stream fails.! JsonSyntaxError subclass: #JsonIncompleteError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonIncompleteError commentStamp: 'tonyg 4/29/2016 11:37' prior: 0! I signal that reading a JSON value failed because more input is required, but that the input seen so far was not incorrect. Compare to JsonInvalidError. Be warned that reading numbers directly out of a stream can be ambiguous!! Consider reading from '1234'. Is the result intended to be 1234, or is there missing input, and the next character will be '5', making the result (possibly) 12345?! JsonSyntaxError subclass: #JsonInvalidError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonInvalidError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! I signal that reading a JSON value failed because the input didn't match the (extended)JSON grammar implemented by class Json.! ----- Method: Dictionary>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | needComma | needComma := false. aStream nextPut: ${. self associationsDo: [ :assoc | needComma ifTrue: [ aStream nextPut: $, ] ifFalse: [ needComma := true ]. assoc key jsonWriteOn: aStream. aStream nextPut: $:. assoc value jsonWriteOn: aStream ]. aStream nextPut: $}.! Dictionary subclass: #JsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: JsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: JsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: JsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: JsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: JsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: True>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'true'! NumberParser subclass: #JsonNumberParser instanceVariableNames: 'fraction' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonNumberParser>>allowPlusSign (in category 'accessing') ----- allowPlusSign ^false! ----- Method: JsonNumberParser>>allowPlusSignInExponent (in category 'accessing') ----- allowPlusSignInExponent ^true! ----- Method: JsonNumberParser>>error: (in category 'error') ----- error: aString JsonSyntaxError signal: aString! ----- Method: JsonNumberParser>>exponentLetters (in category 'accessing') ----- exponentLetters ^'eE'! ----- Method: JsonNumberParser>>initialize (in category 'initialize-release') ----- initialize super initialize. base := 10. fraction := Fraction numerator: 0 denominator: 1! ----- Method: JsonNumberParser>>makeFloatFromMantissa:exponent: (in category 'parsing-private') ----- makeFloatFromMantissa: mantissa exponent: exponent exponent = 0 ifTrue: [ ^mantissa asFloat ]. exponent >= 16 ifTrue: [ exponent > Float emax ifTrue: [ mantissa isZero ifTrue: [ ^0.0 ]. mantissa positive ifTrue: [ ^Float infinity ]. ^Float negativeInfinity ]. ^(mantissa * (self tenRaisedTo: exponent)) asFloat ]. (exponent >= -15 and: [ mantissa highBitOfMagnitude <= 54 ]) ifTrue: [ exponent >= 1 ifTrue: [ ^mantissa asFloat * (self tenRaisedTo: exponent) asFloat ]. ^mantissa asFloat / (self tenRaisedTo: exponent negated) asFloat ]. exponent < Float emin ifTrue: [ mantissa positive ifTrue: [ ^0.0 ]. ^-0.0 ]. ^(fraction setNumerator: mantissa denominator: (self tenRaisedTo: exponent negated)) asFloat! ----- Method: JsonNumberParser>>nextElementaryLargeInteger (in category 'parsing-large int') ----- nextElementaryLargeInteger "Form an unsigned integer with incoming digits from sourceStream. Return this integer, or zero if no digits found. Stop reading if end of digits or if a LargeInteger is formed. Count the number of digits and the position of lastNonZero digit and store them in instVar." | digit value | value := 0. nDigits := 0. lastNonZero := 0. [ value isLarge ifTrue: [ ^value ]. ((digit := (sourceStream next ifNil: [ ^value ]) asInteger - 48 "$0 asInteger") < 0 or: [ digit >= 10 ]) ifTrue: [ sourceStream skip: -1. ^value ]. nDigits := nDigits + 1. digit = 0 ifFalse: [ (lastNonZero = 0 and: [ nDigits > 1 ]) ifTrue: [ self error: 'Leading zeroes are not allowed.' ]. lastNonZero := nDigits. value := value * 10 + digit ] ifTrue: [ value := value * 10 ] ] repeat! ----- Method: JsonNumberParser>>nextNumber: (in category 'parsing-public') ----- nextNumber: negative | numberOfTrailingZeroInIntegerPart numberOfTrailingZeroInFractionPart numberOfNonZeroFractionDigits mantissa value | integerPart := self nextUnsignedIntegerOrNil ifNil: [ ^self error: 'Missing integer part!!' ]. numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. (sourceStream peekFor: $.) ifFalse: [ "No fraction part" negative ifTrue: [ integerPart := integerPart negated ]. self readExponent ifFalse: [ ^integerPart ]. exponent > 0 ifTrue: [ ^integerPart * (self tenRaisedTo: exponent) ]. ^(Fraction numerator: integerPart denominator: (self tenRaisedTo: exponent negated)) asFloat ]. fractionPart := self nextUnsignedIntegerOrNil ifNil: [ self error: 'Missing fraction part!!' ]. fractionPart isZero ifTrue: [ self readExponent ifFalse: [ negative ifTrue: [ integerPart isZero ifTrue: [ ^Float negativeZero ]. ^integerPart negated ]. ^integerPart ]. exponent >= 0 ifTrue: [ negative ifTrue: [ integerPart := integerPart negated ]. ^integerPart * (self tenRaisedTo: exponent) ]. value := self makeFloatFromMantissa: integerPart exponent: exponent. negative ifTrue: [ ^value negated ]. ^value ]. numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. numberOfNonZeroFractionDigits := lastNonZero. self readExponent. exponent := exponent - numberOfNonZeroFractionDigits. mantissa := integerPart * (self tenRaisedTo: numberOfNonZeroFractionDigits) + (fractionPart // (self tenRaisedTo: numberOfTrailingZeroInFractionPart)). value := self makeFloatFromMantissa: mantissa exponent: exponent. negative ifTrue: [ ^value negated ]. ^value! ----- Method: JsonNumberParser>>nextUnsignedIntegerOrNil (in category 'parsing-public') ----- nextUnsignedIntegerOrNil "Form an unsigned integer with incoming digits from sourceStream. Answer this integer, or nil if no digit found. Count the number of digits and the position of lastNonZero digit and store them in instVar" | nPackets high nDigitsHigh lastNonZeroHigh low | "read no more digits than one elementary LargeInteger" high := self nextElementaryLargeInteger. nDigits = 0 ifTrue: [^nil]. "Not enough digits to form a LargeInteger, stop iteration" high isLarge ifFalse: [^high]. "We now have to engage arithmetic with LargeInteger Decompose the integer in a high and low packets of growing size:" nPackets := 1. nDigitsHigh := nDigits. lastNonZeroHigh := lastNonZero. [ low := self nextLargeIntegerBase: 10 nPackets: nPackets . high := high * (self tenRaisedTo: nDigits) + low. lastNonZero = 0 ifFalse: [lastNonZeroHigh := lastNonZero + nDigitsHigh]. nDigitsHigh := nDigitsHigh + nDigits. low isLarge] whileTrue: [nPackets := nPackets * 2]. nDigits := nDigitsHigh. lastNonZero := lastNonZeroHigh. ^high! ----- Method: JsonNumberParser>>tenRaisedTo: (in category 'parsing-private') ----- tenRaisedTo: anInteger anInteger >= 0 ifFalse: [ ^10 raisedToInteger: anInteger ]. anInteger >= 16 ifTrue: [ ^10 raisedToInteger: anInteger ]. ^#(1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000 100000000000 1000000000000 10000000000000 100000000000000 1000000000000000) at: anInteger + 1! ----- Method: ScaledDecimal>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: stream self printOn: stream showingDecimalPlaces: scale! ----- Method: Collection>>jsonWriteOn: (in category '*json') ----- jsonWriteOn: aStream "By default, use array braces " aStream nextPut: $[. self do: [:each | each jsonWriteOn: aStream ] separatedBy: [ aStream nextPut: $, ]. aStream nextPut: $]! TestCase subclass: #JsonTests instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonTests commentStamp: '<historical>' prior: 0! I provide a number of test cases for class Json.! ----- Method: JsonTests>>assertIncompleteJson: (in category 'helpers') ----- assertIncompleteJson: aString self should: [self readFrom: aString] raise: JsonIncompleteError! ----- Method: JsonTests>>assertInvalidJson: (in category 'helpers') ----- assertInvalidJson: aString self should: [self readFrom: aString] raise: JsonInvalidError! ----- Method: JsonTests>>json:equals: (in category 'helpers') ----- json: aString equals: aValue | readValue | readValue := self readFrom: aString. self assert: readValue = aValue.! ----- Method: JsonTests>>readFrom: (in category 'helpers') ----- readFrom: aString ^ (Json newWithConstructors: {JsonDummyTestObject.}) readFrom: aString readStream ! ----- Method: JsonTests>>render:equals: (in category 'helpers') ----- render: anObject equals: aString self assert: (Json render: anObject) = aString! ----- Method: JsonTests>>simpleDummyObject (in category 'helpers') ----- simpleDummyObject ^ JsonDummyTestObject new a: 1; b: 2; c: 3; yourself! ----- Method: JsonTests>>testArray (in category 'tests') ----- testArray self json: '[]' equals: #(). self json: '[[]]' equals: #(#()). self json: '[[], []]' equals: #(#() #()). self json: '["hi", "there"]' equals: #('hi' 'there'). self json: '[["a", "b", null]]' equals: #(('a' 'b' nil)).! ----- Method: JsonTests>>testAtomFalse (in category 'tests') ----- testAtomFalse self json: 'false' equals: false. self json: ' false' equals: false. self json: 'false ' equals: false. self json: ' false ' equals: false. ! ----- Method: JsonTests>>testAtomNull (in category 'tests') ----- testAtomNull self json: 'null' equals: nil. self json: ' null' equals: nil. self json: 'null ' equals: nil. self json: ' null ' equals: nil. ! ----- Method: JsonTests>>testAtomNumber (in category 'tests') ----- testAtomNumber self json: '1' equals: 1. self json: '123' equals: 123. self should: [ self json: '0123' equals: 123 ] raise: JsonSyntaxError. "No leading zeroes." self json: '1.23e2' equals: 123. self json: '-1' equals: -1. self json: '-0' equals: 0. self json: '[-1.2]' equals: #(-1.2).! ----- Method: JsonTests>>testAtomString (in category 'tests') ----- testAtomString self json: '"hi"' equals: 'hi'. self json: '"\""' equals: '"'. self json: '"\\"' equals: '\'. self json: '""' equals: ''. self json: '"a\u0004b"' equals: ({$a. Character value: 4. $b} as: String). self json: '"a\nb"' equals: ({$a. Character lf. $b} as: String).! ----- Method: JsonTests>>testAtomTrue (in category 'tests') ----- testAtomTrue self json: 'true' equals: true. self json: ' true' equals: true. self json: 'true ' equals: true. self json: ' true ' equals: true. ! ----- Method: JsonTests>>testCtor (in category 'tests') ----- testCtor self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}' equals: self simpleDummyObject. self json: (Json render: self simpleDummyObject) equals: self simpleDummyObject.! ----- Method: JsonTests>>testDictionary (in category 'tests') ----- testDictionary self json: '{}' equals: (JsonObject new). self json: '{"a": "a"}' equals: (JsonObject new at: 'a' put: 'a'; yourself). self json: '{"a": [[]]}' equals: (JsonObject new at: 'a' put: #(#()); yourself). self json: '{"a":"b", "b":"a"}' equals: (JsonObject new add: 'a'->'b'; add: 'b'->'a';yourself).! ----- Method: JsonTests>>testDictionaryClass (in category 'tests') ----- testDictionaryClass | parser | parser := Json new. self assert: (parser readFrom: '{}' readStream) class == JsonObject. parser dictionaryClass: OrderedJsonObject. self assert: (parser readFrom: '{}' readStream) class == OrderedJsonObject. self assert: (parser readFrom: '{"x":{"y":{}}}' readStream) x y class == OrderedJsonObject! ----- Method: JsonTests>>testIncomplete (in category 'tests') ----- testIncomplete self assertIncompleteJson: ''. self assertIncompleteJson: ' '. self assertIncompleteJson: ' tr'. self assertIncompleteJson: 'tru'. self assertIncompleteJson: '['. self assertIncompleteJson: '[true'. self assertIncompleteJson: '[true,'. self assertIncompleteJson: '{'. self assertIncompleteJson: '{"hi"'. self assertIncompleteJson: '{"hi":'. self assertIncompleteJson: '{"hi":true'. self assertIncompleteJson: '{"hi":true,'. self assertIncompleteJson: '{"hi":true,"'. self assertIncompleteJson: '"hello'. self assertIncompleteJson: '"hello '. self assertIncompleteJson: '"hello\'. self assertIncompleteJson: '"\u26'.! ----- Method: JsonTests>>testInvalid (in category 'tests') ----- testInvalid self assertInvalidJson: 'x'. self assertInvalidJson: ' x'. self assertInvalidJson: ' trx'. self assertInvalidJson: 'trux'. self assertInvalidJson: '.'. self assertInvalidJson: ':'. self assertInvalidJson: ','. self assertInvalidJson: ']'. self assertInvalidJson: '}'. self assertInvalidJson: '[x'. self assertInvalidJson: '[true t'. self assertInvalidJson: '[true,]'. self assertInvalidJson: '{]'. self assertInvalidJson: '{,'. self assertInvalidJson: '{"hi",'. self assertInvalidJson: '{"hi":x'. self assertInvalidJson: '{"hi":,'. self assertInvalidJson: '{"hi":true "'. self assertInvalidJson: '{"hi":true,}'. self assertInvalidJson: '\u263A'.! ----- Method: JsonTests>>testInvalidUnicodeEscapes (in category 'tests') ----- testInvalidUnicodeEscapes self assertInvalidJson: '"\u26"'. "Note that naively reading four chars runs off the end here, so we might see JsonIncompleteError, which would be wrong." self assertInvalidJson: '"\u2Z"'. self assertInvalidJson: '"\u2Z44"'.! ----- Method: JsonTests>>testJsonObjectAtPutReturnsTheObject (in category 'tests') ----- testJsonObjectAtPutReturnsTheObject { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self assert: (json at: 'foo' put: 1) == json. self assert: (json foo: 1) == json ]! ----- Method: JsonTests>>testMissingCtor (in category 'tests') ----- testMissingCtor self assertInvalidJson: '@Missing[]'! ----- Method: JsonTests>>testMissingCtorNoMap (in category 'tests') ----- testMissingCtorNoMap self should: [Json new readFrom: '@Missing[]' readStream] raise: JsonInvalidError! ----- Method: JsonTests>>testNonStringKeysRaiseError (in category 'tests') ----- testNonStringKeysRaiseError { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self should: [ json at: 1 put: 1 ] raise: Error. self assert: json isEmpty. json at: '1' put: 1. self assert: 1 equals: (json at: '1') ]! ----- Method: JsonTests>>testStreaming (in category 'tests') ----- testStreaming | j | j := Json new stream: 'truefalsetrue[]{}1.234 5.678"A""B"nullnull' readStream. self assert: j readAny equals: true. self assert: j readAny equals: false. self assert: j readAny equals: true. self assert: j readAny equals: #(). self assert: j readAny equals: JsonObject new. self assert: j readAny equals: 1.234. self assert: j readAny equals: 5.678. self assert: j readAny equals: 'A'. self assert: j readAny equals: 'B'. self assert: j readAny equals: nil. self assert: j readAny equals: nil.! ----- Method: JsonTests>>testStringWithUnicode (in category 'tests') ----- testStringWithUnicode | unicodeString | unicodeString := (Unicode value: 16r263A) asString. self json: '"\u263A"' equals:unicodeString. self json: '"\u263a"' equals:unicodeString. self json: '"', unicodeString, '"' equals: unicodeString. self render: unicodeString equals: '"', unicodeString, '"'.! ----- Method: JsonTests>>testWriteAssociation (in category 'tests') ----- testWriteAssociation self render: 'key' -> 'value' equals: '"key": "value"'. self render: 'key' -> 2 equals: '"key": 2'. "keys should be strings" self render: 42 -> 2 equals: '"42": 2'. "try to do _something_ for more complex keys" self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'. ! ----- Method: JsonTests>>testWriteString (in category 'tests') ----- testWriteString self render: '"' equals: '"\""'. self render: '\' equals: '"\\"'. self render: 'hi' equals: '"hi"'. self render: ({$a. Character lf. $b} as: String) equals: '"a\nb"'. self render: ({$a. Character value: 4. $b} as: String) equals: '"a\u0004b"'.! ----- Method: String>>asSerializedJson (in category '*JSON') ----- asSerializedJson " Assume that the receiver is a valid serialized json string. Return an object that understands #jsonWriteOn: and will write this string on its argument. " ^SerializedJson on: self! ----- Method: String>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | index start | aStream nextPut: $". start := 1. [ (index := self indexOfAnyOf: Json escapeSet startingAt: start) = 0 ] whileFalse: [ aStream next: index - start putAll: self startingAt: start. aStream nextPutAll: (Json escapeForCharacter: (self at: index)). start := index + 1 ]. start <= self size ifTrue: [ aStream next: self size + 1 - start putAll: self startingAt: start ]. aStream nextPut: $". ! ----- Method: String>>parseAsJson (in category '*JSON') ----- parseAsJson "Convenience" ^Json readFrom: self readStream! ----- Method: String>>parseAsOrderedJson (in category '*JSON') ----- parseAsOrderedJson "Convenience" ^Json new dictionaryClass: OrderedJsonObject; readFrom: self readStream! Object subclass: #Json instanceVariableNames: 'stream currentCharacter arrayBufferStream stringBufferStream numberParser ctorMap dictionaryClass' classVariableNames: '' poolDictionaries: '' category: 'JSON'! Json class instanceVariableNames: 'escapeArray escapeSet'! !Json commentStamp: 'ul 3/29/2016 04:38' prior: 0! I support reading and writing JSON (see
http://json.org
) formatted data - strings, numbers, boolean, nil, arrays and dictionaries. The implementation is mainly based on RFC 7159 (
https://www.ietf.org/rfc/rfc7159.txt
). It has been extended with syntax for invoking a prearranged list of constructors on read objects. Character encoding is not handled here. That's something you have to do before reading or after writing. Mixed processing is possible, but it's entirely your responsibility to keep things correct. Instance Variables stream: <PositionableStream> currentCharacter: <Character> arrayBufferStream: <WriteStream> stringBufferStream: <ReadWriteStream> numberParser: <ExtendedNumberParser> ctorMap: <Dictionary> For parsing, stream has to be initialized by sending #readFrom: to my instance. The parser will pre-read one character, which will be stored in currentCharacter, so the stream is expected to support position manipulation, hence it should be a PositionableStream. arrayBufferStream is WriteStream on an Array, which holds the values during array parsing (see #readArray). When there's a recursive call, this stream is used as a stack to separate the elements parsed in the previous invocation from the current one. stringBufferStream is a ReadWriteStream on a String, which holds the currently parsed String (see #readString), the currently parsed number (see #readNumber) or the name of the currently parsed constructor (see #readConstructor). Recursion has no effect on this. numberParser is a cached instance of ExtendedNumberParser. It's initialized on stringBufferStream, so it can quickly parse the number stored in it. ctorMap is the Dictionary holding the constructor classes by name. arrayBufferStream, stringBufferStream and numberParser are initialized lazily, and should not be accessed externally. Since these are shared objects, along with stream and currentCharacter, holding state, therefore a single instance of me shouldn't be used to parse multiple streams at the same time. For writing, you can use the methods in the class-side rendering category. To convert individual objects to a string, you can use #asJsonString, to serialize it on a stream, you can use #jsonWriteOn:. ! Json class instanceVariableNames: 'escapeArray escapeSet'! ----- Method: Json class>>escapeForCharacter: (in category 'accessing') ----- escapeForCharacter: c | asciiValue | (asciiValue := c asciiValue) < 128 ifTrue: [ ^escapeArray at: asciiValue + 1 ]. ^nil! ----- Method: Json class>>escapeSet (in category 'accessing') ----- escapeSet ^escapeSet! ----- Method: Json class>>initialize (in category 'class initialization') ----- initialize "Json initialize." | newEscapeArray newEscapeSet | newEscapeArray := Array new: 128. newEscapeSet := CharacterSet new. (0 to: 31), #(127) do: [ :each | newEscapeArray at: each + 1 put: '\u', (each printStringHex padded: #left to: 4 with: $0). newEscapeSet add: (Character value: each) ]. { $" -> '\"'. $\ -> '\\'. $/ -> '\/'. "A hack, so the generated JSON will always be HTML+Javascript compatible, because the generated JSON will not contain the seqence </script>." Character backspace -> '\b'. Character lf -> '\n'. Character newPage -> '\f'. Character cr -> '\r'. Character tab -> '\t'. } do: [ :each | newEscapeArray at: each key asciiValue + 1 put: each value. newEscapeSet add: each key ]. escapeArray := newEscapeArray. escapeSet := newEscapeSet! ----- Method: Json class>>mimeType (in category 'accessing') ----- mimeType ^ 'application/x-json'! ----- Method: Json class>>newWithConstructors: (in category 'instance creation') ----- newWithConstructors: aCollection | m | m := Dictionary new. aCollection do: [:each | (each isKindOf: Association) ifTrue: [m add: each] ifFalse: [m at: each name asString put: each]]. ^ self new ctorMap: m; yourself.! ----- Method: Json class>>readFrom: (in category 'instance creation') ----- readFrom: aStream ^ self new readFrom: aStream.! ----- Method: Json class>>render: (in category 'rendering') ----- render: anObject ^String streamContents: [ :stream | anObject jsonWriteOn: stream ]! ----- Method: Json class>>render:withConstructor:on: (in category 'rendering') ----- render: anObject withConstructor: aConstructorName on: aStream aStream nextPutAll: '@', aConstructorName. anObject jsonWriteOn: aStream. ! ----- Method: Json class>>renderInstanceVariables:of:on: (in category 'rendering') ----- renderInstanceVariables: aCollection of: anObject on: aStream | map | map := Dictionary new. aCollection do: [:ivarName | map at: ivarName put: (anObject instVarNamed: ivarName)]. self render: map withConstructor: anObject class name asString on: aStream! ----- Method: Json>>arrayBufferStream (in category 'private') ----- arrayBufferStream ^arrayBufferStream ifNil: [ arrayBufferStream := (Array new: 10) writeStream ]! ----- Method: Json>>consume: (in category 'private') ----- consume: aString | message | 1 to: aString size do: [ :index | (currentCharacter := stream next) == (aString at: index) ifFalse: [ message := 'Expected ', (aString at: index) printString. currentCharacter ifNil: [ self incomplete: message ] ifNotNil: [ self invalid: message ] ] ]. currentCharacter := stream next! ----- Method: Json>>ctorMap (in category 'accessing') ----- ctorMap ^ ctorMap! ----- Method: Json>>ctorMap: (in category 'accessing') ----- ctorMap: m ctorMap := m! ----- Method: Json>>dictionaryClass: (in category 'accessing') ----- dictionaryClass: aClass "This method allows you to override the default dictionary class to be created while parsing a stream. Instances of aClass are expected to understand #at:put:." dictionaryClass := aClass! ----- Method: Json>>error: (in category 'error handling') ----- error: aString (currentCharacter isNil and: [ stream atEnd ]) ifTrue: [ self incomplete: aString ] ifFalse: [ self invalid: aString ]! ----- Method: Json>>incomplete: (in category 'error handling') ----- incomplete: aString JsonIncompleteError signal: aString! ----- Method: Json>>initialize (in category 'initialize-release') ----- initialize dictionaryClass := JsonObject ! ----- Method: Json>>interpretStringEscape: (in category 'private') ----- interpretStringEscape: aCharacter aCharacter == $b ifTrue: [ ^Character backspace ]. aCharacter == $n ifTrue: [ ^Character lf ]. aCharacter == $f ifTrue: [ ^Character newPage ]. aCharacter == $r ifTrue: [ ^Character cr ]. aCharacter == $t ifTrue: [ ^Character tab ]. aCharacter == $u ifTrue: [ ^self unescapeUnicode ]. aCharacter == $" ifTrue: [ ^aCharacter ]. aCharacter == $\ ifTrue: [ ^aCharacter ]. aCharacter == $/ ifTrue: [ ^aCharacter ]. self error: 'Unexpected escaped character: ', aCharacter asString ! ----- Method: Json>>invalid: (in category 'error handling') ----- invalid: aString JsonInvalidError signal: aString! ----- Method: Json>>readAny (in category 'private') ----- readAny self skipWhitespace. currentCharacter == ${ ifTrue: [ ^self readDictionary ]. currentCharacter == $[ ifTrue: [ ^self readArray ]. currentCharacter == $" ifTrue: [ ^self readString ]. currentCharacter == $t ifTrue: [ self consume: 'rue'. ^true ]. currentCharacter == $f ifTrue: [ self consume: 'alse'. ^false ]. currentCharacter == $n ifTrue: [ self consume: 'ull'. ^nil ]. currentCharacter == $- ifTrue: [ ^self readNumber: true ]. (#($0 $1 $2 $3 $4 $5 $6 $7 $8 $9) instVarsInclude: currentCharacter) ifTrue: [ ^self readNumber: false ]. currentCharacter == $@ ifTrue: [ ^self readConstructor ]. self invalid: 'Unknown Json input'! ----- Method: Json>>readArray (in category 'private') ----- readArray | initialPosition | currentCharacter := stream next. self skipWhitespace. currentCharacter == $] ifTrue: [ currentCharacter := stream next. ^#() ]. initialPosition := self arrayBufferStream position. [ arrayBufferStream nextPut: self readAny. self skipWhitespace. currentCharacter == $] ifTrue: [ | result | result := arrayBufferStream originalContents copyFrom: initialPosition + 1 to: arrayBufferStream position. arrayBufferStream position: initialPosition. currentCharacter := stream next. ^result ]. currentCharacter == $, ifFalse: [ self error: 'Unexpected character: ', currentCharacter asString ]. currentCharacter := stream next ] repeat. ! ----- Method: Json>>readConstructor (in category 'private') ----- readConstructor ctorMap ifNil: [ ^self error: 'No constructors were declared.' ]. self resetStringBufferStream. [ (currentCharacter := stream next) == $. or: [ currentCharacter isLetter ] ] whileTrue: [ stringBufferStream nextPut: currentCharacter ]. (ctorMap at: stringBufferStream contents ifAbsent: nil) ifNotNil: [ :constructor | ^constructor constructFromJson: self readAny ]. self error: 'Unknown constructor: ', stringBufferStream contents! ----- Method: Json>>readDictionary (in category 'private') ----- readDictionary | result key commaNeeded | result := dictionaryClass new. commaNeeded := false. currentCharacter := stream next. [ self skipWhitespace. currentCharacter == $} ifTrue: [ currentCharacter := stream next. ^result ]. commaNeeded ifFalse: [ commaNeeded := true ] ifTrue: [ currentCharacter == $, ifFalse: [ self error: 'Missing comma' ]. currentCharacter := stream next. self skipWhitespace ]. currentCharacter == $" ifFalse: [ self error: 'Key in dictionary must be string' ]. key := self readString. self skipWhitespace. currentCharacter == $: ifFalse: [ self error: 'Missing colon' ]. currentCharacter := stream next. result at: key put: self readAny ] repeat! ----- Method: Json>>readFrom: (in category 'parsing') ----- readFrom: aStream | result | stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next. result := self readAny. currentCharacter ifNotNil: [ stream skip: -1 ]. "Undo prereading." ^result! ----- Method: Json>>readNumber: (in category 'private') ----- readNumber: negative | result | negative ifFalse: [ stream skip: -1 ]. result := (numberParser ifNil: [ numberParser := JsonNumberParser new on: stream; failBlock: [ self error: 'Invalid number.' ]; yourself ]) nextNumber: negative. currentCharacter := stream next. ^result ! ----- Method: Json>>readString (in category 'private') ----- readString self resetStringBufferStream. [ currentCharacter := stream next ifNil: [self incomplete: 'Unexpected end of string' ]. currentCharacter == $" ifTrue: [ currentCharacter := stream next. ^stringBufferStream contents ]. currentCharacter == $\ ifTrue: [ stringBufferStream nextPut: (self interpretStringEscape: (stream next ifNil: [ self incomplete: 'Unexpected end of string' ])) ] ifFalse: [ stringBufferStream nextPut: currentCharacter ] ] repeat! ----- Method: Json>>resetStringBufferStream (in category 'private') ----- resetStringBufferStream stringBufferStream ifNil: [ stringBufferStream := ReadWriteStream on: (String new: 64) ] ifNotNil: [ stringBufferStream resetToStart ]! ----- Method: Json>>skipWhitespace (in category 'private') ----- skipWhitespace currentCharacter ifNil: [ self incomplete: 'Input stream is empty' ]. [ currentCharacter isSeparator ] whileTrue: [ currentCharacter := stream next ifNil: [ self incomplete: 'Input stream is empty' ]. ]! ----- Method: Json>>stream (in category 'accessing') ----- stream "Answer the value of stream" ^ stream! ----- Method: Json>>stream: (in category 'private') ----- stream: aStream "For testing purposes only" stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next! ----- Method: Json>>unescapeUnicode (in category 'private') ----- unescapeUnicode | code digitValue | code := 0. 1 to: 4 do: [ :index | (digitValue := (stream next ifNil: [ self incomplete: 'Unexpected end of stream' ]) digitValue) < 0 ifTrue: [ self invalid: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ code := code * 16 + digitValue ] ifFalse: [ self invalid: 'Invalid hexadecimal digit' ] ]. ^Character value: code! Object subclass: #JsonDummyTestObject instanceVariableNames: 'a b c' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonDummyTestObject class>>constructFromJson: (in category 'as yet unclassified') ----- constructFromJson: j ^ self new a: (j at: 'a'); b: (j at: 'b'); c: (j at: 'c'); yourself! ----- Method: JsonDummyTestObject>>= (in category 'as yet unclassified') ----- = other ^ other class == self class and: [ a = other a and: [ b = other b and: [ c = other c]]]! ----- Method: JsonDummyTestObject>>a (in category 'accessing') ----- a "Answer the value of a" ^ a! ----- Method: JsonDummyTestObject>>a: (in category 'accessing') ----- a: anObject "Set the value of a" a := anObject! ----- Method: JsonDummyTestObject>>b (in category 'accessing') ----- b "Answer the value of b" ^ b! ----- Method: JsonDummyTestObject>>b: (in category 'accessing') ----- b: anObject "Set the value of b" b := anObject! ----- Method: JsonDummyTestObject>>c (in category 'accessing') ----- c "Answer the value of c" ^ c! ----- Method: JsonDummyTestObject>>c: (in category 'accessing') ----- c: anObject "Set the value of c" c := anObject! ----- Method: JsonDummyTestObject>>jsonWriteOn: (in category 'as yet unclassified') ----- jsonWriteOn: s Json renderInstanceVariables: {#a. #b. #c} of: self on: s ! ----- Method: Object>>asJsonString (in category '*JSON') ----- asJsonString ^ String streamContents: [:str | self jsonWriteOn: str ]! Object subclass: #SerializedJson instanceVariableNames: 'jsonString' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: SerializedJson class>>on: (in category 'instance creation') ----- on: aString ^self new jsonString: aString; yourself! ----- Method: SerializedJson>>jsonString (in category 'accessing') ----- jsonString ^jsonString ! ----- Method: SerializedJson>>jsonString: (in category 'accessing') ----- jsonString: aString jsonString := aString ! ----- Method: SerializedJson>>jsonWriteOn: (in category 'json-writing') ----- jsonWriteOn: stream stream nextPutAll: jsonString!
1
0
0
0
The Trunk: JSON-ul.52.mcz
by commits@source.squeak.org
08 Sep '22
08 Sep '22
Christoph Thiede uploaded a new version of JSON to project The Trunk:
http://source.squeak.org/trunk/JSON-ul.52.mcz
==================== Summary ==================== Name: JSON-ul.52 Author: ul Time: 28 March 2020, 5:14:19.385987 am UUID: ce84535f-78e3-483e-b833-f6ac1d4e0f54 Ancestors: JSON-ul.51 - tightened up the range of possible valid decimal exponents ==================== Snapshot ==================== SystemOrganization addCategory: #JSON! ----- Method: Integer>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream ^ self printOn: aWriteStream base: 10 ! ----- Method: Association>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream self key asString jsonWriteOn: aStream. aStream nextPut: $:; space. self value jsonWriteOn: aStream.! ----- Method: False>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'false'! ----- Method: Number>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream self printOn: aWriteStream base: 10! OrderedDictionary subclass: #OrderedJsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: OrderedJsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: OrderedJsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: OrderedJsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: OrderedJsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: OrderedJsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: OrderedJsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: Time>>jsonWriteOn: (in category '*JSON') ----- jsonWriteOn: stream stream nextPut: $". self print24: true showSeconds: true on: stream. stream nextPut: $"! ----- Method: WriteStream>>jsonPrint: (in category '*json-printing') ----- jsonPrint: anObject anObject jsonWriteOn: self! ----- Method: Text>>jsonWriteOn: (in category '*json-printing') ----- jsonWriteOn: aStream self string jsonWriteOn: aStream! ----- Method: UndefinedObject>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream aWriteStream nextPutAll: 'null'! Error subclass: #JsonSyntaxError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonSyntaxError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! Class Json signals (possibly-indirect) instances of me when reading a JSON value from an input stream fails.! JsonSyntaxError subclass: #JsonIncompleteError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonIncompleteError commentStamp: 'tonyg 4/29/2016 11:37' prior: 0! I signal that reading a JSON value failed because more input is required, but that the input seen so far was not incorrect. Compare to JsonInvalidError. Be warned that reading numbers directly out of a stream can be ambiguous!! Consider reading from '1234'. Is the result intended to be 1234, or is there missing input, and the next character will be '5', making the result (possibly) 12345?! JsonSyntaxError subclass: #JsonInvalidError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonInvalidError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! I signal that reading a JSON value failed because the input didn't match the (extended)JSON grammar implemented by class Json.! ----- Method: Dictionary>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | needComma | needComma := false. aStream nextPut: ${. self associationsDo: [ :assoc | needComma ifTrue: [ aStream nextPut: $, ] ifFalse: [ needComma := true ]. assoc key jsonWriteOn: aStream. aStream nextPut: $:. assoc value jsonWriteOn: aStream ]. aStream nextPut: $}.! Dictionary subclass: #JsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: JsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: JsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: JsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: JsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: JsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: True>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'true'! NumberParser subclass: #JsonNumberParser instanceVariableNames: 'fraction' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonNumberParser>>allowPlusSign (in category 'accessing') ----- allowPlusSign ^false! ----- Method: JsonNumberParser>>allowPlusSignInExponent (in category 'accessing') ----- allowPlusSignInExponent ^true! ----- Method: JsonNumberParser>>error: (in category 'error') ----- error: aString JsonSyntaxError signal: aString! ----- Method: JsonNumberParser>>exponentLetters (in category 'accessing') ----- exponentLetters ^'eE'! ----- Method: JsonNumberParser>>initialize (in category 'initialize-release') ----- initialize super initialize. base := 10. fraction := Fraction numerator: 0 denominator: 1! ----- Method: JsonNumberParser>>makeFloatFromMantissa:exponent: (in category 'parsing-private') ----- makeFloatFromMantissa: mantissa exponent: exponent exponent = 0 ifTrue: [ ^mantissa asFloat ]. exponent >= 16 ifTrue: [ exponent > 324 ifTrue: [ "(Float emax + 53 * 2 ln / 10 ln) ceiling => 324" mantissa isZero ifTrue: [ ^0.0 ]. mantissa positive ifTrue: [ ^Float infinity ]. ^Float negativeInfinity ]. ^(mantissa * (self tenRaisedTo: exponent)) asFloat ]. (exponent >= -15 and: [ mantissa highBitOfMagnitude <= 54 ]) ifTrue: [ exponent >= 1 ifTrue: [ ^mantissa asFloat * (self tenRaisedTo: exponent) asFloat ]. ^mantissa asFloat / (self tenRaisedTo: exponent negated) asFloat ]. exponent < -324 ifTrue: [ "-1 * (53 - Float emin * 2 ln / 10 ln) ceiling => -324" mantissa positive ifTrue: [ ^0.0 ]. ^-0.0 ]. ^(fraction setNumerator: mantissa denominator: (self tenRaisedTo: exponent negated)) asFloat! ----- Method: JsonNumberParser>>nextElementaryLargeInteger (in category 'parsing-large int') ----- nextElementaryLargeInteger "Form an unsigned integer with incoming digits from sourceStream. Return this integer, or zero if no digits found. Stop reading if end of digits or if a LargeInteger is formed. Count the number of digits and the position of lastNonZero digit and store them in instVar." | digit value | value := 0. nDigits := 0. lastNonZero := 0. [ value isLarge ifTrue: [ ^value ]. ((digit := (sourceStream next ifNil: [ ^value ]) asInteger - 48 "$0 asInteger") < 0 or: [ digit >= 10 ]) ifTrue: [ sourceStream skip: -1. ^value ]. nDigits := nDigits + 1. digit = 0 ifFalse: [ (lastNonZero = 0 and: [ nDigits > 1 ]) ifTrue: [ self error: 'Leading zeroes are not allowed.' ]. lastNonZero := nDigits. value := value * 10 + digit ] ifTrue: [ value := value * 10 ] ] repeat! ----- Method: JsonNumberParser>>nextNumber: (in category 'parsing-public') ----- nextNumber: negative | numberOfTrailingZeroInIntegerPart numberOfTrailingZeroInFractionPart numberOfNonZeroFractionDigits mantissa value | integerPart := self nextUnsignedIntegerOrNil ifNil: [ ^self error: 'Missing integer part!!' ]. numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. (sourceStream peekFor: $.) ifFalse: [ "No fraction part" negative ifTrue: [ integerPart := integerPart negated ]. self readExponent ifFalse: [ ^integerPart ]. exponent > 0 ifTrue: [ ^integerPart * (self tenRaisedTo: exponent) ]. ^(Fraction numerator: integerPart denominator: (self tenRaisedTo: exponent negated)) asFloat ]. fractionPart := self nextUnsignedIntegerOrNil ifNil: [ self error: 'Missing fraction part!!' ]. fractionPart isZero ifTrue: [ self readExponent ifFalse: [ negative ifTrue: [ integerPart isZero ifTrue: [ ^Float negativeZero ]. ^integerPart negated ]. ^integerPart ]. exponent >= 0 ifTrue: [ negative ifTrue: [ integerPart := integerPart negated ]. ^integerPart * (self tenRaisedTo: exponent) ]. value := self makeFloatFromMantissa: integerPart exponent: exponent. negative ifTrue: [ ^value negated ]. ^value ]. numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. numberOfNonZeroFractionDigits := lastNonZero. self readExponent. exponent := exponent - numberOfNonZeroFractionDigits. mantissa := integerPart * (self tenRaisedTo: numberOfNonZeroFractionDigits) + (fractionPart // (self tenRaisedTo: numberOfTrailingZeroInFractionPart)). value := self makeFloatFromMantissa: mantissa exponent: exponent. negative ifTrue: [ ^value negated ]. ^value! ----- Method: JsonNumberParser>>nextUnsignedIntegerOrNil (in category 'parsing-public') ----- nextUnsignedIntegerOrNil "Form an unsigned integer with incoming digits from sourceStream. Answer this integer, or nil if no digit found. Count the number of digits and the position of lastNonZero digit and store them in instVar" | nPackets high nDigitsHigh lastNonZeroHigh low | "read no more digits than one elementary LargeInteger" high := self nextElementaryLargeInteger. nDigits = 0 ifTrue: [^nil]. "Not enough digits to form a LargeInteger, stop iteration" high isLarge ifFalse: [^high]. "We now have to engage arithmetic with LargeInteger Decompose the integer in a high and low packets of growing size:" nPackets := 1. nDigitsHigh := nDigits. lastNonZeroHigh := lastNonZero. [ low := self nextLargeIntegerBase: 10 nPackets: nPackets . high := high * (self tenRaisedTo: nDigits) + low. lastNonZero = 0 ifFalse: [lastNonZeroHigh := lastNonZero + nDigitsHigh]. nDigitsHigh := nDigitsHigh + nDigits. low isLarge] whileTrue: [nPackets := nPackets * 2]. nDigits := nDigitsHigh. lastNonZero := lastNonZeroHigh. ^high! ----- Method: JsonNumberParser>>tenRaisedTo: (in category 'parsing-private') ----- tenRaisedTo: anInteger anInteger >= 0 ifFalse: [ ^10 raisedToInteger: anInteger ]. anInteger >= 16 ifTrue: [ ^10 raisedToInteger: anInteger ]. ^#(1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000 100000000000 1000000000000 10000000000000 100000000000000 1000000000000000) at: anInteger + 1! ----- Method: ScaledDecimal>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: stream self printOn: stream showingDecimalPlaces: scale! ----- Method: Collection>>jsonWriteOn: (in category '*json') ----- jsonWriteOn: aStream "By default, use array braces " aStream nextPut: $[. self do: [:each | each jsonWriteOn: aStream ] separatedBy: [ aStream nextPut: $, ]. aStream nextPut: $]! TestCase subclass: #JsonTests instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonTests commentStamp: '<historical>' prior: 0! I provide a number of test cases for class Json.! ----- Method: JsonTests>>assertIncompleteJson: (in category 'helpers') ----- assertIncompleteJson: aString self should: [self readFrom: aString] raise: JsonIncompleteError! ----- Method: JsonTests>>assertInvalidJson: (in category 'helpers') ----- assertInvalidJson: aString self should: [self readFrom: aString] raise: JsonInvalidError! ----- Method: JsonTests>>json:equals: (in category 'helpers') ----- json: aString equals: aValue | readValue | readValue := self readFrom: aString. self assert: readValue = aValue.! ----- Method: JsonTests>>readFrom: (in category 'helpers') ----- readFrom: aString ^ (Json newWithConstructors: {JsonDummyTestObject.}) readFrom: aString readStream ! ----- Method: JsonTests>>render:equals: (in category 'helpers') ----- render: anObject equals: aString self assert: (Json render: anObject) = aString! ----- Method: JsonTests>>simpleDummyObject (in category 'helpers') ----- simpleDummyObject ^ JsonDummyTestObject new a: 1; b: 2; c: 3; yourself! ----- Method: JsonTests>>testArray (in category 'tests') ----- testArray self json: '[]' equals: #(). self json: '[[]]' equals: #(#()). self json: '[[], []]' equals: #(#() #()). self json: '["hi", "there"]' equals: #('hi' 'there'). self json: '[["a", "b", null]]' equals: #(('a' 'b' nil)).! ----- Method: JsonTests>>testAtomFalse (in category 'tests') ----- testAtomFalse self json: 'false' equals: false. self json: ' false' equals: false. self json: 'false ' equals: false. self json: ' false ' equals: false. ! ----- Method: JsonTests>>testAtomNull (in category 'tests') ----- testAtomNull self json: 'null' equals: nil. self json: ' null' equals: nil. self json: 'null ' equals: nil. self json: ' null ' equals: nil. ! ----- Method: JsonTests>>testAtomNumber (in category 'tests') ----- testAtomNumber self json: '1' equals: 1. self json: '123' equals: 123. self should: [ self json: '0123' equals: 123 ] raise: JsonSyntaxError. "No leading zeroes." self json: '1.23e2' equals: 123. self json: '-1' equals: -1. self json: '-0' equals: 0. self json: '[-1.2]' equals: #(-1.2).! ----- Method: JsonTests>>testAtomString (in category 'tests') ----- testAtomString self json: '"hi"' equals: 'hi'. self json: '"\""' equals: '"'. self json: '"\\"' equals: '\'. self json: '""' equals: ''. self json: '"a\u0004b"' equals: ({$a. Character value: 4. $b} as: String). self json: '"a\nb"' equals: ({$a. Character lf. $b} as: String).! ----- Method: JsonTests>>testAtomTrue (in category 'tests') ----- testAtomTrue self json: 'true' equals: true. self json: ' true' equals: true. self json: 'true ' equals: true. self json: ' true ' equals: true. ! ----- Method: JsonTests>>testCtor (in category 'tests') ----- testCtor self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}' equals: self simpleDummyObject. self json: (Json render: self simpleDummyObject) equals: self simpleDummyObject.! ----- Method: JsonTests>>testDictionary (in category 'tests') ----- testDictionary self json: '{}' equals: (JsonObject new). self json: '{"a": "a"}' equals: (JsonObject new at: 'a' put: 'a'; yourself). self json: '{"a": [[]]}' equals: (JsonObject new at: 'a' put: #(#()); yourself). self json: '{"a":"b", "b":"a"}' equals: (JsonObject new add: 'a'->'b'; add: 'b'->'a';yourself).! ----- Method: JsonTests>>testDictionaryClass (in category 'tests') ----- testDictionaryClass | parser | parser := Json new. self assert: (parser readFrom: '{}' readStream) class == JsonObject. parser dictionaryClass: OrderedJsonObject. self assert: (parser readFrom: '{}' readStream) class == OrderedJsonObject. self assert: (parser readFrom: '{"x":{"y":{}}}' readStream) x y class == OrderedJsonObject! ----- Method: JsonTests>>testIncomplete (in category 'tests') ----- testIncomplete self assertIncompleteJson: ''. self assertIncompleteJson: ' '. self assertIncompleteJson: ' tr'. self assertIncompleteJson: 'tru'. self assertIncompleteJson: '['. self assertIncompleteJson: '[true'. self assertIncompleteJson: '[true,'. self assertIncompleteJson: '{'. self assertIncompleteJson: '{"hi"'. self assertIncompleteJson: '{"hi":'. self assertIncompleteJson: '{"hi":true'. self assertIncompleteJson: '{"hi":true,'. self assertIncompleteJson: '{"hi":true,"'. self assertIncompleteJson: '"hello'. self assertIncompleteJson: '"hello '. self assertIncompleteJson: '"hello\'. self assertIncompleteJson: '"\u26'.! ----- Method: JsonTests>>testInvalid (in category 'tests') ----- testInvalid self assertInvalidJson: 'x'. self assertInvalidJson: ' x'. self assertInvalidJson: ' trx'. self assertInvalidJson: 'trux'. self assertInvalidJson: '.'. self assertInvalidJson: ':'. self assertInvalidJson: ','. self assertInvalidJson: ']'. self assertInvalidJson: '}'. self assertInvalidJson: '[x'. self assertInvalidJson: '[true t'. self assertInvalidJson: '[true,]'. self assertInvalidJson: '{]'. self assertInvalidJson: '{,'. self assertInvalidJson: '{"hi",'. self assertInvalidJson: '{"hi":x'. self assertInvalidJson: '{"hi":,'. self assertInvalidJson: '{"hi":true "'. self assertInvalidJson: '{"hi":true,}'. self assertInvalidJson: '\u263A'.! ----- Method: JsonTests>>testInvalidUnicodeEscapes (in category 'tests') ----- testInvalidUnicodeEscapes self assertInvalidJson: '"\u26"'. "Note that naively reading four chars runs off the end here, so we might see JsonIncompleteError, which would be wrong." self assertInvalidJson: '"\u2Z"'. self assertInvalidJson: '"\u2Z44"'.! ----- Method: JsonTests>>testJsonObjectAtPutReturnsTheObject (in category 'tests') ----- testJsonObjectAtPutReturnsTheObject { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self assert: (json at: 'foo' put: 1) == json. self assert: (json foo: 1) == json ]! ----- Method: JsonTests>>testMissingCtor (in category 'tests') ----- testMissingCtor self assertInvalidJson: '@Missing[]'! ----- Method: JsonTests>>testMissingCtorNoMap (in category 'tests') ----- testMissingCtorNoMap self should: [Json new readFrom: '@Missing[]' readStream] raise: JsonInvalidError! ----- Method: JsonTests>>testNonStringKeysRaiseError (in category 'tests') ----- testNonStringKeysRaiseError { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self should: [ json at: 1 put: 1 ] raise: Error. self assert: json isEmpty. json at: '1' put: 1. self assert: 1 equals: (json at: '1') ]! ----- Method: JsonTests>>testStreaming (in category 'tests') ----- testStreaming | j | j := Json new stream: 'truefalsetrue[]{}1.234 5.678"A""B"nullnull' readStream. self assert: j readAny equals: true. self assert: j readAny equals: false. self assert: j readAny equals: true. self assert: j readAny equals: #(). self assert: j readAny equals: JsonObject new. self assert: j readAny equals: 1.234. self assert: j readAny equals: 5.678. self assert: j readAny equals: 'A'. self assert: j readAny equals: 'B'. self assert: j readAny equals: nil. self assert: j readAny equals: nil.! ----- Method: JsonTests>>testStringWithUnicode (in category 'tests') ----- testStringWithUnicode | unicodeString | unicodeString := (Unicode value: 16r263A) asString. self json: '"\u263A"' equals:unicodeString. self json: '"\u263a"' equals:unicodeString. self json: '"', unicodeString, '"' equals: unicodeString. self render: unicodeString equals: '"', unicodeString, '"'.! ----- Method: JsonTests>>testWriteAssociation (in category 'tests') ----- testWriteAssociation self render: 'key' -> 'value' equals: '"key": "value"'. self render: 'key' -> 2 equals: '"key": 2'. "keys should be strings" self render: 42 -> 2 equals: '"42": 2'. "try to do _something_ for more complex keys" self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'. ! ----- Method: JsonTests>>testWriteString (in category 'tests') ----- testWriteString self render: '"' equals: '"\""'. self render: '\' equals: '"\\"'. self render: 'hi' equals: '"hi"'. self render: ({$a. Character lf. $b} as: String) equals: '"a\nb"'. self render: ({$a. Character value: 4. $b} as: String) equals: '"a\u0004b"'.! ----- Method: String>>asSerializedJson (in category '*JSON') ----- asSerializedJson " Assume that the receiver is a valid serialized json string. Return an object that understands #jsonWriteOn: and will write this string on its argument. " ^SerializedJson on: self! ----- Method: String>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | index start | aStream nextPut: $". start := 1. [ (index := self indexOfAnyOf: Json escapeSet startingAt: start) = 0 ] whileFalse: [ aStream next: index - start putAll: self startingAt: start. aStream nextPutAll: (Json escapeForCharacter: (self at: index)). start := index + 1 ]. start <= self size ifTrue: [ aStream next: self size + 1 - start putAll: self startingAt: start ]. aStream nextPut: $". ! ----- Method: String>>parseAsJson (in category '*JSON') ----- parseAsJson "Convenience" ^Json readFrom: self readStream! ----- Method: String>>parseAsOrderedJson (in category '*JSON') ----- parseAsOrderedJson "Convenience" ^Json new dictionaryClass: OrderedJsonObject; readFrom: self readStream! Object subclass: #Json instanceVariableNames: 'stream currentCharacter arrayBufferStream stringBufferStream numberParser ctorMap dictionaryClass' classVariableNames: '' poolDictionaries: '' category: 'JSON'! Json class instanceVariableNames: 'escapeArray escapeSet'! !Json commentStamp: 'ul 3/29/2016 04:38' prior: 0! I support reading and writing JSON (see
http://json.org
) formatted data - strings, numbers, boolean, nil, arrays and dictionaries. The implementation is mainly based on RFC 7159 (
https://www.ietf.org/rfc/rfc7159.txt
). It has been extended with syntax for invoking a prearranged list of constructors on read objects. Character encoding is not handled here. That's something you have to do before reading or after writing. Mixed processing is possible, but it's entirely your responsibility to keep things correct. Instance Variables stream: <PositionableStream> currentCharacter: <Character> arrayBufferStream: <WriteStream> stringBufferStream: <ReadWriteStream> numberParser: <ExtendedNumberParser> ctorMap: <Dictionary> For parsing, stream has to be initialized by sending #readFrom: to my instance. The parser will pre-read one character, which will be stored in currentCharacter, so the stream is expected to support position manipulation, hence it should be a PositionableStream. arrayBufferStream is WriteStream on an Array, which holds the values during array parsing (see #readArray). When there's a recursive call, this stream is used as a stack to separate the elements parsed in the previous invocation from the current one. stringBufferStream is a ReadWriteStream on a String, which holds the currently parsed String (see #readString), the currently parsed number (see #readNumber) or the name of the currently parsed constructor (see #readConstructor). Recursion has no effect on this. numberParser is a cached instance of ExtendedNumberParser. It's initialized on stringBufferStream, so it can quickly parse the number stored in it. ctorMap is the Dictionary holding the constructor classes by name. arrayBufferStream, stringBufferStream and numberParser are initialized lazily, and should not be accessed externally. Since these are shared objects, along with stream and currentCharacter, holding state, therefore a single instance of me shouldn't be used to parse multiple streams at the same time. For writing, you can use the methods in the class-side rendering category. To convert individual objects to a string, you can use #asJsonString, to serialize it on a stream, you can use #jsonWriteOn:. ! Json class instanceVariableNames: 'escapeArray escapeSet'! ----- Method: Json class>>escapeForCharacter: (in category 'accessing') ----- escapeForCharacter: c | asciiValue | (asciiValue := c asciiValue) < 128 ifTrue: [ ^escapeArray at: asciiValue + 1 ]. ^nil! ----- Method: Json class>>escapeSet (in category 'accessing') ----- escapeSet ^escapeSet! ----- Method: Json class>>initialize (in category 'class initialization') ----- initialize "Json initialize." | newEscapeArray newEscapeSet | newEscapeArray := Array new: 128. newEscapeSet := CharacterSet new. (0 to: 31), #(127) do: [ :each | newEscapeArray at: each + 1 put: '\u', (each printStringHex padded: #left to: 4 with: $0). newEscapeSet add: (Character value: each) ]. { $" -> '\"'. $\ -> '\\'. $/ -> '\/'. "A hack, so the generated JSON will always be HTML+Javascript compatible, because the generated JSON will not contain the seqence </script>." Character backspace -> '\b'. Character lf -> '\n'. Character newPage -> '\f'. Character cr -> '\r'. Character tab -> '\t'. } do: [ :each | newEscapeArray at: each key asciiValue + 1 put: each value. newEscapeSet add: each key ]. escapeArray := newEscapeArray. escapeSet := newEscapeSet! ----- Method: Json class>>mimeType (in category 'accessing') ----- mimeType ^ 'application/x-json'! ----- Method: Json class>>newWithConstructors: (in category 'instance creation') ----- newWithConstructors: aCollection | m | m := Dictionary new. aCollection do: [:each | (each isKindOf: Association) ifTrue: [m add: each] ifFalse: [m at: each name asString put: each]]. ^ self new ctorMap: m; yourself.! ----- Method: Json class>>readFrom: (in category 'instance creation') ----- readFrom: aStream ^ self new readFrom: aStream.! ----- Method: Json class>>render: (in category 'rendering') ----- render: anObject ^String streamContents: [ :stream | anObject jsonWriteOn: stream ]! ----- Method: Json class>>render:withConstructor:on: (in category 'rendering') ----- render: anObject withConstructor: aConstructorName on: aStream aStream nextPutAll: '@', aConstructorName. anObject jsonWriteOn: aStream. ! ----- Method: Json class>>renderInstanceVariables:of:on: (in category 'rendering') ----- renderInstanceVariables: aCollection of: anObject on: aStream | map | map := Dictionary new. aCollection do: [:ivarName | map at: ivarName put: (anObject instVarNamed: ivarName)]. self render: map withConstructor: anObject class name asString on: aStream! ----- Method: Json>>arrayBufferStream (in category 'private') ----- arrayBufferStream ^arrayBufferStream ifNil: [ arrayBufferStream := (Array new: 10) writeStream ]! ----- Method: Json>>consume: (in category 'private') ----- consume: aString | message | 1 to: aString size do: [ :index | (currentCharacter := stream next) == (aString at: index) ifFalse: [ message := 'Expected ', (aString at: index) printString. currentCharacter ifNil: [ self incomplete: message ] ifNotNil: [ self invalid: message ] ] ]. currentCharacter := stream next! ----- Method: Json>>ctorMap (in category 'accessing') ----- ctorMap ^ ctorMap! ----- Method: Json>>ctorMap: (in category 'accessing') ----- ctorMap: m ctorMap := m! ----- Method: Json>>dictionaryClass: (in category 'accessing') ----- dictionaryClass: aClass "This method allows you to override the default dictionary class to be created while parsing a stream. Instances of aClass are expected to understand #at:put:." dictionaryClass := aClass! ----- Method: Json>>error: (in category 'error handling') ----- error: aString (currentCharacter isNil and: [ stream atEnd ]) ifTrue: [ self incomplete: aString ] ifFalse: [ self invalid: aString ]! ----- Method: Json>>incomplete: (in category 'error handling') ----- incomplete: aString JsonIncompleteError signal: aString! ----- Method: Json>>initialize (in category 'initialize-release') ----- initialize dictionaryClass := JsonObject ! ----- Method: Json>>interpretStringEscape: (in category 'private') ----- interpretStringEscape: aCharacter aCharacter == $b ifTrue: [ ^Character backspace ]. aCharacter == $n ifTrue: [ ^Character lf ]. aCharacter == $f ifTrue: [ ^Character newPage ]. aCharacter == $r ifTrue: [ ^Character cr ]. aCharacter == $t ifTrue: [ ^Character tab ]. aCharacter == $u ifTrue: [ ^self unescapeUnicode ]. aCharacter == $" ifTrue: [ ^aCharacter ]. aCharacter == $\ ifTrue: [ ^aCharacter ]. aCharacter == $/ ifTrue: [ ^aCharacter ]. self error: 'Unexpected escaped character: ', aCharacter asString ! ----- Method: Json>>invalid: (in category 'error handling') ----- invalid: aString JsonInvalidError signal: aString! ----- Method: Json>>readAny (in category 'private') ----- readAny self skipWhitespace. currentCharacter == ${ ifTrue: [ ^self readDictionary ]. currentCharacter == $[ ifTrue: [ ^self readArray ]. currentCharacter == $" ifTrue: [ ^self readString ]. currentCharacter == $t ifTrue: [ self consume: 'rue'. ^true ]. currentCharacter == $f ifTrue: [ self consume: 'alse'. ^false ]. currentCharacter == $n ifTrue: [ self consume: 'ull'. ^nil ]. currentCharacter == $- ifTrue: [ ^self readNumber: true ]. (#($0 $1 $2 $3 $4 $5 $6 $7 $8 $9) instVarsInclude: currentCharacter) ifTrue: [ ^self readNumber: false ]. currentCharacter == $@ ifTrue: [ ^self readConstructor ]. self invalid: 'Unknown Json input'! ----- Method: Json>>readArray (in category 'private') ----- readArray | initialPosition | currentCharacter := stream next. self skipWhitespace. currentCharacter == $] ifTrue: [ currentCharacter := stream next. ^#() ]. initialPosition := self arrayBufferStream position. [ arrayBufferStream nextPut: self readAny. self skipWhitespace. currentCharacter == $] ifTrue: [ | result | result := arrayBufferStream originalContents copyFrom: initialPosition + 1 to: arrayBufferStream position. arrayBufferStream position: initialPosition. currentCharacter := stream next. ^result ]. currentCharacter == $, ifFalse: [ self error: 'Unexpected character: ', currentCharacter asString ]. currentCharacter := stream next ] repeat. ! ----- Method: Json>>readConstructor (in category 'private') ----- readConstructor ctorMap ifNil: [ ^self error: 'No constructors were declared.' ]. self resetStringBufferStream. [ (currentCharacter := stream next) == $. or: [ currentCharacter isLetter ] ] whileTrue: [ stringBufferStream nextPut: currentCharacter ]. (ctorMap at: stringBufferStream contents ifAbsent: nil) ifNotNil: [ :constructor | ^constructor constructFromJson: self readAny ]. self error: 'Unknown constructor: ', stringBufferStream contents! ----- Method: Json>>readDictionary (in category 'private') ----- readDictionary | result key commaNeeded | result := dictionaryClass new. commaNeeded := false. currentCharacter := stream next. [ self skipWhitespace. currentCharacter == $} ifTrue: [ currentCharacter := stream next. ^result ]. commaNeeded ifFalse: [ commaNeeded := true ] ifTrue: [ currentCharacter == $, ifFalse: [ self error: 'Missing comma' ]. currentCharacter := stream next. self skipWhitespace ]. currentCharacter == $" ifFalse: [ self error: 'Key in dictionary must be string' ]. key := self readString. self skipWhitespace. currentCharacter == $: ifFalse: [ self error: 'Missing colon' ]. currentCharacter := stream next. result at: key put: self readAny ] repeat! ----- Method: Json>>readFrom: (in category 'parsing') ----- readFrom: aStream | result | stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next. result := self readAny. currentCharacter ifNotNil: [ stream skip: -1 ]. "Undo prereading." ^result! ----- Method: Json>>readNumber: (in category 'private') ----- readNumber: negative | result | negative ifFalse: [ stream skip: -1 ]. result := (numberParser ifNil: [ numberParser := JsonNumberParser new on: stream; failBlock: [ self error: 'Invalid number.' ]; yourself ]) nextNumber: negative. currentCharacter := stream next. ^result ! ----- Method: Json>>readString (in category 'private') ----- readString self resetStringBufferStream. [ currentCharacter := stream next ifNil: [self incomplete: 'Unexpected end of string' ]. currentCharacter == $" ifTrue: [ currentCharacter := stream next. ^stringBufferStream contents ]. currentCharacter == $\ ifTrue: [ stringBufferStream nextPut: (self interpretStringEscape: (stream next ifNil: [ self incomplete: 'Unexpected end of string' ])) ] ifFalse: [ stringBufferStream nextPut: currentCharacter ] ] repeat! ----- Method: Json>>resetStringBufferStream (in category 'private') ----- resetStringBufferStream stringBufferStream ifNil: [ stringBufferStream := ReadWriteStream on: (String new: 64) ] ifNotNil: [ stringBufferStream resetToStart ]! ----- Method: Json>>skipWhitespace (in category 'private') ----- skipWhitespace currentCharacter ifNil: [ self incomplete: 'Input stream is empty' ]. [ currentCharacter isSeparator ] whileTrue: [ currentCharacter := stream next ifNil: [ self incomplete: 'Input stream is empty' ]. ]! ----- Method: Json>>stream (in category 'accessing') ----- stream "Answer the value of stream" ^ stream! ----- Method: Json>>stream: (in category 'private') ----- stream: aStream "For testing purposes only" stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next! ----- Method: Json>>unescapeUnicode (in category 'private') ----- unescapeUnicode | code digitValue | code := 0. 1 to: 4 do: [ :index | (digitValue := (stream next ifNil: [ self incomplete: 'Unexpected end of stream' ]) digitValue) < 0 ifTrue: [ self invalid: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ code := code * 16 + digitValue ] ifFalse: [ self invalid: 'Invalid hexadecimal digit' ] ]. ^Character value: code! Object subclass: #JsonDummyTestObject instanceVariableNames: 'a b c' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonDummyTestObject class>>constructFromJson: (in category 'as yet unclassified') ----- constructFromJson: j ^ self new a: (j at: 'a'); b: (j at: 'b'); c: (j at: 'c'); yourself! ----- Method: JsonDummyTestObject>>= (in category 'as yet unclassified') ----- = other ^ other class == self class and: [ a = other a and: [ b = other b and: [ c = other c]]]! ----- Method: JsonDummyTestObject>>a (in category 'accessing') ----- a "Answer the value of a" ^ a! ----- Method: JsonDummyTestObject>>a: (in category 'accessing') ----- a: anObject "Set the value of a" a := anObject! ----- Method: JsonDummyTestObject>>b (in category 'accessing') ----- b "Answer the value of b" ^ b! ----- Method: JsonDummyTestObject>>b: (in category 'accessing') ----- b: anObject "Set the value of b" b := anObject! ----- Method: JsonDummyTestObject>>c (in category 'accessing') ----- c "Answer the value of c" ^ c! ----- Method: JsonDummyTestObject>>c: (in category 'accessing') ----- c: anObject "Set the value of c" c := anObject! ----- Method: JsonDummyTestObject>>jsonWriteOn: (in category 'as yet unclassified') ----- jsonWriteOn: s Json renderInstanceVariables: {#a. #b. #c} of: self on: s ! ----- Method: Object>>asJsonString (in category '*JSON') ----- asJsonString ^ String streamContents: [:str | self jsonWriteOn: str ]! Object subclass: #SerializedJson instanceVariableNames: 'jsonString' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: SerializedJson class>>on: (in category 'instance creation') ----- on: aString ^self new jsonString: aString; yourself! ----- Method: SerializedJson>>jsonString (in category 'accessing') ----- jsonString ^jsonString ! ----- Method: SerializedJson>>jsonString: (in category 'accessing') ----- jsonString: aString jsonString := aString ! ----- Method: SerializedJson>>jsonWriteOn: (in category 'json-writing') ----- jsonWriteOn: stream stream nextPutAll: jsonString!
1
0
0
0
The Trunk: JSON-ul.53.mcz
by commits@source.squeak.org
08 Sep '22
08 Sep '22
Christoph Thiede uploaded a new version of JSON to project The Trunk:
http://source.squeak.org/trunk/JSON-ul.53.mcz
==================== Summary ==================== Name: JSON-ul.53 Author: ul Time: 29 May 2020, 8:24:04.195764 pm UUID: 106f38be-cebe-4dbc-9ea3-b7e3dbb6d1ac Ancestors: JSON-ul.52 - improve number parser performance by separating the factors of the exponent - improved number parser precision ==================== Snapshot ==================== SystemOrganization addCategory: #JSON! ----- Method: Integer>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream ^ self printOn: aWriteStream base: 10 ! ----- Method: Association>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream self key asString jsonWriteOn: aStream. aStream nextPut: $:; space. self value jsonWriteOn: aStream.! ----- Method: False>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'false'! ----- Method: Number>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream self printOn: aWriteStream base: 10! OrderedDictionary subclass: #OrderedJsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: OrderedJsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: OrderedJsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: OrderedJsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: OrderedJsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: OrderedJsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: OrderedJsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: Time>>jsonWriteOn: (in category '*JSON') ----- jsonWriteOn: stream stream nextPut: $". self print24: true showSeconds: true on: stream. stream nextPut: $"! ----- Method: WriteStream>>jsonPrint: (in category '*json-printing') ----- jsonPrint: anObject anObject jsonWriteOn: self! ----- Method: Text>>jsonWriteOn: (in category '*json-printing') ----- jsonWriteOn: aStream self string jsonWriteOn: aStream! ----- Method: UndefinedObject>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream aWriteStream nextPutAll: 'null'! Error subclass: #JsonSyntaxError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonSyntaxError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! Class Json signals (possibly-indirect) instances of me when reading a JSON value from an input stream fails.! JsonSyntaxError subclass: #JsonIncompleteError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonIncompleteError commentStamp: 'tonyg 4/29/2016 11:37' prior: 0! I signal that reading a JSON value failed because more input is required, but that the input seen so far was not incorrect. Compare to JsonInvalidError. Be warned that reading numbers directly out of a stream can be ambiguous!! Consider reading from '1234'. Is the result intended to be 1234, or is there missing input, and the next character will be '5', making the result (possibly) 12345?! JsonSyntaxError subclass: #JsonInvalidError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonInvalidError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! I signal that reading a JSON value failed because the input didn't match the (extended)JSON grammar implemented by class Json.! ----- Method: Dictionary>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | needComma | needComma := false. aStream nextPut: ${. self associationsDo: [ :assoc | needComma ifTrue: [ aStream nextPut: $, ] ifFalse: [ needComma := true ]. assoc key jsonWriteOn: aStream. aStream nextPut: $:. assoc value jsonWriteOn: aStream ]. aStream nextPut: $}.! Dictionary subclass: #JsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: JsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: JsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: JsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: JsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: JsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: True>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'true'! NumberParser subclass: #JsonNumberParser instanceVariableNames: 'fraction leadingZeroesAllowed' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonNumberParser>>allowPlusSign (in category 'accessing') ----- allowPlusSign ^false! ----- Method: JsonNumberParser>>allowPlusSignInExponent (in category 'accessing') ----- allowPlusSignInExponent ^true! ----- Method: JsonNumberParser>>error: (in category 'error') ----- error: aString JsonSyntaxError signal: aString! ----- Method: JsonNumberParser>>exponentLetters (in category 'accessing') ----- exponentLetters ^'eE'! ----- Method: JsonNumberParser>>fiveRaisedTo: (in category 'parsing-private') ----- fiveRaisedTo: anInteger anInteger >= 0 ifFalse: [ ^5 raisedToInteger: anInteger ]. anInteger >= 28 ifTrue: [ ^5 raisedToInteger: anInteger ]. ^#(1 5 25 125 625 3125 15625 78125 390625 1953125 9765625 48828125 244140625 1220703125 6103515625 30517578125 152587890625 762939453125 3814697265625 19073486328125 95367431640625 476837158203125 2384185791015625 11920928955078125 59604644775390625 298023223876953125 1490116119384765625 7450580596923828125) at: anInteger + 1! ----- Method: JsonNumberParser>>initialize (in category 'initialize-release') ----- initialize super initialize. base := 10. fraction := Fraction numerator: 0 denominator: 1. leadingZeroesAllowed := false! ----- Method: JsonNumberParser>>makeFloatFromMantissa:exponent: (in category 'parsing-private') ----- makeFloatFromMantissa: mantissa exponent: exponent exponent = 0 ifTrue: [ ^mantissa asFloat ]. exponent >= 16 ifTrue: [ exponent > 324 ifTrue: [ "(Float emax + 53 * 2 ln / 10 ln) ceiling => 324" mantissa isZero ifTrue: [ ^0.0 ]. mantissa positive ifTrue: [ ^Float infinity ]. ^Float negativeInfinity ]. ^(mantissa * (self fiveRaisedTo: exponent)) asFloat timesTwoPower: exponent ]. (exponent >= -15 and: [ mantissa highBitOfMagnitude <= 53 ]) ifTrue: [ exponent >= 1 ifTrue: [ ^mantissa asFloat * (self fiveRaisedTo: exponent) asFloat timesTwoPower: exponent ]. ^mantissa asFloat / (self fiveRaisedTo: exponent negated) asFloat timesTwoPower: exponent ]. exponent >= 1 ifTrue: [ ^(mantissa * (self fiveRaisedTo: exponent)) asFloat timesTwoPower: exponent ]. exponent < -324 ifTrue: [ "-1 * (53 - Float emin * 2 ln / 10 ln) ceiling => -324" mantissa positive ifTrue: [ ^0.0 ]. ^-0.0 ]. ^(fraction setNumerator: mantissa denominator: (self fiveRaisedTo: exponent negated)) reduced asFloat timesTwoPower: exponent ! ----- Method: JsonNumberParser>>nextElementaryLargeInteger (in category 'parsing-large int') ----- nextElementaryLargeInteger "Form an unsigned integer with incoming digits from sourceStream. Return this integer, or zero if no digits found. Stop reading if end of digits or if a LargeInteger is formed. Count the number of digits and the position of lastNonZero digit and store them in instVar." | digit value | value := 0. nDigits := 0. lastNonZero := 0. [ value isLarge ifTrue: [ ^value ]. ((digit := (sourceStream next ifNil: [ ^value ]) asInteger - 48 "$0 asInteger") < 0 or: [ digit >= 10 ]) ifTrue: [ sourceStream skip: -1. ^value ]. nDigits := nDigits + 1. digit = 0 ifFalse: [ (lastNonZero = 0 and: [ nDigits > 1 and: [ leadingZeroesAllowed not ] ]) ifTrue: [ self error: 'Leading zeroes are not allowed.' ]. lastNonZero := nDigits. value := value * 10 + digit ] ifTrue: [ value := value * 10 ] ] repeat! ----- Method: JsonNumberParser>>nextNumber: (in category 'parsing-public') ----- nextNumber: negative | numberOfTrailingZeroInIntegerPart oldLeadingZeroesAllowed numberOfTrailingZeroInFractionPart numberOfNonZeroFractionDigits mantissa value | integerPart := self nextUnsignedIntegerOrNil ifNil: [ ^self error: 'Missing integer part!!' ]. numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. (sourceStream peekFor: $.) ifFalse: [ "No fraction part" negative ifTrue: [ integerPart := integerPart negated ]. self readExponent ifFalse: [ ^integerPart ]. exponent > 0 ifTrue: [ ^integerPart * (self fiveRaisedTo: exponent) bitShift: exponent ]. ^(Fraction numerator: integerPart denominator: (self fiveRaisedTo: exponent negated)) reduced asFloat timesTwoPower: exponent negated ]. oldLeadingZeroesAllowed := leadingZeroesAllowed. leadingZeroesAllowed := true. fractionPart := self nextUnsignedIntegerOrNil. leadingZeroesAllowed := oldLeadingZeroesAllowed. fractionPart ifNil: [ self error: 'Missing fraction part!!' ]. fractionPart isZero ifTrue: [ self readExponent ifFalse: [ negative ifTrue: [ integerPart isZero ifTrue: [ ^Float negativeZero ]. ^integerPart negated ]. ^integerPart ]. exponent >= 0 ifTrue: [ negative ifTrue: [ integerPart := integerPart negated ]. ^integerPart * (self fiveRaisedTo: exponent) bitShift: exponent ]. value := self makeFloatFromMantissa: integerPart exponent: exponent. negative ifTrue: [ ^value negated ]. ^value ]. numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. numberOfNonZeroFractionDigits := lastNonZero. self readExponent. exponent := exponent - numberOfNonZeroFractionDigits. mantissa := (integerPart * (self fiveRaisedTo: numberOfNonZeroFractionDigits) bitShift: numberOfNonZeroFractionDigits) + (fractionPart // (self fiveRaisedTo: numberOfTrailingZeroInFractionPart) bitShift: 0 - numberOfTrailingZeroInFractionPart). value := self makeFloatFromMantissa: mantissa exponent: exponent. negative ifTrue: [ ^value negated ]. ^value! ----- Method: JsonNumberParser>>nextUnsignedIntegerOrNil (in category 'parsing-public') ----- nextUnsignedIntegerOrNil "Form an unsigned integer with incoming digits from sourceStream. Answer this integer, or nil if no digit found. Count the number of digits and the position of lastNonZero digit and store them in instVar" | nPackets high nDigitsHigh lastNonZeroHigh low | "read no more digits than one elementary LargeInteger" high := self nextElementaryLargeInteger. nDigits = 0 ifTrue: [^nil]. "Not enough digits to form a LargeInteger, stop iteration" high isLarge ifFalse: [^high]. "We now have to engage arithmetic with LargeInteger Decompose the integer in a high and low packets of growing size:" nPackets := 1. nDigitsHigh := nDigits. lastNonZeroHigh := lastNonZero. [ low := self nextLargeIntegerBase: 10 nPackets: nPackets . high := (high * (self fiveRaisedTo: nDigits) bitShift: nDigits) + low. lastNonZero = 0 ifFalse: [lastNonZeroHigh := lastNonZero + nDigitsHigh]. nDigitsHigh := nDigitsHigh + nDigits. low isLarge] whileTrue: [nPackets := nPackets * 2]. nDigits := nDigitsHigh. lastNonZero := lastNonZeroHigh. ^high! ----- Method: ScaledDecimal>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: stream self printOn: stream showingDecimalPlaces: scale! ----- Method: Collection>>jsonWriteOn: (in category '*json') ----- jsonWriteOn: aStream "By default, use array braces " aStream nextPut: $[. self do: [:each | each jsonWriteOn: aStream ] separatedBy: [ aStream nextPut: $, ]. aStream nextPut: $]! TestCase subclass: #JsonTests instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonTests commentStamp: '<historical>' prior: 0! I provide a number of test cases for class Json.! ----- Method: JsonTests>>assertIncompleteJson: (in category 'helpers') ----- assertIncompleteJson: aString self should: [self readFrom: aString] raise: JsonIncompleteError! ----- Method: JsonTests>>assertInvalidJson: (in category 'helpers') ----- assertInvalidJson: aString self should: [self readFrom: aString] raise: JsonInvalidError! ----- Method: JsonTests>>json:equals: (in category 'helpers') ----- json: aString equals: aValue | readValue | readValue := self readFrom: aString. self assert: readValue = aValue.! ----- Method: JsonTests>>readFrom: (in category 'helpers') ----- readFrom: aString ^ (Json newWithConstructors: {JsonDummyTestObject.}) readFrom: aString readStream ! ----- Method: JsonTests>>render:equals: (in category 'helpers') ----- render: anObject equals: aString self assert: (Json render: anObject) = aString! ----- Method: JsonTests>>simpleDummyObject (in category 'helpers') ----- simpleDummyObject ^ JsonDummyTestObject new a: 1; b: 2; c: 3; yourself! ----- Method: JsonTests>>testArray (in category 'tests') ----- testArray self json: '[]' equals: #(). self json: '[[]]' equals: #(#()). self json: '[[], []]' equals: #(#() #()). self json: '["hi", "there"]' equals: #('hi' 'there'). self json: '[["a", "b", null]]' equals: #(('a' 'b' nil)).! ----- Method: JsonTests>>testAtomFalse (in category 'tests') ----- testAtomFalse self json: 'false' equals: false. self json: ' false' equals: false. self json: 'false ' equals: false. self json: ' false ' equals: false. ! ----- Method: JsonTests>>testAtomNull (in category 'tests') ----- testAtomNull self json: 'null' equals: nil. self json: ' null' equals: nil. self json: 'null ' equals: nil. self json: ' null ' equals: nil. ! ----- Method: JsonTests>>testAtomNumber (in category 'tests') ----- testAtomNumber self json: '1' equals: 1. self json: '123' equals: 123. self should: [ self json: '0123' equals: 123 ] raise: JsonSyntaxError. "No leading zeroes." self json: '1.23e2' equals: 123. self json: '-1' equals: -1. self json: '-0' equals: 0. self json: '[-1.2]' equals: #(-1.2).! ----- Method: JsonTests>>testAtomString (in category 'tests') ----- testAtomString self json: '"hi"' equals: 'hi'. self json: '"\""' equals: '"'. self json: '"\\"' equals: '\'. self json: '""' equals: ''. self json: '"a\u0004b"' equals: ({$a. Character value: 4. $b} as: String). self json: '"a\nb"' equals: ({$a. Character lf. $b} as: String).! ----- Method: JsonTests>>testAtomTrue (in category 'tests') ----- testAtomTrue self json: 'true' equals: true. self json: ' true' equals: true. self json: 'true ' equals: true. self json: ' true ' equals: true. ! ----- Method: JsonTests>>testCtor (in category 'tests') ----- testCtor self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}' equals: self simpleDummyObject. self json: (Json render: self simpleDummyObject) equals: self simpleDummyObject.! ----- Method: JsonTests>>testDictionary (in category 'tests') ----- testDictionary self json: '{}' equals: (JsonObject new). self json: '{"a": "a"}' equals: (JsonObject new at: 'a' put: 'a'; yourself). self json: '{"a": [[]]}' equals: (JsonObject new at: 'a' put: #(#()); yourself). self json: '{"a":"b", "b":"a"}' equals: (JsonObject new add: 'a'->'b'; add: 'b'->'a';yourself).! ----- Method: JsonTests>>testDictionaryClass (in category 'tests') ----- testDictionaryClass | parser | parser := Json new. self assert: (parser readFrom: '{}' readStream) class == JsonObject. parser dictionaryClass: OrderedJsonObject. self assert: (parser readFrom: '{}' readStream) class == OrderedJsonObject. self assert: (parser readFrom: '{"x":{"y":{}}}' readStream) x y class == OrderedJsonObject! ----- Method: JsonTests>>testIncomplete (in category 'tests') ----- testIncomplete self assertIncompleteJson: ''. self assertIncompleteJson: ' '. self assertIncompleteJson: ' tr'. self assertIncompleteJson: 'tru'. self assertIncompleteJson: '['. self assertIncompleteJson: '[true'. self assertIncompleteJson: '[true,'. self assertIncompleteJson: '{'. self assertIncompleteJson: '{"hi"'. self assertIncompleteJson: '{"hi":'. self assertIncompleteJson: '{"hi":true'. self assertIncompleteJson: '{"hi":true,'. self assertIncompleteJson: '{"hi":true,"'. self assertIncompleteJson: '"hello'. self assertIncompleteJson: '"hello '. self assertIncompleteJson: '"hello\'. self assertIncompleteJson: '"\u26'.! ----- Method: JsonTests>>testInvalid (in category 'tests') ----- testInvalid self assertInvalidJson: 'x'. self assertInvalidJson: ' x'. self assertInvalidJson: ' trx'. self assertInvalidJson: 'trux'. self assertInvalidJson: '.'. self assertInvalidJson: ':'. self assertInvalidJson: ','. self assertInvalidJson: ']'. self assertInvalidJson: '}'. self assertInvalidJson: '[x'. self assertInvalidJson: '[true t'. self assertInvalidJson: '[true,]'. self assertInvalidJson: '{]'. self assertInvalidJson: '{,'. self assertInvalidJson: '{"hi",'. self assertInvalidJson: '{"hi":x'. self assertInvalidJson: '{"hi":,'. self assertInvalidJson: '{"hi":true "'. self assertInvalidJson: '{"hi":true,}'. self assertInvalidJson: '\u263A'.! ----- Method: JsonTests>>testInvalidUnicodeEscapes (in category 'tests') ----- testInvalidUnicodeEscapes self assertInvalidJson: '"\u26"'. "Note that naively reading four chars runs off the end here, so we might see JsonIncompleteError, which would be wrong." self assertInvalidJson: '"\u2Z"'. self assertInvalidJson: '"\u2Z44"'.! ----- Method: JsonTests>>testJsonObjectAtPutReturnsTheObject (in category 'tests') ----- testJsonObjectAtPutReturnsTheObject { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self assert: (json at: 'foo' put: 1) == json. self assert: (json foo: 1) == json ]! ----- Method: JsonTests>>testMissingCtor (in category 'tests') ----- testMissingCtor self assertInvalidJson: '@Missing[]'! ----- Method: JsonTests>>testMissingCtorNoMap (in category 'tests') ----- testMissingCtorNoMap self should: [Json new readFrom: '@Missing[]' readStream] raise: JsonInvalidError! ----- Method: JsonTests>>testNonStringKeysRaiseError (in category 'tests') ----- testNonStringKeysRaiseError { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self should: [ json at: 1 put: 1 ] raise: Error. self assert: json isEmpty. json at: '1' put: 1. self assert: 1 equals: (json at: '1') ]! ----- Method: JsonTests>>testStreaming (in category 'tests') ----- testStreaming | j | j := Json new stream: 'truefalsetrue[]{}1.234 5.678"A""B"nullnull' readStream. self assert: j readAny equals: true. self assert: j readAny equals: false. self assert: j readAny equals: true. self assert: j readAny equals: #(). self assert: j readAny equals: JsonObject new. self assert: j readAny equals: 1.234. self assert: j readAny equals: 5.678. self assert: j readAny equals: 'A'. self assert: j readAny equals: 'B'. self assert: j readAny equals: nil. self assert: j readAny equals: nil.! ----- Method: JsonTests>>testStringWithUnicode (in category 'tests') ----- testStringWithUnicode | unicodeString | unicodeString := (Unicode value: 16r263A) asString. self json: '"\u263A"' equals:unicodeString. self json: '"\u263a"' equals:unicodeString. self json: '"', unicodeString, '"' equals: unicodeString. self render: unicodeString equals: '"', unicodeString, '"'.! ----- Method: JsonTests>>testWriteAssociation (in category 'tests') ----- testWriteAssociation self render: 'key' -> 'value' equals: '"key": "value"'. self render: 'key' -> 2 equals: '"key": 2'. "keys should be strings" self render: 42 -> 2 equals: '"42": 2'. "try to do _something_ for more complex keys" self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'. ! ----- Method: JsonTests>>testWriteString (in category 'tests') ----- testWriteString self render: '"' equals: '"\""'. self render: '\' equals: '"\\"'. self render: 'hi' equals: '"hi"'. self render: ({$a. Character lf. $b} as: String) equals: '"a\nb"'. self render: ({$a. Character value: 4. $b} as: String) equals: '"a\u0004b"'.! ----- Method: String>>asSerializedJson (in category '*JSON') ----- asSerializedJson " Assume that the receiver is a valid serialized json string. Return an object that understands #jsonWriteOn: and will write this string on its argument. " ^SerializedJson on: self! ----- Method: String>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | index start | aStream nextPut: $". start := 1. [ (index := self indexOfAnyOf: Json escapeSet startingAt: start) = 0 ] whileFalse: [ aStream next: index - start putAll: self startingAt: start. aStream nextPutAll: (Json escapeForCharacter: (self at: index)). start := index + 1 ]. start <= self size ifTrue: [ aStream next: self size + 1 - start putAll: self startingAt: start ]. aStream nextPut: $". ! ----- Method: String>>parseAsJson (in category '*JSON') ----- parseAsJson "Convenience" ^Json readFrom: self readStream! ----- Method: String>>parseAsOrderedJson (in category '*JSON') ----- parseAsOrderedJson "Convenience" ^Json new dictionaryClass: OrderedJsonObject; readFrom: self readStream! Object subclass: #Json instanceVariableNames: 'stream currentCharacter arrayBufferStream stringBufferStream numberParser ctorMap dictionaryClass' classVariableNames: '' poolDictionaries: '' category: 'JSON'! Json class instanceVariableNames: 'escapeArray escapeSet'! !Json commentStamp: 'ul 3/29/2016 04:38' prior: 0! I support reading and writing JSON (see
http://json.org
) formatted data - strings, numbers, boolean, nil, arrays and dictionaries. The implementation is mainly based on RFC 7159 (
https://www.ietf.org/rfc/rfc7159.txt
). It has been extended with syntax for invoking a prearranged list of constructors on read objects. Character encoding is not handled here. That's something you have to do before reading or after writing. Mixed processing is possible, but it's entirely your responsibility to keep things correct. Instance Variables stream: <PositionableStream> currentCharacter: <Character> arrayBufferStream: <WriteStream> stringBufferStream: <ReadWriteStream> numberParser: <ExtendedNumberParser> ctorMap: <Dictionary> For parsing, stream has to be initialized by sending #readFrom: to my instance. The parser will pre-read one character, which will be stored in currentCharacter, so the stream is expected to support position manipulation, hence it should be a PositionableStream. arrayBufferStream is WriteStream on an Array, which holds the values during array parsing (see #readArray). When there's a recursive call, this stream is used as a stack to separate the elements parsed in the previous invocation from the current one. stringBufferStream is a ReadWriteStream on a String, which holds the currently parsed String (see #readString), the currently parsed number (see #readNumber) or the name of the currently parsed constructor (see #readConstructor). Recursion has no effect on this. numberParser is a cached instance of ExtendedNumberParser. It's initialized on stringBufferStream, so it can quickly parse the number stored in it. ctorMap is the Dictionary holding the constructor classes by name. arrayBufferStream, stringBufferStream and numberParser are initialized lazily, and should not be accessed externally. Since these are shared objects, along with stream and currentCharacter, holding state, therefore a single instance of me shouldn't be used to parse multiple streams at the same time. For writing, you can use the methods in the class-side rendering category. To convert individual objects to a string, you can use #asJsonString, to serialize it on a stream, you can use #jsonWriteOn:. ! Json class instanceVariableNames: 'escapeArray escapeSet'! ----- Method: Json class>>escapeForCharacter: (in category 'accessing') ----- escapeForCharacter: c | asciiValue | (asciiValue := c asciiValue) < 128 ifTrue: [ ^escapeArray at: asciiValue + 1 ]. ^nil! ----- Method: Json class>>escapeSet (in category 'accessing') ----- escapeSet ^escapeSet! ----- Method: Json class>>initialize (in category 'class initialization') ----- initialize "Json initialize." | newEscapeArray newEscapeSet | newEscapeArray := Array new: 128. newEscapeSet := CharacterSet new. (0 to: 31), #(127) do: [ :each | newEscapeArray at: each + 1 put: '\u', (each printStringHex padded: #left to: 4 with: $0). newEscapeSet add: (Character value: each) ]. { $" -> '\"'. $\ -> '\\'. $/ -> '\/'. "A hack, so the generated JSON will always be HTML+Javascript compatible, because the generated JSON will not contain the seqence </script>." Character backspace -> '\b'. Character lf -> '\n'. Character newPage -> '\f'. Character cr -> '\r'. Character tab -> '\t'. } do: [ :each | newEscapeArray at: each key asciiValue + 1 put: each value. newEscapeSet add: each key ]. escapeArray := newEscapeArray. escapeSet := newEscapeSet! ----- Method: Json class>>mimeType (in category 'accessing') ----- mimeType ^ 'application/x-json'! ----- Method: Json class>>newWithConstructors: (in category 'instance creation') ----- newWithConstructors: aCollection | m | m := Dictionary new. aCollection do: [:each | (each isKindOf: Association) ifTrue: [m add: each] ifFalse: [m at: each name asString put: each]]. ^ self new ctorMap: m; yourself.! ----- Method: Json class>>readFrom: (in category 'instance creation') ----- readFrom: aStream ^ self new readFrom: aStream.! ----- Method: Json class>>render: (in category 'rendering') ----- render: anObject ^String streamContents: [ :stream | anObject jsonWriteOn: stream ]! ----- Method: Json class>>render:withConstructor:on: (in category 'rendering') ----- render: anObject withConstructor: aConstructorName on: aStream aStream nextPutAll: '@', aConstructorName. anObject jsonWriteOn: aStream. ! ----- Method: Json class>>renderInstanceVariables:of:on: (in category 'rendering') ----- renderInstanceVariables: aCollection of: anObject on: aStream | map | map := Dictionary new. aCollection do: [:ivarName | map at: ivarName put: (anObject instVarNamed: ivarName)]. self render: map withConstructor: anObject class name asString on: aStream! ----- Method: Json>>arrayBufferStream (in category 'private') ----- arrayBufferStream ^arrayBufferStream ifNil: [ arrayBufferStream := (Array new: 10) writeStream ]! ----- Method: Json>>consume: (in category 'private') ----- consume: aString | message | 1 to: aString size do: [ :index | (currentCharacter := stream next) == (aString at: index) ifFalse: [ message := 'Expected ', (aString at: index) printString. currentCharacter ifNil: [ self incomplete: message ] ifNotNil: [ self invalid: message ] ] ]. currentCharacter := stream next! ----- Method: Json>>ctorMap (in category 'accessing') ----- ctorMap ^ ctorMap! ----- Method: Json>>ctorMap: (in category 'accessing') ----- ctorMap: m ctorMap := m! ----- Method: Json>>dictionaryClass: (in category 'accessing') ----- dictionaryClass: aClass "This method allows you to override the default dictionary class to be created while parsing a stream. Instances of aClass are expected to understand #at:put:." dictionaryClass := aClass! ----- Method: Json>>error: (in category 'error handling') ----- error: aString (currentCharacter isNil and: [ stream atEnd ]) ifTrue: [ self incomplete: aString ] ifFalse: [ self invalid: aString ]! ----- Method: Json>>incomplete: (in category 'error handling') ----- incomplete: aString JsonIncompleteError signal: aString! ----- Method: Json>>initialize (in category 'initialize-release') ----- initialize dictionaryClass := JsonObject ! ----- Method: Json>>interpretStringEscape: (in category 'private') ----- interpretStringEscape: aCharacter aCharacter == $b ifTrue: [ ^Character backspace ]. aCharacter == $n ifTrue: [ ^Character lf ]. aCharacter == $f ifTrue: [ ^Character newPage ]. aCharacter == $r ifTrue: [ ^Character cr ]. aCharacter == $t ifTrue: [ ^Character tab ]. aCharacter == $u ifTrue: [ ^self unescapeUnicode ]. aCharacter == $" ifTrue: [ ^aCharacter ]. aCharacter == $\ ifTrue: [ ^aCharacter ]. aCharacter == $/ ifTrue: [ ^aCharacter ]. self error: 'Unexpected escaped character: ', aCharacter asString ! ----- Method: Json>>invalid: (in category 'error handling') ----- invalid: aString JsonInvalidError signal: aString! ----- Method: Json>>readAny (in category 'private') ----- readAny self skipWhitespace. currentCharacter == ${ ifTrue: [ ^self readDictionary ]. currentCharacter == $[ ifTrue: [ ^self readArray ]. currentCharacter == $" ifTrue: [ ^self readString ]. currentCharacter == $t ifTrue: [ self consume: 'rue'. ^true ]. currentCharacter == $f ifTrue: [ self consume: 'alse'. ^false ]. currentCharacter == $n ifTrue: [ self consume: 'ull'. ^nil ]. currentCharacter == $- ifTrue: [ ^self readNumber: true ]. (#($0 $1 $2 $3 $4 $5 $6 $7 $8 $9) instVarsInclude: currentCharacter) ifTrue: [ ^self readNumber: false ]. currentCharacter == $@ ifTrue: [ ^self readConstructor ]. self invalid: 'Unknown Json input'! ----- Method: Json>>readArray (in category 'private') ----- readArray | initialPosition | currentCharacter := stream next. self skipWhitespace. currentCharacter == $] ifTrue: [ currentCharacter := stream next. ^#() ]. initialPosition := self arrayBufferStream position. [ arrayBufferStream nextPut: self readAny. self skipWhitespace. currentCharacter == $] ifTrue: [ | result | result := arrayBufferStream originalContents copyFrom: initialPosition + 1 to: arrayBufferStream position. arrayBufferStream position: initialPosition. currentCharacter := stream next. ^result ]. currentCharacter == $, ifFalse: [ self error: 'Unexpected character: ', currentCharacter asString ]. currentCharacter := stream next ] repeat. ! ----- Method: Json>>readConstructor (in category 'private') ----- readConstructor ctorMap ifNil: [ ^self error: 'No constructors were declared.' ]. self resetStringBufferStream. [ (currentCharacter := stream next) == $. or: [ currentCharacter isLetter ] ] whileTrue: [ stringBufferStream nextPut: currentCharacter ]. (ctorMap at: stringBufferStream contents ifAbsent: nil) ifNotNil: [ :constructor | ^constructor constructFromJson: self readAny ]. self error: 'Unknown constructor: ', stringBufferStream contents! ----- Method: Json>>readDictionary (in category 'private') ----- readDictionary | result key commaNeeded | result := dictionaryClass new. commaNeeded := false. currentCharacter := stream next. [ self skipWhitespace. currentCharacter == $} ifTrue: [ currentCharacter := stream next. ^result ]. commaNeeded ifFalse: [ commaNeeded := true ] ifTrue: [ currentCharacter == $, ifFalse: [ self error: 'Missing comma' ]. currentCharacter := stream next. self skipWhitespace ]. currentCharacter == $" ifFalse: [ self error: 'Key in dictionary must be string' ]. key := self readString. self skipWhitespace. currentCharacter == $: ifFalse: [ self error: 'Missing colon' ]. currentCharacter := stream next. result at: key put: self readAny ] repeat! ----- Method: Json>>readFrom: (in category 'parsing') ----- readFrom: aStream | result | stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next. result := self readAny. currentCharacter ifNotNil: [ stream skip: -1 ]. "Undo prereading." ^result! ----- Method: Json>>readNumber: (in category 'private') ----- readNumber: negative | result | negative ifFalse: [ stream skip: -1 ]. result := (numberParser ifNil: [ numberParser := JsonNumberParser new on: stream; failBlock: [ self error: 'Invalid number.' ]; yourself ]) nextNumber: negative. currentCharacter := stream next. ^result ! ----- Method: Json>>readString (in category 'private') ----- readString self resetStringBufferStream. [ currentCharacter := stream next ifNil: [self incomplete: 'Unexpected end of string' ]. currentCharacter == $" ifTrue: [ currentCharacter := stream next. ^stringBufferStream contents ]. currentCharacter == $\ ifTrue: [ stringBufferStream nextPut: (self interpretStringEscape: (stream next ifNil: [ self incomplete: 'Unexpected end of string' ])) ] ifFalse: [ stringBufferStream nextPut: currentCharacter ] ] repeat! ----- Method: Json>>resetStringBufferStream (in category 'private') ----- resetStringBufferStream stringBufferStream ifNil: [ stringBufferStream := ReadWriteStream on: (String new: 64) ] ifNotNil: [ stringBufferStream resetToStart ]! ----- Method: Json>>skipWhitespace (in category 'private') ----- skipWhitespace currentCharacter ifNil: [ self incomplete: 'Input stream is empty' ]. [ currentCharacter isSeparator ] whileTrue: [ currentCharacter := stream next ifNil: [ self incomplete: 'Input stream is empty' ]. ]! ----- Method: Json>>stream (in category 'accessing') ----- stream "Answer the value of stream" ^ stream! ----- Method: Json>>stream: (in category 'private') ----- stream: aStream "For testing purposes only" stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next! ----- Method: Json>>unescapeUnicode (in category 'private') ----- unescapeUnicode | code digitValue | code := 0. 1 to: 4 do: [ :index | (digitValue := (stream next ifNil: [ self incomplete: 'Unexpected end of stream' ]) digitValue) < 0 ifTrue: [ self invalid: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ code := code * 16 + digitValue ] ifFalse: [ self invalid: 'Invalid hexadecimal digit' ] ]. ^Character value: code! Object subclass: #JsonDummyTestObject instanceVariableNames: 'a b c' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonDummyTestObject class>>constructFromJson: (in category 'as yet unclassified') ----- constructFromJson: j ^ self new a: (j at: 'a'); b: (j at: 'b'); c: (j at: 'c'); yourself! ----- Method: JsonDummyTestObject>>= (in category 'as yet unclassified') ----- = other ^ other class == self class and: [ a = other a and: [ b = other b and: [ c = other c]]]! ----- Method: JsonDummyTestObject>>a (in category 'accessing') ----- a "Answer the value of a" ^ a! ----- Method: JsonDummyTestObject>>a: (in category 'accessing') ----- a: anObject "Set the value of a" a := anObject! ----- Method: JsonDummyTestObject>>b (in category 'accessing') ----- b "Answer the value of b" ^ b! ----- Method: JsonDummyTestObject>>b: (in category 'accessing') ----- b: anObject "Set the value of b" b := anObject! ----- Method: JsonDummyTestObject>>c (in category 'accessing') ----- c "Answer the value of c" ^ c! ----- Method: JsonDummyTestObject>>c: (in category 'accessing') ----- c: anObject "Set the value of c" c := anObject! ----- Method: JsonDummyTestObject>>jsonWriteOn: (in category 'as yet unclassified') ----- jsonWriteOn: s Json renderInstanceVariables: {#a. #b. #c} of: self on: s ! ----- Method: Object>>asJsonString (in category '*JSON') ----- asJsonString ^ String streamContents: [:str | self jsonWriteOn: str ]! Object subclass: #SerializedJson instanceVariableNames: 'jsonString' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: SerializedJson class>>on: (in category 'instance creation') ----- on: aString ^self new jsonString: aString; yourself! ----- Method: SerializedJson>>jsonString (in category 'accessing') ----- jsonString ^jsonString ! ----- Method: SerializedJson>>jsonString: (in category 'accessing') ----- jsonString: aString jsonString := aString ! ----- Method: SerializedJson>>jsonWriteOn: (in category 'json-writing') ----- jsonWriteOn: stream stream nextPutAll: jsonString!
1
0
0
0
The Trunk: JSON-ul.54.mcz
by commits@source.squeak.org
08 Sep '22
08 Sep '22
Christoph Thiede uploaded a new version of JSON to project The Trunk:
http://source.squeak.org/trunk/JSON-ul.54.mcz
==================== Summary ==================== Name: JSON-ul.54 Author: ul Time: 30 May 2020, 4:52:22.332812 am UUID: 76e4e8be-cb72-4d09-9a5d-c27622b213c6 Ancestors: JSON-ul.53 - further improved number parser performance and fixed precision in less common cases ==================== Snapshot ==================== SystemOrganization addCategory: #JSON! ----- Method: Integer>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream ^ self printOn: aWriteStream base: 10 ! ----- Method: Association>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream self key asString jsonWriteOn: aStream. aStream nextPut: $:; space. self value jsonWriteOn: aStream.! ----- Method: False>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'false'! ----- Method: Number>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream self printOn: aWriteStream base: 10! OrderedDictionary subclass: #OrderedJsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: OrderedJsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: OrderedJsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: OrderedJsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: OrderedJsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: OrderedJsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: OrderedJsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: Time>>jsonWriteOn: (in category '*JSON') ----- jsonWriteOn: stream stream nextPut: $". self print24: true showSeconds: true on: stream. stream nextPut: $"! ----- Method: WriteStream>>jsonPrint: (in category '*json-printing') ----- jsonPrint: anObject anObject jsonWriteOn: self! ----- Method: Text>>jsonWriteOn: (in category '*json-printing') ----- jsonWriteOn: aStream self string jsonWriteOn: aStream! ----- Method: UndefinedObject>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream aWriteStream nextPutAll: 'null'! Error subclass: #JsonSyntaxError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonSyntaxError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! Class Json signals (possibly-indirect) instances of me when reading a JSON value from an input stream fails.! JsonSyntaxError subclass: #JsonIncompleteError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonIncompleteError commentStamp: 'tonyg 4/29/2016 11:37' prior: 0! I signal that reading a JSON value failed because more input is required, but that the input seen so far was not incorrect. Compare to JsonInvalidError. Be warned that reading numbers directly out of a stream can be ambiguous!! Consider reading from '1234'. Is the result intended to be 1234, or is there missing input, and the next character will be '5', making the result (possibly) 12345?! JsonSyntaxError subclass: #JsonInvalidError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonInvalidError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! I signal that reading a JSON value failed because the input didn't match the (extended)JSON grammar implemented by class Json.! ----- Method: Dictionary>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | needComma | needComma := false. aStream nextPut: ${. self associationsDo: [ :assoc | needComma ifTrue: [ aStream nextPut: $, ] ifFalse: [ needComma := true ]. assoc key jsonWriteOn: aStream. aStream nextPut: $:. assoc value jsonWriteOn: aStream ]. aStream nextPut: $}.! Dictionary subclass: #JsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: JsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: JsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: JsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: JsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: JsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: True>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'true'! NumberParser subclass: #JsonNumberParser instanceVariableNames: 'fraction leadingZeroesAllowed' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonNumberParser>>allowPlusSign (in category 'accessing') ----- allowPlusSign ^false! ----- Method: JsonNumberParser>>allowPlusSignInExponent (in category 'accessing') ----- allowPlusSignInExponent ^true! ----- Method: JsonNumberParser>>error: (in category 'error') ----- error: aString JsonSyntaxError signal: aString! ----- Method: JsonNumberParser>>exponentLetters (in category 'accessing') ----- exponentLetters ^'eE'! ----- Method: JsonNumberParser>>fiveRaisedTo: (in category 'parsing-private') ----- fiveRaisedTo: anInteger anInteger >= 0 ifFalse: [ ^1 / (self fiveRaisedTo: 0 - anInteger) ]. anInteger >= 28 ifTrue: [ ^5 raisedToInteger: anInteger ]. ^#(1 5 25 125 625 3125 15625 78125 390625 1953125 9765625 48828125 244140625 1220703125 6103515625 30517578125 152587890625 762939453125 3814697265625 19073486328125 95367431640625 476837158203125 2384185791015625 11920928955078125 59604644775390625 298023223876953125 1490116119384765625 7450580596923828125) at: anInteger + 1! ----- Method: JsonNumberParser>>initialize (in category 'initialize-release') ----- initialize super initialize. base := 10. fraction := Fraction numerator: 0 denominator: 1. leadingZeroesAllowed := false! ----- Method: JsonNumberParser>>makeFloatFromMantissa:exponent: (in category 'parsing-private') ----- makeFloatFromMantissa: mantissa exponent: exponent "Return a Float equal to mantissa * 10 ^ exponent." exponent = 0 ifTrue: [ ^mantissa asFloat ]. exponent > 0 ifTrue: [ exponent <= 22 "Can 5 raisedToInteger: exponent be represented exactly as a Float? ((1 << Float precision - 1) log: 5) floor => 22" ifFalse: [ exponent > 324 ifTrue: [ "(Float emax + Float precision * (2 log: 10)) ceiling => 324" mantissa isZero ifTrue: [ ^0.0 ]. mantissa positive ifTrue: [ ^Float infinity ]. ^Float negativeInfinity ]. ^(mantissa * (self fiveRaisedTo: exponent)) asFloat timesTwoPower: exponent ] ifTrue: [ mantissa highBitOfMagnitude <= Float precision ifTrue: [ "Mantissa can also be represented as an exact Float. Float >> #* should be exact." ^mantissa asFloat * (self fiveRaisedTo: exponent) asFloat timesTwoPower: exponent ] ]. ^(mantissa * (self fiveRaisedTo: exponent)) asFloat timesTwoPower: exponent ]. "exponent < 0" exponent >= -22 "Can 5 raisedToInteger: 0 - exponent be represented exactly as a Float?" ifTrue: [ mantissa highBitOfMagnitude <= Float precision ifTrue: [ "Mantissa can also be represented as an exact Float. Float >> #/ should be exact." ^mantissa asFloat / (self fiveRaisedTo: 0 - exponent) asFloat timesTwoPower: exponent ] ] ifFalse: [ exponent < -324 ifTrue: [ "-1 * (Float precision - Float emin * (2 log: 10)) ceiling => -324" mantissa positive ifTrue: [ ^0.0 ]. ^-0.0 ] ]. "No luck. Let Fraction >> #asFloat do the heavy lifting." ^(fraction setNumerator: mantissa denominator: ((self fiveRaisedTo: 0 - exponent) bitShift: 0 - exponent)) asFloat "Can't use #timesTwoPower: here. No #reduced here, because there's none in NumberParser either"! ----- Method: JsonNumberParser>>nextElementaryLargeInteger (in category 'parsing-large int') ----- nextElementaryLargeInteger "Form an unsigned integer with incoming digits from sourceStream. Return this integer, or zero if no digits found. Stop reading if end of digits or if a LargeInteger is formed. Count the number of digits and the position of lastNonZero digit and store them in instVar." | digit value | value := 0. nDigits := 0. lastNonZero := 0. [ value isLarge ifTrue: [ ^value ]. ((digit := (sourceStream next ifNil: [ ^value ]) asInteger - 48 "$0 asInteger") < 0 or: [ digit >= 10 ]) ifTrue: [ sourceStream skip: -1. ^value ]. nDigits := nDigits + 1. digit = 0 ifFalse: [ (lastNonZero = 0 and: [ nDigits > 1 and: [ leadingZeroesAllowed not ] ]) ifTrue: [ self error: 'Leading zeroes are not allowed.' ]. lastNonZero := nDigits. value := value * 10 + digit ] ifTrue: [ value := value * 10 ] ] repeat! ----- Method: JsonNumberParser>>nextNumber: (in category 'parsing-public') ----- nextNumber: negative | numberOfTrailingZeroInIntegerPart oldLeadingZeroesAllowed numberOfTrailingZeroInFractionPart numberOfNonZeroFractionDigits mantissa value | integerPart := self nextUnsignedIntegerOrNil ifNil: [ ^self error: 'Missing integer part!!' ]. numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. (sourceStream peekFor: $.) ifFalse: [ "No fraction part" negative ifTrue: [ integerPart := integerPart negated ]. self readExponent ifFalse: [ ^integerPart ]. exponent > 0 ifTrue: [ ^integerPart * (self fiveRaisedTo: exponent) bitShift: exponent ]. ^self makeFloatFromMantissa: integerPart exponent: exponent ]. oldLeadingZeroesAllowed := leadingZeroesAllowed. leadingZeroesAllowed := true. fractionPart := self nextUnsignedIntegerOrNil. leadingZeroesAllowed := oldLeadingZeroesAllowed. fractionPart ifNil: [ self error: 'Missing fraction part!!' ]. fractionPart isZero ifTrue: [ self readExponent ifFalse: [ negative ifTrue: [ integerPart isZero ifTrue: [ ^Float negativeZero ]. ^integerPart negated ]. ^integerPart ]. exponent >= 0 ifTrue: [ negative ifTrue: [ integerPart := integerPart negated ]. ^integerPart * (self fiveRaisedTo: exponent) bitShift: exponent ]. value := self makeFloatFromMantissa: integerPart exponent: exponent. negative ifTrue: [ ^value negated ]. ^value ]. numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. numberOfNonZeroFractionDigits := lastNonZero. self readExponent. exponent := exponent - numberOfNonZeroFractionDigits. mantissa := (integerPart * (self fiveRaisedTo: numberOfNonZeroFractionDigits) bitShift: numberOfNonZeroFractionDigits) + (fractionPart // (self fiveRaisedTo: numberOfTrailingZeroInFractionPart) bitShift: 0 - numberOfTrailingZeroInFractionPart). value := self makeFloatFromMantissa: mantissa exponent: exponent. negative ifTrue: [ ^value negated ]. ^value! ----- Method: JsonNumberParser>>nextUnsignedIntegerOrNil (in category 'parsing-public') ----- nextUnsignedIntegerOrNil "Form an unsigned integer with incoming digits from sourceStream. Answer this integer, or nil if no digit found. Count the number of digits and the position of lastNonZero digit and store them in instVar" | nPackets high nDigitsHigh lastNonZeroHigh low | "read no more digits than one elementary LargeInteger" high := self nextElementaryLargeInteger. nDigits = 0 ifTrue: [^nil]. "Not enough digits to form a LargeInteger, stop iteration" high isLarge ifFalse: [^high]. "We now have to engage arithmetic with LargeInteger Decompose the integer in a high and low packets of growing size:" nPackets := 1. nDigitsHigh := nDigits. lastNonZeroHigh := lastNonZero. [ low := self nextLargeIntegerBase: 10 nPackets: nPackets . high := (high * (self fiveRaisedTo: nDigits) bitShift: nDigits) + low. lastNonZero = 0 ifFalse: [lastNonZeroHigh := lastNonZero + nDigitsHigh]. nDigitsHigh := nDigitsHigh + nDigits. low isLarge] whileTrue: [nPackets := nPackets * 2]. nDigits := nDigitsHigh. lastNonZero := lastNonZeroHigh. ^high! ----- Method: ScaledDecimal>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: stream self printOn: stream showingDecimalPlaces: scale! ----- Method: Collection>>jsonWriteOn: (in category '*json') ----- jsonWriteOn: aStream "By default, use array braces " aStream nextPut: $[. self do: [:each | each jsonWriteOn: aStream ] separatedBy: [ aStream nextPut: $, ]. aStream nextPut: $]! TestCase subclass: #JsonTests instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonTests commentStamp: '<historical>' prior: 0! I provide a number of test cases for class Json.! ----- Method: JsonTests>>assertIncompleteJson: (in category 'helpers') ----- assertIncompleteJson: aString self should: [self readFrom: aString] raise: JsonIncompleteError! ----- Method: JsonTests>>assertInvalidJson: (in category 'helpers') ----- assertInvalidJson: aString self should: [self readFrom: aString] raise: JsonInvalidError! ----- Method: JsonTests>>json:equals: (in category 'helpers') ----- json: aString equals: aValue | readValue | readValue := self readFrom: aString. self assert: readValue = aValue.! ----- Method: JsonTests>>readFrom: (in category 'helpers') ----- readFrom: aString ^ (Json newWithConstructors: {JsonDummyTestObject.}) readFrom: aString readStream ! ----- Method: JsonTests>>render:equals: (in category 'helpers') ----- render: anObject equals: aString self assert: (Json render: anObject) = aString! ----- Method: JsonTests>>simpleDummyObject (in category 'helpers') ----- simpleDummyObject ^ JsonDummyTestObject new a: 1; b: 2; c: 3; yourself! ----- Method: JsonTests>>testArray (in category 'tests') ----- testArray self json: '[]' equals: #(). self json: '[[]]' equals: #(#()). self json: '[[], []]' equals: #(#() #()). self json: '["hi", "there"]' equals: #('hi' 'there'). self json: '[["a", "b", null]]' equals: #(('a' 'b' nil)).! ----- Method: JsonTests>>testAtomFalse (in category 'tests') ----- testAtomFalse self json: 'false' equals: false. self json: ' false' equals: false. self json: 'false ' equals: false. self json: ' false ' equals: false. ! ----- Method: JsonTests>>testAtomNull (in category 'tests') ----- testAtomNull self json: 'null' equals: nil. self json: ' null' equals: nil. self json: 'null ' equals: nil. self json: ' null ' equals: nil. ! ----- Method: JsonTests>>testAtomNumber (in category 'tests') ----- testAtomNumber self json: '1' equals: 1. self json: '123' equals: 123. self should: [ self json: '0123' equals: 123 ] raise: JsonSyntaxError. "No leading zeroes." self json: '1.23e2' equals: 123. self json: '-1' equals: -1. self json: '-0' equals: 0. self json: '[-1.2]' equals: #(-1.2).! ----- Method: JsonTests>>testAtomString (in category 'tests') ----- testAtomString self json: '"hi"' equals: 'hi'. self json: '"\""' equals: '"'. self json: '"\\"' equals: '\'. self json: '""' equals: ''. self json: '"a\u0004b"' equals: ({$a. Character value: 4. $b} as: String). self json: '"a\nb"' equals: ({$a. Character lf. $b} as: String).! ----- Method: JsonTests>>testAtomTrue (in category 'tests') ----- testAtomTrue self json: 'true' equals: true. self json: ' true' equals: true. self json: 'true ' equals: true. self json: ' true ' equals: true. ! ----- Method: JsonTests>>testCtor (in category 'tests') ----- testCtor self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}' equals: self simpleDummyObject. self json: (Json render: self simpleDummyObject) equals: self simpleDummyObject.! ----- Method: JsonTests>>testDictionary (in category 'tests') ----- testDictionary self json: '{}' equals: (JsonObject new). self json: '{"a": "a"}' equals: (JsonObject new at: 'a' put: 'a'; yourself). self json: '{"a": [[]]}' equals: (JsonObject new at: 'a' put: #(#()); yourself). self json: '{"a":"b", "b":"a"}' equals: (JsonObject new add: 'a'->'b'; add: 'b'->'a';yourself).! ----- Method: JsonTests>>testDictionaryClass (in category 'tests') ----- testDictionaryClass | parser | parser := Json new. self assert: (parser readFrom: '{}' readStream) class == JsonObject. parser dictionaryClass: OrderedJsonObject. self assert: (parser readFrom: '{}' readStream) class == OrderedJsonObject. self assert: (parser readFrom: '{"x":{"y":{}}}' readStream) x y class == OrderedJsonObject! ----- Method: JsonTests>>testIncomplete (in category 'tests') ----- testIncomplete self assertIncompleteJson: ''. self assertIncompleteJson: ' '. self assertIncompleteJson: ' tr'. self assertIncompleteJson: 'tru'. self assertIncompleteJson: '['. self assertIncompleteJson: '[true'. self assertIncompleteJson: '[true,'. self assertIncompleteJson: '{'. self assertIncompleteJson: '{"hi"'. self assertIncompleteJson: '{"hi":'. self assertIncompleteJson: '{"hi":true'. self assertIncompleteJson: '{"hi":true,'. self assertIncompleteJson: '{"hi":true,"'. self assertIncompleteJson: '"hello'. self assertIncompleteJson: '"hello '. self assertIncompleteJson: '"hello\'. self assertIncompleteJson: '"\u26'.! ----- Method: JsonTests>>testInvalid (in category 'tests') ----- testInvalid self assertInvalidJson: 'x'. self assertInvalidJson: ' x'. self assertInvalidJson: ' trx'. self assertInvalidJson: 'trux'. self assertInvalidJson: '.'. self assertInvalidJson: ':'. self assertInvalidJson: ','. self assertInvalidJson: ']'. self assertInvalidJson: '}'. self assertInvalidJson: '[x'. self assertInvalidJson: '[true t'. self assertInvalidJson: '[true,]'. self assertInvalidJson: '{]'. self assertInvalidJson: '{,'. self assertInvalidJson: '{"hi",'. self assertInvalidJson: '{"hi":x'. self assertInvalidJson: '{"hi":,'. self assertInvalidJson: '{"hi":true "'. self assertInvalidJson: '{"hi":true,}'. self assertInvalidJson: '\u263A'.! ----- Method: JsonTests>>testInvalidUnicodeEscapes (in category 'tests') ----- testInvalidUnicodeEscapes self assertInvalidJson: '"\u26"'. "Note that naively reading four chars runs off the end here, so we might see JsonIncompleteError, which would be wrong." self assertInvalidJson: '"\u2Z"'. self assertInvalidJson: '"\u2Z44"'.! ----- Method: JsonTests>>testJsonObjectAtPutReturnsTheObject (in category 'tests') ----- testJsonObjectAtPutReturnsTheObject { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self assert: (json at: 'foo' put: 1) == json. self assert: (json foo: 1) == json ]! ----- Method: JsonTests>>testMissingCtor (in category 'tests') ----- testMissingCtor self assertInvalidJson: '@Missing[]'! ----- Method: JsonTests>>testMissingCtorNoMap (in category 'tests') ----- testMissingCtorNoMap self should: [Json new readFrom: '@Missing[]' readStream] raise: JsonInvalidError! ----- Method: JsonTests>>testNonStringKeysRaiseError (in category 'tests') ----- testNonStringKeysRaiseError { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self should: [ json at: 1 put: 1 ] raise: Error. self assert: json isEmpty. json at: '1' put: 1. self assert: 1 equals: (json at: '1') ]! ----- Method: JsonTests>>testStreaming (in category 'tests') ----- testStreaming | j | j := Json new stream: 'truefalsetrue[]{}1.234 5.678"A""B"nullnull' readStream. self assert: j readAny equals: true. self assert: j readAny equals: false. self assert: j readAny equals: true. self assert: j readAny equals: #(). self assert: j readAny equals: JsonObject new. self assert: j readAny equals: 1.234. self assert: j readAny equals: 5.678. self assert: j readAny equals: 'A'. self assert: j readAny equals: 'B'. self assert: j readAny equals: nil. self assert: j readAny equals: nil.! ----- Method: JsonTests>>testStringWithUnicode (in category 'tests') ----- testStringWithUnicode | unicodeString | unicodeString := (Unicode value: 16r263A) asString. self json: '"\u263A"' equals:unicodeString. self json: '"\u263a"' equals:unicodeString. self json: '"', unicodeString, '"' equals: unicodeString. self render: unicodeString equals: '"', unicodeString, '"'.! ----- Method: JsonTests>>testWriteAssociation (in category 'tests') ----- testWriteAssociation self render: 'key' -> 'value' equals: '"key": "value"'. self render: 'key' -> 2 equals: '"key": 2'. "keys should be strings" self render: 42 -> 2 equals: '"42": 2'. "try to do _something_ for more complex keys" self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'. ! ----- Method: JsonTests>>testWriteString (in category 'tests') ----- testWriteString self render: '"' equals: '"\""'. self render: '\' equals: '"\\"'. self render: 'hi' equals: '"hi"'. self render: ({$a. Character lf. $b} as: String) equals: '"a\nb"'. self render: ({$a. Character value: 4. $b} as: String) equals: '"a\u0004b"'.! ----- Method: String>>asSerializedJson (in category '*JSON') ----- asSerializedJson " Assume that the receiver is a valid serialized json string. Return an object that understands #jsonWriteOn: and will write this string on its argument. " ^SerializedJson on: self! ----- Method: String>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | index start | aStream nextPut: $". start := 1. [ (index := self indexOfAnyOf: Json escapeSet startingAt: start) = 0 ] whileFalse: [ aStream next: index - start putAll: self startingAt: start. aStream nextPutAll: (Json escapeForCharacter: (self at: index)). start := index + 1 ]. start <= self size ifTrue: [ aStream next: self size + 1 - start putAll: self startingAt: start ]. aStream nextPut: $". ! ----- Method: String>>parseAsJson (in category '*JSON') ----- parseAsJson "Convenience" ^Json readFrom: self readStream! ----- Method: String>>parseAsOrderedJson (in category '*JSON') ----- parseAsOrderedJson "Convenience" ^Json new dictionaryClass: OrderedJsonObject; readFrom: self readStream! Object subclass: #Json instanceVariableNames: 'stream currentCharacter arrayBufferStream stringBufferStream numberParser ctorMap dictionaryClass' classVariableNames: '' poolDictionaries: '' category: 'JSON'! Json class instanceVariableNames: 'escapeArray escapeSet'! !Json commentStamp: 'ul 3/29/2016 04:38' prior: 0! I support reading and writing JSON (see
http://json.org
) formatted data - strings, numbers, boolean, nil, arrays and dictionaries. The implementation is mainly based on RFC 7159 (
https://www.ietf.org/rfc/rfc7159.txt
). It has been extended with syntax for invoking a prearranged list of constructors on read objects. Character encoding is not handled here. That's something you have to do before reading or after writing. Mixed processing is possible, but it's entirely your responsibility to keep things correct. Instance Variables stream: <PositionableStream> currentCharacter: <Character> arrayBufferStream: <WriteStream> stringBufferStream: <ReadWriteStream> numberParser: <ExtendedNumberParser> ctorMap: <Dictionary> For parsing, stream has to be initialized by sending #readFrom: to my instance. The parser will pre-read one character, which will be stored in currentCharacter, so the stream is expected to support position manipulation, hence it should be a PositionableStream. arrayBufferStream is WriteStream on an Array, which holds the values during array parsing (see #readArray). When there's a recursive call, this stream is used as a stack to separate the elements parsed in the previous invocation from the current one. stringBufferStream is a ReadWriteStream on a String, which holds the currently parsed String (see #readString), the currently parsed number (see #readNumber) or the name of the currently parsed constructor (see #readConstructor). Recursion has no effect on this. numberParser is a cached instance of ExtendedNumberParser. It's initialized on stringBufferStream, so it can quickly parse the number stored in it. ctorMap is the Dictionary holding the constructor classes by name. arrayBufferStream, stringBufferStream and numberParser are initialized lazily, and should not be accessed externally. Since these are shared objects, along with stream and currentCharacter, holding state, therefore a single instance of me shouldn't be used to parse multiple streams at the same time. For writing, you can use the methods in the class-side rendering category. To convert individual objects to a string, you can use #asJsonString, to serialize it on a stream, you can use #jsonWriteOn:. ! Json class instanceVariableNames: 'escapeArray escapeSet'! ----- Method: Json class>>escapeForCharacter: (in category 'accessing') ----- escapeForCharacter: c | asciiValue | (asciiValue := c asciiValue) < 128 ifTrue: [ ^escapeArray at: asciiValue + 1 ]. ^nil! ----- Method: Json class>>escapeSet (in category 'accessing') ----- escapeSet ^escapeSet! ----- Method: Json class>>initialize (in category 'class initialization') ----- initialize "Json initialize." | newEscapeArray newEscapeSet | newEscapeArray := Array new: 128. newEscapeSet := CharacterSet new. (0 to: 31), #(127) do: [ :each | newEscapeArray at: each + 1 put: '\u', (each printStringHex padded: #left to: 4 with: $0). newEscapeSet add: (Character value: each) ]. { $" -> '\"'. $\ -> '\\'. $/ -> '\/'. "A hack, so the generated JSON will always be HTML+Javascript compatible, because the generated JSON will not contain the seqence </script>." Character backspace -> '\b'. Character lf -> '\n'. Character newPage -> '\f'. Character cr -> '\r'. Character tab -> '\t'. } do: [ :each | newEscapeArray at: each key asciiValue + 1 put: each value. newEscapeSet add: each key ]. escapeArray := newEscapeArray. escapeSet := newEscapeSet! ----- Method: Json class>>mimeType (in category 'accessing') ----- mimeType ^ 'application/x-json'! ----- Method: Json class>>newWithConstructors: (in category 'instance creation') ----- newWithConstructors: aCollection | m | m := Dictionary new. aCollection do: [:each | (each isKindOf: Association) ifTrue: [m add: each] ifFalse: [m at: each name asString put: each]]. ^ self new ctorMap: m; yourself.! ----- Method: Json class>>readFrom: (in category 'instance creation') ----- readFrom: aStream ^ self new readFrom: aStream.! ----- Method: Json class>>render: (in category 'rendering') ----- render: anObject ^String streamContents: [ :stream | anObject jsonWriteOn: stream ]! ----- Method: Json class>>render:withConstructor:on: (in category 'rendering') ----- render: anObject withConstructor: aConstructorName on: aStream aStream nextPutAll: '@', aConstructorName. anObject jsonWriteOn: aStream. ! ----- Method: Json class>>renderInstanceVariables:of:on: (in category 'rendering') ----- renderInstanceVariables: aCollection of: anObject on: aStream | map | map := Dictionary new. aCollection do: [:ivarName | map at: ivarName put: (anObject instVarNamed: ivarName)]. self render: map withConstructor: anObject class name asString on: aStream! ----- Method: Json>>arrayBufferStream (in category 'private') ----- arrayBufferStream ^arrayBufferStream ifNil: [ arrayBufferStream := (Array new: 10) writeStream ]! ----- Method: Json>>consume: (in category 'private') ----- consume: aString | message | 1 to: aString size do: [ :index | (currentCharacter := stream next) == (aString at: index) ifFalse: [ message := 'Expected ', (aString at: index) printString. currentCharacter ifNil: [ self incomplete: message ] ifNotNil: [ self invalid: message ] ] ]. currentCharacter := stream next! ----- Method: Json>>ctorMap (in category 'accessing') ----- ctorMap ^ ctorMap! ----- Method: Json>>ctorMap: (in category 'accessing') ----- ctorMap: m ctorMap := m! ----- Method: Json>>dictionaryClass: (in category 'accessing') ----- dictionaryClass: aClass "This method allows you to override the default dictionary class to be created while parsing a stream. Instances of aClass are expected to understand #at:put:." dictionaryClass := aClass! ----- Method: Json>>error: (in category 'error handling') ----- error: aString (currentCharacter isNil and: [ stream atEnd ]) ifTrue: [ self incomplete: aString ] ifFalse: [ self invalid: aString ]! ----- Method: Json>>incomplete: (in category 'error handling') ----- incomplete: aString JsonIncompleteError signal: aString! ----- Method: Json>>initialize (in category 'initialize-release') ----- initialize dictionaryClass := JsonObject ! ----- Method: Json>>interpretStringEscape: (in category 'private') ----- interpretStringEscape: aCharacter aCharacter == $b ifTrue: [ ^Character backspace ]. aCharacter == $n ifTrue: [ ^Character lf ]. aCharacter == $f ifTrue: [ ^Character newPage ]. aCharacter == $r ifTrue: [ ^Character cr ]. aCharacter == $t ifTrue: [ ^Character tab ]. aCharacter == $u ifTrue: [ ^self unescapeUnicode ]. aCharacter == $" ifTrue: [ ^aCharacter ]. aCharacter == $\ ifTrue: [ ^aCharacter ]. aCharacter == $/ ifTrue: [ ^aCharacter ]. self error: 'Unexpected escaped character: ', aCharacter asString ! ----- Method: Json>>invalid: (in category 'error handling') ----- invalid: aString JsonInvalidError signal: aString! ----- Method: Json>>readAny (in category 'private') ----- readAny self skipWhitespace. currentCharacter == ${ ifTrue: [ ^self readDictionary ]. currentCharacter == $[ ifTrue: [ ^self readArray ]. currentCharacter == $" ifTrue: [ ^self readString ]. currentCharacter == $t ifTrue: [ self consume: 'rue'. ^true ]. currentCharacter == $f ifTrue: [ self consume: 'alse'. ^false ]. currentCharacter == $n ifTrue: [ self consume: 'ull'. ^nil ]. currentCharacter == $- ifTrue: [ ^self readNumber: true ]. (#($0 $1 $2 $3 $4 $5 $6 $7 $8 $9) instVarsInclude: currentCharacter) ifTrue: [ ^self readNumber: false ]. currentCharacter == $@ ifTrue: [ ^self readConstructor ]. self invalid: 'Unknown Json input'! ----- Method: Json>>readArray (in category 'private') ----- readArray | initialPosition | currentCharacter := stream next. self skipWhitespace. currentCharacter == $] ifTrue: [ currentCharacter := stream next. ^#() ]. initialPosition := self arrayBufferStream position. [ arrayBufferStream nextPut: self readAny. self skipWhitespace. currentCharacter == $] ifTrue: [ | result | result := arrayBufferStream originalContents copyFrom: initialPosition + 1 to: arrayBufferStream position. arrayBufferStream position: initialPosition. currentCharacter := stream next. ^result ]. currentCharacter == $, ifFalse: [ self error: 'Unexpected character: ', currentCharacter asString ]. currentCharacter := stream next ] repeat. ! ----- Method: Json>>readConstructor (in category 'private') ----- readConstructor ctorMap ifNil: [ ^self error: 'No constructors were declared.' ]. self resetStringBufferStream. [ (currentCharacter := stream next) == $. or: [ currentCharacter isLetter ] ] whileTrue: [ stringBufferStream nextPut: currentCharacter ]. (ctorMap at: stringBufferStream contents ifAbsent: nil) ifNotNil: [ :constructor | ^constructor constructFromJson: self readAny ]. self error: 'Unknown constructor: ', stringBufferStream contents! ----- Method: Json>>readDictionary (in category 'private') ----- readDictionary | result key commaNeeded | result := dictionaryClass new. commaNeeded := false. currentCharacter := stream next. [ self skipWhitespace. currentCharacter == $} ifTrue: [ currentCharacter := stream next. ^result ]. commaNeeded ifFalse: [ commaNeeded := true ] ifTrue: [ currentCharacter == $, ifFalse: [ self error: 'Missing comma' ]. currentCharacter := stream next. self skipWhitespace ]. currentCharacter == $" ifFalse: [ self error: 'Key in dictionary must be string' ]. key := self readString. self skipWhitespace. currentCharacter == $: ifFalse: [ self error: 'Missing colon' ]. currentCharacter := stream next. result at: key put: self readAny ] repeat! ----- Method: Json>>readFrom: (in category 'parsing') ----- readFrom: aStream | result | stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next. result := self readAny. currentCharacter ifNotNil: [ stream skip: -1 ]. "Undo prereading." ^result! ----- Method: Json>>readNumber: (in category 'private') ----- readNumber: negative | result | negative ifFalse: [ stream skip: -1 ]. result := (numberParser ifNil: [ numberParser := JsonNumberParser new on: stream; failBlock: [ self error: 'Invalid number.' ]; yourself ]) nextNumber: negative. currentCharacter := stream next. ^result ! ----- Method: Json>>readString (in category 'private') ----- readString self resetStringBufferStream. [ currentCharacter := stream next ifNil: [self incomplete: 'Unexpected end of string' ]. currentCharacter == $" ifTrue: [ currentCharacter := stream next. ^stringBufferStream contents ]. currentCharacter == $\ ifTrue: [ stringBufferStream nextPut: (self interpretStringEscape: (stream next ifNil: [ self incomplete: 'Unexpected end of string' ])) ] ifFalse: [ stringBufferStream nextPut: currentCharacter ] ] repeat! ----- Method: Json>>resetStringBufferStream (in category 'private') ----- resetStringBufferStream stringBufferStream ifNil: [ stringBufferStream := ReadWriteStream on: (String new: 64) ] ifNotNil: [ stringBufferStream resetToStart ]! ----- Method: Json>>skipWhitespace (in category 'private') ----- skipWhitespace currentCharacter ifNil: [ self incomplete: 'Input stream is empty' ]. [ currentCharacter isSeparator ] whileTrue: [ currentCharacter := stream next ifNil: [ self incomplete: 'Input stream is empty' ]. ]! ----- Method: Json>>stream (in category 'accessing') ----- stream "Answer the value of stream" ^ stream! ----- Method: Json>>stream: (in category 'private') ----- stream: aStream "For testing purposes only" stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next! ----- Method: Json>>unescapeUnicode (in category 'private') ----- unescapeUnicode | code digitValue | code := 0. 1 to: 4 do: [ :index | (digitValue := (stream next ifNil: [ self incomplete: 'Unexpected end of stream' ]) digitValue) < 0 ifTrue: [ self invalid: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ code := code * 16 + digitValue ] ifFalse: [ self invalid: 'Invalid hexadecimal digit' ] ]. ^Character value: code! Object subclass: #JsonDummyTestObject instanceVariableNames: 'a b c' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonDummyTestObject class>>constructFromJson: (in category 'as yet unclassified') ----- constructFromJson: j ^ self new a: (j at: 'a'); b: (j at: 'b'); c: (j at: 'c'); yourself! ----- Method: JsonDummyTestObject>>= (in category 'as yet unclassified') ----- = other ^ other class == self class and: [ a = other a and: [ b = other b and: [ c = other c]]]! ----- Method: JsonDummyTestObject>>a (in category 'accessing') ----- a "Answer the value of a" ^ a! ----- Method: JsonDummyTestObject>>a: (in category 'accessing') ----- a: anObject "Set the value of a" a := anObject! ----- Method: JsonDummyTestObject>>b (in category 'accessing') ----- b "Answer the value of b" ^ b! ----- Method: JsonDummyTestObject>>b: (in category 'accessing') ----- b: anObject "Set the value of b" b := anObject! ----- Method: JsonDummyTestObject>>c (in category 'accessing') ----- c "Answer the value of c" ^ c! ----- Method: JsonDummyTestObject>>c: (in category 'accessing') ----- c: anObject "Set the value of c" c := anObject! ----- Method: JsonDummyTestObject>>jsonWriteOn: (in category 'as yet unclassified') ----- jsonWriteOn: s Json renderInstanceVariables: {#a. #b. #c} of: self on: s ! ----- Method: Object>>asJsonString (in category '*JSON') ----- asJsonString ^ String streamContents: [:str | self jsonWriteOn: str ]! Object subclass: #SerializedJson instanceVariableNames: 'jsonString' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: SerializedJson class>>on: (in category 'instance creation') ----- on: aString ^self new jsonString: aString; yourself! ----- Method: SerializedJson>>jsonString (in category 'accessing') ----- jsonString ^jsonString ! ----- Method: SerializedJson>>jsonString: (in category 'accessing') ----- jsonString: aString jsonString := aString ! ----- Method: SerializedJson>>jsonWriteOn: (in category 'json-writing') ----- jsonWriteOn: stream stream nextPutAll: jsonString!
1
0
0
0
The Trunk: JSON-ul.55.mcz
by commits@source.squeak.org
08 Sep '22
08 Sep '22
Christoph Thiede uploaded a new version of JSON to project The Trunk:
http://source.squeak.org/trunk/JSON-ul.55.mcz
==================== Summary ==================== Name: JSON-ul.55 Author: ul Time: 6 September 2020, 2:04:29.148771 am UUID: 67ed013e-3c3f-4407-8171-066e7574c412 Ancestors: JSON-ul.54 - fix #unescapeUnicode when the encoded character value is outside of BMP (reported by Christoph Thiede on squeak-dev) - #unescapeUnicode won't accept invalid unicode surrogate values any more - added a test case to cover the positive case of #unescapeUnicode - fixed Json class >> #mimeType ==================== Snapshot ==================== SystemOrganization addCategory: #JSON! ----- Method: Integer>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream ^ self printOn: aWriteStream base: 10 ! ----- Method: Association>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream self key asString jsonWriteOn: aStream. aStream nextPut: $:; space. self value jsonWriteOn: aStream.! ----- Method: False>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'false'! ----- Method: Number>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream self printOn: aWriteStream base: 10! OrderedDictionary subclass: #OrderedJsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: OrderedJsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: OrderedJsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: OrderedJsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: OrderedJsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: OrderedJsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: OrderedJsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: Time>>jsonWriteOn: (in category '*JSON') ----- jsonWriteOn: stream stream nextPut: $". self print24: true showSeconds: true on: stream. stream nextPut: $"! ----- Method: WriteStream>>jsonPrint: (in category '*json-printing') ----- jsonPrint: anObject anObject jsonWriteOn: self! ----- Method: Text>>jsonWriteOn: (in category '*json-printing') ----- jsonWriteOn: aStream self string jsonWriteOn: aStream! ----- Method: UndefinedObject>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aWriteStream aWriteStream nextPutAll: 'null'! Error subclass: #JsonSyntaxError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonSyntaxError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! Class Json signals (possibly-indirect) instances of me when reading a JSON value from an input stream fails.! JsonSyntaxError subclass: #JsonIncompleteError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonIncompleteError commentStamp: 'tonyg 4/29/2016 11:37' prior: 0! I signal that reading a JSON value failed because more input is required, but that the input seen so far was not incorrect. Compare to JsonInvalidError. Be warned that reading numbers directly out of a stream can be ambiguous!! Consider reading from '1234'. Is the result intended to be 1234, or is there missing input, and the next character will be '5', making the result (possibly) 12345?! JsonSyntaxError subclass: #JsonInvalidError instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonInvalidError commentStamp: 'tonyg 4/29/2016 11:06' prior: 0! I signal that reading a JSON value failed because the input didn't match the (extended)JSON grammar implemented by class Json.! ----- Method: Dictionary>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | needComma | needComma := false. aStream nextPut: ${. self associationsDo: [ :assoc | needComma ifTrue: [ aStream nextPut: $, ] ifFalse: [ needComma := true ]. assoc key jsonWriteOn: aStream. aStream nextPut: $:. assoc value jsonWriteOn: aStream ]. aStream nextPut: $}.! Dictionary subclass: #JsonObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonObject class>>fromAssociations: (in category 'as yet unclassified') ----- fromAssociations: collectionOfAssociations | result | result := self new. collectionOfAssociations do: [:each | result at: each key put: each value ]. ^ result! ----- Method: JsonObject>>at: (in category 'accessing') ----- at: aKey "make it work more like javascript objects" ^ self at: aKey ifAbsent: [nil]! ----- Method: JsonObject>>at:put: (in category 'accessing') ----- at: aString put: anObject aString isString ifFalse: [ self error: 'String expected' ]. super at: aString put: anObject. ^self! ----- Method: JsonObject>>doesNotUnderstand: (in category 'error handling') ----- doesNotUnderstand: aMessage | key precedence | key := aMessage selector. (precedence := key precedence) = 1 ifTrue: [ ^self at: key ifAbsent: nil ]. (precedence = 3 and: [ (key indexOf: $:) = key size ]) ifTrue: [ ^self at: key allButLast asSymbol put: aMessage arguments first ]. ^super doesNotUnderstand: aMessage! ----- Method: JsonObject>>name (in category 'accessing') ----- name "override 'cause Object defines this" ^self at: 'name'! ----- Method: JsonObject>>value (in category 'accessing') ----- value "override 'cause Object defines this" ^self at: 'value'! ----- Method: True>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream aStream nextPutAll: 'true'! NumberParser subclass: #JsonNumberParser instanceVariableNames: 'fraction leadingZeroesAllowed' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonNumberParser>>allowPlusSign (in category 'accessing') ----- allowPlusSign ^false! ----- Method: JsonNumberParser>>allowPlusSignInExponent (in category 'accessing') ----- allowPlusSignInExponent ^true! ----- Method: JsonNumberParser>>error: (in category 'error') ----- error: aString JsonSyntaxError signal: aString! ----- Method: JsonNumberParser>>exponentLetters (in category 'accessing') ----- exponentLetters ^'eE'! ----- Method: JsonNumberParser>>fiveRaisedTo: (in category 'parsing-private') ----- fiveRaisedTo: anInteger anInteger >= 0 ifFalse: [ ^1 / (self fiveRaisedTo: 0 - anInteger) ]. anInteger >= 28 ifTrue: [ ^5 raisedToInteger: anInteger ]. ^#(1 5 25 125 625 3125 15625 78125 390625 1953125 9765625 48828125 244140625 1220703125 6103515625 30517578125 152587890625 762939453125 3814697265625 19073486328125 95367431640625 476837158203125 2384185791015625 11920928955078125 59604644775390625 298023223876953125 1490116119384765625 7450580596923828125) at: anInteger + 1! ----- Method: JsonNumberParser>>initialize (in category 'initialize-release') ----- initialize super initialize. base := 10. fraction := Fraction numerator: 0 denominator: 1. leadingZeroesAllowed := false! ----- Method: JsonNumberParser>>makeFloatFromMantissa:exponent: (in category 'parsing-private') ----- makeFloatFromMantissa: mantissa exponent: exponent "Return a Float equal to mantissa * 10 ^ exponent." exponent = 0 ifTrue: [ ^mantissa asFloat ]. exponent > 0 ifTrue: [ exponent <= 22 "Can 5 raisedToInteger: exponent be represented exactly as a Float? ((1 << Float precision - 1) log: 5) floor => 22" ifFalse: [ exponent > 324 ifTrue: [ "(Float emax + Float precision * (2 log: 10)) ceiling => 324" mantissa isZero ifTrue: [ ^0.0 ]. mantissa positive ifTrue: [ ^Float infinity ]. ^Float negativeInfinity ]. ^(mantissa * (self fiveRaisedTo: exponent)) asFloat timesTwoPower: exponent ] ifTrue: [ mantissa highBitOfMagnitude <= Float precision ifTrue: [ "Mantissa can also be represented as an exact Float. Float >> #* should be exact." ^mantissa asFloat * (self fiveRaisedTo: exponent) asFloat timesTwoPower: exponent ] ]. ^(mantissa * (self fiveRaisedTo: exponent)) asFloat timesTwoPower: exponent ]. "exponent < 0" exponent >= -22 "Can 5 raisedToInteger: 0 - exponent be represented exactly as a Float?" ifTrue: [ mantissa highBitOfMagnitude <= Float precision ifTrue: [ "Mantissa can also be represented as an exact Float. Float >> #/ should be exact." ^mantissa asFloat / (self fiveRaisedTo: 0 - exponent) asFloat timesTwoPower: exponent ] ] ifFalse: [ exponent < -324 ifTrue: [ "-1 * (Float precision - Float emin * (2 log: 10)) ceiling => -324" mantissa positive ifTrue: [ ^0.0 ]. ^-0.0 ] ]. "No luck. Let Fraction >> #asFloat do the heavy lifting." ^(fraction setNumerator: mantissa denominator: ((self fiveRaisedTo: 0 - exponent) bitShift: 0 - exponent)) asFloat "Can't use #timesTwoPower: here. No #reduced here, because there's none in NumberParser either"! ----- Method: JsonNumberParser>>nextElementaryLargeInteger (in category 'parsing-large int') ----- nextElementaryLargeInteger "Form an unsigned integer with incoming digits from sourceStream. Return this integer, or zero if no digits found. Stop reading if end of digits or if a LargeInteger is formed. Count the number of digits and the position of lastNonZero digit and store them in instVar." | digit value | value := 0. nDigits := 0. lastNonZero := 0. [ value isLarge ifTrue: [ ^value ]. ((digit := (sourceStream next ifNil: [ ^value ]) asInteger - 48 "$0 asInteger") < 0 or: [ digit >= 10 ]) ifTrue: [ sourceStream skip: -1. ^value ]. nDigits := nDigits + 1. digit = 0 ifFalse: [ (lastNonZero = 0 and: [ nDigits > 1 and: [ leadingZeroesAllowed not ] ]) ifTrue: [ self error: 'Leading zeroes are not allowed.' ]. lastNonZero := nDigits. value := value * 10 + digit ] ifTrue: [ value := value * 10 ] ] repeat! ----- Method: JsonNumberParser>>nextNumber: (in category 'parsing-public') ----- nextNumber: negative | numberOfTrailingZeroInIntegerPart oldLeadingZeroesAllowed numberOfTrailingZeroInFractionPart numberOfNonZeroFractionDigits mantissa value | integerPart := self nextUnsignedIntegerOrNil ifNil: [ ^self error: 'Missing integer part!!' ]. numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. (sourceStream peekFor: $.) ifFalse: [ "No fraction part" negative ifTrue: [ integerPart := integerPart negated ]. self readExponent ifFalse: [ ^integerPart ]. exponent > 0 ifTrue: [ ^integerPart * (self fiveRaisedTo: exponent) bitShift: exponent ]. ^self makeFloatFromMantissa: integerPart exponent: exponent ]. oldLeadingZeroesAllowed := leadingZeroesAllowed. leadingZeroesAllowed := true. fractionPart := self nextUnsignedIntegerOrNil. leadingZeroesAllowed := oldLeadingZeroesAllowed. fractionPart ifNil: [ self error: 'Missing fraction part!!' ]. fractionPart isZero ifTrue: [ self readExponent ifFalse: [ negative ifTrue: [ integerPart isZero ifTrue: [ ^Float negativeZero ]. ^integerPart negated ]. ^integerPart ]. exponent >= 0 ifTrue: [ negative ifTrue: [ integerPart := integerPart negated ]. ^integerPart * (self fiveRaisedTo: exponent) bitShift: exponent ]. value := self makeFloatFromMantissa: integerPart exponent: exponent. negative ifTrue: [ ^value negated ]. ^value ]. numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. numberOfNonZeroFractionDigits := lastNonZero. self readExponent. exponent := exponent - numberOfNonZeroFractionDigits. mantissa := (integerPart * (self fiveRaisedTo: numberOfNonZeroFractionDigits) bitShift: numberOfNonZeroFractionDigits) + (fractionPart // (self fiveRaisedTo: numberOfTrailingZeroInFractionPart) bitShift: 0 - numberOfTrailingZeroInFractionPart). value := self makeFloatFromMantissa: mantissa exponent: exponent. negative ifTrue: [ ^value negated ]. ^value! ----- Method: JsonNumberParser>>nextUnsignedIntegerOrNil (in category 'parsing-public') ----- nextUnsignedIntegerOrNil "Form an unsigned integer with incoming digits from sourceStream. Answer this integer, or nil if no digit found. Count the number of digits and the position of lastNonZero digit and store them in instVar" | nPackets high nDigitsHigh lastNonZeroHigh low | "read no more digits than one elementary LargeInteger" high := self nextElementaryLargeInteger. nDigits = 0 ifTrue: [^nil]. "Not enough digits to form a LargeInteger, stop iteration" high isLarge ifFalse: [^high]. "We now have to engage arithmetic with LargeInteger Decompose the integer in a high and low packets of growing size:" nPackets := 1. nDigitsHigh := nDigits. lastNonZeroHigh := lastNonZero. [ low := self nextLargeIntegerBase: 10 nPackets: nPackets . high := (high * (self fiveRaisedTo: nDigits) bitShift: nDigits) + low. lastNonZero = 0 ifFalse: [lastNonZeroHigh := lastNonZero + nDigitsHigh]. nDigitsHigh := nDigitsHigh + nDigits. low isLarge] whileTrue: [nPackets := nPackets * 2]. nDigits := nDigitsHigh. lastNonZero := lastNonZeroHigh. ^high! ----- Method: ScaledDecimal>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: stream self printOn: stream showingDecimalPlaces: scale! ----- Method: Collection>>jsonWriteOn: (in category '*json') ----- jsonWriteOn: aStream "By default, use array braces " aStream nextPut: $[. self do: [:each | each jsonWriteOn: aStream ] separatedBy: [ aStream nextPut: $, ]. aStream nextPut: $]! TestCase subclass: #JsonTests instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'JSON'! !JsonTests commentStamp: '<historical>' prior: 0! I provide a number of test cases for class Json.! ----- Method: JsonTests>>assertIncompleteJson: (in category 'helpers') ----- assertIncompleteJson: aString self should: [self readFrom: aString] raise: JsonIncompleteError! ----- Method: JsonTests>>assertInvalidJson: (in category 'helpers') ----- assertInvalidJson: aString self should: [self readFrom: aString] raise: JsonInvalidError! ----- Method: JsonTests>>json:equals: (in category 'helpers') ----- json: aString equals: aValue | readValue | readValue := self readFrom: aString. self assert: aValue equals: readValue! ----- Method: JsonTests>>readFrom: (in category 'helpers') ----- readFrom: aString ^ (Json newWithConstructors: {JsonDummyTestObject.}) readFrom: aString readStream ! ----- Method: JsonTests>>render:equals: (in category 'helpers') ----- render: anObject equals: aString self assert: (Json render: anObject) = aString! ----- Method: JsonTests>>simpleDummyObject (in category 'helpers') ----- simpleDummyObject ^ JsonDummyTestObject new a: 1; b: 2; c: 3; yourself! ----- Method: JsonTests>>testArray (in category 'tests') ----- testArray self json: '[]' equals: #(). self json: '[[]]' equals: #(#()). self json: '[[], []]' equals: #(#() #()). self json: '["hi", "there"]' equals: #('hi' 'there'). self json: '[["a", "b", null]]' equals: #(('a' 'b' nil)).! ----- Method: JsonTests>>testAtomFalse (in category 'tests') ----- testAtomFalse self json: 'false' equals: false. self json: ' false' equals: false. self json: 'false ' equals: false. self json: ' false ' equals: false. ! ----- Method: JsonTests>>testAtomNull (in category 'tests') ----- testAtomNull self json: 'null' equals: nil. self json: ' null' equals: nil. self json: 'null ' equals: nil. self json: ' null ' equals: nil. ! ----- Method: JsonTests>>testAtomNumber (in category 'tests') ----- testAtomNumber self json: '1' equals: 1. self json: '123' equals: 123. self should: [ self json: '0123' equals: 123 ] raise: JsonSyntaxError. "No leading zeroes." self json: '1.23e2' equals: 123. self json: '-1' equals: -1. self json: '-0' equals: 0. self json: '[-1.2]' equals: #(-1.2).! ----- Method: JsonTests>>testAtomString (in category 'tests') ----- testAtomString self json: '"hi"' equals: 'hi'. self json: '"\""' equals: '"'. self json: '"\\"' equals: '\'. self json: '""' equals: ''. self json: '"a\u0004b"' equals: ({$a. Character value: 4. $b} as: String). self json: '"a\nb"' equals: ({$a. Character lf. $b} as: String). self json: '"a\uD834\uDD1Eb"' equals: ({$a. Character value: 16r1D11E. $b} as: String). ! ----- Method: JsonTests>>testAtomTrue (in category 'tests') ----- testAtomTrue self json: 'true' equals: true. self json: ' true' equals: true. self json: 'true ' equals: true. self json: ' true ' equals: true. ! ----- Method: JsonTests>>testCtor (in category 'tests') ----- testCtor self json: '@JsonDummyTestObject {"a": 1, "b": 2, "c": 3}' equals: self simpleDummyObject. self json: (Json render: self simpleDummyObject) equals: self simpleDummyObject.! ----- Method: JsonTests>>testDictionary (in category 'tests') ----- testDictionary self json: '{}' equals: (JsonObject new). self json: '{"a": "a"}' equals: (JsonObject new at: 'a' put: 'a'; yourself). self json: '{"a": [[]]}' equals: (JsonObject new at: 'a' put: #(#()); yourself). self json: '{"a":"b", "b":"a"}' equals: (JsonObject new add: 'a'->'b'; add: 'b'->'a';yourself).! ----- Method: JsonTests>>testDictionaryClass (in category 'tests') ----- testDictionaryClass | parser | parser := Json new. self assert: (parser readFrom: '{}' readStream) class == JsonObject. parser dictionaryClass: OrderedJsonObject. self assert: (parser readFrom: '{}' readStream) class == OrderedJsonObject. self assert: (parser readFrom: '{"x":{"y":{}}}' readStream) x y class == OrderedJsonObject! ----- Method: JsonTests>>testIncomplete (in category 'tests') ----- testIncomplete self assertIncompleteJson: ''. self assertIncompleteJson: ' '. self assertIncompleteJson: ' tr'. self assertIncompleteJson: 'tru'. self assertIncompleteJson: '['. self assertIncompleteJson: '[true'. self assertIncompleteJson: '[true,'. self assertIncompleteJson: '{'. self assertIncompleteJson: '{"hi"'. self assertIncompleteJson: '{"hi":'. self assertIncompleteJson: '{"hi":true'. self assertIncompleteJson: '{"hi":true,'. self assertIncompleteJson: '{"hi":true,"'. self assertIncompleteJson: '"hello'. self assertIncompleteJson: '"hello '. self assertIncompleteJson: '"hello\'. self assertIncompleteJson: '"\u26'.! ----- Method: JsonTests>>testInvalid (in category 'tests') ----- testInvalid self assertInvalidJson: 'x'. self assertInvalidJson: ' x'. self assertInvalidJson: ' trx'. self assertInvalidJson: 'trux'. self assertInvalidJson: '.'. self assertInvalidJson: ':'. self assertInvalidJson: ','. self assertInvalidJson: ']'. self assertInvalidJson: '}'. self assertInvalidJson: '[x'. self assertInvalidJson: '[true t'. self assertInvalidJson: '[true,]'. self assertInvalidJson: '{]'. self assertInvalidJson: '{,'. self assertInvalidJson: '{"hi",'. self assertInvalidJson: '{"hi":x'. self assertInvalidJson: '{"hi":,'. self assertInvalidJson: '{"hi":true "'. self assertInvalidJson: '{"hi":true,}'. self assertInvalidJson: '\u263A'.! ----- Method: JsonTests>>testInvalidUnicodeEscapes (in category 'tests') ----- testInvalidUnicodeEscapes self assertInvalidJson: '"\u26"'. "Note that naively reading four chars runs off the end here, so we might see JsonIncompleteError, which would be wrong." self assertInvalidJson: '"\u2Z"'. self assertInvalidJson: '"\u2Z44"'.! ----- Method: JsonTests>>testJsonObjectAtPutReturnsTheObject (in category 'tests') ----- testJsonObjectAtPutReturnsTheObject { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self assert: (json at: 'foo' put: 1) == json. self assert: (json foo: 1) == json ]! ----- Method: JsonTests>>testMissingCtor (in category 'tests') ----- testMissingCtor self assertInvalidJson: '@Missing[]'! ----- Method: JsonTests>>testMissingCtorNoMap (in category 'tests') ----- testMissingCtorNoMap self should: [Json new readFrom: '@Missing[]' readStream] raise: JsonInvalidError! ----- Method: JsonTests>>testNonStringKeysRaiseError (in category 'tests') ----- testNonStringKeysRaiseError { JsonObject. OrderedJsonObject } do: [ :jsonObjectClass | | json | json := jsonObjectClass new. self should: [ json at: 1 put: 1 ] raise: Error. self assert: json isEmpty. json at: '1' put: 1. self assert: 1 equals: (json at: '1') ]! ----- Method: JsonTests>>testStreaming (in category 'tests') ----- testStreaming | j | j := Json new stream: 'truefalsetrue[]{}1.234 5.678"A""B"nullnull' readStream. self assert: j readAny equals: true. self assert: j readAny equals: false. self assert: j readAny equals: true. self assert: j readAny equals: #(). self assert: j readAny equals: JsonObject new. self assert: j readAny equals: 1.234. self assert: j readAny equals: 5.678. self assert: j readAny equals: 'A'. self assert: j readAny equals: 'B'. self assert: j readAny equals: nil. self assert: j readAny equals: nil.! ----- Method: JsonTests>>testStringWithUnicode (in category 'tests') ----- testStringWithUnicode | unicodeString | unicodeString := (Unicode value: 16r263A) asString. self json: '"\u263A"' equals:unicodeString. self json: '"\u263a"' equals:unicodeString. self json: '"', unicodeString, '"' equals: unicodeString. self render: unicodeString equals: '"', unicodeString, '"'.! ----- Method: JsonTests>>testWriteAssociation (in category 'tests') ----- testWriteAssociation self render: 'key' -> 'value' equals: '"key": "value"'. self render: 'key' -> 2 equals: '"key": 2'. "keys should be strings" self render: 42 -> 2 equals: '"42": 2'. "try to do _something_ for more complex keys" self render: #(42 43 44) -> 2 equals: '"#(42 43 44)": 2'. ! ----- Method: JsonTests>>testWriteString (in category 'tests') ----- testWriteString self render: '"' equals: '"\""'. self render: '\' equals: '"\\"'. self render: 'hi' equals: '"hi"'. self render: ({$a. Character lf. $b} as: String) equals: '"a\nb"'. self render: ({$a. Character value: 4. $b} as: String) equals: '"a\u0004b"'.! ----- Method: String>>asSerializedJson (in category '*JSON') ----- asSerializedJson " Assume that the receiver is a valid serialized json string. Return an object that understands #jsonWriteOn: and will write this string on its argument. " ^SerializedJson on: self! ----- Method: String>>jsonWriteOn: (in category '*JSON-writing') ----- jsonWriteOn: aStream | index start | aStream nextPut: $". start := 1. [ (index := self indexOfAnyOf: Json escapeSet startingAt: start) = 0 ] whileFalse: [ aStream next: index - start putAll: self startingAt: start. aStream nextPutAll: (Json escapeForCharacter: (self at: index)). start := index + 1 ]. start <= self size ifTrue: [ aStream next: self size + 1 - start putAll: self startingAt: start ]. aStream nextPut: $". ! ----- Method: String>>parseAsJson (in category '*JSON') ----- parseAsJson "Convenience" ^Json readFrom: self readStream! ----- Method: String>>parseAsOrderedJson (in category '*JSON') ----- parseAsOrderedJson "Convenience" ^Json new dictionaryClass: OrderedJsonObject; readFrom: self readStream! Object subclass: #Json instanceVariableNames: 'stream currentCharacter arrayBufferStream stringBufferStream numberParser ctorMap dictionaryClass' classVariableNames: '' poolDictionaries: '' category: 'JSON'! Json class instanceVariableNames: 'escapeArray escapeSet'! !Json commentStamp: 'ul 3/29/2016 04:38' prior: 0! I support reading and writing JSON (see
http://json.org
) formatted data - strings, numbers, boolean, nil, arrays and dictionaries. The implementation is mainly based on RFC 7159 (
https://www.ietf.org/rfc/rfc7159.txt
). It has been extended with syntax for invoking a prearranged list of constructors on read objects. Character encoding is not handled here. That's something you have to do before reading or after writing. Mixed processing is possible, but it's entirely your responsibility to keep things correct. Instance Variables stream: <PositionableStream> currentCharacter: <Character> arrayBufferStream: <WriteStream> stringBufferStream: <ReadWriteStream> numberParser: <ExtendedNumberParser> ctorMap: <Dictionary> For parsing, stream has to be initialized by sending #readFrom: to my instance. The parser will pre-read one character, which will be stored in currentCharacter, so the stream is expected to support position manipulation, hence it should be a PositionableStream. arrayBufferStream is WriteStream on an Array, which holds the values during array parsing (see #readArray). When there's a recursive call, this stream is used as a stack to separate the elements parsed in the previous invocation from the current one. stringBufferStream is a ReadWriteStream on a String, which holds the currently parsed String (see #readString), the currently parsed number (see #readNumber) or the name of the currently parsed constructor (see #readConstructor). Recursion has no effect on this. numberParser is a cached instance of ExtendedNumberParser. It's initialized on stringBufferStream, so it can quickly parse the number stored in it. ctorMap is the Dictionary holding the constructor classes by name. arrayBufferStream, stringBufferStream and numberParser are initialized lazily, and should not be accessed externally. Since these are shared objects, along with stream and currentCharacter, holding state, therefore a single instance of me shouldn't be used to parse multiple streams at the same time. For writing, you can use the methods in the class-side rendering category. To convert individual objects to a string, you can use #asJsonString, to serialize it on a stream, you can use #jsonWriteOn:. ! Json class instanceVariableNames: 'escapeArray escapeSet'! ----- Method: Json class>>escapeForCharacter: (in category 'accessing') ----- escapeForCharacter: c | asciiValue | (asciiValue := c asciiValue) < 128 ifTrue: [ ^escapeArray at: asciiValue + 1 ]. ^nil! ----- Method: Json class>>escapeSet (in category 'accessing') ----- escapeSet ^escapeSet! ----- Method: Json class>>initialize (in category 'class initialization') ----- initialize "Json initialize." | newEscapeArray newEscapeSet | newEscapeArray := Array new: 128. newEscapeSet := CharacterSet new. (0 to: 31), #(127) do: [ :each | newEscapeArray at: each + 1 put: '\u', (each printStringHex padded: #left to: 4 with: $0). newEscapeSet add: (Character value: each) ]. { $" -> '\"'. $\ -> '\\'. $/ -> '\/'. "A hack, so the generated JSON will always be HTML+Javascript compatible, because the generated JSON will not contain the seqence </script>." Character backspace -> '\b'. Character lf -> '\n'. Character newPage -> '\f'. Character cr -> '\r'. Character tab -> '\t'. } do: [ :each | newEscapeArray at: each key asciiValue + 1 put: each value. newEscapeSet add: each key ]. escapeArray := newEscapeArray. escapeSet := newEscapeSet! ----- Method: Json class>>mimeType (in category 'accessing') ----- mimeType ^'application/json'! ----- Method: Json class>>newWithConstructors: (in category 'instance creation') ----- newWithConstructors: aCollection | m | m := Dictionary new. aCollection do: [:each | (each isKindOf: Association) ifTrue: [m add: each] ifFalse: [m at: each name asString put: each]]. ^ self new ctorMap: m; yourself.! ----- Method: Json class>>readFrom: (in category 'instance creation') ----- readFrom: aStream ^ self new readFrom: aStream.! ----- Method: Json class>>render: (in category 'rendering') ----- render: anObject ^String streamContents: [ :stream | anObject jsonWriteOn: stream ]! ----- Method: Json class>>render:withConstructor:on: (in category 'rendering') ----- render: anObject withConstructor: aConstructorName on: aStream aStream nextPutAll: '@', aConstructorName. anObject jsonWriteOn: aStream. ! ----- Method: Json class>>renderInstanceVariables:of:on: (in category 'rendering') ----- renderInstanceVariables: aCollection of: anObject on: aStream | map | map := Dictionary new. aCollection do: [:ivarName | map at: ivarName put: (anObject instVarNamed: ivarName)]. self render: map withConstructor: anObject class name asString on: aStream! ----- Method: Json>>arrayBufferStream (in category 'private') ----- arrayBufferStream ^arrayBufferStream ifNil: [ arrayBufferStream := (Array new: 10) writeStream ]! ----- Method: Json>>consume: (in category 'private') ----- consume: aString | message | 1 to: aString size do: [ :index | (currentCharacter := stream next) == (aString at: index) ifFalse: [ message := 'Expected ', (aString at: index) printString. currentCharacter ifNil: [ self incomplete: message ] ifNotNil: [ self invalid: message ] ] ]. currentCharacter := stream next! ----- Method: Json>>ctorMap (in category 'accessing') ----- ctorMap ^ ctorMap! ----- Method: Json>>ctorMap: (in category 'accessing') ----- ctorMap: m ctorMap := m! ----- Method: Json>>dictionaryClass: (in category 'accessing') ----- dictionaryClass: aClass "This method allows you to override the default dictionary class to be created while parsing a stream. Instances of aClass are expected to understand #at:put:." dictionaryClass := aClass! ----- Method: Json>>error: (in category 'error handling') ----- error: aString (currentCharacter isNil and: [ stream atEnd ]) ifTrue: [ self incomplete: aString ] ifFalse: [ self invalid: aString ]! ----- Method: Json>>incomplete: (in category 'error handling') ----- incomplete: aString JsonIncompleteError signal: aString! ----- Method: Json>>initialize (in category 'initialize-release') ----- initialize dictionaryClass := JsonObject ! ----- Method: Json>>interpretStringEscape: (in category 'private') ----- interpretStringEscape: aCharacter aCharacter == $b ifTrue: [ ^Character backspace ]. aCharacter == $n ifTrue: [ ^Character lf ]. aCharacter == $f ifTrue: [ ^Character newPage ]. aCharacter == $r ifTrue: [ ^Character cr ]. aCharacter == $t ifTrue: [ ^Character tab ]. aCharacter == $u ifTrue: [ ^self unescapeUnicode ]. aCharacter == $" ifTrue: [ ^aCharacter ]. aCharacter == $\ ifTrue: [ ^aCharacter ]. aCharacter == $/ ifTrue: [ ^aCharacter ]. self error: 'Unexpected escaped character: ', aCharacter asString ! ----- Method: Json>>invalid: (in category 'error handling') ----- invalid: aString JsonInvalidError signal: aString! ----- Method: Json>>readAny (in category 'private') ----- readAny self skipWhitespace. currentCharacter == ${ ifTrue: [ ^self readDictionary ]. currentCharacter == $[ ifTrue: [ ^self readArray ]. currentCharacter == $" ifTrue: [ ^self readString ]. currentCharacter == $t ifTrue: [ self consume: 'rue'. ^true ]. currentCharacter == $f ifTrue: [ self consume: 'alse'. ^false ]. currentCharacter == $n ifTrue: [ self consume: 'ull'. ^nil ]. currentCharacter == $- ifTrue: [ ^self readNumber: true ]. (#($0 $1 $2 $3 $4 $5 $6 $7 $8 $9) instVarsInclude: currentCharacter) ifTrue: [ ^self readNumber: false ]. currentCharacter == $@ ifTrue: [ ^self readConstructor ]. self invalid: 'Unknown Json input'! ----- Method: Json>>readArray (in category 'private') ----- readArray | initialPosition | currentCharacter := stream next. self skipWhitespace. currentCharacter == $] ifTrue: [ currentCharacter := stream next. ^#() ]. initialPosition := self arrayBufferStream position. [ arrayBufferStream nextPut: self readAny. self skipWhitespace. currentCharacter == $] ifTrue: [ | result | result := arrayBufferStream originalContents copyFrom: initialPosition + 1 to: arrayBufferStream position. arrayBufferStream position: initialPosition. currentCharacter := stream next. ^result ]. currentCharacter == $, ifFalse: [ self error: 'Unexpected character: ', currentCharacter asString ]. currentCharacter := stream next ] repeat. ! ----- Method: Json>>readConstructor (in category 'private') ----- readConstructor ctorMap ifNil: [ ^self error: 'No constructors were declared.' ]. self resetStringBufferStream. [ (currentCharacter := stream next) == $. or: [ currentCharacter isLetter ] ] whileTrue: [ stringBufferStream nextPut: currentCharacter ]. (ctorMap at: stringBufferStream contents ifAbsent: nil) ifNotNil: [ :constructor | ^constructor constructFromJson: self readAny ]. self error: 'Unknown constructor: ', stringBufferStream contents! ----- Method: Json>>readDictionary (in category 'private') ----- readDictionary | result key commaNeeded | result := dictionaryClass new. commaNeeded := false. currentCharacter := stream next. [ self skipWhitespace. currentCharacter == $} ifTrue: [ currentCharacter := stream next. ^result ]. commaNeeded ifFalse: [ commaNeeded := true ] ifTrue: [ currentCharacter == $, ifFalse: [ self error: 'Missing comma' ]. currentCharacter := stream next. self skipWhitespace ]. currentCharacter == $" ifFalse: [ self error: 'Key in dictionary must be string' ]. key := self readString. self skipWhitespace. currentCharacter == $: ifFalse: [ self error: 'Missing colon' ]. currentCharacter := stream next. result at: key put: self readAny ] repeat! ----- Method: Json>>readFrom: (in category 'parsing') ----- readFrom: aStream | result | stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next. result := self readAny. currentCharacter ifNotNil: [ stream skip: -1 ]. "Undo prereading." ^result! ----- Method: Json>>readNumber: (in category 'private') ----- readNumber: negative | result | negative ifFalse: [ stream skip: -1 ]. result := (numberParser ifNil: [ numberParser := JsonNumberParser new on: stream; failBlock: [ self error: 'Invalid number.' ]; yourself ]) nextNumber: negative. currentCharacter := stream next. ^result ! ----- Method: Json>>readString (in category 'private') ----- readString self resetStringBufferStream. [ currentCharacter := stream next ifNil: [self incomplete: 'Unexpected end of string' ]. currentCharacter == $" ifTrue: [ currentCharacter := stream next. ^stringBufferStream contents ]. currentCharacter == $\ ifTrue: [ stringBufferStream nextPut: (self interpretStringEscape: (stream next ifNil: [ self incomplete: 'Unexpected end of string' ])) ] ifFalse: [ stringBufferStream nextPut: currentCharacter ] ] repeat! ----- Method: Json>>resetStringBufferStream (in category 'private') ----- resetStringBufferStream stringBufferStream ifNil: [ stringBufferStream := ReadWriteStream on: (String new: 64) ] ifNotNil: [ stringBufferStream resetToStart ]! ----- Method: Json>>skipWhitespace (in category 'private') ----- skipWhitespace currentCharacter ifNil: [ self incomplete: 'Input stream is empty' ]. [ currentCharacter isSeparator ] whileTrue: [ currentCharacter := stream next ifNil: [ self incomplete: 'Input stream is empty' ]. ]! ----- Method: Json>>stream (in category 'accessing') ----- stream "Answer the value of stream" ^ stream! ----- Method: Json>>stream: (in category 'private') ----- stream: aStream "For testing purposes only" stream := aStream. numberParser ifNotNil: [ numberParser on: stream ]. currentCharacter := stream next! ----- Method: Json>>unescapeUnicode (in category 'private') ----- unescapeUnicode | code digitValue lowSurrogateCode | code := 0. 1 to: 4 do: [ :index | (digitValue := (stream next ifNil: [ self incomplete: 'Unexpected end of stream' ]) digitValue) < 0 ifTrue: [ self invalid: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ code := code * 16 + digitValue ] ifFalse: [ self invalid: 'Invalid hexadecimal digit' ] ]. code < 16rD800 ifTrue: [ ^code asCharacter ]. code > 16rDFFF ifTrue: [ ^code asCharacter ]. code <= 16rDBFF ifFalse: [ self invalid: 'High surrogate value expected' ]. "Parse the escaped low surrogate" (stream next ifNil: [ self incomplete: 'Unexpected end of string' ]) == $\ ifFalse: [ self invalid: 'Escaped low surrogate expected.' ]. (stream next ifNil: [ self incomplete: 'Unexpected end of string' ]) == $u ifFalse: [ self invalid: 'Escaped low surrogate expected.' ]. lowSurrogateCode := 0. 1 to: 4 do: [ :index | (digitValue := (stream next ifNil: [ self incomplete: 'Unexpected end of stream' ]) digitValue) < 0 ifTrue: [ self invalid: 'Invalid hexadecimal digit' ]. digitValue < 16 ifTrue: [ lowSurrogateCode := lowSurrogateCode * 16 + digitValue ] ifFalse: [ self invalid: 'Invalid hexadecimal digit' ] ]. (lowSurrogateCode >= 16rDC00 and: [ lowSurrogateCode <= 16rDFFF ]) ifFalse: [ ^self invalid: 'Escaped low surrogate expected.' ]. ^((code - 16rD800 bitShift: 10) + lowSurrogateCode + '16r2400') asCharacter! Object subclass: #JsonDummyTestObject instanceVariableNames: 'a b c' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: JsonDummyTestObject class>>constructFromJson: (in category 'as yet unclassified') ----- constructFromJson: j ^ self new a: (j at: 'a'); b: (j at: 'b'); c: (j at: 'c'); yourself! ----- Method: JsonDummyTestObject>>= (in category 'as yet unclassified') ----- = other ^ other class == self class and: [ a = other a and: [ b = other b and: [ c = other c]]]! ----- Method: JsonDummyTestObject>>a (in category 'accessing') ----- a "Answer the value of a" ^ a! ----- Method: JsonDummyTestObject>>a: (in category 'accessing') ----- a: anObject "Set the value of a" a := anObject! ----- Method: JsonDummyTestObject>>b (in category 'accessing') ----- b "Answer the value of b" ^ b! ----- Method: JsonDummyTestObject>>b: (in category 'accessing') ----- b: anObject "Set the value of b" b := anObject! ----- Method: JsonDummyTestObject>>c (in category 'accessing') ----- c "Answer the value of c" ^ c! ----- Method: JsonDummyTestObject>>c: (in category 'accessing') ----- c: anObject "Set the value of c" c := anObject! ----- Method: JsonDummyTestObject>>jsonWriteOn: (in category 'as yet unclassified') ----- jsonWriteOn: s Json renderInstanceVariables: {#a. #b. #c} of: self on: s ! ----- Method: Object>>asJsonString (in category '*JSON') ----- asJsonString ^ String streamContents: [:str | self jsonWriteOn: str ]! Object subclass: #SerializedJson instanceVariableNames: 'jsonString' classVariableNames: '' poolDictionaries: '' category: 'JSON'! ----- Method: SerializedJson class>>on: (in category 'instance creation') ----- on: aString ^self new jsonString: aString; yourself! ----- Method: SerializedJson>>jsonString (in category 'accessing') ----- jsonString ^jsonString ! ----- Method: SerializedJson>>jsonString: (in category 'accessing') ----- jsonString: aString jsonString := aString ! ----- Method: SerializedJson>>jsonWriteOn: (in category 'json-writing') ----- jsonWriteOn: stream stream nextPutAll: jsonString!
1
0
0
0
← Newer
1
...
11
12
13
14
15
16
Older →
Jump to page:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Results per page:
10
25
50
100
200