Best way to deserialize duplicate JSON API responses (Using Newtonsoft.JSON)
I'm using a HTTP client to contact an API, and then deserializing the JSON response into an instance of its respective object type.
My issue is this - the API has two versions: - Version 1: The old API, but still active, however, it's not maintained and may be missing bits of data that are present in requests made to v2 of the API. - Version 2: The new API. Almost identical responses to v1, but all of the fields are obfuscated.
So an API response for version 1 and 2 may look like this: Version 1:
If I were to to create a class for this response, it may look like this:
public class Person { [JsonProperty("id")] public readonly int Id; [JsonProperty("name")] public readonly string Name; [JsonProperty("bleh")] public readonly string Bleh; public Person() {}}
public class Person { [JsonProperty("id")] public readonly int Id; [JsonProperty("name")] public readonly string Name; [JsonProperty("bleh")] public readonly string Bleh; public Person() {}}
As you can see, this makes it difficult to reuse the same class type for v2 data. Is there a "clean" way I to achieve using only on class type for deserialization of both API versions?