Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring spaces before and/or after the colon in DefaultPrettyPrinter #1042

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer;
import com.fasterxml.jackson.core.util.BufferRecycler;
import com.fasterxml.jackson.core.util.BufferRecyclers;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.core.util.JacksonFeature;
import com.fasterxml.jackson.core.util.Separators;

/**
* The main factory class of Jackson package, used to configure and
Expand Down Expand Up @@ -197,7 +197,7 @@ public static int collectDefaults() {
*/
protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();

public final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
public final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = new SerializedString(Separators.DEFAULT_ROOT_VALUE_SEPARATOR);

/**
* @since 2.10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.fasterxml.jackson.core.io.CharTypes;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.core.util.JacksonFeatureSet;
import com.fasterxml.jackson.core.util.VersionUtil;

Expand Down Expand Up @@ -92,7 +91,7 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
* @since 2.1
*/
protected SerializableString _rootValueSeparator
= DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
= JsonFactory.DEFAULT_ROOT_VALUE_SEPARATOR;

/**
* Flag that is set if quoting is not to be added around
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class DefaultPrettyPrinter
* root values: a single space character.
*
* @since 2.1
* @deprecated in 2.16. Use the Separators API instead.
*/
@Deprecated
public final static SerializedString DEFAULT_ROOT_VALUE_SEPARATOR = new SerializedString(" ");

/**
Expand Down Expand Up @@ -70,16 +72,21 @@ public interface Indenter

/**
* String printed between root-level values, if any.
*
* @deprecated in 2.16. Use Separators API instead.
*/
protected final SerializableString _rootSeparator;
protected 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.
*
* @deprecated in 2.16. Use Separators API instead.
*/
@Deprecated
protected boolean _spacesInObjectEntries = true;

// // // State:
Expand All @@ -99,15 +106,25 @@ public interface Indenter
* @since 2.9
*/
protected String _objectFieldValueSeparatorWithSpaces;

/**
* @since 2.16
*/
protected String _objectEntrySeparator;

/**
* @since 2.16
*/
protected String _arrayValueSeparator;

/*
/**********************************************************
/* Life-cycle (construct, configure)
/**********************************************************
*/

public DefaultPrettyPrinter() {
this(DEFAULT_ROOT_VALUE_SEPARATOR);
this(DEFAULT_SEPARATORS);
}

