By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

JSON deserialization using DataContractJsonSerializer

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
>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

P: n/a
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

P: n/a
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

P: n/a
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.