Muhwu
Muhwu

Reputation: 1181

Deserializing JSON object to runtime type in WinRT (C#)

I have a small WinRT client app to my online service (Azure Web Service). The server sends a JSON encoded object with (with potential additional metadata) to the client and the client's responsibility would be to deserialize this data properly into classes and forward it to appropriate handlers.

Currently, the objects received can be deserialized with a simple

TodoItem todo = JsonConvert.DeserializeObject<TodoItem>(message.Content);

However, there can be multiple types of items received. So what I am currently thinking is this:

  1. I include the type info in the header serverside, such as "Content-Object: TodoItem"
  2. I define attributes to TodoItem on the client side (see below)
  3. Upon receiving a message from the server, I find the class using the attribute I defined.
  4. I call the deserialization method with the resolved type

(Example of the attribute mentioned in 2.)

[BackendObjectType="TodoItem"]
public class TodoItem 

My problem with this approach however is the Type to Generics in the deserialization as I can't call:

Type t = ResolveType(message);
JsonConvert.DeserializeObject<t>(message.Content);

I tried finding some solutions to this and getting method info for the DeserializeObject and calling it using reflection seemed to be the way to go. However, GetMethod() does not exist in WinRT and I was not able to find an alternative I could use to retrieve the generic version of the DeserializeObject (as fetching by the name gives me the non-generic overload). I don't mind using reflection and GetMethod as I can cache (?) the methods and call them every time a message is received without having to resolve it every time.

So how do I achieve the latter part and/or is there another way to approach this?

Upvotes: 2

Views: 1506

Answers (3)

Aravind
Aravind

Reputation: 1549

Try this

http://json2csharp.com/

Put your Json string here it will generate a class

then

 public static T DeserializeFromJson<T>(string json)
    {
        T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
        return deserializedProduct;
    }

 var container = DeserializeFromJson<ClassName>(JsonString);

Upvotes: 0

Muhwu
Muhwu

Reputation: 1181

Alright, I feel like this was not really a problem at all to begin with as I discovered the DeserializeObject(string, Type, JsonSerializerSettings) overload for the method. It works splendidly. However, I would still like to hear some feedback on the approach. Do you think using attributes as a way to resolve the type names is reasonable or are there better ways? I don't want to use the class names directly though, because I don't want to risk any sort of man-in-the-middle things be able to initialize whatever.

Upvotes: 2

Maris
Maris

Reputation: 4776

Just a few minutes ago we have posted the alternative way to do what you want. Please look here, if you will have any questions feel free to ask:

Prblem in Deserialization of JSON

Upvotes: 0

Related Questions