Skip to content

Commit 6cfdce3

Browse files
committed
Add JsonWriteFeature skeleton for #481
1 parent e9e2a62 commit 6cfdce3

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.fasterxml.jackson.core.json;
2+
3+
import java.math.BigDecimal;
4+
5+
import com.fasterxml.jackson.core.*;
6+
7+
/**
8+
* Token writer features specific to JSON backend.
9+
*
10+
* @since 2.10
11+
*/
12+
public enum JsonWriteFeature
13+
implements FormatFeature
14+
{
15+
// // // Support for non-standard data format constructs: comments
16+
17+
// // Quoting-related features
18+
19+
/**
20+
* Feature that determines whether JSON Object field names are
21+
* quoted using double-quotes, as specified by JSON specification
22+
* or not. Ability to disable quoting was added to support use
23+
* cases where they are not usually expected, which most commonly
24+
* occurs when used straight from Javascript.
25+
*<p>
26+
* Feature is enabled by default (since it is required by JSON specification).
27+
*/
28+
QUOTE_FIELD_NAMES(true, JsonGenerator.Feature.QUOTE_FIELD_NAMES),
29+
30+
/**
31+
* Feature that determines whether "NaN" ("not a number", that is, not
32+
* real number) float/double values are output as JSON strings.
33+
* The values checked are Double.Nan,
34+
* Double.POSITIVE_INFINITY and Double.NEGATIVE_INIFINTY (and
35+
* associated Float values).
36+
* If feature is disabled, these numbers are still output using
37+
* associated literal values, resulting in non-conforming
38+
* output.
39+
*<p>
40+
* Feature is enabled by default.
41+
*/
42+
WRITE_NAN_AS_STRINGS(true, JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS),
43+
44+
/**
45+
* Feature that forces all Java numbers to be written as JSON strings.
46+
* Default state is 'false', meaning that Java numbers are to
47+
* be serialized using basic numeric serialization (as JSON
48+
* numbers, integral or floating point). If enabled, all such
49+
* numeric values are instead written out as JSON Strings.
50+
*<p>
51+
* One use case is to avoid problems with Javascript limitations:
52+
* since Javascript standard specifies that all number handling
53+
* should be done using 64-bit IEEE 754 floating point values,
54+
* result being that some 64-bit integer values can not be
55+
* accurately represent (as mantissa is only 51 bit wide).
56+
*<p>
57+
* Feature is disabled by default.
58+
*/
59+
WRITE_NUMBERS_AS_STRINGS(false, JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS),
60+
61+
/**
62+
* Feature that determines whether {@link java.math.BigDecimal} entries are
63+
* serialized using {@link java.math.BigDecimal#toPlainString()} to prevent
64+
* values to be written using scientific notation.
65+
*<p>
66+
* Feature is disabled by default, so default output mode is used; this generally
67+
* depends on how {@link BigDecimal} has been created.
68+
*/
69+
WRITE_BIGDECIMAL_AS_PLAIN(false, JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN),
70+
71+
/**
72+
* Feature that specifies that all characters beyond 7-bit ASCII
73+
* range (i.e. code points of 128 and above) need to be output
74+
* using format-specific escapes (for JSON, backslash escapes),
75+
* if format uses escaping mechanisms (which is generally true
76+
* for textual formats but not for binary formats).
77+
*<p>
78+
* Note that this setting may not necessarily make sense for all
79+
* data formats (for example, binary formats typically do not use
80+
* any escaping mechanisms; and some textual formats do not have
81+
* general-purpose escaping); if so, settings is simply ignored.
82+
* Put another way, effects of this feature are data-format specific.
83+
*<p>
84+
* Feature is disabled by default.
85+
*/
86+
// @SuppressWarnings("deprecation")
87+
ESCAPE_NON_ASCII(false, JsonGenerator.Feature.ESCAPE_NON_ASCII),
88+
89+
//23-Nov-2015, tatu: for [core#223], if and when it gets implemented
90+
/**
91+
* Feature that specifies handling of UTF-8 content that contains
92+
* characters beyond BMP (Basic Multilingual Plane), which are
93+
* represented in UCS-2 (Java internal character encoding) as two
94+
* "surrogate" characters. If feature is enabled, these surrogate
95+
* pairs are separately escaped using backslash escapes; if disabled,
96+
* native output (4-byte UTF-8 sequence, or, with char-backed output
97+
* targets, writing of surrogates as is which is typically converted
98+
* by {@link java.io.Writer} into 4-byte UTF-8 sequence eventually)
99+
* is used.
100+
*<p>
101+
* Note that the original JSON specification suggests use of escaping;
102+
* but that this is not correct from standard UTF-8 handling perspective.
103+
* Because of two competing goals, this feature was added to allow either
104+
* behavior to be used, but defaulting to UTF-8 specification compliant
105+
* mode.
106+
*<p>
107+
* Feature is disabled by default.
108+
*/
109+
// ESCAPE_UTF8_SURROGATES(false, JsonGenerator.Feature.ESCAPE_UTF8_SURROGATES),
110+
111+
;
112+
113+
final private boolean _defaultState;
114+
final private int _mask;
115+
116+
/**
117+
* For backwards compatibility we may need to map to one of existing {@link JsonGenerator.Feature}s;
118+
* if so, this is the feature to enable/disable.
119+
*/
120+
final private JsonGenerator.Feature _mappedFeature;
121+
122+
/**
123+
* Method that calculates bit set (flags) of all features that
124+
* are enabled by default.
125+
*/
126+
public static int collectDefaults()
127+
{
128+
int flags = 0;
129+
for (JsonWriteFeature f : values()) {
130+
if (f.enabledByDefault()) {
131+
flags |= f.getMask();
132+
}
133+
}
134+
return flags;
135+
}
136+
137+
private JsonWriteFeature(boolean defaultState,
138+
JsonGenerator.Feature mapTo) {
139+
_defaultState = defaultState;
140+
_mask = (1 << ordinal());
141+
_mappedFeature = mapTo;
142+
}
143+
144+
@Override
145+
public boolean enabledByDefault() { return _defaultState; }
146+
@Override
147+
public int getMask() { return _mask; }
148+
@Override
149+
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
150+
151+
public JsonGenerator.Feature mappedFeature() { return _mappedFeature; }
152+
}

0 commit comments

Comments
 (0)