Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
is json possible?

lacaca
Registered User
Quote
2019-01-28 10:04:49

Found this old entry: https://www.ambiera.com/forum.php?t=4929 that hint that encoding and decoding json might be possible. (maybe only in the webgl target)
But both mentioned urls are not working anymore.
Is JSON possible and if so how?

var myObj = { "name":"John", "age":31, "city":"New York" };
var myJSON = JSON.stringify(myObj);
alert('Hello World!'+myObj.name);


is not the way to go, as JSON is not defined.
https://www.allspark.com/forums/...


niko
Moderator
Quote
2019-01-28 13:14:01

No, the current JavaScript engine doesn't include the JSON interface, unfortunately. Maybe you can find an implementation for it on the web and use that.


xanimalkingx
Registered User
Quote
2019-02-10 19:36:42

lacaca wrote:
Found this old entry: https://www.ambiera.com/forum.php?t=4929 that hint that encoding and decoding json might be possible. (maybe only in the webgl target)
But both mentioned urls are not working anymore.
Is JSON possible and if so how?

var myObj = { "name":"John", "age":31, "city":"New York" };
var myJSON = JSON.stringify(myObj);
alert('Hello World!'+myObj.name);


is not the way to go, as JSON is not defined.

No, because the Coppercube JavaScript engine is running via ES5 standards. ES6 and higher has JSON and many more things like Math.clamp.


crusly
Registered User
Quote
2019-11-17 13:24:34

Hi,

I read your post and did try to figure out how to do it...

I wrote this little code... hope it can help you.



*** OLD VERSION CODE DELETED ***
shared a better solution in the next --> POST REPLY (see down)




crusly
Registered User
Quote
2019-12-01 01:07:51

*** UPDATED ***

Here the COMPLETE RECURSIVE SOLUTION (based on Mozilla documentation).



// ----------------------------------------------------------------------
//
// ******************** Recursive JSON* Mozilla Sample *************************
//
// * Works for all standar data (is recursive): string, number, boolean, Array, Object, null.
// * Tested on CCB 6.3
//

var mozilla_json = function () {
this.parse = function (text) {
return (eval('(' + text + ')'));
}
this.stringify = function (object) {
if (object instanceof Object) {
var text = "";
if (object.constructor === Array) {
for (var i = 0; i < object.length; text += this.stringify(object[i]) + ",", i++);
return "[" + text.substr(0, text.length - 1) + "]";
}
if (object.toString !== Object.prototype.toString) {
return "\"" + object.toString().replace(/"/g, "\\$&") + "\"";
}
for (var prop in object) {
text += "\"" + prop.replace(/"/g, "\\$&") + "\":" + this.stringify(object[prop]) + ",";
}
return "{" + text.substr(0, text.length - 1) + "}";
}
return typeof object === "string" ? "\"" + object.replace(/"/g, "\\$&") + "\"" : String(object);
}
}

var JSON = new mozilla_json();

// ----------------------------------------------------------------------
// --> TEST
// ----------------------------------------------------------------------

var someTextObj = '{"prop1":"text-value-one","prop2":"text-value-two","prop3":3,';
someTextObj += '"prop4":4.4444,"prop5":true,"prop6":false,"prop7":null,"prop8":[1,2,3],';
someTextObj += '"prop9":["one","two","three"], "prop10":{"subp1":"hello", "subp2":"world"},';
someTextObj += '"prop11":Infinity,"prop12":"\\n" }';

print();
print(" --> Test 1: use it to restore data from disk or stream ( JSON.parse ) --------------\n");

var myObj = JSON.parse(someTextObj);
print("TEST OK... \"someTextObj\" parsed on ( \"myObj\" ).\n\n\n");

print(" --> Test 2: Get Fun..!, and access object variables: --------------------------------\n");

for (i in myObj) {
print(" myObj." + i + " = " + myObj[i] + " --> Type: " + typeof (myObj[i]));
}
print("\n");
print(" myObj.prop8[0] = " + myObj.prop8[0] + " --> Type: " + typeof (myObj.prop8[0]) + " --> (value in Array)");
print(" myObj.prop9[1] = " + myObj.prop9[1] + " --> Type: " + typeof (myObj.prop9[1]) + " --> (value in Array)");
print(" myObj.prop10.subp1 = " + myObj.prop10.subp1 + " --> Type: " + typeof (myObj.prop10.subp1) + " --> (prop in Object)");
print(" myObj.prop10.subp2 = " + myObj.prop10.subp2 + " --> Type: " + typeof (myObj.prop10.subp2) + " --> (prop in Object)");

print("\n\n");

print(" --> Test 3: use it to store or stream ( JSON.stringify ) -----------------------------\n");

print(JSON.stringify(myObj) + "\n\n");
print();





crusly
Registered User
Quote
2019-12-01 01:15:38

Here the output on the CopperCube "Message Log" Window:




--> Test 1: use it to restore data from disk or stream ( JSON.parse ) --------------

TEST OK... "someTextObj" parsed on ( "myObj" ).



--> Test 2: Get Fun..!, and access object variables: --------------------------------

myObj.prop1 = text-value-one --> Type: string
myObj.prop2 = text-value-two --> Type: string
myObj.prop3 = 3 --> Type: number
myObj.prop4 = 4.4444 --> Type: number
myObj.prop5 = true --> Type: boolean
myObj.prop6 = false --> Type: boolean
myObj.prop7 = null --> Type: object
myObj.prop8 = 1,2,3 --> Type: object
myObj.prop9 = one,two,three --> Type: object
myObj.prop10 = [object Object] --> Type: object
myObj.prop11 = Infinity --> Type: number
myObj.prop12 =
--> Type: string


myObj.prop8[0] = 1 --> Type: number --> (value in Array)
myObj.prop9[1] = two --> Type: string --> (value in Array)
myObj.prop10.subp1 = hello --> Type: string --> (prop in Object)
myObj.prop10.subp2 = world --> Type: string --> (prop in Object)



--> Test 3: use it to store or stream ( JSON.stringify ) -----------------------------

{"prop1":"text-value-one","prop2":"text-value-two","prop3":3,"prop4":4.4444,"prop5":true,"prop6":false,"prop7":null,"prop8":[1,2,3],"prop9":["one","two","three"],"prop10":{"subp1":"hello","subp2":"world"},"prop11":Infinity,"prop12":"
"}




Robo
Guest
Quote
2019-12-03 15:13:45

That's awesome - cheers !

always wanted to know how to do that so I can use sort & filter sections of data ...


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Interna?ional" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact