Skip to content

Commit c4ab04d

Browse files
committed
refactor: use Context configuration for PrettyFormatInlineArrays instead of Feature bit
Address review feedback about limited bits in 64-bit Feature bitmask. Move inline arrays configuration from JSONWriter.Feature enum to JSONWriter.Context, following the pattern used by other non-Feature configuration like dateFormat, zoneId, and maxLevel. API changes: - Remove PrettyFormatInlineArrays from JSONWriter.Feature enum - Add Context.setPrettyFormatInlineArrays(boolean) for per-call config - Add JSONFactory.setDefaultWriterPrettyFormatInlineArrays(boolean) for global default The implementation uses a levelArray bitmask to track array vs object context at each nesting level, enabling proper indentation behavior where objects remain pretty-printed while arrays stay inline.
1 parent 150f8e4 commit c4ab04d

5 files changed

Lines changed: 195 additions & 59 deletions

File tree

core/src/main/java/com/alibaba/fastjson2/JSONFactory.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public static String getProperty(String key) {
7979
static String defaultWriterFormat;
8080
static ZoneId defaultWriterZoneId;
8181
static boolean defaultWriterAlphabetic;
82+
static boolean defaultWriterPrettyFormatInlineArrays;
8283
static boolean defaultSkipTransient;
8384
static final boolean disableReferenceDetect;
8485
static final boolean disableArrayMapping;
@@ -743,6 +744,26 @@ public static void setDefaultWriterAlphabetic(boolean defaultWriterAlphabetic) {
743744
defaultObjectWriterProvider.setAlphabetic(defaultWriterAlphabetic);
744745
}
745746

747+
/**
748+
* Checks if the default writer uses inline arrays when pretty formatting.
749+
*
750+
* @return true if inline arrays is enabled, false otherwise
751+
* @since 2.0.61
752+
*/
753+
public static boolean isDefaultWriterPrettyFormatInlineArrays() {
754+
return defaultWriterPrettyFormatInlineArrays;
755+
}
756+
757+
/**
758+
* Sets whether the default writer should use inline arrays when pretty formatting.
759+
*
760+
* @param inlineArrays true to enable inline arrays, false to disable
761+
* @since 2.0.61
762+
*/
763+
public static void setDefaultWriterPrettyFormatInlineArrays(boolean inlineArrays) {
764+
JSONFactory.defaultWriterPrettyFormatInlineArrays = inlineArrays;
765+
}
766+
746767
/**
747768
* Checks if reference detection is disabled.
748769
*

core/src/main/java/com/alibaba/fastjson2/JSONWriter.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected JSONWriter(
129129
} else {
130130
pretty = PRETTY_NON;
131131
}
132-
prettyInlineArrays = (context.features & PrettyFormatInlineArrays.mask) != 0;
132+
prettyInlineArrays = context.prettyFormatInlineArrays;
133133
}
134134

135135
/**
@@ -3256,6 +3256,7 @@ public static final class Context {
32563256
LabelFilter labelFilter;
32573257
ContextValueFilter contextValueFilter;
32583258
ContextNameFilter contextNameFilter;
3259+
boolean prettyFormatInlineArrays;
32593260

32603261
/**
32613262
* Creates a new Context with the specified object writer provider.
@@ -3272,6 +3273,7 @@ public Context(ObjectWriterProvider provider) {
32723273
this.provider = provider;
32733274
this.zoneId = defaultWriterZoneId;
32743275
this.maxLevel = defaultMaxLevel;
3276+
this.prettyFormatInlineArrays = JSONFactory.defaultWriterPrettyFormatInlineArrays;
32753277

32763278
String format = defaultWriterFormat;
32773279
if (format != null) {
@@ -3289,6 +3291,7 @@ public Context(Feature... features) {
32893291
this.provider = getDefaultObjectWriterProvider();
32903292
this.zoneId = defaultWriterZoneId;
32913293
this.maxLevel = defaultMaxLevel;
3294+
this.prettyFormatInlineArrays = JSONFactory.defaultWriterPrettyFormatInlineArrays;
32923295

32933296
String format = defaultWriterFormat;
32943297
if (format != null) {
@@ -3311,6 +3314,7 @@ public Context(String format, Feature... features) {
33113314
this.provider = getDefaultObjectWriterProvider();
33123315
this.zoneId = defaultWriterZoneId;
33133316
this.maxLevel = defaultMaxLevel;
3317+
this.prettyFormatInlineArrays = JSONFactory.defaultWriterPrettyFormatInlineArrays;
33143318

33153319
for (int i = 0; i < features.length; i++) {
33163320
this.features |= features[i].mask;
@@ -3340,6 +3344,7 @@ public Context(ObjectWriterProvider provider, Feature... features) {
33403344
this.provider = provider;
33413345
this.zoneId = defaultWriterZoneId;
33423346
this.maxLevel = defaultMaxLevel;
3347+
this.prettyFormatInlineArrays = JSONFactory.defaultWriterPrettyFormatInlineArrays;
33433348

33443349
for (int i = 0; i < features.length; i++) {
33453350
this.features |= features[i].mask;
@@ -3883,6 +3888,26 @@ public int getMaxLevel() {
38833888
public void setMaxLevel(int maxLevel) {
38843889
this.maxLevel = maxLevel;
38853890
}
3891+
3892+
/**
3893+
* Checks if inline arrays is enabled for pretty formatting.
3894+
*
3895+
* @return true if inline arrays is enabled, false otherwise
3896+
* @since 2.0.61
3897+
*/
3898+
public boolean isPrettyFormatInlineArrays() {
3899+
return prettyFormatInlineArrays;
3900+
}
3901+
3902+
/**
3903+
* Sets whether to use inline arrays when pretty formatting.
3904+
*
3905+
* @param prettyFormatInlineArrays true to enable inline arrays, false to disable
3906+
* @since 2.0.61
3907+
*/
3908+
public void setPrettyFormatInlineArrays(boolean prettyFormatInlineArrays) {
3909+
this.prettyFormatInlineArrays = prettyFormatInlineArrays;
3910+
}
38863911
}
38873912

38883913
protected static final long MASK_WRITE_MAP_NULL_VALUE = 1 << 4;
@@ -4441,18 +4466,7 @@ public enum Feature {
44414466
*
44424467
* @since 2.0.61
44434468
*/
4444-
WriteFloatSpecialAsString(1L << 45),
4445-
4446-
/**
4447-
* Feature that controls whether arrays are formatted inline (on a single line) when using PrettyFormat.
4448-
* When enabled along with PrettyFormat, array elements will be written on a single line like [1, 2, 3]
4449-
* instead of having each element on a separate line.
4450-
*
4451-
* <p>This feature requires {@link PrettyFormat} to also be enabled to have any effect.</p>
4452-
*
4453-
* @since 2.0.61
4454-
*/
4455-
PrettyFormatInlineArrays(1L << 46);
4469+
WriteFloatSpecialAsString(1L << 45);
44564470

44574471
public final long mask;
44584472

0 commit comments

Comments
 (0)