What is serialization?
Serialization is the process of converting the object into stream of bytes to send it over a network or store it in a file in such a way that the object can be reconstructed again.
What is JSON?
JSON is the data interchange format, having a fixed structure. When de-serialized it return the Javascript object.
I did some Googling but can't find the how they are related? They are often mentioned together. I need to know what is going on under the hood when we say that we need to serialize the data in JSON.
JSON is basically structured text.
One example would be a client-server architecture, such as web browser - server communication is. The data from the server can be serialized from native format to JSON (text) and transferred through the network (stream of bytes) to the client (web browser). When the data gets to the client (web browser), it is deserialized to a Javascript object and handled (maybe something is displayed to the user or the UI manipulated - non-essential).
A Java example
Let's say we've a simple class in our server application, which is written in Java:
class Person {
String name;
int age;
}
and we've an instance of this class:
Person person = new Person();
person.name = "John";
person.age = 32;
We've chosen to use JSON as the data format in server-to-client communications. Converting our instance of a person to JSON looks like this:
String json = "{\"person\": {\"name\": \"John\", \"age\": 32} }";
Now we just write this String to HTTP response, from where the client reads it - to a string. The string is then deserialized to a Javascript object. Deserialization could look like this (more about JSON.parse at MDN / JSON.parse):
/* We get responseText by AJAX and it looks exactly the same that we sent from our web
server - IT IS TEXT, the variable 'json' contents in our Java application. */
var responseText = "..."
var data = JSON.parse( responseText ); // JSON.parse is supported by modern browsers
Data looks something like this if written directly as a Javascript object:
data = {
person: {
name: john,
age: 32
}
};
So, we started in the server side from a Java-object. We serialized the Java-object to a JSON-string and sent it through the network to the client. The client deserialized the JSON-string to a Javascript-object (which can be properly dealt with in language-specific way).
JSON is a format that allows you to represent your objects as a string / text, which can then be stored, sent etc.
Another example would be XML, which is also often used for serialization.
Example:
Object
var Person = new Object();
Person.FirstName = "Foo";
Person.LastName = "Foo";
Person.Age = 24;
JSON example serialization
{
Person:
{
FirstName: "Foo",
LastName: "Bar",
Age: 42
}
}
XML example serialization
<Person>
<FirstName>Foo</FirstName>
<LastName>Bar</LastName>
<Age>42</Age>
</Person>
Since you asked "what is going on under the hood": Simply explained, the serialization algorithm would iterate all properties of the object and store their names and values in a string of the requested format (JSON, XML, ...)
JSON represent collections of key/value pairs. JSON Parsers (like JSON.parse, json_decode) will map this representation to an appropriate data structure that reflects these collections. JSON Serializers (like JSON.stringify, json_encode) do the inverse, they take a data structure and map it to JSON.
For example in PHP:
$obj['a'] = 'b'; //Associative Array
$obj['c'] = 'd';
Its JSON representation would be:
'{
"a" : "b",
"c" : "d"
}'
Keys and values of the respective data structure (in this case an associative array).
In Javascript:
var obj = {
a : 'b',
c : 'd'
}
This is a Javascript object, when serialized it maps to the same JSON:
'{
"a" : "b",
"c" : "d"
}'
JSON describes a precise string format, which is why I've marked my JSON in between ' '. A string representation of your data structures can be very useful for many things, like sending them as a message.
Related
What is the best practice to pass an array of objects throught query string in REST style?
For example, the array:
examples[] = [
{
name: "foo",
value: "1"
},
{
name: "bar",
value: "2"
}
]
I thought about it:
/items?examples[0][name]=foo&examples[0][value]=1&examples[1}[name]=bar&examples[1][value]=2
Are there other ways to do this?
Upd:
I need readable URL to show it to the user in the address field. It should display state of some filters in the table, I'm not sending it to the backend.
Since you're parsing this manually in JS, you could keep the structure you have and just write a parsing function
var items = {};
location.search.split("?")[1].split("&").map((q) => {
var [token, value] = q.split("="),
[idx, key] = /\[([0-9+])\]\[(\w+)\]/g.exec(token).slice(1, 3);
if (!items[idx]){
items[idx] = {};
}
items[idx][key] = value;
})
This will yield you something with a structure like
{
"0": {
"key1": "data"
"key2": "data:
},
"1": {
"key1": "data"
"key2": "data"
}
}
If you need it to end up an array, it would be pretty easy to convert, but keeping it as an object with numeric strings for keys will prevent an error if it's not sequential.
Also, note there's no error checking or anything here, so if you're going to have query string params that aren't in that format, you'll want to test for that and handle them differently.
You shouldn't take care about how pass data for a backend, Angular do it for you.
About your example, you probably want to update or save several item. So it's not into the url that you will pass your data but into the Request Body :
this.httpService.post(yourUrl, examples, yourHttpOptions).subscribe( (response) => {
// you manage your response data
});
REST does not care how you encode information into your identifiers. You can use any scheme you want, so long as it is consistent with the production rules defined by RFC 3986.
REST cares a little bit about how you share information about creating URI, in the sense that that information should be shared in some readily standardizable form, like an HTML form, or a URI Template.
We don't, to my knowledge, have a "readily standardizable form" that describes how to transform a json array to a query string.
But... REST does allow code on demand; embedding, for example, a bunch of java script into a resource where that javascript knows how to encode the json into the URI... that is in bounds, so long as you have the code on demand itself referenced in a readily standardizable way (like we have with HTML and script tags).
In practice? urlencode the json representation and put it onto the query string directly. That will get you through until you start to discover the real requirements that your URI design needs to support (requirements like: operators needing to be able to understand the access logs).
I have json object in java, which I return to browser using obj.toString(). response.setContentType("application/json") is setting return type to json.
example:
obj = {
name: "pp",
age: 24
}
Application on tomcat: response received is
obj = {
name: "pp",
age: 24
}
but Application on glassfish: response received is
obj = {
name: "pp",
age: "24"
}
Notice that age is treated as string which is causing lot of problem.
Above is just an dummy json, I am having huge json with lot of key's and values and most of the values are Integer or floats. So How Can i get json in which data type of values are preserved.
In application as it is very old appication initialy we used xml response. While upgrading the sytem we went for json response. To reduce code change, we just converted the xml to json using function in json.jar.
JSONObject xmlJSONObj = XML.toJSONObject(returnStrXML);
Problem was here while converting xml to json. In my environment i had diffrent jar which smartly converted xml in json perserving its data types. While the json.jar converts all the values to string by default. Changing the jar solved the problem.
I have JSON that looks like this:
{
"records": [{
"skills": "",
"u_past_assignment_groups": "",
"urgency": "3",
"correlation_id": "",
"u_program_name": "",
"u_software_name": "",
"group_list": ""
}]
}
(This is a truncated version.) But the point is that it is a single element array of key,value pairs.
In native JavaScript it is a very straightforward step to use JSON.parse or eval in order to convert this JSON to an actual array of key/value pairs, but, in .Net I cannot figure out how to deserialize this JSON into anything meaningful. I have tried every permutation I can come up with of JavaScriptSerializer.Deserialize and JavaScriptSerializer.DeserializeObject. No matter what I try I continue to get some sort of error about being unable to deserialize or being unable to cast. Can someone look at this code snippet and tell me how (in native .Net) I can deserialize this into a list or array of key/value pairs?
Do I have to create my own object to store this data? I mean, I'd hate to have to do that. It looks like it should easily conform to a native .net data structure.
//process response
Stream respStream = resp.GetResponseStream();
StreamReader respReader = new StreamReader(respStream);
string response = respReader.ReadToEnd();
respStream.Close();
JavaScriptSerializer jsDes = new JavaScriptSerializer();
//***This is the part I can't figure out.***
var objResp = jsDes.DeserializeObject(response);
Dictionary<string, string>[] dicResp = (Dictionary<string, string>[])objResp;
//******************************************
pOutput.InnerText = "Incident Number: " + dicResp[0]["key"];
You didn't say if you can use third party libraries, but I think Json.NET is better (more features and performance) than the JavascriptSerializer. You could acomplish what you want by using:
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
More about Json.Net: http://james.newtonking.com/json
This should work. I pasted your example in the input.txt file.
string json = File.ReadAllText("input.txt");
JavaScriptSerializer jsDes = new JavaScriptSerializer();
Dictionary<string, object> objResp = (Dictionary<string, object>)jsDes.DeserializeObject(json);
Object[] records =(Object[])objResp["records"];
Dictionary<string,object> results= (Dictionary<string,object>) records[0];
Console.WriteLine(result["urgency"]);
I presume that it's complex to read because of your JSON structure. You have "records" as the first entry in an empty array (you open and close the JSON with {}).
Further, inside records, you have another unindexed array whose first entry is the actual dictionary that contains your key/value pairs.
I have an existing "blackbox" web service. I need to append a session ID to the end of that output so that Javascript and similar clients can resume the stateful session.
Given the output below, what is the correct syntax to append or prepend an arbitrary GUID, so that it can be properly deserialized as valid JSON?
Note This data below is perfect. If I can somehow add a "removable" bit of information, using JSON.NET the string GUID, that would be ideal.
Output from REST call
"{\"sa\":[\"BHDQ9TLPeaeVuSSgXv9bsOIVFUWbOpivMKhGki7YPLzIXEyHuxRAZhDgts2sEcBQpLBuKJZCtcmSlzWZ9iK0AAA=\",\"BAhyo7T0Wq1WBLXnyN4vo1L94rWLhCCv4DqROi+p9XHO6UeS0Gw6xh1JAKOtXBU2fA432LkNqng8cUt1eAX0bqs=\",\"BGFmyTreWY5pICAcf3itoqbfhs5brOmIDLNF3V7p7slPYdCSVhwWUT5mHD6Lb5kNi\/Qy9tracNUtVgvo3f51FrI=\",\"BMV7RIwoz+LdFgD2fq7UZ7E88KFq\/03381NDYFIKYgUKxEzuXoj6hZfSB0slX5fdaL44Lf6i\/UjDzPQt2XUG8NE=\",\"BL8BnU5WvFn7vIlKi14dWsqykNf1\/nmE55YXFGwLx9Qu3VvDblULt\/U8CXPI1vD8+wMXCRnkunXqxlsFqgghf8w=\"],\"sb\":[\"BInTtgTAn\/zkmrkporhV5DvPZRq5YWm8e\/m02oq55UfY3RxIhOplJgwLjgKMHKYDthYEBcqNNNuVbbWnbtKVAqA=\",\"BJbh5y95wHGjmAPDFNqgewnBxtqVke0sloDD2S3IdrWZ95JfP77rtXZ4lTG8g9PuTLJbl4exZUnM16260WxJ9wU=\",\"BKevE9i2J8CicXHX3elCoQPEpTOmJyGOlBskIbFMFGQFhJ5TD7N1221rhhH9HY6DsfRojmefozsQYzo7Pokp+Hg=\",\"BJbVTRyh8WwCxfR7jRXnran4td7k5+vEfM+HWxeAibneSjdMRQ1Fg6QxKLu+Zu1aPdXqD8M29kABOTAiYopVuQE=\",\"BFv3alDqjo7ckdB2vuxJ15Gur1xsgATjLe9drt\/XU9AkbN+AELCv+mF1Xy8+83L2A1p8aGxF4b7dsrMed27u1j4=\"],\"sz\":\"BF1IiqMz0KmT4gZN6euJquWFt2UmVjyOEdaX0jH8uQMAPG8DBoyneT2PJ9NQTE2xBOP9TtAb1d2O+iCojFqzkvI=\"}"
The output above comes from Chrome. I'm not sure if Chrome adds additional quotes, etc but when I debug System.String on the server, I see the same thing being sent to the WCF service.
The end-usage for this will be a Chrome and Firefox plug in
Well if I am correctly understanding:
You get JSON from a blackbox service. It contains some properties and values. You want to add a new property with some GUID and send it to browser.
If this is correct, try following:
var json=<WHAT YOU GET FROM SERVICE>;
var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);
obj.sid="this is the new session id"; //ADD NEW PROPERTY
var j=JsonConvert.SerializeObject(obj); //GET BACK JSON STRING WITH NEW PROPERTY
Of if you just want to add session id on client side (inside your plugin) the utilize JSON2 javascript library and use following code (as also suggested by Josh in comments):
var o = JSON.parse(<REST OUTPUT>);
o.sid = <YOUR SESSION ID>;
To convert back to JSON string.
var jsn = JSON.stringify(o);
There is no way to modify that particular response without breaking existing clients. If you can break existing clients, or if you are working with clients that you control, you could wrap the object in another object, setting two keys: GUID and data. For example:
var json = JsonConvert.SerializeObject(new {
data = foo,
GUID = bar,
});
Where bar is the GUID that you want to use, and foo is one of two things:
The JSON string from the response. This will result in the final object looking like so:
{
data: "{\"sa\":[\"BHDQ9TLPeaeVuSSgXv9bsOIVFUWbOpivMKhGki7YPLzIXEyHuxRAZhDgts2sEcBQpLBuKJZCtcmSlzWZ9iK0AAA=\",\"BAhyo7T0Wq1WBLXnyN4vo1L94rWLhCCv4DqROi+p9XHO6UeS0Gw6xh1JAKOtXBU2fA432LkNqng8cUt1eAX0bqs=\",\"BGFmyTreWY5pICAcf3itoqbfhs5brOmIDLNF3V7p7slPYdCSVhwWUT5mHD6Lb5kNi\/Qy9tracNUtVgvo3f51FrI=\",\"BMV7RIwoz+LdFgD2fq7UZ7E88KFq\/03381NDYFIKYgUKxEzuXoj6hZfSB0slX5fdaL44Lf6i\/UjDzPQt2XUG8NE=\",\"BL8BnU5WvFn7vIlKi14dWsqykNf1\/nmE55YXFGwLx9Qu3VvDblULt\/U8CXPI1vD8+wMXCRnkunXqxlsFqgghf8w=\"],\"sb\":[\"BInTtgTAn\/zkmrkporhV5DvPZRq5YWm8e\/m02oq55UfY3RxIhOplJgwLjgKMHKYDthYEBcqNNNuVbbWnbtKVAqA=\",\"BJbh5y95wHGjmAPDFNqgewnBxtqVke0sloDD2S3IdrWZ95JfP77rtXZ4lTG8g9PuTLJbl4exZUnM16260WxJ9wU=\",\"BKevE9i2J8CicXHX3elCoQPEpTOmJyGOlBskIbFMFGQFhJ5TD7N1221rhhH9HY6DsfRojmefozsQYzo7Pokp+Hg=\",\"BJbVTRyh8WwCxfR7jRXnran4td7k5+vEfM+HWxeAibneSjdMRQ1Fg6QxKLu+Zu1aPdXqD8M29kABOTAiYopVuQE=\",\"BFv3alDqjo7ckdB2vuxJ15Gur1xsgATjLe9drt\/XU9AkbN+AELCv+mF1Xy8+83L2A1p8aGxF4b7dsrMed27u1j4=\"],\"sz\":\"BF1IiqMz0KmT4gZN6euJquWFt2UmVjyOEdaX0jH8uQMAPG8DBoyneT2PJ9NQTE2xBOP9TtAb1d2O+iCojFqzkvI=\"}",
guid: "00000000-0000-0000-0000-000000000000"
}
And you would get at the data through two calls to JSON.parse (or the equivalent).
The deserialized object from the JSON response. This will result in the final object looking like so (most data removed for brevity sake):
{
data: {
sa: [],
sb: [],
sz: ""
},
guid: "00000000-0000-0000-0000-000000000000"
}
And you would access data through response.data.
Why any modification can break existing clients
Where the current response is an object, there are only a few ways to modify it:
Injecting a key into the object. This assumes that no client uses Object.keys() or in any way iterates the key set (e.g. for (k in obj)). While this may be true, this is an assumption.
Adding another object to the end: }, {. Doing so would require that the response be transformed into an array:
[{}, {}]
This would break any client that is assumes the response is an object.
Wrapping the current response in a surrounding object (as proposed above). This as well breaks any clients that assumes a certain structure for the response.
{data:{}, guid: ""}
I can only send string or numeric values,how to send an array?
You'll probably get a slew of answers all saying "JSON".
Here are some case specific examples. 'data' holds what you send.
var numArray = [17, 42, 23];
data = '[' + numArray + ']';
var strArray = ['foo', 'bar', 'baz'];
data = '["' + numArray.join('", "') + '"]';
For the general case, use a function that recursively encodes objects to JSON. If you really want me to, I'll post an example implementation, but it's a fun project so you might want to give it a try. If you're using a Javascript library, it might have a JSON encoder of it's own (such as jQuery's serializeArray).
There's are no built-in JSON serializers in javascript so you will need to use a library. A good one is json2.js.
Here's a sample:
// suppose you have an array of some objects:
var array = [{ prop1: 'value1', prop2: 10 }, { prop1: 'value2', prop2: 20 }];
// convert it to json string:
var jsonString = JSON.stringify(array);
// TODO: send the jsonString variable as a parameter in an ajax request
// On the server side you will need a JSON deserializer
// to convert values back to an array of objects
I can only send string or numeric values
Actually you can only send strings. When you include a number it is just turned into a string; it's up to the server-side script to parse that back into a number if it wants to.
how to send an array?
The native HTML-forms way of sending multiple values is using multiple inputs. You can reproduce this by including the same named parameter multiple times. To send [1,2,3]:
http://www.example.com/ajax.script?a=1&a=2&a=3
The same multiple-parameter-instance query string can be sent in an AJAX post request.
If you are using a higher-level framework to create the form-encoded string, it depends on the framework how it accepts multiple parameter instances. For example with jQuery you can post an object like {a: [1,2,3]}.
The other way is to forget the standard HTML form encoding and just pass all your values in whatever special encoding you like that allows you to retain datatypes and structure. Usually this would be JavaScript value literals (JSON):
http://www.example.com/ajax.script?a=%5B1%2C2%2C3%D
(that is, [1,2,3], URL-encoded.) You then need a JSON parser on the server to recreate native array values there.
You can actually send Arrays in a much cleaner way instead of trying to encoding JSON.
For example, this works
$.post("yourapp.php", { 'data[]': ["iphone", "galaxy"] });
See http://api.jquery.com/jQuery.post/ for more.