-
-
Notifications
You must be signed in to change notification settings - Fork 816
Description
Jackson version: 2.15.3
Hi, I worked on Grafana dashboards json files and need to adapt the DefaultPrettyPrinter.
I would like tu suggest some improvment to custom DefaultPrettyPrinter without the need to override some methods :
- Object key and value separator : remove space after object key
current : "key" : "value"
expected : "key": "value"
I could not override this behavior, either with withSeparators
or withoutSpacesInObjectEntries
methods :
// could not change spaces before or after separators
public DefaultPrettyPrinter withSeparators(Separators separators) {
this._separators = separators;
this._objectFieldValueSeparatorWithSpaces = " " + separators.getObjectFieldValueSeparator() + " ";
return this;
}
// disable spaces before and after
public DefaultPrettyPrinter withoutSpacesInObjectEntries() {
return this._withSpaces(false);
}
// need to override this method
public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException {
if (this._spacesInObjectEntries) {
g.writeRaw(this._objectFieldValueSeparatorWithSpaces);
} else {
g.writeRaw(this._separators.getObjectFieldValueSeparator());
}
}
Need to override writeObjectFieldValueSeparator
method :
@Override
public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException {
g.writeRaw(": ");
}
Maybe add new method withSeparators(String prefix, Separators separators, String suffix)
?
public DefaultPrettyPrinter withSeparators(String prefix, Separators separators, String suffix) {
this._separators = separators;
this._objectFieldValueSeparatorWithSpaces = prefix + separators.getObjectFieldValueSeparator() + suffix;
return this;
}
- Array indenter : ident each array object
current :
{
"array" : [ {
[...]
}, {
[...]
} ]
}
expected :
{
"array" : [
{
[...]
},
{
[...]
}
]
}
This could be done with :
DefaultPrettyPrinter pp = new DefaultPrettyPrinter ();
pp.withArrayIndenter(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
But it was not easy to find the good indenter, maybe add some documentation to indenters availables or add withArrayIndenterLineFeed
method ?
- Empty object/array : remove space
current :
{
"links": [ ]
"custom": { }
}
expected :
{
"links": []
"custom": {}
}
I need to override writeEndObject
and writeEndArray
method to comment the behavior when nrOfValues = 0
:
@Override
public void writeEndObject(JsonGenerator g, int nrOfEntries) throws IOException {
if (!this._objectIndenter.isInline()) {
--this._nesting;
}
if (nrOfEntries > 0) {
this._objectIndenter.writeIndentation(g, this._nesting);
}
// else {
// g.writeRaw(' ');
// }
g.writeRaw('}');
}
@Override
public void writeEndArray(JsonGenerator g, int nrOfValues) throws IOException {
if (!this._arrayIndenter.isInline()) {
--this._nesting;
}
if (nrOfValues > 0) {
this._arrayIndenter.writeIndentation(g, this._nesting);
}
// else {
// g.writeRaw(' ');
// }
g.writeRaw(']');
}
Maybe add new methods withEmptyArrayContent(String content)
?
Finally, here is my custom class for those who could need it :
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
public class GrafanaPrettyPrinter extends DefaultPrettyPrinter {
@Override
public DefaultPrettyPrinter createInstance() {
GrafanaPrettyPrinter pp = new GrafanaPrettyPrinter();
pp._arrayIndenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
return pp;
}
@Override
public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException {
g.writeRaw(": ");
}
@Override
public void writeEndObject(JsonGenerator g, int nrOfEntries) throws IOException {
if (!this._objectIndenter.isInline()) {
--this._nesting;
}
if (nrOfEntries > 0) {
this._objectIndenter.writeIndentation(g, this._nesting);
}
g.writeRaw('}');
}
@Override
public void writeEndArray(JsonGenerator g, int nrOfValues) throws IOException {
if (!this._arrayIndenter.isInline()) {
--this._nesting;
}
if (nrOfValues > 0) {
this._arrayIndenter.writeIndentation(g, this._nesting);
}
g.writeRaw(']');
}
}