Serialization

May 17, 2012 at 8:44 AM
Edited May 17, 2012 at 8:49 AM
class MyArray<T>{
 Dictionary<int, T> _Dic = new Dictionary<int, T>();
  public int Properties1 { get; set; }
  public string Properties2 { get; set; }
  public T this[int i]{
    get { return _Dic[i]; }
    set { _Dic[i] = value; }
  }
}

  MyArray<int> obj = new MyArray<int>();

  obj.Properties1 = 1234;
  obj.Properties2 = "neo";
  obj[0] = 10;
  obj[1] = 20;

  JsonConvert.SerializeObject(obj);

 
 //Serialized received:
   {"Properties1":1234,"Properties2":"neo"}

 // Expected serialized object:
   { [10,20] , "Properties1":1234,"Properties2":"neo" }

 

In browser this can be achieved by the following code:

var a = [10,20];
a.property1 = 1234;
a.property2 = "neo";

 

I havn't come across a single place where this issue is adresses.

Can someone put light into it?

May 17, 2012 at 9:57 AM

Please find the detailed illustration at http://sandeepankundu.blogspot.in/2012/05/obray-curry-dish-using-obect-and-array.html

Coordinator
May 17, 2012 at 1:08 PM

JavaScript != JSON. That's not valid JSON.

May 18, 2012 at 5:43 AM
Edited May 18, 2012 at 5:49 AM

Thanks James for the reply.

I like the way you have replied.

I agree that is not a valid JSON and the point I want to emphasize is that when the concept of Array with properties, then there is no valid JSON for it ?

ECMA also doesn't adresses this issue.

We can create such object in server and in client, but the transport DTO (i.e. the JSON) is not having the support for it, yet !

Do you agree!

If so, how to address it ? Moreover you have mentioned JSON.NET - "Serialize all the things" :)

 

Below are links detailing my findings:

http://sandeepankundu.blogspot.in/2012/05/obray-curry-dish-using-obect-and-array.html

http://sandeepankundu.blogspot.in/2012/05/obray-curry-dish-using-obect-and-array_17.html

 

/Sandeepan