Deserialize JSON that might be an integer or a list of strings in C#

Viewed 4009

I am using C# to deserialize a collection of JSON strings. Each string has a property employer_normalized that is supposed to contain a string and List<int> where the values in the List are always positive. In some cases employer_normalized is set to -1, so I want to override these cases with some behavior that sets employer_normalized to null.

Here is my class:

public class EmployerNormalized
{
  public string company;
  public List<int> code;
}

Good JSON

"employer_normalized": {
        "company": "self",
        "code": [
          "4581 ",
          "6732 ",
          "9121",
          "9999 ",
          "5947 ",
          "8322 ",
          "8351 ",
          "7335 ",
          "9999 ",
          "4225 ",
          "8399 "
        ]
      }

Bad JSON

"employer_normalized": -1

I am currently using Json.NET to do my JSON parsing. What is an elegant solution to solving this problem? Is it best to just set the employer_normalized value to null if it is -1? If so, how can I do this?

2 Answers
Related