JSON deserialization using DataContractJsonSerializer

A

Andrew

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
 
M

Marc Gravell

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
 
A

Andrew

Hi,

Thanks for the idea but it doesn't work completely.
Deserializing the object raises an exception:

"The data contract type 'Item' cannot be deserialized because the member
'SerializaionId' is not public. Making the member public will fix this
error. Alternatively, you can make it internal, and use the
InternalsVisibleToAttribute attribute on your assembly in order to enable
serialization of internal members - see documentation for more details. Be
aware that doing so has certain security implications"

If I make it public it doesn't look too good to have both of them available.
I tried with internal but it really needs public as the exception say.
 
A

Andrew

when I mean both of them I mean both ItemId and SerializationId

Andrew said:
Hi,

Thanks for the idea but it doesn't work completely.
Deserializing the object raises an exception:

"The data contract type 'Item' cannot be deserialized because the member
'SerializaionId' is not public. Making the member public will fix this
error. Alternatively, you can make it internal, and use the
InternalsVisibleToAttribute attribute on your assembly in order to enable
serialization of internal members - see documentation for more details. Be
aware that doing so has certain security implications"

If I make it public it doesn't look too good to have both of them
available.
I tried with internal but it really needs public as the exception say.


Marc Gravell said:
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
 
M

Marc Gravell

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; }
}
 
A

Andrew

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.
 
M

Marc Gravell

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
 
A

Andrew

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 said:
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
 
M

Marc Gravell

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
 
A

Andrew

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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top