Permalink
Browse files

JsonObject now HAS a dictionary, and IS none

  • Loading branch information...
1 parent 4675fa9 commit ccd6e1a3bbeb471cf6bf9419c887ce9b4d856ef1 @timfel timfel committed Jun 18, 2010
Showing with 77 additions and 13 deletions.
  1. +77 −13 JSON/JsonObject.st
View
@@ -1,13 +1,16 @@
-Dictionary subclass: JsonObject [
+Object subclass: JsonObject [
+ | dictionary |
<category: 'JSON'>
<comment: nil>
JsonObject class >> fromAssociations: collectionOfAssociations [
| result |
- result := self new.
+ result := Dictionary new.
collectionOfAssociations do: [:each | result at: each key put: each value].
- ^result
+ ^(self new)
+ dictionary: result;
+ yourself
]
at: aKey [
@@ -17,29 +20,90 @@ Dictionary subclass: JsonObject [
^self at: aKey ifAbsent: [nil]
]
+ dictionary: aDictionary [
+ <category: 'accessing'>
+ aDictionary ifNotNil: [:foo | dictionary := aDictionary]
+ ]
+
+ dictionaryEquals: aDictionary [
+ <category: 'accessing'>
+ ^dictionary = aDictionary
+ ]
+
+ = aJsonObject [
+ <category: 'overrides-to-dictionary'>
+ ^aJsonObject dictionaryEquals: dictionary
+ ]
+
+ at: aKey put: aValue [
+ "override cause object defines this"
+
+ <category: 'overrides-to-dictionary'>
+ ^dictionary at: aKey put: aValue
+ ]
+
+ bindingOf: varName [
+ <category: 'overrides-to-dictionary'>
+ ^dictionary bindingOf: varName
+ ]
+
+ flattenOnStream: aStream [
+ <category: 'overrides-to-dictionary'>
+ ^dictionary flattenOnStream: aStream
+ ]
+
+ isDictionary [
+ <category: 'overrides-to-dictionary'>
+ ^true
+ ]
+
+ javascriptOn: aStream [
+ <category: 'overrides-to-dictionary'>
+ dictionary javascriptOn: aStream
+ ]
+
+ jsonOn: aStream [
+ <category: 'overrides-to-dictionary'>
+ dictionary jsonOn: aStream
+ ]
+
name [
"override 'cause Object defines this"
- <category: 'accessing'>
+ <category: 'overrides-to-dictionary'>
^self at: 'name'
]
+ storeOn: aStream [
+ <category: 'overrides-to-dictionary'>
+ ^dictionary storeOn: aStream
+ ]
+
value [
"override 'cause Object defines this"
- <category: 'accessing'>
+ <category: 'overrides-to-dictionary'>
^self at: 'value'
]
doesNotUnderstand: aMessage [
- | key |
- key := aMessage selector.
- key isUnary ifTrue: [^self at: key ifAbsent: [nil]].
- ^(key isKeyword and: [(key occurrencesOf: $:) = 1])
- ifTrue:
- [key := key allButLast asSymbol.
- self at: key put: aMessage arguments first]
- ifFalse: [super doesNotUnderstand: aMessage]
+ | key result |
+
+ [result := aMessage sendTo: dictionary.
+ ^result] on: MessageNotUnderstood
+ do:
+ [key := aMessage selector.
+ key isUnary ifTrue: [^self at: key].
+ ^(key isKeyword and: [(key occurrencesOf: $:) = 1])
+ ifTrue:
+ [key := key allButLast asSymbol.
+ dictionary at: key put: aMessage arguments first]
+ ifFalse: [super doesNotUnderstand: aMessage]]
+ ]
+
+ initialize [
+ super initialize.
+ dictionary := Dictionary new
]
]

0 comments on commit ccd6e1a

Please sign in to comment.