question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make JsonPropertyAttribute allow multiple usages on same property

See original GitHub issue

Hi,

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:open
  • Created 6 years ago
  • Reactions:36
  • Comments:5

github_iconTop GitHub Comments

31reactions
hazeb03commented, Jul 1, 2018

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; }

[JsonProperty("name2")]
private string CodeModel2 { set { CodeModel = value; } }

}

Ref https://stackoverflow.com/questions/43714050/multiple-jsonproperty-name-assigned-to-single-property

12reactions
Stefan-1313commented, Jul 19, 2022

This could be implemented by creating an attribute called JsonAlias. We can have multiple JsonAlias attributes which would all map to the variable when deserializing. However, the JsonProperty 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):

public class ResultList<T>
{
	[JsonProperty("items")]
	[JsonAlias("values")]
	[JsonAlias("results")]
	public IReadOnlyList<T> Items { get; set; }
	
	[JsonProperty("hasMore")]
	[JsonAlias("has-more")]
	public bool HasMore { get; set; }
	
	[JsonProperty("offset")]
	public long? Offset { get; set; }
}

This idea is taken from here: https://stackoverflow.com/a/70781282/7568866

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found