Gson: Request - Annotation for specific fields to not print as "Pretty"?

Created on 26 Jul 2017  路  1Comment  路  Source: google/gson

Example class:

public class Example {
    @Expose int a;
    @Expose boolean b;
    @Expose String c;
    @Expose int[] d; //Would be nice if we could get something like.. @Compact, which would bypass the pretty formatting.
}

Pretty Output:

{
"a": 999,
"b": false,
"c": "Lalalala",
"d": [
0,
1,
2,
3,
4
]
}

Desired Output: (Mostly pretty formatting, except for array "d")

{
"a": 999,
"b": false,
"c": "Lalalala",
"d": [0,1,2,3,4]
}

Is this already possible? If not, I'd like to suggest adding a new custom annotation which would indicate that the field does not want to be formatted "pretty".. such as an annotation called Compact

>All comments

This is not supported. If you need a custom formatter you'll have to supply it yourself iterating over a JsonObject model representation. Gson doesn't want to be in the business of formatting JSON and dealing with all the crazy customizations therein.

Was this page helpful?
0 / 5 - 0 ratings