3

This is the sample code what I am doing to log the JSON object to the console and including error stack trace as part of field2. While I log to the console, that is not readable.

Sample Code to understand issue.

package goal.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestClass {

    String field1 = "hello";
    String field2 = "hi\r\ntest";

    public String getField1() {
        return field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }

    public String getField2() {
        return field2;
    }

    public void setField2(String field2) {
        this.field2 = field2;
    }

    public static void main(String[] args) throws JsonProcessingException {
        whatIsPossibleNow();
        whatINeed();
    }

    static void whatIsPossibleNow() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(new TestClass());
        System.out.println(result);
        /**
         * output
         * {
         *   "field1" : "hello",
         *   "field2" : "hi\r\ntest"
         * }
         */
    }

    static void whatINeed() throws JsonProcessingException {
        TestClass obj = new TestClass();
        /**
         *
         *  field2 also need to formatted while printing
         *         expected output
         *         {
         *             "field1" : "hello",
         *             "field2" : "hi
         *                         test"
         *         }
         *  output of the field2 should be something this what this prints
         */
        System.out.println(obj.getField2());
    }

    static void howCouldBe() throws JsonProcessingException {
        /**
         *  how to achieve this now ??
         */
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValueAsString(new TestClass()).prettyPrint();
    }
}

I am confused if there is any other way achieving as I need to pretty print object while deserializing.

Vivek Adhikari
  • 221
  • 2
  • 7
  • Jackson's "Pretty print" is for writing JSON. The format you want isn't valid JSON so it can't be done with Jackson. You already are printing `field2` how you want it, so you'll have to print the rest yourself, too. – RoToRa Feb 22 '23 at 10:27
  • Its a valid JSON but printed differently. It just that, its not linear. My question is not on values rather on printing. I know jackson doesn't support this now, is there any lib or anything - that we can achieve this. I could be achieving this after customizing Pretty Writer to be human readable if prettyPrint() to be void - just to make sure it would not modify state of data. ``` objectMapper.writeValueAsString(new TestClass()).prettyPrint(); – Vivek Adhikari Feb 23 '23 at 02:58
  • No, it's not valid JSON. In JSON it's illegal to have a literal line break character in strings. – RoToRa Feb 28 '23 at 13:31
  • Its a data that represent, so its valid. I know there is limitation on computation now for this. That's why I was more interested to have void method on objectmapper to print data that is human readable. As long as, if that doesn't updates the state of data - its a pretty print anyway. So i am asking if there is lib or existing code that someone is already done. Thanks – Vivek Adhikari Mar 01 '23 at 04:44

0 Answers0