Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.core.util;
  
  import java.io.*;
  import java.util.Arrays;
  
Default com.fasterxml.jackson.core.PrettyPrinter implementation that uses 2-space indentation with platform-default linefeeds. Usually this class is not instantiated directly, but instead method com.fasterxml.jackson.core.JsonGenerator.useDefaultPrettyPrinter() is used, which will use an instance of this class for operation.
 
 @SuppressWarnings("serial")
 public class DefaultPrettyPrinter
         java.io.Serializable
 {
     private static final long serialVersionUID = -5512586643324525213L;

    
Constant that specifies default "root-level" separator to use between root values: a single space character.

Since:
2.1
 
     public final static SerializedString DEFAULT_ROOT_VALUE_SEPARATOR = new SerializedString(" ");
    
    
Interface that defines objects that can produce indentation used to separate object entries and array values. Indentation in this context just means insertion of white space, independent of whether linefeeds are output.
 
     public interface Indenter
     {
         void writeIndentation(JsonGenerator jgint level)
             throws IOExceptionJsonGenerationException;

        

Returns:
True if indenter is considered inline (does not add linefeeds), false otherwise
 
         boolean isInline();
     }
     
     // // // Config, indentation
 
    
By default, let's use only spaces to separate array values.
 
     protected Indenter _arrayIndenter = .;

    
By default, let's use linefeed-adding indenter for separate object entries. We'll further configure indenter to use system-specific linefeeds, and 2 spaces per level (as opposed to, say, single tabs)
 
     protected Indenter _objectIndenter = .;

    
String printed between root-level values, if any.
 
     protected final SerializableString _rootSeparator;
     
     // // // Config, other white space configuration
 
    
By default we will add spaces around colons used to separate object fields and values. If disabled, will not use spaces around colon.
 
     protected boolean _spacesInObjectEntries = true;
 
     // // // State:
 
    
Number of open levels of nesting. Used to determine amount of indentation to use.
 
     protected transient int _nesting = 0;
 
     /*
     /**********************************************************
     /* Life-cycle (construct, configure)
     /**********************************************************
     */
 
     public DefaultPrettyPrinter() {
         this();
     }

    
Constructor that specifies separator String to use between root values; if null, no separator is printed.

Note: simply constructs a com.fasterxml.jackson.core.io.SerializedString out of parameter, calls DefaultPrettyPrinter(com.fasterxml.jackson.core.SerializableString)

Parameters:
rootSeparator
Since:
2.1
    public DefaultPrettyPrinter(String rootSeparator) {
        this((rootSeparator == null) ? null : new SerializedString(rootSeparator));
    }

    
Constructor that specifies separator String to use between root values; if null, no separator is printed.

Parameters:
rootSeparator
Since:
2.1
    public DefaultPrettyPrinter(SerializableString rootSeparator) {
         = rootSeparator;
    }
    
        this(basebase._rootSeparator);
    }
            SerializableString rootSeparator)
    {
         = base._arrayIndenter;
         = base._objectIndenter;
         = base._spacesInObjectEntries;
         = base._nesting;
         = rootSeparator;
    }
    {
        if ( == rootSeparator ||
                (rootSeparator != null && rootSeparator.equals())) {
            return this;
        }
        return new DefaultPrettyPrinter(thisrootSeparator);
    }
    
    public void indentArraysWith(Indenter i)
    {
         = (i == null) ? . : i;
    }
    public void indentObjectsWith(Indenter i)
    {
         = (i == null) ? . : i;
    }
    public void spacesInObjectEntries(boolean b) {  = b; }
    /*
    /**********************************************************
    /* Instantiatable impl
    /**********************************************************
     */
    
    @Override
        return new DefaultPrettyPrinter(this);
    }
    
    /*
    /**********************************************************
    /* PrettyPrinter impl
    /**********************************************************
     */
    @Override
    public void writeRootValueSeparator(JsonGenerator jg)
        throws IOExceptionJsonGenerationException
    {
        if ( != null) {
            jg.writeRaw();
        }
    }
    @Override
    public void writeStartObject(JsonGenerator jg)
        throws IOExceptionJsonGenerationException
    {
        jg.writeRaw('{');
        if (!.isInline()) {
            ++;
        }
    }
    @Override
    public void beforeObjectEntries(JsonGenerator jg)
        throws IOExceptionJsonGenerationException
    {
    }

    
