3

So I am attempting to turn my complex object .NET into a totally different object in JSON. Basically, I have an array of objects that could be any type that derives off my base class and I want to turn that array of objects into my own JSON object. I don't think I can just do a simple JsonConvert.Serialize() method call because my JSON object will be structured differently.

So here is a 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 that is a simple view of my real class structure but hopefully some guidance on how to convert this will lend me to be able 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 I 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 my object 'myObjects' and serialize it into a 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 structure it a little different in JSON than my .NET object reflects. There are some other properties I want to add to my objects as well but they are not listed here (would may this post EVEN bigger). I basically just need a way to take my objects and serialize them in my own custom way. I need to make sure I serialize the derived type so I can carry along some of those properties though. Can anyone help guide me in the right direction on how to solve this problem?

1
  • 1
    In ASP .NET MVC you can just return Json(myObjects); but you would need to model myObjects after the expected result you want. Commented Jan 30, 2013 at 15:18

1 Answer 1

0

You could use the Json.Linq classes to dynamically deserialize them by reading the types after you deserialize 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 that would handle your Json you posted, and if there's any errors, sorry I did this completely from memory on a tablet :P

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

1 Comment

I ended up going another way for the solution. I think something like this would work though if I needed to go this route. Thanks.

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.