I have a jquery mobile app that makes ajax calls to my asp.net web service to get data from a database. I create an object in C# in the webservice which is an array of the elements I need to pass to the javascript client. Now if asp.net uses json, does that
imply that the length of my class name members affects the number of bytes sent over the wire? For example, will example 1 below result in more efficient networking than example 2?
example 1:
public class TestClass
{
public string a;
public int b;
public int c;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<TestClass> FetchData()
{
var stuff = new List<TestClass>();
stuff.Add(new TestClass() { a = "item1", b= 1982, c= 456 });
stuff.Add(new TestClass() { a = "item2", b= 3456, c= 123 });
return (stuff);
}
example 2:
public class TestClass
{
public string areallyreallylongname1;
public int areallyreallylongname2;
public int areallyreallylongname3;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<TestClass> FetchData()
{
var stuff = new List<TestClass>();
stuff.Add(new TestClass() { areallyreallylongname1 = "item1", areallyreallylongname2= 1982, areallyreallylongname3= 456 });
stuff.Add(new TestClass() { areallyreallylongname1 = "item2", areallyreallylongname3= 3456, areallyreallylongname3= 123 });
return (stuff);
}
Over on the javascript side, I end up with an object array in either case. But during the transmission
from server to client, will the respective json strings be something like this:
Yep. Example 1 has less data to move across the wire and it will be more efficient. That said, the data in your example is so small you would not notice the difference. It would really only be noticable if you had lots and lots of transactions, or much
larger transactions.
OK, thanks! Too bad there isn't some optimization going on under the covers. This was just a toy sample to illustrate the problem, the real app will have much larger transactions. Will keep the member names short! Cheers ...
AnOttawan
Member
1 Points
2 Posts
do class member names matter in json serialization?
Aug 07, 2012 08:29 PM|LINK
I have a jquery mobile app that makes ajax calls to my asp.net web service to get data from a database. I create an object in C# in the webservice which is an array of the elements I need to pass to the javascript client. Now if asp.net uses json, does that imply that the length of my class name members affects the number of bytes sent over the wire? For example, will example 1 below result in more efficient networking than example 2?
example 1:
public class TestClass { public string a; public int b; public int c; } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<TestClass> FetchData() { var stuff = new List<TestClass>(); stuff.Add(new TestClass() { a = "item1", b= 1982, c= 456 }); stuff.Add(new TestClass() { a = "item2", b= 3456, c= 123 }); return (stuff); }example 2:
public class TestClass { public string areallyreallylongname1; public int areallyreallylongname2; public int areallyreallylongname3; } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<TestClass> FetchData() { var stuff = new List<TestClass>(); stuff.Add(new TestClass() { areallyreallylongname1 = "item1", areallyreallylongname2= 1982, areallyreallylongname3= 456 }); stuff.Add(new TestClass() { areallyreallylongname1 = "item2", areallyreallylongname3= 3456, areallyreallylongname3= 123 }); return (stuff); }Over on the javascript side, I end up with an object array in either case. But during the transmission from server to client, will the respective json strings be something like this:
ex1 : [{ 'a': 'item1', 'b': '1982', 'c':'456' }, { 'a': 'item2', 'b': '3456','c':'123'}]
ex2 : [{ 'areallyreallylongname1': 'item1', 'areallyreallylongname2': '1982', 'areallyreallylongname3':'456' }, { 'areallyreallylongname1': 'item2', 'areallyreallylongname2': '3456','areallyreallylongname3':'123'}]
Thanks!
MattsDotNetU...
Contributor
3178 Points
515 Posts
Re: do class member names matter in json serialization?
Aug 07, 2012 08:32 PM|LINK
Yep. Example 1 has less data to move across the wire and it will be more efficient. That said, the data in your example is so small you would not notice the difference. It would really only be noticable if you had lots and lots of transactions, or much larger transactions.
AnOttawan
Member
1 Points
2 Posts
Re: do class member names matter in json serialization?
Aug 07, 2012 09:07 PM|LINK
OK, thanks! Too bad there isn't some optimization going on under the covers. This was just a toy sample to illustrate the problem, the real app will have much larger transactions. Will keep the member names short! Cheers ...