Browse Source

Object>>asJSON now converts to a JSON object and #asJSONString to a JSON
string (like the old #asJSON method)

Nicolas Petton 13 years ago
parent
commit
a27cb9e1f9
3 changed files with 37 additions and 6 deletions
  1. 12 1
      js/Kernel.deploy.js
  2. 20 4
      js/Kernel.js
  3. 5 1
      st/Kernel.st

+ 12 - 1
js/Kernel.deploy.js

@@ -484,7 +484,7 @@ smalltalk.method({
 selector: 'asJSON',
 fn: function (){
 var self=this;
-return JSON.stringify(self);
+return smalltalk.send((smalltalk.JSON || JSON), "_parse_", [smalltalk.send(self, "_asJSONString", [])]);
 return self;}
 }),
 smalltalk.Object);
@@ -568,6 +568,17 @@ return self;}
 }),
 smalltalk.Object);
 
+smalltalk.addMethod(
+'_asJSONString',
+smalltalk.method({
+selector: 'asJSONString',
+fn: function (){
+var self=this;
+return smalltalk.send((smalltalk.JSON || JSON), "_stringify_", [self]);
+return self;}
+}),
+smalltalk.Object);
+
 
 smalltalk.addMethod(
 '_initialize',

+ 20 - 4
js/Kernel.js

@@ -695,12 +695,12 @@ selector: unescape('asJSON'),
 category: 'converting',
 fn: function (){
 var self=this;
-return JSON.stringify(self);
+return smalltalk.send((smalltalk.JSON || JSON), "_parse_", [smalltalk.send(self, "_asJSONString", [])]);
 return self;},
 args: [],
-source: unescape('asJSON%0A%09%3Creturn%20JSON.stringify%28self%29%3E'),
-messageSends: [],
-referencedClasses: []
+source: unescape('asJSON%0A%09%5EJSON%20parse%3A%20self%20asJSONString'),
+messageSends: ["parse:", "asJSONString"],
+referencedClasses: ["JSON"]
 }),
 smalltalk.Object);
 
@@ -818,6 +818,22 @@ referencedClasses: []
 }),
 smalltalk.Object);
 
+smalltalk.addMethod(
+unescape('_asJSONString'),
+smalltalk.method({
+selector: unescape('asJSONString'),
+category: 'converting',
+fn: function (){
+var self=this;
+return smalltalk.send((smalltalk.JSON || JSON), "_stringify_", [self]);
+return self;},
+args: [],
+source: unescape('asJSONString%0A%09%5EJSON%20stringify%3A%20self'),
+messageSends: ["stringify:"],
+referencedClasses: ["JSON"]
+}),
+smalltalk.Object);
+
 
 smalltalk.addMethod(
 unescape('_initialize'),

+ 5 - 1
st/Kernel.st

@@ -70,7 +70,11 @@ asJavascript
 !
 
 asJSON
-	<return JSON.stringify(self)>
+	^JSON parse: self asJSONString
+!
+
+asJSONString
+	^JSON stringify: self
 ! !
 
 !Object methodsFor: 'copying'!