469,646 Members | 1,596 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

JSON deserialization using DataContractJsonSerializer

Hi,

I am using DataContractJsonSerializer to deserialize JSON string in C#
objects but I am having a problem.

Suppose I have a class:

class Item
{
public ItemId Id { get; set; }
}

class ItemId
{
public int Value { get; set; }
}
The JSON string for a Item object looks like this:

{ "Id": 100 }

The deserialization does not work since it expects that Id member is of type
int.
Even if I add a constructor like this:

class ItemId
{
public ItemId(int v)
{
Value = v;
}
public int Value { get; set; }
}

deserialization still doesn't work.

There are some workarounds like chaning the JSON and add Value but I don't
want that.

Is there a way to make deserialization to create the ItemId directly from
int ?

Thaks
Oct 23 '08 #1
7 15841
Well, you could use a different member for the serialization?

[DataContract]
class Item {
public ItemId Id {get;set;}

[DataMember(Name="Id")]
private int SerializationId {
get {return Id.Value;}
set {Id = new ItemId(value);}
}
}

That do?

Marc
Oct 23 '08 #2
What framework are you using? The following (below) works fine for me...

You can always make it public and use [Browsable(false)] and
[EditorBrowsable(EditorBrowsableState.Never)] to make it less visible?

Marc
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.ComponentModel;
[DataContract]
class Item
{
public ItemId Id { get; set; }

[DataMember(Name = "Id")]
public int SerializationId
{
get { return Id.Value; }
set { Id = new ItemId(value); }
}

static void Main()
{
DataContractJsonSerializer json = new
DataContractJsonSerializer(typeof(Item));
using(MemoryStream ms = new MemoryStream()) {
Item item = new Item { Id = new ItemId(4) };
json.WriteObject(ms, item);
ms.Position = 0;
string txt = Encoding.UTF8.GetString(ms.GetBuffer(), 0,
(int)ms.Length);
item = (Item)json.ReadObject(ms);
System.Console.WriteLine(item.Id.Value);
}
}
}

struct ItemId
{
private readonly int value;
public int Value { get { return value; } }
public ItemId(int value) { this.value = value; }
}
Oct 23 '08 #3
It works fine for you because you use public access for SerializationId.
Initially you give me the idea to use private and it doesn't work.

Using [Browsable(false)] and [EditorBrowsable(EditorBrowsableState.Never)]
is not an option.

It seems the only option I have is to have 2 public properties which is not
nice.

Thanks.
"Marc Gravell" <ma**********@gmail.comwrote in message
news:ee**************@TK2MSFTNGP04.phx.gbl...
What framework are you using? The following (below) works fine for me...

You can always make it public and use [Browsable(false)] and
[EditorBrowsable(EditorBrowsableState.Never)] to make it less visible?

Marc
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.ComponentModel;
[DataContract]
class Item
{
public ItemId Id { get; set; }

[DataMember(Name = "Id")]
public int SerializationId
{
get { return Id.Value; }
set { Id = new ItemId(value); }
}

static void Main()
{
DataContractJsonSerializer json = new
DataContractJsonSerializer(typeof(Item));
using(MemoryStream ms = new MemoryStream()) {
Item item = new Item { Id = new ItemId(4) };
json.WriteObject(ms, item);
ms.Position = 0;
string txt = Encoding.UTF8.GetString(ms.GetBuffer(), 0,
(int)ms.Length);
item = (Item)json.ReadObject(ms);
System.Console.WriteLine(item.Id.Value);
}
}
}

struct ItemId
{
private readonly int value;
public int Value { get { return value; } }
public ItemId(int value) { this.value = value; }
}

Oct 23 '08 #4
>It works fine for you because you use public access
>for SerializationId.
That was actually a hangover from trying the [Browsable] etc. Sorry for
the confusion, but it works fine for me "private" too.

Are you using CF/Silverlight? In regular .NET, DataContractSerializer
(and JSON) works with public *and* non-public members.

Marc
Oct 23 '08 #5
Oh, okay, I see what you mean now :)
Yes, I am trying to make it work in Silverlight 2 RTM
Is there any way without making both properties public ?
"Marc Gravell" <ma**********@gmail.comwrote in message
news:Oy*************@TK2MSFTNGP02.phx.gbl...
It works fine for you because you use public access
for SerializationId.

That was actually a hangover from trying the [Browsable] etc. Sorry for
the confusion, but it works fine for me "private" too.

Are you using CF/Silverlight? In regular .NET, DataContractSerializer (and
JSON) works with public *and* non-public members.

Marc

Oct 23 '08 #6
Is there any way without making both properties public ?

At a *push*, maybe a surrogate (IDataContractSurrogate), but I kinda
suspect that it won't exist in Silverlight, and it is also a *lot* of
work. Silverlight is *very* picky about member access / reflection - so
it you want the flat serialization but a structured object model, you
are going to have to compromise with some unsightly (extra) public
properties (ideally with the "hide me" attributes to make them less
annoying).

Marc
Oct 23 '08 #7
Thanks alot for the insights.
I will have to live with the extra public property and I will try to use the
suggested attributes.

Another (ugly) solution is to have 2 separate objects, internal one for
deserialization and a public one for client use.
The public one will hold inside the internal one and forward properties to
it.

What do you think ?

"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Is there any way without making both properties public ?

At a *push*, maybe a surrogate (IDataContractSurrogate), but I kinda
suspect that it won't exist in Silverlight, and it is also a *lot* of
work. Silverlight is *very* picky about member access / reflection - so it
you want the flat serialization but a structured object model, you are
going to have to compromise with some unsightly (extra) public properties
(ideally with the "hide me" attributes to make them less annoying).

Marc

Oct 23 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by George Jempty | last post: by
16 posts views Thread by G Matthew J | last post: by
20 posts views Thread by Luke Matuszewski | last post: by
23 posts views Thread by dhtmlkitchen | last post: by
2 posts views Thread by vunet | last post: by
4 posts views Thread by =?Utf-8?B?UmFqaXYgVmVybWE=?= | last post: by
1 post views Thread by =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post: by
reply views Thread by Nightcrawler | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.