Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4260464
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:55:24+00:00 2026-05-21T05:55:24+00:00

I have a Map<A,B> fieldOfC as a field of a class C. When I

  • 0

I have a Map<A,B> fieldOfC as a field of a class C. When I try to deserialize C with Jackson, an Exception is thrown because it can’t find a Deserializer for Map’s key A. So, I guess the solution is to extend StdJsonDeserializer and do it manually.
My problem is that I can’t find an example on how to use the parser and the context of the method “deserialize” that I have to implement.

Can anyone write the code for this simple example so I can use it as a start to build my real deserializer?

public class A{
  private String a1;
  private Integer a2;
}

public class B{
  private String b1;
}

public class C{
  @JsonDeserialize(keyUsing=ADeserializer.class)
  //also tried this: @JsonDeserialize(keyAs=A.class) without success
  private Map<A,B> fieldOfC;
  private String c1;
}

public class ADeserializer extends StdKeyDeserializer {

  protected ADeserializer(Class<A> cls) {
    super(cls);
  }

  protected Object _parse(String key, DeserializationContext ctxt) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(key, A.class);
  }
}

Thanks in advance

EDIT: googling, I found a test of the same problem I have. This is exactly my problem

EDIT: changed extended class from StdDeserializer to StdKeyDeserializer as I read here in method findKeyDeserializer(org.codehaus.jackson.map.DeserializationConfig, org.codehaus.jackson.type.JavaType, org.codehaus.jackson.map.BeanProperty)

EDIT: After solving this issue I got this one that is related.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:55:25+00:00Added an answer on May 21, 2026 at 5:55 am

    I am a complete newbie with Jackson, but the following works for me.

    First I add a JsonCreator method to A:

    public class A {
        private String a1;
        private Integer a2;
        public String getA1() { return a1; }
        public Integer getA2() { return a2; }
        public void setA1(String a1) { this.a1 = a1; }
        public void setA2(Integer a2) { this.a2 = a2; }
        
        @JsonCreator
        public static A fromJSON(String val) throws JsonParseException, JsonMappingException, IOException {
            ObjectMapper mapper = new ObjectMapper();
            A a = mapper.readValue(val,A.class);
            return a;
        }
    } 
    

    That alone solves the deserialization problem. The harder part for me was the correct serialization of the keys. What I did there was to define a key serializer that serializes named classes as there JSON serialization, like this:

    public class KeySerializer extends SerializerBase<Object> {
        private static final SerializerBase<Object> DEFAULT = new StdKeySerializer();
    
        private Set<Class<?>> objectKeys_ = Collections.synchronizedSet(new HashSet<Class<?>>());
    
        protected KeySerializer(Class<?>... objectKeys) {
            super(Object.class);
            for(Class<?> cl:objectKeys) {
                objectKeys_.add(cl);
            }
        }
    
    
        @Override
        public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException {
            return DEFAULT.getSchema(provider, typeHint);
        }
    
    
        @Override
        public void serialize(Object value, JsonGenerator jgen,
                SerializerProvider provider) throws IOException,
                JsonGenerationException {
            if (objectKeys_.contains(value.getClass())) {
                ObjectMapper mapper = new ObjectMapper();
                StringWriter writer = new StringWriter();
                mapper.writeValue(writer, value);
                jgen.writeFieldName(writer.toString());
            } else {
                DEFAULT.serialize(value, jgen, provider);
            }
        }
    }
    

    Then to prove it works, serializing and deserializing an instance of class C:

        ObjectMapper mapper = new ObjectMapper();
        StdSerializerProvider provider = new StdSerializerProvider();
        provider.setKeySerializer(new KeySerializer(A.class));
        mapper.setSerializerProvider(provider);
        
        StringWriter out = new StringWriter();
        mapper.writeValue(out, c);
        String json = out.toString();
        System.out.println("JSON= "+json);
        
        C c2 = mapper.readValue(json, C.class);
        System.out.print("C2= ");
        StringWriter outC2 = new StringWriter();
        mapper.writeValue(outC2, c2);
        System.out.println(outC2.toString());
    

    For me this produced the output:

    JSON = {"c1":"goo","map":{"{\"a1\":\"1ccf\",\"a2\":7376}":{"b1":"5ox"},"{\"a1\":\"1cd2\",\"a2\":7379}":{"b1":"5p0"},"{\"a1\":\"1cd5\",\"a2\":7382}":{"b1":"5p3"},"{\"a1\":\"1cd8\",\"a2\":7385}":{"b1":"5p6"}}}
    C2 =   {"c1":"goo","map":{"{\"a1\":\"1ccf\",\"a2\":7376}":{"b1":"5ox"},"{\"a1\":\"1cd2\",\"a2\":7379}":{"b1":"5p0"},"{\"a1\":\"1cd5\",\"a2\":7382}":{"b1":"5p3"},"{\"a1\":\"1cd8\",\"a2\":7385}":{"b1":"5p6"}}}
    

    I feel there ought to have been a better way of doing saying how to serialize the key by using annotations, but I could not work it out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Map whose key and value and custom classes. The key class
I have image map that can I move, but this map will be so
So lets say I have: map<pair<int, MyEnum>, string> myMap; and I add a key
I have map with key and value and my goal is to get list
If I have: map = { 'stack':'overflow' } try: map['experts-exchange'] except: <--- What is
I have unsorted map of key value pairs. input = { xa => xavalue,
I have: Map<String, String> vars = new HashMap<String, String>(); String r = restOperations.getForObject(url, String.class,
I have map.resources :posts and I want to be able to serve post bodies
I have map with multiple different colors markers with some info window. Everything works
I want to display google map in Android ,i have map API even map

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.