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.

Add `@JsonKey` annotation (similar to `@JsonValue`) for customizable serialization of Map keys

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Anusiencommented, Oct 26, 2020

I’ve started working on this.

1reaction
CidToricommented, Oct 7, 2020

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 use toString()?

Read more comments on GitHub >

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

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