Permalink
Browse files

rewrite to support JSON more strictly and fix some bugs

  • Loading branch information...
1 parent 9648430 commit f0b8ac7a512940705ad931058326ad1b8e7233a0 @lm committed Feb 16, 2012
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,6 +1,6 @@
Error subclass: JsonSyntaxError [
- <category: 'JSON'>
+ <category: 'json'>
<comment: 'Class Json signals instances of me when an input stream contains invalid JSON input.'>
]
@@ -1,12 +1,13 @@
Association extend [
- jsonWriteOn: aStream [
- <category: '*JSON-writing'>
- self key asString jsonWriteOn: aStream.
- aStream
- nextPut: $:;
- space.
- self value jsonWriteOn: aStream
+ storeAsJsonOn: aStream [
+ <category: 'json-storing'>
+
+ self key asString storeAsJsonOn: aStream.
+ aStream
+ nextPut: $:;
+ space.
+ self value storeAsJsonOn: aStream
]
]
@@ -0,0 +1,12 @@
+CharacterArray extend [
+
+ storeAsJsonOn: aStream [
+ <category: 'json-storing'>
+
+ aStream nextPut: $".
+ self asString do: [ :char | Json putEscapedCharacter: char on: aStream].
+ aStream nextPut: $".
+ ]
+
+]
+
@@ -1,13 +1,13 @@
Collection extend [
- jsonWriteOn: aStream [
- "By default, use array braces"
-
- <category: '*json'>
- aStream nextPut: $[.
- self do: [:each | each jsonWriteOn: aStream]
- separatedBy: [aStream nextPut: $,].
- aStream nextPut: $]
+ storeAsJsonOn: aStream [
+ "By default, use array braces"
+
+ <category: 'json-storing'>
+
+ aStream nextPut: $[.
+ self do: [:each | each storeAsJsonOn: aStream] separatedBy: [aStream nextPutAll: ', '].
+ aStream nextPut: $]
]
]
@@ -1,17 +1,20 @@
Dictionary extend [
- jsonWriteOn: aStream [
- <category: '*JSON-writing'>
- | 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: $}
+ storeAsJsonOn: aStream [
+ <category: 'json-storing'>
+
+ | isFirst |
+
+ isFirst := true.
+ aStream nextPut: ${.
+
+ self associationsDo: [ :association |
+ isFirst ifFalse: [
+ isFirst := false.
+ aStream nextPutAll: ', '].
+ association storeAsJsonOn: aStream].
+
+ aStream nextPut: $}
]
]
View
@@ -1,8 +1,9 @@
False extend [
- jsonWriteOn: aStream [
- <category: '*JSON-writing'>
- aStream nextPutAll: 'false'
+ storeAsJsonOn: aStream [
+ <category: 'json-storing'>
+
+ aStream nextPutAll: 'false'
]
]
@@ -1,8 +1,9 @@
Integer extend [
- jsonWriteOn: aWriteStream [
- <category: '*JSON-writing'>
- ^self printOn: aWriteStream base: 10
+ storeAsJsonOn: aWriteStream [
+ <category: 'json-storing'>
+
+ ^self printOn: aWriteStream base: 10
]
]
View
@@ -1,8 +1,41 @@
Number extend [
- jsonWriteOn: aWriteStream [
- <category: '*JSON-writing'>
- self printOn: aWriteStream base: 10
+ storeAsJsonOn: aWriteStream [
+ <category: 'json-storing'>
+
+ self printOn: aWriteStream
+ ]
+
+]
+
+Float extend [
+
+ storeAsJsonOn: aWriteStream [
+ <category: 'json-storing'>
+
+ self isFinite
+ ifTrue: [super storeAsJsonOn: aWriteStream]
+ ifFalse: [aWriteStream nextPutAll: 'null'].
+ ]
+
+]
+
+Fraction extend [
+
+ storeAsJsonOn: aWriteStream [
+ <category: 'json-storing'>
+
+ self asFloat storeAsJsonOn: aWriteStream.
+ ]
+
+]
+
+ScaledDecimal extend [
+
+ storeAsJsonOn: aWriteStream [
+ <category: 'json-storing'>
+
+ self asFloat storeAsJsonOn: aWriteStream.
]
]
@@ -1,8 +1,9 @@
Object extend [
asJsonString [
- <category: '*JSON'>
- ^String streamContents: [:str | self jsonWriteOn: str]
+ <category: '*JSON'>
+
+ ^String streamContents: [ :string | self storeAsJsonOn: string]
]
]
View
@@ -1,15 +0,0 @@
-String extend [
-
- jsonWriteOn: aStream [
- <category: '*JSON-writing'>
- | replacement |
- aStream nextPut: $".
- self do:
- [:ch |
- replacement := (Json escapeForCharacter: ch) asString.
- aStream nextPutAll: replacement].
- aStream nextPut: $"
- ]
-
-]
-
View
@@ -1,8 +1,9 @@
True extend [
- jsonWriteOn: aStream [
- <category: '*JSON-writing'>
- aStream nextPutAll: 'true'
+ storeAsJsonOn: aStream [
+ <category: 'json-storing'>
+
+ aStream nextPutAll: 'true'
]
]
@@ -1,8 +1,9 @@
UndefinedObject extend [
- jsonWriteOn: aWriteStream [
- <category: '*JSON-writing'>
- aWriteStream nextPutAll: 'null'
+ storeAsJsonOn: aWriteStream [
+ <category: 'json-storing'>
+
+ aWriteStream nextPutAll: 'null'
]
]
@@ -1,8 +1,9 @@
WriteStream extend [
jsonPrint: anObject [
- <category: '*json-printing'>
- anObject jsonWriteOn: self
+ <category: 'json-storing'>
+
+ anObject storeAsJsonOn: self
]
]
View
8 README
@@ -1,8 +0,0 @@
-This is a Gnu Smalltalk port of Squeak JSON which is used for the SCouchDB
-driver (www.squeaksource.com/SCouchDB).
-
-It uses Gitocello to track Squeak JSON in the squeakJson branch. I try to
-keep the code in Squeak mostly compatible. Gnu Smalltalk specific changes
-that cannot be easily incorporated into the Squeak package are in the master
-branch.
-
View
@@ -14,7 +14,7 @@
<filein>Json/JsonSyntaxError.st</filein>
<filein>Json/extensions/Number.st</filein>
<filein>Json/extensions/Object.st</filein>
- <filein>Json/extensions/String.st</filein>
+ <filein>Json/extensions/CharacterArray.st</filein>
<filein>Json/extensions/True.st</filein>
<filein>Json/extensions/UndefinedObject.st</filein>
<filein>Json/extensions/WriteStream.st</filein>
View
@@ -0,0 +1,2 @@
+JSON implementation for GNU Smalltalk, originally based on Squeak
+implementation www.squeaksource.com/SCouchDB.
Oops, something went wrong.

0 comments on commit f0b8ac7

Please sign in to comment.