0

I am trying to deserialize an object using Json.Net. I have a simple object called MyObject that holds a serialized object in MyObject.Body. I am storing the class type in MyObject.ClassType. I do not want to write a giant switch statement to figure out what type to deserialize to. Can I do this dynamically?

string value = "fullClassName";
switch (value)
{
    case "Cat":
        var cat = JsonConvert.DeserializeObject<Cat>(myObject.Body);
        break;
    case "Dog":
        var dog = JsonConvert.DeserializeObject<Dog>(myObject.Body);
        break;
}


public class MyObject
{
    public string Body { get; set; }
    public string ClassType { get; set; }
}    


var myAnimal = JsonConvert.DeserializeObject<1of10TypesOfObjects> (myObject.Body);
2
  • 1
    You can use reflection to get type if you can make sure the "fullClassName" will match your class name. Commented Sep 22, 2017 at 14:58
  • I'm not familiar with reflection could you be a bit more specific? Commented Sep 22, 2017 at 14:59

3 Answers 3

2

You can do that with:

JsonConvert.DeserializeObject(string value, Type type)

This will deserialize the JSON using the type information rather than a generic type parameter.

var myObjects = new List<MyObject>
{
    new MyObject
    {
        ClassType = typeof(Cat).FullName,
        Body = JsonConvert.SerializeObject(new Cat { Fluffiness = 10 })
    },
    new MyObject
    {
        ClassType = typeof(Dog).FullName,
        Body = JsonConvert.SerializeObject(new Dog { Loudness = 3 })
    }
};

List<object> objects =
    myObjects
        .Select(myObject =>
            JsonConvert.DeserializeObject(
            myObject.Body,
            typeof(Cat).Assembly.GetType(myObject.ClassType)))
        .ToList();

See:

Sign up to request clarification or add additional context in comments.

Comments

1

I put my answer on.

string typeName = "ConsoleApplication1.Cat";
var type = Type.GetType(typeName);
var myObject = new MyObject {Body = "{ Color: 'red' }"};

var res = JsonConvert.DeserializeObject(myObject.Body, type);

Please make sure the type name string must be a fullname, which included the namespace. Eg. "ConsoleApplication1.Cat". And you must need to do some exception handling to avoid the type not found if someone enter the wrong typeName.

Comments

0

Sure, you can do it. Please make sure the serialized string is corresponding to the type you try to de-serialize. For example:

class Dog
{
    public string Name { get; set; }
}

class Cat
{
    public string Type { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        Dog dog = new Dog { Name = "My Dog" };
        string strDog = JsonConvert.SerializeObject(dog);

        Cat cat = new Cat { Type = "My Cat" };
        string strCat = JsonConvert.SerializeObject(cat);

        var dog2 = JsonConvert.DeserializeObject<Dog>(strDog);
        var cat2 = JsonConvert.DeserializeObject<Cat>(strCat);
        Console.WriteLine(cat2.Type); 
        Console.WriteLine(dog2.Name);          

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.