Skip to content

Instantly share code, notes, and snippets.

@chuwy
Created June 17, 2015 12:29
Show Gist options
  • Save chuwy/84ed38389b50d766f17f to your computer and use it in GitHub Desktop.
Save chuwy/84ed38389b50d766f17f to your computer and use it in GitHub Desktop.
package com.snowplowanalytics.schemaguru.utils;
import java.io.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
public class SuperPrinter extends DefaultPrettyPrinter
{
protected transient int _nesting = 0;
public SuperPrinter() {}
public SuperPrinter(SuperPrinter base)
{
_arrayIndenter = base._arrayIndenter;
_objectIndenter = base._objectIndenter;
_nesting = base._nesting;
}
@Override
public SuperPrinter createInstance() {
return new SuperPrinter(this);
}
@Override
public void writeStartObject(JsonGenerator jg) throws IOException, JsonGenerationException
{
jg.writeRaw('{');
if (!_objectIndenter.isInline()) {
++_nesting;
}
}
@Override
public void beforeObjectEntries(JsonGenerator jg) throws IOException, JsonGenerationException
{
_objectIndenter.writeIndentation(jg, _nesting);
}
@Override
public void writeObjectFieldValueSeparator(JsonGenerator jg) throws IOException, JsonGenerationException
{
jg.writeRaw(':');
}
@Override
public void writeObjectEntrySeparator(JsonGenerator jg) throws IOException, JsonGenerationException
{
jg.writeRaw(',');
_objectIndenter.writeIndentation(jg, _nesting);
}
@Override
public void writeEndObject(JsonGenerator jg, int nrOfEntries) throws IOException, JsonGenerationException
{
if (!_objectIndenter.isInline()) {
--_nesting;
}
if (nrOfEntries > 0) {
_objectIndenter.writeIndentation(jg, _nesting);
} else {
jg.writeRaw(' ');
}
jg.writeRaw('}');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment