Javascript object serialize readable - javascript

How do I serialize a javascript object.
I've tried JSON.stringify, which is great, but it serializes into JSON, with quotes around all the keys.
I'm looking for something that's not going to give me quotes around keys and still produce something readable, with padding.
Thanks.

I think you should look into YAML. We here use YAML, but let me look into it more, to see whether I can cite you some example.
Go through this stackoverflow discussion: Pure Javascript YAML library that supports both dump and load?

Related

How to decompile this?

I am decompiling a google chrome extension, because it seems suspicious.
The extension was written in javascript, but can somebody tell me exactly what symbols like this are, and how to "translate" them back to normal strings?
"\x63\x68\x61\x72\x43\x6F\x64\x65"
Jsbin of the full file:
http://jsbin.com/OnEviRa/1/
the code seems to be using an array to hide all the strings, and they seemed to have changed variable names to hide their meaning.
I can't give you a good automated way to change variable names to something meaningful, but the strings are easy.
you can evaluate the array, then use a regex to replace all occurences of _0x13d2[number] with "evaluated result".
here is a fiddle of it

Where to start with JSON? (Closed)

I'm trying to get started with JSON. I've set the link to the JSON.js file using a script link, and I've set my objects using JSON, but it returns no results at all when I try to refer to the object. The JSON doesn't seem to be working at all. If anyone could point me in the right direction it'd be appreciated. I've looked all over the internet and haven't found much to help me.
Thanks guys. The JSON website helped me figure it out
Get Firefox, get Firebug, learn to use it to see what javascript is being loaded, and where the errors are.
Oh, and post some code.
Take a look-see at this
http://json.org/
basically you need to understand that json is a way to stream javascript object literals and arrays from a server to the client (and vice-versa). Open up firebug/webkit and in the console try
var obj = JSON.parse('{"test": 1}')
and you will see that obj is an object literal with a test property.
edit -- note that the link I provided mentions that json is a "is a lightweight data-interchange format" -- so its does not need to be javascript specific. But I think in practice you will get the most mileage using json in conjunction with javascript.
Here's a couple of links that might help:
http://secretgeek.net/json_3mins.asp
http://www.hunlock.com/blogs/Mastering_JSON_(_JavaScript_Object_Notation_)

json: how to get the json-string into a javascript object

i am converting my ajax code from xml to json, but i am missing something basic here:
when i receive the json-string on the client-side, what is the recommended way to convert it into a javascript object.
for example i get this string:
{"connectionid":12345}
and i would like to do something like this:
alert(xmlhttp.responseText.connectionid);
thanks!
Use JSON.parse(), or eval(), if you like to live dangerously (or fully trust where your JSON comes from).
If you happen to be using jQuery, you get $.parseJSON().
Most browsers (the recent ones at least.. not IE7) have a native JSON object that you can use to parse and stringify JSON.
alert(JSON.parse(xmlhttp.responseText).connectionid);
In browsers that don't support the JSON object, you can either use a JSON parser from JSON.org or use eval(), however eval() is quite dangerous and i definitly don't advise you to use it.
Call eval on the response text.
var response = eval(xmlHttp.responseText);
alert(response.connectionId);
you could use eval
check this out : http://www.json.org/js.html
edit - oops, others typed faster :(

Converting XML-RPC to JSON in JavaScript

Can anyone recommend a lightweight JavaScript XML-RPC library?
After researching this a while ago, I couldn't find anything I was comfortable with, so I kinda ended up writing my own.
However, maybe that was stupid, as there must be something suitable out there!?
My own pseudo-library is mainly missing a way to turn an XML-RPC response into JSON - or rather, converting the respective responseXML to a JavaScript object (converting the data types as needed).
This isn't hard to do, but why reinvent the wheel...
Any help would be greatly appreciated!
I know this two libraries, that you can basically use to convert your XML-RPC responses to JSON:
XML to JSON Converter
xml2json.js
This seems to do what you (and I) want:
http://kuriositaet.de/javascript/jsxmlrpc.html
http://kuriositaet.de/javascript/xmlrpc.html
http://sourceforge.net/projects/jsxmlrpc/
Another small XML to JSON (and vice versa) JavaScript library is here -> http://code.google.com/p/x2js/
I could use this too. Generic XML to Json is rather unconvenient for XML-RPC.

A working Json library for Javascript?

I went to http://www.json.org/js.html and downloaded the json2.js, thinking i'd be fine, afterall that site is on the top in a google search for 'json javascript' - also they have this really cool url :)
So i've been working a bit with it and it seemed fine, but now i start running into trouble with it - it simply won't parse certain stuff i encode with Newtonsoft's JSON .NET serializer. Ok so perhaps the .net seralizer messes up? Not how i see it - it produces a fine javascript string that looks like perfect json.
The problem comes when it has to encode a single quote ' and perhaps double quotes ".
Take a look at these examples (only parts of the full string)
{"Id":10651,"Text":"\'69"}
{"Id":184,"Text":"13\""}
Am I missing something? it's part of a bigger string and all put in a javascript variable like this
var jsonObject = '[{"Id":46,"Type":2,.....................
I'm thinking it has to escape the singlequote in the string to avoid conflicting with my wrapping of the string in single quotes, and escape the double quote to avoid conflicting with the json format?
So either i'm doing something wrong or the json2.js is not doing it right? Or yeah perhaps the .net json is messing up - i'm kinda thinking i'm messing it up, but i've been trying to do all sorts of stuff to help with the parsing like escaping/unescaping etc. before the serializing/deserializing.
Ok i solved the problem. Actually the hint Joel gave me in the comment on my question to try and eval it instead lead me to thinking i can trust this json i'm trying to parse/eval, and since i know it at the time of building the page, why not hardcode it into the webpage AS an object - no escaping of quotes or anything and no evaluating and best of all - no strings :P
So thanks to both you Joel and you torial :)
This may be a lead...
http://binnyva.blogspot.com/2006/10/invalid-json.html
And if you are serializing, perhaps protect yourself by serializing to ' and from '.

Categories

Resources