When is the @JsonProperty property used and what is it used for?
Here's a good example. I use it to rename the variable because the JSON is coming from a .Net
environment where properties start with an upper-case letter.
public class Parameter {
@JsonProperty("Name")
public String name;
@JsonProperty("Value")
public String value;
}
This correctly parses to/from the JSON:
"Parameter":{
"Name":"Parameter-Name",
"Value":"Parameter-Value"
}
What [JsonProperty] used for in c#?
Per the documentation: this is the json key that will be used when serializing/deserializing this object to/from a json string.
So, if the value of FileName
is file.txt
, the serialized result would be
{
"fileName": "file.txt"
}
Different @JsonProperty config for READ and WRITE on same field?
You could use the getters and setters to get that extra customization. The get will act as READ and the set as WRITE. Note that you don't need the access properties or the field level annotation:
public abstract class StandingMixIn {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
Integer positionNumber;
// No annotation on the field
String positionText;
@JsonProperty(value = "positionText")
public String getPositionText() {
return positionText;
}
@JsonProperty(value = "position")
public void setPositionText(String positionText) {
this.positionText = positionText;
}
}
@JsonProperty annotation on field as well as getter/setter
My observations based on a few tests has been that whichever name differs from the property name is one which takes effect:
For eg. consider a slight modification of your case:
@JsonProperty("fileName")
private String fileName;
@JsonProperty("fileName")
public String getFileName()
{
return fileName;
}
@JsonProperty("fileName1")
public void setFileName(String fileName)
{
this.fileName = fileName;
}
Both fileName
field, and method getFileName
, have the correct property name of fileName
and setFileName
has a different one fileName1
, in this case Jackson will look for a fileName1
attribute in json at the point of deserialization and will create a attribute called fileName1
at the point of serialization.
Now, coming to your case, where all the three @JsonProperty differ from the default propertyname of fileName
, it would just pick one of them as the attribute(FILENAME
), and had any on of the three differed, it would have thrown an exception:
java.lang.IllegalStateException: Conflicting property name definitions
Usage of Jackson @JsonProperty annotation for kotlin data classes
@JsonProperty
annotations in your code are all put on private fields within your data class and by default Jackson doesn't scan private fields for annotations. You have to instruct it to do otherwise by putting @JsonAutoDetect
annotation:
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
data class CurrencyInfo(
@JsonProperty("currency_info") var currencyInfo: CurrencyInfoItem?
)
or alternatively you can move your annotations on accessor methods:
data class CurrencyInfo(
@get:JsonProperty("currency_info") var currencyInfo: CurrencyInfoItem?
)
@JsonProperty annotation is getting ignored in mongodb collection
In MongoDB you're saving an object with his properties names.
JsonProperty annotation mapping the deserialization and the serialization with given object.
Marker annotation that can be used to define a non-static method as a
"setter" or "getter" for a logical property (depending on its
signature), or non-static object field to be used (serialized,
deserialized) as a logical property. Default value ("") indicates that
the field name is used as the property name without any modifications,
but it can be specified to non-empty value to specify different name.
Property name refers to name used externally, as the field name in
JSON objects.
https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonProperty.html
By using @Field
property annotation you can save property in different name as in object.
Related Topics
Java List.Add() Unsupportedoperationexception
How to Get the Current Time in Yyyy-Mm-Dd Hh:Mi:Sec.Millisecond Format in Java
Can Java 8 Code Be Compiled to Run on Java 7 Jvm
Handling Exceptions from Java Executorservice Tasks
How to Use Raw SQL Within a Spring Repository
Java.Lang.Outofmemoryerror: Gc Overhead Limit Exceeded
How to Re-Run Failed Junit Tests Immediately
What Should I Set Java_Home Environment Variable on MACos X 10.6
Casting Object Array to Integer Array Error
Java Unsupported Major Minor Version 52.0
Similarity String Comparison in Java
Can't Read and Write a Tiff Image File Using Java Imageio Standard Library
How to Sort List of Objects by Some Property
Differencebetween "Class.Forname()" and "Class.Forname().Newinstance()"
Bounding Generics with 'Super' Keyword
How to Get Active User's Userdetails