Add `@JsonKey` annotation (similar to `@JsonValue`) for customizable serialization of Map keys
See original GitHub issueDescribe the bug
When serializing a map key, if the key’s type uses @JsonValue
on one of its attributes, and if that attribute’s type uses @JsonValue
on one of its own attributes, the second @JsonValue
is ignored, and toString()
is used instead.
Version information 2.10.0
To Reproduce
class Inner {
@JsonValue
String string;
Inner(String string) {
this.string = string;
}
public String toString() {
return "Inner(String="+this.string+")";
}
}
class Outer {
@JsonValue
Inner inner;
Outer(Inner inner) {
this.inner = inner;
}
}
public void test() throws Exception {
Outer outer = new Outer(new Inner("key"));
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(outer)); // outputs "key", as expected
System.out.println(mapper.writeValueAsString(Collections.singletonMap(outer,"value"))); // outputs {"Inner(String=key)":"value"}, expected {"key":"value"}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
java - How to customly serialize or convert a Map property with ...
To use a simple map just use the attribute 'keyUsing' in the annotations, and also you have to define the custom serializer and...
Read more >More Jackson Annotations - Baeldung
The @JsonAppend annotation is used to add virtual properties to an object in addition to regular ones when that object is serialized. This...
Read more >Jackson Annotations for JSON - Spring Framework Guru
The Jackson library provides annotations that you can use in POJO's to control both serialization and deserialization between POJOs and JSON.
Read more >Jackson Annotations for JSON (Part 2): Serialization - DZone
The @JsonGetter annotation is used to customize the generated JSON keys. This is accomplished with the value argument of @JsonGetter. The value ...
Read more >json_serializable | Dart Package - Pub.dev
Automatically generate code for converting to and from JSON by annotating Dart ... fields by annotating them with JsonKey and providing custom arguments....
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
I’ve started working on this.
Yep, for example : while serializing a map key, if there is a
@JsonKey
, use it (with chaining), else if there is a@JsonValue
, use it (with chaining too), else usetoString()
?