1

I have a class in the back-end of an asp.net application. This needs to be serialized to JSON and send to the front-end. This is no problem, except for the serialization. The class looks like this (class names and such are changed):

public class ClassA
{
  // There are some properties here that need to be serialized normally
  public string Name { get; set; }

  // This array needs custom serialization
  public ClassB[] Objects { get; set; }
}

With ClassB looking like this:

public class ClassB
{
  public EnumA SomeEnum { get; set; }
}

public enum EnumA{ A = 0, B = 1, C = 2 }

I need to serialize this to something like this:

{"name":"someName","objects":[2,1,1,0]}

Here the objects array should be filled with the numeral value of the someEnum property of ClassB.

So far I have this function that I added to ClassA, but I feel like this should be done differently:

public string ToJson()
        {
            string result = "{";
            result += "\"name\":\"" + Name + "\",";

            result += "\"objects\":[";
            for (int i = 0; i < Objects.Length; i++)
            {
                result += (int)Objects[i].SomeEnum;
                if (i < Objects.Length - 1)
                    result += ",";
            }
            result += "]}";

            return result;
        }

How do I go about this in a more nice way? I also need to deserialize the generated JSON back to a ClassA object. All help is appreciated.

Martijn
  • 739
  • 9
  • 26
  • Are you using Newtonsoft or System.Text.Json (not in this case, but elsewhere)? – Fildor Dec 11 '19 at 08:41
  • It looks like you're not using any framework to serialize or deserialize the Json. I Recommend either Newtonsoft if you're working with the .NET Framework, or the built in System.Text.Json if you're working with .NET Core – MindSwipe Dec 11 '19 at 08:42
  • I would like to rewrite the function that I made and add deserialization using System.Text.Json if possible. Thanks. – Martijn Dec 11 '19 at 08:47

1 Answers1

2

You can use Newtonsoft JSON.

First, add JsonProperty attributes to your classes:

public class ClassA
{
  [JsonProperty("name")]
  public string Name { get; set; }

  [JsonProperty("objects")]
  public ClassB[] Objects { get; set; }
}

Now create a ClassBConverter:

public class ClassBConverter : JsonConverter<ClassB> {
    ...

    public override void WriteJson(JsonWriter writer, ClassB value, JsonSerializer serializer)
    {
        writer.WriteValue((int)value.SomeEnum);
    }

    public override ClassB ReadJson(JsonReader reader, Type objectType, ClassB existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        int i = (int)((long) reader.Value);
        return new ClassB { SomeEnum = i };
    }
}

And then do:

JsonConvert.(De)SerializeObject<ClassA>(yourJSONString, new ClassBConverter());
Martijn
  • 739
  • 9
  • 26
Sweeper
  • 213,210
  • 22
  • 193
  • 313