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:
Version 2:
If I were to to create a class for this response, it may look like this:
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•2mo ago
Message Not Public
Sign In & Join Server To View
If you have no further questions, please use /close to mark the forum thread as answered
wat
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
DTO?
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
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•2mo ago
Message Not Public
Sign In & Join Server To View
Making a new type won't hurt you
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
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•2mo ago
Message Not Public
Sign In & Join Server To View
Tebe on fire
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•2mo ago
Message Not Public
Sign In & Join Server To View
you were given the appropriate solution in the first response literally
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
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•2mo ago
Message Not Public
Sign In & Join Server To View