@JsonProperty(access = Access.READ_ONLY) - unexpected behaviour
See original GitHub issueHey,
I was hoping to make use of @JsonProperty(access = Access.READ_ONLY), but failed.
Assume this class:
public class TestPojo
{
private String firstName;
private String lastName;
@JsonProperty(access = Access.READ_ONLY)
public String getFullName()
{
return firstName + " " + lastName;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
}
I couldn’t find a way to stop the deserializer from attempting to deserialize the field “fullName”.
The only thing that helps is to create a setter and annotate it with @JsonIgnore
. However, that setter does not make sense and I don’t want to have it. Is this a bug in behaviour or am I missing something? Thanks
Issue Analytics
- State:
- Created 8 years ago
- Reactions:4
- Comments:29 (14 by maintainers)
Top Results From Across the Web
JsonProperty(access = JsonProperty.Access.WRITE_ONLY ...
It seems this unexpected behaviour is a bug (see ... adapted to mimic the intended behaviour of the @JsonProperty(access = JsonProperty.
Read more >JsonProperty.Access (Jackson-annotations 2.6.0 API)
Access setting that means that the property may only be read for serialization, but not written (set) during deserialization. WRITE_ONLY. public static final ......
Read more >Management & Monitoring - Micronaut Documentation
The Netty access logger now supports excluding requests based on a set of regular expression ... This is an important behavioural change from...
Read more >Breaking changes in EF Core 5.0 - Microsoft Learn
The following API and behavior changes have the potential to break ... However, in cases where the navigation is used to access foreign...
Read more >KEPServerEX Manual - Kepware
Configuration session assigned to <name> promoted to write access. 282. Configuration session assigned to <name> demoted to read only.
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 FreeTop 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
Top GitHub Comments
Hi,
is there some easy workaround, until this is fixed? I mean instead of implementing a noop setter method?
The annotation @JsonIgnoreProperties sounds like it could help here. But it also has no effect, if I add it to a getter, regardless which values I choose for allowGetters or allowSetters.
Update: It seems that I found a solution by adding
@JsonIgnoreProperties(value="some_field",
allowGetters = true, allowSetters =false)
to the class. So this could be a solution, if we don’t need the @JsonIgnoreProperties annotation on class level for other purposes.
Also: my earlier explanation was exactly backwards. Javadocs state it correctly (if not well):
READ_ONLY
refers to POJO being handled in read-only way, andWRITE_ONLY
opposite. So Java-centric, not JSON-centric.I really should have named values differently (GETTER_ONLY, SETTER_ONLY, perhaps).