/**
Expand All @@ -118,7 +135,9 @@ public DefaultPrettyPrinter() {
* calls {@link #DefaultPrettyPrinter(SerializableString)}
*
* @param rootSeparator String to use as root value separator
* @deprecated in 2.16. Use the Separators API instead.
*/
@Deprecated
public DefaultPrettyPrinter(String rootSeparator) {
this((rootSeparator == null) ? null : new SerializedString(rootSeparator));
}
Expand All @@ -128,16 +147,17 @@ public DefaultPrettyPrinter(String rootSeparator) {
* if null, no separator is printed.
*
* @param rootSeparator String to use as root value separator
* @deprecated in 2.16. Use the Separators API instead.
*/
@Deprecated
public DefaultPrettyPrinter(SerializableString rootSeparator) {
_rootSeparator = rootSeparator;
withSeparators(DEFAULT_SEPARATORS);
}

public DefaultPrettyPrinter(DefaultPrettyPrinter base) {
this(base, base._rootSeparator);
this(DEFAULT_SEPARATORS.withRootSeparator(rootSeparator.getValue()));
}

/**
* @deprecated in 2.16. Use the Separators API instead.
*/
@Deprecated
public DefaultPrettyPrinter(DefaultPrettyPrinter base,
SerializableString rootSeparator)
{
Expand All @@ -148,17 +168,58 @@ public DefaultPrettyPrinter(DefaultPrettyPrinter base,

_separators = base._separators;
_objectFieldValueSeparatorWithSpaces = base._objectFieldValueSeparatorWithSpaces;
_objectEntrySeparator = base._objectEntrySeparator;
_arrayValueSeparator = base._arrayValueSeparator;

_rootSeparator = rootSeparator;
}

/**
* @since 2.16
*/
public DefaultPrettyPrinter(Separators separators)
{
_separators = separators;

_rootSeparator = separators.getRootSeparator() == null ? null : new SerializedString(separators.getRootSeparator());
_objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSpacing().apply(
separators.getObjectFieldValueSeparator());
_objectEntrySeparator = separators.getObjectEntrySpacing().apply(separators.getObjectEntrySeparator());
_arrayValueSeparator = separators.getArrayValueSpacing().apply(separators.getArrayValueSeparator());
}

/**
* Copy constructor
*
* @since 2.16
*/
public DefaultPrettyPrinter(DefaultPrettyPrinter base) {
_rootSeparator = base._rootSeparator;

_arrayIndenter = base._arrayIndenter;
_objectIndenter = base._objectIndenter;
_spacesInObjectEntries = base._spacesInObjectEntries;
_nesting = base._nesting;

_separators = base._separators;
_objectFieldValueSeparatorWithSpaces = base._objectFieldValueSeparatorWithSpaces;
_objectEntrySeparator = base._objectEntrySeparator;
_arrayValueSeparator = base._arrayValueSeparator;
}

/**
* @deprecated in 2.16. Use the Separators API instead.
*/
@Deprecated
public DefaultPrettyPrinter withRootSeparator(SerializableString rootSeparator)
{
if (_rootSeparator == rootSeparator ||
(rootSeparator != null && rootSeparator.equals(_rootSeparator))) {
return this;
}
return new DefaultPrettyPrinter(this, rootSeparator);
Separators separators = _separators.withRootSeparator(rootSeparator == null ? null : rootSeparator.getValue());
return new DefaultPrettyPrinter(this)
.withSeparators(separators);
}

/**
Expand All @@ -167,7 +228,9 @@ public DefaultPrettyPrinter withRootSeparator(SerializableString rootSeparator)
* @return This pretty-printer instance (for call chaining)
*
* @since 2.6
* @deprecated in 2.16. Use the Separators API instead.
*/
@Deprecated
public DefaultPrettyPrinter withRootSeparator(String rootSeparator) {
return withRootSeparator((rootSeparator == null) ? null : new SerializedString(rootSeparator));
}
Expand All @@ -180,7 +243,7 @@ public void indentObjectsWith(Indenter i) {
_objectIndenter = (i == null) ? NopIndenter.instance : i;
}

// @since 2.3
/** @since 2.3 */
public DefaultPrettyPrinter withArrayIndenter(Indenter i) {
if (i == null) {
i = NopIndenter.instance;
Expand All @@ -193,7 +256,7 @@ public DefaultPrettyPrinter withArrayIndenter(Indenter i) {
return pp;
}

// @since 2.3
/** @since 2.3 */
public DefaultPrettyPrinter withObjectIndenter(Indenter i) {
if (i == null) {
i = NopIndenter.instance;
Expand All @@ -215,7 +278,9 @@ public DefaultPrettyPrinter withObjectIndenter(Indenter i) {
* @return This pretty-printer instance (for call chaining)
*
* @since 2.3
* @deprecated in 2.16. Use the Separators API instead.
*/
@Deprecated
public DefaultPrettyPrinter withSpacesInObjectEntries() {
return _withSpaces(true);
}
Expand All @@ -229,7 +294,9 @@ public DefaultPrettyPrinter withSpacesInObjectEntries() {
* @return This pretty-printer instance (for call chaining)
*
* @since 2.3
* @deprecated in 2.16. Use the Separators API instead.
*/
@Deprecated
public DefaultPrettyPrinter withoutSpacesInObjectEntries() {
return _withSpaces(false);
}
Expand All @@ -239,7 +306,9 @@ protected DefaultPrettyPrinter _withSpaces(boolean state)
if (_spacesInObjectEntries == state) {
return this;
}
DefaultPrettyPrinter pp = new DefaultPrettyPrinter(this);

Separators copy = _separators.withObjectFieldValueSpacing(state ? Separators.Spacing.BOTH : Separators.Spacing.NONE);
DefaultPrettyPrinter pp = withSeparators(copy);
pp._spacesInObjectEntries = state;
return pp;
}
Expand All @@ -254,9 +323,16 @@ protected DefaultPrettyPrinter _withSpaces(boolean state)
* @since 2.9
*/
public DefaultPrettyPrinter withSeparators(Separators separators) {
_separators = separators;
_objectFieldValueSeparatorWithSpaces = " " + separators.getObjectFieldValueSeparator() + " ";
return this;
DefaultPrettyPrinter result = new DefaultPrettyPrinter(this);
result._separators = separators;

result._rootSeparator = separators.getRootSeparator() == null ? null : new SerializedString(separators.getRootSeparator());
result._objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSpacing().apply(
separators.getObjectFieldValueSeparator());
result._objectEntrySeparator = separators.getObjectEntrySpacing().apply(separators.getObjectEntrySeparator());
result._arrayValueSeparator = separators.getArrayValueSpacing().apply(separators.getArrayValueSeparator());

return result;
}

/*
Expand Down Expand Up @@ -315,11 +391,7 @@ public void beforeObjectEntries(JsonGenerator g) throws IOException
@Override
public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException
{
if (_spacesInObjectEntries) {
g.writeRaw(_objectFieldValueSeparatorWithSpaces);
} else {
g.writeRaw(_separators.getObjectFieldValueSeparator());
}
g.writeRaw(_objectFieldValueSeparatorWithSpaces);
}

/**
Expand All @@ -334,7 +406,7 @@ public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException
@Override
public void writeObjectEntrySeparator(JsonGenerator g) throws IOException
{
g.writeRaw(_separators.getObjectEntrySeparator());
g.writeRaw(_objectEntrySeparator);
_objectIndenter.writeIndentation(g, _nesting);
}

Expand Down Expand Up @@ -378,7 +450,7 @@ public void beforeArrayValues(JsonGenerator g) throws IOException {
@Override
public void writeArrayValueSeparator(JsonGenerator g) throws IOException
{
g.writeRaw(_separators.getArrayValueSeparator());
g.writeRaw(_arrayValueSeparator);
_arrayIndenter.writeIndentation(g, _nesting);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.core.util.Separators.Spacing;

/**
* {@link PrettyPrinter} implementation that adds no indentation,
Expand Down Expand Up @@ -46,7 +47,7 @@ public MinimalPrettyPrinter() {

public MinimalPrettyPrinter(String rootValueSeparator) {
_rootValueSeparator = rootValueSeparator;
_separators = DEFAULT_SEPARATORS;
_separators = DEFAULT_SEPARATORS.withObjectFieldValueSpacing(Spacing.NONE);
}

public void setRootValueSeparator(String sep) {
Expand Down
Loading