Method called after an object field has been output, but before the value is output.

Default handling (without pretty-printing) will output a single colon to separate the two. Pretty-printer is to output a colon as well, but can surround that with other (white-space) decoration.

    @Override
        throws IOExceptionJsonGenerationException
    {
        if () {
            jg.writeRaw(" : ");
        } else {
            jg.writeRaw(':');
        }
    }

    
Method called after an object entry (field:value) has been completely output, and before another value is to be output.

Default handling (without pretty-printing) will output a single comma to separate the two. Pretty-printer is to output a comma as well, but can surround that with other (white-space) decoration.

    @Override
    public void writeObjectEntrySeparator(JsonGenerator jg)
        throws IOExceptionJsonGenerationException
    {
        jg.writeRaw(',');
    }
    @Override
    public void writeEndObject(JsonGenerator jgint nrOfEntries)
        throws IOExceptionJsonGenerationException
    {
        if (!.isInline()) {
            --;
        }
        if (nrOfEntries > 0) {
            .writeIndentation(jg);
        } else {
            jg.writeRaw(' ');
        }
        jg.writeRaw('}');
    }
    @Override
    public void writeStartArray(JsonGenerator jg)
        throws IOExceptionJsonGenerationException
    {
        if (!.isInline()) {
            ++;
        }
        jg.writeRaw('[');
    }
    @Override
    public void beforeArrayValues(JsonGenerator jg)
        throws IOExceptionJsonGenerationException
    {
    }

    
Method called after an array value has been completely output, and before another value is to be output.

Default handling (without pretty-printing) will output a single comma to separate the two. Pretty-printer is to output a comma as well, but can surround that with other (white-space) decoration.

    @Override
    public void writeArrayValueSeparator(JsonGenerator jg)
        throws IOExceptionJsonGenerationException
    {
        jg.writeRaw(',');
    }
    @Override
    public void writeEndArray(JsonGenerator jgint nrOfValues)
        throws IOExceptionJsonGenerationException
    {
        if (!.isInline()) {
            --;
        }
        if (nrOfValues > 0) {
            .writeIndentation(jg);
        } else {
            jg.writeRaw(' ');
        }
        jg.writeRaw(']');
    }
    /*
    /**********************************************************
    /* Helper classes
    /**********************************************************
     */

    
Dummy implementation that adds no indentation whatsoever
    public static class NopIndenter
        implements Indenterjava.io.Serializable
    {
        public static final NopIndenter instance = new NopIndenter();
        @Override
        public void writeIndentation(JsonGenerator jgint level)
            throws IOExceptionJsonGenerationException
        { }
        @Override
        public boolean isInline() { return true; }
    }

    
This is a very simple indenter that only every adds a single space for indentation. It is used as the default indenter for array values.
    public static class FixedSpaceIndenter
        extends NopIndenter
    {
        public static final FixedSpaceIndenter instance = new FixedSpaceIndenter();
        @Override
        public void writeIndentation(JsonGenerator jgint level)
            throws IOExceptionJsonGenerationException
        {
            jg.writeRaw(' ');
        }
        @Override
        public boolean isInline() { return true; }
    }

    
Default linefeed-based indenter uses system-specific linefeeds and 2 spaces for indentation per level.
    public static class Lf2SpacesIndenter
        extends NopIndenter
    {
        public static final Lf2SpacesIndenter instance = new Lf2SpacesIndenter();
        private final static String SYS_LF;
        static {
            String lf = null;
            try {
                lf = System.getProperty("line.separator");
            } catch (Throwable t) { } // access exception?
             = (lf == null) ? "\n" : lf;
        }
        final static int SPACE_COUNT = 64;
        final static char[] SPACES = new char[];
        static {
            Arrays.fill(' ');
        }
        @Override
        public boolean isInline() { return false; }
        @Override
        public void writeIndentation(JsonGenerator jgint level)
            throws IOExceptionJsonGenerationException
        {
            jg.writeRaw();
            if (level > 0) { // should we err on negative values (as there's some flaw?)
                level += level// 2 spaces per level
                while (level > ) { // should never happen but...
                    jg.writeRaw(, 0, ); 
                    level -= .;
                }
                jg.writeRaw(, 0, level);
            }
        }
    }
New to GrepCode? Check out our FAQ X