C
C#•4d ago
UnemployedNinja

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:
{
"id": "1"
"name": "John"
"bleh": "Something else"
}
{
"id": "1"
"name": "John"
"bleh": "Something else"
}
Version 2:
{
"lLL": "1"
"llLLlLLl": "John"
"LLlLLllL": "Something else"
}
{
"lLL": "1"
"llLLlLLl": "John"
"LLlLLllL": "Something else"
}
If I were to to create a class for this response, it may look like this:
c#
public class Person {
[JsonProperty("id")]
public readonly int Id;

[JsonProperty("name")]
public readonly string Name;

[JsonProperty("bleh")]
public readonly string Bleh;

public Person() {}
}
c#
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?
19 Replies
Unknown User
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX•4d ago
If you have no further questions, please use /close to mark the forum thread as answered
UnemployedNinja
UnemployedNinja•4d ago
wat
Unknown User
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
UnemployedNinja
UnemployedNinja•4d ago
DTO?
Unknown User
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
UnemployedNinja
UnemployedNinja•4d ago
So I basically would have to duplicate every class type and just change the [JsonProperty]? PersonV1 & PersonV2? I'm trying to avoid having 2 different class types :/
Unknown User
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Salman
Salman•4d ago
Making a new type won't hurt you
Unknown User
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
UnemployedNinja
UnemployedNinja•4d ago
I'm not sure what you mean by that, but the response types/versionings are not my design... There is no copy/paste
Unknown User
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Salman
Salman•4d ago
Tebe on fire
UnemployedNinja
UnemployedNinja•4d ago
Oh I see what you mean, ok. I misunderstood. And yea, I don't mind copy/paste lol, but it's having duplicate class types for like 40 different responses is what I'm trying to avoid lol I'd also prefer if I could just have one class type returned, so I was just trying to see if anyone had a solution 😭
Unknown User
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Salman
Salman•4d ago
you were given the appropriate solution in the first response literally
Unknown User
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
UnemployedNinja
UnemployedNinja•4d ago
I think I'll just deobfuscate the JSON string before I deserialize lol ty for your input Also, would you mind explaining what you mean by DTO? Because I have no idea :/
Unknown User
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server