Skip to content

Commit

Permalink
Add JsonWriteFeature skeleton for #481
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 26, 2018
1 parent e9e2a62 commit 6cfdce3
Showing 1 changed file with 152 additions and 0 deletions.
152 changes: 152 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/json/JsonWriteFeature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package com.fasterxml.jackson.core.json;

import java.math.BigDecimal;

import com.fasterxml.jackson.core.*;

/**
* Token writer features specific to JSON backend.
*
* @since 2.10
*/
public enum JsonWriteFeature
implements FormatFeature
{
// // // Support for non-standard data format constructs: comments

// // Quoting-related features

/**
* Feature that determines whether JSON Object field names are
* quoted using double-quotes, as specified by JSON specification
* or not. Ability to disable quoting was added to support use
* cases where they are not usually expected, which most commonly
* occurs when used straight from Javascript.
*<p>
* Feature is enabled by default (since it is required by JSON specification).
*/
QUOTE_FIELD_NAMES(true, JsonGenerator.Feature.QUOTE_FIELD_NAMES),

/**
* Feature that determines whether "NaN" ("not a number", that is, not
* real number) float/double values are output as JSON strings.
* The values checked are Double.Nan,
* Double.POSITIVE_INFINITY and Double.NEGATIVE_INIFINTY (and
* associated Float values).
* If feature is disabled, these numbers are still output using
* associated literal values, resulting in non-conforming
* output.
*<p>
* Feature is enabled by default.
*/
WRITE_NAN_AS_STRINGS(true, JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS),

/**
* Feature that forces all Java numbers to be written as JSON strings.
* Default state is 'false', meaning that Java numbers are to
* be serialized using basic numeric serialization (as JSON
* numbers, integral or floating point). If enabled, all such
* numeric values are instead written out as JSON Strings.
*<p>
* One use case is to avoid problems with Javascript limitations:
* since Javascript standard specifies that all number handling
* should be done using 64-bit IEEE 754 floating point values,
* result being that some 64-bit integer values can not be
* accurately represent (as mantissa is only 51 bit wide).
*<p>
* Feature is disabled by default.
*/
WRITE_NUMBERS_AS_STRINGS(false, JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS),

/**
* Feature that determines whether {@link java.math.BigDecimal} entries are
* serialized using {@link java.math.BigDecimal#toPlainString()} to prevent
* values to be written using scientific notation.
*<p>
* Feature is disabled by default, so default output mode is used; this generally
* depends on how {@link BigDecimal} has been created.
*/
WRITE_BIGDECIMAL_AS_PLAIN(false, JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN),

/**
* Feature that specifies that all characters beyond 7-bit ASCII
* range (i.e. code points of 128 and above) need to be output
* using format-specific escapes (for JSON, backslash escapes),
* if format uses escaping mechanisms (which is generally true
* for textual formats but not for binary formats).
*<p>
* Note that this setting may not necessarily make sense for all
* data formats (for example, binary formats typically do not use
* any escaping mechanisms; and some textual formats do not have
* general-purpose escaping); if so, settings is simply ignored.
* Put another way, effects of this feature are data-format specific.
*<p>
* Feature is disabled by default.
*/
// @SuppressWarnings("deprecation")
ESCAPE_NON_ASCII(false, JsonGenerator.Feature.ESCAPE_NON_ASCII),

//23-Nov-2015, tatu: for [core#223], if and when it gets implemented
/**
* Feature that specifies handling of UTF-8 content that contains
* characters beyond BMP (Basic Multilingual Plane), which are
* represented in UCS-2 (Java internal character encoding) as two
* "surrogate" characters. If feature is enabled, these surrogate
* pairs are separately escaped using backslash escapes; if disabled,
* native output (4-byte UTF-8 sequence, or, with char-backed output
* targets, writing of surrogates as is which is typically converted
* by {@link java.io.Writer} into 4-byte UTF-8 sequence eventually)
* is used.
*<p>
* Note that the original JSON specification suggests use of escaping;
* but that this is not correct from standard UTF-8 handling perspective.
* Because of two competing goals, this feature was added to allow either
* behavior to be used, but defaulting to UTF-8 specification compliant
* mode.
*<p>
* Feature is disabled by default.
*/
// ESCAPE_UTF8_SURROGATES(false, JsonGenerator.Feature.ESCAPE_UTF8_SURROGATES),

;

final private boolean _defaultState;
final private int _mask;

/**
* For backwards compatibility we may need to map to one of existing {@link JsonGenerator.Feature}s;
* if so, this is the feature to enable/disable.
*/
final private JsonGenerator.Feature _mappedFeature;

/**
* Method that calculates bit set (flags) of all features that
* are enabled by default.
*/
public static int collectDefaults()
{
int flags = 0;
for (JsonWriteFeature f : values()) {
if (f.enabledByDefault()) {
flags |= f.getMask();
}
}
return flags;
}

private JsonWriteFeature(boolean defaultState,
JsonGenerator.Feature mapTo) {
_defaultState = defaultState;
_mask = (1 << ordinal());
_mappedFeature = mapTo;
}

@Override
public boolean enabledByDefault() { return _defaultState; }
@Override
public int getMask() { return _mask; }
@Override
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }

public JsonGenerator.Feature mappedFeature() { return _mappedFeature; }
}

0 comments on commit 6cfdce3

Please sign in to comment.