简体   繁体   中英

How to Serialize a Custom Object to Json in C#

I have a class like below

public class CustomObject
{
public string param1;
public IEnumerable<ExampleClass1> param2;
public Dictionary<ExampleClass1, IEnumerable<ExampleClass2>> param3;

//Constructor of the class with values set.
}

public class ExampleClass1
{
    public string example1String;
    public int example1Int;

//Constructor of the class with values set.
}

public class ExampleClass2
{
    public string example2String;
    public int example2Int;

//Constructor of the class with values set.
}

Input :

CustomObject
  param1 : "abc"
  param2 : List<ExampleClass1>
              {abc1,1}
              {abc2,2}
  param3 : Map<ExampleClass1, List<ExampleClass2>>
               <{abc1,1},[{xyz1,2},{xyz2,2}]>
               <{abc2,2},[{xyz3,3},{xyz4,4}]>

Output :

  param1 : "abc"
  param2 :    {abc1,1}
              {abc2,2}
  param3 :<{abc1,1},[{xyz1,2},{xyz2,2}]>

Expected Output

  param1 : "abc"
  param2 :    {abc1,1}
              {abc2,2}
  param3 :{abc1,1},[{xyz1,2},{xyz2,2}]
          {abc2,2},[{xyz3,3},{xyz4,4}]

I want to serialize this class and used the below approach to serialize the object to json string.

var serializedContent = JsonConvert.SerializeObject(CustomObject);

The output is correct for param1 and param2 , but param3 is not getting serialized properly , It is showing only one record.

How can i solve this ? I saw examples where it is possible to serialize a Dictionary but , if it is a member of a Class how do i achieve the complete serialization of the class ?

Take a look at this specifically in CustomObject :

public Dictionary<ExampleClass1, IEnumerable<ExampleClass2>> param3;

You set up a map, with the key being a class. How does a map work where the key is an object? It doesn't. You have to specify the "value" of that class. A quick way to do this is to override ToString .

So try this:

public class ExampleClass1
{
    public string example1String;
    public int example1Int;

    public override string ToString()
    {
        return example1String;
    }
}

Keep in mind that making ExampleClass1 a key, you must ensure no other ExampleClass1 exist with the same value, otherwise you will have duplicate keys in your JSON which makes it tough to analyze.

The caveat: I have no idea how you would deserialize this back to an actual object as it won't be able to convert a string to ExampleClass1 . You will probably have to do something goofy where you do a composite key of your data, then make a custom JSON converter to deserialize it.

My suggestion to you is not use a class as your map key when you want to serialize to/from JSON.

ETA: data I used for testing:

var cobj = new CustomObject
{
    param1 = "abc",
    param2 = new ExampleClass1[]
    {
        new ExampleClass1 { example1String = "abc1", example1Int = 1 },
        new ExampleClass1 { example1String = "abc2", example1Int = 2 }
    },
    param3 = new Dictionary<ExampleClass1, IEnumerable<ExampleClass2>>
    {
        { new ExampleClass1{ example1String = "abc1", example1Int = 1 }, new ExampleClass2[] { new ExampleClass2 { example2String = "xyz1", example2Int = 2 }, new ExampleClass2 { example2String = "xyz2", example2Int = 3 } } },
        { new ExampleClass1{ example1String = "abc2", example1Int = 4 }, new ExampleClass2[] { new ExampleClass2 { example2String = "xyz3", example2Int = 5 }, new ExampleClass2 { example2String = "xyz4", example2Int = 6 } } }
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM