3

I have data that looks like this:

{
    "status": "success",
    "data": {
        "irrelevant": {
            "serialNumber": "XYZ",
            "version": "4.6"
        },
        "data": {
            "lib": {
                "files": [
                    "data1",
                    "data2",
                    "data3",
                    "data4" 
                ],
                "another file": [
                    "file.jar",
                    "lib.jar" 
                ],
                "dirs": []
            },
            "jvm": {
                "maxHeap": 10,
                "maxPermSize": "12"
            },
            "serverId": "134",
            "version": "2.3"
        }
    }
}

Here is the function I'm using to prettify the JSON data:

public static String stringify(Object o, int space) {
    ObjectMapper mapper = new ObjectMapper();
    try {
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
    } catch (Exception e) {
        return null;
    }
}

I am using the Jackson JSON Processor to format JSON data into a String. For some reason the JSON format is not in the format that I need. When passing the data to that function, the format I'm getting is this:

{
    "status": "success",
    "data": {
        "irrelevant": {
            "serialNumber": "XYZ",
            "version": "4.6"
        },
        "another data": {
            "lib": {
                "files": [ "data1", "data2", "data3", "data4" ],
                "another file": [ "file.jar", "lib.jar" ],
                "dirs": []
            },
            "jvm": {
                "maxHeap": 10,
                "maxPermSize": "12"
            },
            "serverId": "134",
            "version": "2.3"
        }
    }
}

As you can see under the "another data" object, the arrays are displayed as one whole line instead of a new line for each item in the array. I'm not sure how to modify my stringify function for it to format the JSON data correctly.

2 Answers 2

5

You should check how DefaultPrettyPrinter looks like. Really interesting in this class is the _arrayIndenter property. The default value for this property is FixedSpaceIndenter class. You should change it with Lf2SpacesIndenter class.

Your method should looks like this:

public static String stringify(Object o) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
        printer.indentArraysWith(new Lf2SpacesIndenter());
        return mapper.writer(printer).writeValueAsString(o);
    } catch (Exception e) {
        return null;
    }
}
0
3

I don't have enough reputation to add the comment, but referring to the above answer
Lf2SpacesIndenter is removed from the newer Jackson's API (2.7 and up), so instead use:

printer.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);

Source of the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.