Hi there!

I hope it's a simple question but I cannot find an answer myself. So, 
asking for help.
I need to implement custom map serializer when mam mets some condition. the 
problem is, that I need to replace the map with a primitive type, e.g. 
string. As a simple example - when map conforms to some rule I want to 
replace it with, let's say, string value. I understand there could be at 
least one case when primitive, like string, cannot be used witout a key. 
It's probably if the map is the root node.Maybe I could generate a key for 
such a case? 
Anyways, I cannot achieve it in any way I tried. Here's the MVE for my 
question. I want to make it producing this:
{
  "Second" : "Two",
  "Third" : "magic string",
  "First" : 1
}

instead of default:

{
  "Second" : "Two",
  "Third" : {
    "Eleventh" : 11,
    "Twelfth" : "12"
  },
  "First" : 1
}

Here's the mve itself. Thank you!


public class MapParseTest

{

    private final ObjectMapper mapper;


    public MapParseTest()

    {

        mapper = new ObjectMapper();

        SimpleModule module = new SimpleModule();

        module.setSerializerModifier(new MyMapSerializerModifier());

        mapper.registerModule(module);

    }


    public void doTest() throws JsonProcessingException

    {

        MyMap map1 = new MyMap();

        MyMap map2 = new MyMap();


        map1.setName("shtame");

        map2.setName("magic name");


        map1.put("First", 1);

        map1.put("Second", "Two");

        map1.put("Third", map2);


        map2.put("Eleventh", 11);

        map2.put("Twelfth", "12");


        String result = 
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map1);

        System.*out*.println(result);

    }


    public static void main(String[] args) throws JsonProcessingException

    {

        MapParseTest test = new MapParseTest();

        test.doTest();

    }


    @Setter

    @Getter

    public static class MyMap extends HashMap<String, Object>

    {

        private String name;

    }


    public static class MyMapSerializer extends JsonSerializer<MyMap>

    {

        private final JsonSerializer<Object> defaultSerializer;


        public MyMapSerializer(JsonSerializer<Object> defaultSerializer)

        {

            this.defaultSerializer = defaultSerializer;

        }


        @Override

        public void serialize(MyMap value,

                              JsonGenerator gen,

                              SerializerProvider provider) throws 
IOException

        {

            String className = value.getName();

            if (className.equalsIgnoreCase("magic name"))

            {

                gen.writeString("magic string");

            } else

            {

                defaultSerializer.serialize(value, gen, provider);

            }

        }

    }


    public static class MyMapSerializerModifier extends 
BeanSerializerModifier

    {

        @Override

        public JsonSerializer<?> modifyMapSerializer(SerializationConfig 
config,

                                                     MapType mapType,

                                                     BeanDescription 
beanDesc,

                                                     JsonSerializer<?> 
serializer)

        {

            Class<?> type = beanDesc.getBeanClass();

            if (type.equals(MyMap.class))

            {

                return new MyMapSerializer((JsonSerializer<Object>) 
serializer);

            }

            return serializer;

        }

    }

}




-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/6b788097-7d65-4c9e-b1fd-7f67383d586cn%40googlegroups.com.

Reply via email to