0

I have custom JsonSerializer:

new JsonSerializer<T>() {
    @Override
    public void serialize(T instance, JsonGenerator gen, SerializerProvider sp) throws IOException, JsonProcessingException
    {
        try {
            instance = someFunction(instance);
            SerializableString serializableString = new SerializedString(defaultMapper.writeValueAsString(instance));
            gen.writeRawValue(serializableString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

For my pourpuses, i need a way to pass gen.getOutputContext() to the defaultMapper.writeValueAsString(instance). Is there a way to achieve this? Or am I using the ObjectMapper the wrong way? Thank you for any insight.

EDIT: the defaultMapper is of type ObjectMapper.

Lubomír Grund
  • 145
  • 1
  • 13
  • Why do you need that? – cassiomolin Sep 18 '18 at 08:54
  • 1
    @CassioMazzochiMolin am using jackson ObjectMapper to create bases for .csv file for translating data ... i have special wrapper for texts that are visible to end users, so i can create rows using custom serialiser (with key, oldValue, newValue (to be edited), but i need "path" for orientation and integration of translations into data mode)l. However some other classes require their own custom serializer (similar to one provided in question) and i loose context - as a result i only get "path" from the last custom-serialized node, not from root – Lubomír Grund Sep 18 '18 at 10:17

2 Answers2

2

The ObjectMapper actually have exactly the method i needed built in - ObjectMapper.writeValue(JsonGenerator, T). Modified (and functional) code from my question:

new JsonSerializer<T>() {
    @Override
    public void serialize(T instance, JsonGenerator gen, SerializerProvider sp) throws IOException, JsonProcessingException
    {
        try {
            instance = someFunction(instance);
            defaultMapper.writeValue(gen, instance)
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Lubomír Grund
  • 145
  • 1
  • 13
0

Here to help do my best to help you out!

I don't have the java environment setup, but an example using the Jackson JSON package is here: http://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/

Let me know if this helps!

Based on the tutorial, depending how your object is set up you can use the Object Mapper Object using the "writeVaueAsString(object o) with the method "writerWithDefaultPrettyPrinter() method to get a readable JSON String

However this isn't a "custom" Serializer/Deserializer.

Would this solution work for you?

ACupofJoe
  • 21
  • 2
  • This stackoverflow thread might answer your question. https://stackoverflow.com/questions/7161638/how-do-i-use-a-custom-serializer-with-jackson – ACupofJoe Sep 18 '18 at 08:37
  • Thank you for your answer, however my problem is not that the mapper is not working - i am using custom serializers to do some data transformation and i need to know the "path" of certain objects (sets of keys from root to the object). However some of the other objects require custom serialization and therefore when i need to acces the "path" (via `gen.getOutputContext()`), all i get is "path" from the last custom serialized object, not from the root. Therefore i am looking for a way to pass `gen.getOutputContext()` to my `default mapper` to get around this. – Lubomír Grund Sep 18 '18 at 08:47