Make JsonPropertyAttribute allow multiple usages on same property
See original GitHub issueHi,
I need to deserialize a series of documents that are almost similar but might change few details.
I was hoping to spare myself the need of creating multiple classes by decorating the same target property with more instances of the JsonPropertyAttribute
but this is not possible.
Ideally it would be nice to be able to do something like
public class ResultList<T>
{
[JsonProperty("items")]
[JsonProperty("values")]
[JsonProperty("results")]
public IReadOnlyList<T> Items { get; set; }
[JsonProperty("hasMore")]
[JsonProperty("has-more")]
public bool HasMore { get; set; }
[JsonProperty("offset")]
public long? Offset { get; set; }
}
Since most of the time this happens when deserializing a result list, the ambiguity introduced when serializing wouldn’t a big issue. In that case, picking the value of the first attribute found would be good enough.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:36
- Comments:5
Top Results From Across the Web
c# - multiple JsonProperty Name assigned to single property
I have two format of JSON which I want to Deserialize to one class. I know we can't apply two [JsonProperty] attribute to...
Read more >Mapping Multiple JSON Fields to One Java Field
These will allow us to map more than one JSON property onto the same Java field. First, we're going to use the @JsonProperty...
Read more >How to support multiple names for a property using System ...
Only one attribute is allowed. But with a custom contract you could write code that determines the property name. One of the use...
Read more >How to assign same json property name, one having value ...
Use the JsonPropertyAttribute to specify another name //Here is the Json. Expand ▽. { "adRoots": null, "allowedTagTypes": [ 0, 1, ...
Read more >C# - Deserialize JSON using different property names
If the JSON property names are different from the class property names 1) Use the JsonPropertyName attribute or 2) Use a naming policy ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A simple solution which does not require a converter: just add a second, private property to your class, mark it with [JsonProperty(“name2”)], and have it set the first property:
public class Specifications { [JsonProperty(“name1”)] public string CodeModel { get; set; }
}
Ref https://stackoverflow.com/questions/43714050/multiple-jsonproperty-name-assigned-to-single-property
This could be implemented by creating an attribute called
JsonAlias
. We can have multipleJsonAlias
attributes which would all map to the variable when deserializing. However, theJsonProperty
is still allowed to be used only once, and will be used for serialization. This would also prevent any ambiguity.We then would get something like (based on the example in the first post of this issue):
This idea is taken from here: https://stackoverflow.com/a/70781282/7568866