JSON.NET with custom Serializer for custom object

So I'm trying to turn my complex .NET object into a completely different object in JSON. Basically, I have an array of objects, which can be any type that my base class outputs, and I want to turn that array of objects into my own JSON object. I don't think I can just make a simple call to JsonConvert.Serialize () because my JSON object will be structured differently.

So here is the mockup of my .NET classes:

public abstract class A
{
   public string Id { get; set; }
   public string Title { get; set; }
   public bool Enabled { get; set; }
   public List<X> MyCollection { get; set; }
}

public class B : A
{
   public string Foo { get; set; }
}

public class C : A
{
   public int Bar { get; set; }
}

public abstract class X
{
   public string Id { get; set; }
   public string Title { get; set; }
   public bool Enabled { get; set; }
}

public class Y : X
{
   public string Hello { get; set; }
}

public class Z : X
{
   public string World { get; set; }
}

      

Obviously this is a simple look at my real class structure, but hopefully some guidance on how to convert this will give me a chance to convert my real class. Basically my classes (A, B, C) will contain a list of classes (X, Y, C).

So, let's say I have an array / collection of these objects listed above:

List<A> myObjects = new List<A>();
A myVar = new B();
myVar.Title = "Number 1";
myVar.MyCollection.Add(new Y());
myVar.MyCollection.Add(new Y());
myVar.MyCollection.Add(new Z());
myObjects.Add(myVar);

myVar = new B();
myVar.Title = "Number 2";
myVar.MyCollection.Add(new Z());
myObjects.Add(myVar);

myVar = new C();
myVar.Title = "Number 3";
myVar.MyCollection.Add(new Y());
myVar.MyCollection.Add(new Y());
myObjects.Add(myVar);

      

I want to take myObjects object and serialize it to JSON structure like this:

[
   {
      name: "Number 1", //This is the value of A.Title
      type: "B", //This is a NEW property I want to add to the JSON
      enabled: true, //This is the value of A.Enabled
      foo: "SomeValue", //This is the value of B.Foo
      myCollection: [
         { name: "Y1", type: "Y", enabled: true, hello: "SomeValue" }
         { name: "Y2", type: "Y", enabled: true, hello: "SomeOtherValue" }
         { name: "Z1", type: "Z", enabled: true, world: "SomeValue" }
      ]
   },
   {
      name: "Number 2",
      type: "B",
      enabled: true,
      foo: "SomeValue",
      myCollection: [
         { name: "Z2", type: "Z", enabled: true, world: "SomeValue" }
      ]
   },
   {
      name: "Number 3",
      type: "C",
      enabled: true,
      bar: "SomeValue",
      myCollection: [
         { name: "Y3", type: "Y", enabled: true, hello: "SomeValue" }
         { name: "Y4", type: "Y", enabled: true, hello: "SomeOtherValue" }
      ]
   }
]

      

Basically, I want to add my own properties and change it slightly to JSON than reflecting the .NET object. There are other properties that I want to add to my objects, but they are not listed here (can this post EVEN more). I just need a way to get my objects and serialize them in my own way. I need to make sure I serialize the derived type so I can carry some of these properties. Can anyone help me in the right direction to figure out how to solve this problem?

+3


source to share


1 answer


You can use Json.Linq classes to deserialize them dynamically by reading the types after deserializing the primary class.

var objects = JArray.Parse(jsonString)
List<A> objectList = new List<A>();
foreach (var obj in objects) {
    A newObj = null;
    switch ((string)obj["type"]) {
        case "B":
            newObj = obj.ToObject<B>();
            break;
        case "C":
            newObj = obj.ToObject<C>();
            break;
    }
    newObj.MyCollection.Clear();
    foreach (var x in obj["myCollection"]) {
        var newX = null;
        switch ((string)x["type"]) {
            case "Y":
                newX = x.ToObject<Y>();
                break;
            case "Z":
                newX = x.ToObject<Z>();
                break;
        }
        newObj.MyCollection.Add(newX);
    }
    objectList.Add(newObj);
}

      



As far as I can tell, what your Json you posted would handle, and if there are any errors, sorry, I did this entirely from memory on a tablet: P

0


source







All Articles