Serializing and deserializing data - JavaScript - javascript

I need to perform a lot of serializing and de-serializing in an application.
The data is POST parameters and Cookie headers.
Post data example:
Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21
Set-Cookie data example:
ASP.NET_SessionId=esur3bqqgrmnci45um4xegye; path=/
Does anyone have any fast libraries to suggest or efficient ways to perform these operations?

Consider using a JSON stringify method:
http://zumbrunn.com/mochazone/JSON.stringify+and+JSON.parse/
JSON is a fairly universal serialization format that you can parse in ASP.Net. Basically it converts a JS object into a string of text that represents the object itself (as if you were to type the object declaration out literally).

Related

Creating Binary Data Structures in Javascript

I'm building a web app, and need to be able to encode user-generated data as concisely as possible before transmitting it to my server. In the past I've used Flash, and it had a very neat system where for any class that you want to serialize, you could write a pair of functions that would describe exactly how to serialize the data. For example:
out.writeShort(session);
out.writeUnsignedInt(itemID);
out.writeObject(arbitraryData);
out.writeShort(score);
You would have to write an equivalent function to read bytes from the serialized data and build the class from it.
Once data is serialized it could be encoded into a Base64 string for safe network transmission to the server.
I can't figure out how to do this in Javascript? JSON is nice and easy but it's incredibly wasteful, sending all object key/value pairs, and unless I'm mistaken everything is encoded as a string? So the value false is encoded as the string "false"?
Any advice on how to implement this in Javascript would be greatly appreciated! Use of libraries is fine so long as they work both on Node and in browser.
Look at this answer. You can use BSON format (Binary JSON) and it doesn't have those features of JSON you mentioned.

Is there any performance overhead with sending JSON objects instead of stringified JSON through node js APIs?

While writing node js APIs, we can send plain JSON objects as params (body params), I think there must be some additional overhead for the formatting instead what if I stringify the JSON while sending to API and will parse back to original JSON while processing it.
Can you guys please suggest if this approach gives any performance advantage?
JSON is "JavaScript Object Notation" — i.e. some string format to serialize Object, to save, transfer and restore Object to/via/from a string. JSON.stringify() does not mean you stringify JSON: JSON here is not an object of stringifying, but a namespace like in Math.sqrt(). You cannot transfer Object as is via HTTP or IPC: for this purpose, you have a text serialization format, i.e. JSON.

JSON to String on client side for ASP.NET Script Service?

From this site, I've learned that ASP.NET script services accepting JSON actually require them to be serialized JSON strings (see: "JSON, objects, and strings: oh my!" section of the link). Is there a quick and easy way to serialize them for ASP.NET AJAX consumption on the client side instead of trying to manually convert a bunch of existing objects to JSON-looking strings?
Thanks in advance!
You can use JSON.stringify() to serialize client-side objects for consumption in ASP.NET's Script Services.
Using that approach, you can map client-side objects to server-side objects very easily. ASP.NET will automatically handle converting the JSON to objects (or even collections of objects) for you.
The article writer is confusing Javascript objects with JSON strings. There is no such thing as a "JSON object".
Naturally if you try to send an object to a web service, it has to be serialised, as the request data can only contain text, not objects. The standard way of serialising data to be posted is URL encoding it, so that is what jQuery does.
There is no JSON serialisation built into Javascript or jQuery. You would have to do the serialising yourself or find a library that does it. Here are some options: Serializing to JSON in jQuery
Also, the data sent in the example is not valid JSON. It looks like this:
"{'fname':'dave', 'lname':'ward'}"
To be valid JSON it should look like this:
'{"fname":"dave", "lname":"ward"}'

Working with identifiers as arrays and sending to MVC controller

I have some identifiers on my page. Named id[0], id[1] all the way to id[20] and sometimes more. I need to send these to an MVC controller and I would like to package them up as one object and unpack at the controller. Can someone tell me if this is possible, my knowledge of javascript is just basic so I'd really appreciate advice on which way to go. For example can I use JSON or serialize. btw I'm using jQuery.
Gordon
You can send your data in JSON.
See > ASP.NET MVC How to pass JSON object from View to Controller as Parameter
If you already have the id array (id[0], id1 , ... id[20]) you can convert it to JSON string with respect of JSON.stringify(id) where JSON.stringify function defined in the json2.js.
On the server side you can use for example Deserialize method of the JavaScriptSerializer to convert the data to List<T> where T is type of id[i] (for example string).
How to send data with respect of jQuery.ajax you already know from another answer.

Should I use JSON or AJAX for response data?

Why JSON? I have done some tests today, and the request time for both JSON, or a normal AJAX request was the same. In the "normal request" I have returned the complete text+html tags, in the JSON request, logically I returned a "json return type" and I have created the HTML with client-side JavaScript.
I don't get it, why are the big sites (Google Reader etc), or even small sites using JSON? Or do I not understand when I should use JSON?
You are perhaps a little confused.
JSON and AJAX are not an either-or choice.
JSON and XML is a choice.
JSON and AJAX are distinct and largely unrelated, although AJAX often uses JSON, it can just easily use XML, HTML or plain text.
Or are you referring to the X in AJAX (XML)? If so, the arguments for JSON are basically:
JSON has a smaller payload than equivalent XML; and
JSON is easier to deal with in Javascript (compare eval'ing a JSON object to walking an XML fragment).
Other than that, it's largely personal preference.
JSON is just a data-interchange format. It describes in what way the data is represented during transmission. You can not replace Ajax with JSON.
Ajax stands for Asynchronous JavaScript and XML, but when using JSON you could say that you're using AJAJ (Asynchronous JavaScript and JSON).
Maybe you are thinking of the jQuery methods $.getJSON() and $.get()?
The difference is that $.getJSON() automatically assumes that it's JSON data, while $.get() will just fetch the data as plain text.
When using $.getJSON() you're also able to fetch data between domains.

Categories

Resources