public static final class JSONWriter.Context
extends java.lang.Object
The Context class is responsible for:
Context instances can be created in several ways:
// Using default configuration
JSONWriter.Context context = new JSONWriter.Context();
// With specific features enabled
JSONWriter.Context context = new JSONWriter.Context(
JSONWriter.Feature.PrettyFormat,
JSONWriter.Feature.WriteMapNullValue
);
// With custom date format
JSONWriter.Context context = new JSONWriter.Context("yyyy-MM-dd HH:mm:ss");
// With custom provider and features
ObjectWriterProvider provider = JSONFactory.getDefaultObjectWriterProvider();
JSONWriter.Context context = new JSONWriter.Context(provider,
JSONWriter.Feature.PrettyFormat
);
Once created, a Context can be configured further:
context.setZoneId(ZoneId.of("UTC"));
context.setLocale(Locale.US);
context.setMaxLevel(1000);
context.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Context instances are typically used when creating JSONWriter instances:
JSONWriter.Context context = new JSONWriter.Context();
context.config(JSONWriter.Feature.PrettyFormat);
try (JSONWriter writer = JSONWriter.of(context)) {
writer.writeAny(object);
String json = writer.toString();
}
Note that Context instances are not thread-safe and should not be shared between multiple concurrent writing operations. Each JSONWriter should have its own Context instance or use the default context provided by factory methods.
JSONWriter,
JSONWriter.Feature,
ObjectWriterProvider| Modifier and Type | Field and Description |
|---|---|
(package private) AfterFilter |
afterFilter |
(package private) BeforeFilter |
beforeFilter |
(package private) ContextNameFilter |
contextNameFilter |
(package private) ContextValueFilter |
contextValueFilter |
(package private) java.lang.String |
dateFormat |
(package private) boolean |
dateFormatISO8601 |
(package private) boolean |
dateFormatMillis |
(package private) java.time.format.DateTimeFormatter |
dateFormatter |
(package private) boolean |
dateFormatUnixTime |
(package private) static java.time.ZoneId |
DEFAULT_ZONE_ID |
(package private) long |
features |
(package private) boolean |
formatHasDay |
(package private) boolean |
formatHasHour |
(package private) boolean |
formatyyyyMMddhhmmss19 |
(package private) boolean |
hasFilter |
(package private) LabelFilter |
labelFilter |
(package private) java.util.Locale |
locale |
(package private) int |
maxLevel |
(package private) NameFilter |
nameFilter |
(package private) PropertyFilter |
propertyFilter |
(package private) PropertyPreFilter |
propertyPreFilter |
ObjectWriterProvider |
provider |
(package private) ValueFilter |
valueFilter |
(package private) java.time.ZoneId |
zoneId |
| Constructor and Description |
|---|
Context(JSONWriter.Feature... features)
Creates a new Context with the specified features.
|
Context(ObjectWriterProvider provider)
Creates a new Context with the specified object writer provider.
|
Context(ObjectWriterProvider provider,
JSONWriter.Feature... features)
Creates a new Context with the specified object writer provider and features.
|
Context(java.lang.String format,
JSONWriter.Feature... features)
Creates a new Context with the specified date format and features.
|
| Modifier and Type | Method and Description |
|---|---|
void |
config(JSONWriter.Feature... features)
Configures features for this context.
|
void |
config(JSONWriter.Feature feature,
boolean state)
Configures a specific feature for this context.
|
void |
configFilter(Filter... filters)
Configures filters for this context.
|
AfterFilter |
getAfterFilter()
Gets the after filter for this context.
|
BeforeFilter |
getBeforeFilter()
Gets the before filter for this context.
|
ContextNameFilter |
getContextNameFilter()
Gets the context name filter for this context.
|
ContextValueFilter |
getContextValueFilter()
Gets the context value filter for this context.
|
java.lang.String |
getDateFormat()
Gets the date format pattern for this context.
|
java.time.format.DateTimeFormatter |
getDateFormatter()
Gets the date formatter for this context.
|
long |
getFeatures()
Gets the features bitmask for this context.
|
LabelFilter |
getLabelFilter()
Gets the label filter for this context.
|
int |
getMaxLevel()
Gets the maximum nesting level allowed for this context.
|
NameFilter |
getNameFilter()
Gets the name filter for this context.
|
<T> ObjectWriter<T> |
getObjectWriter(java.lang.Class<T> objectType)
Gets the ObjectWriter for the specified object type.
|
<T> ObjectWriter<T> |
getObjectWriter(java.lang.reflect.Type objectType,
java.lang.Class<T> objectClass)
Gets the ObjectWriter for the specified object type and class.
|
PropertyFilter |
getPropertyFilter()
Gets the property filter for this context.
|
PropertyPreFilter |
getPropertyPreFilter()
Gets the property pre-filter for this context.
|
ObjectWriterProvider |
getProvider()
Gets the ObjectWriterProvider used by this context.
|
ValueFilter |
getValueFilter()
Gets the value filter for this context.
|
java.time.ZoneId |
getZoneId()
Gets the ZoneId used by this context.
|
boolean |
isDateFormatHasDay()
Checks if the date format includes day information.
|
boolean |
isDateFormatHasHour()
Checks if the date format includes hour information.
|
boolean |
isDateFormatISO8601()
Checks if the date format is configured to use ISO8601 format.
|
boolean |
isDateFormatMillis()
Checks if the date format is configured to use milliseconds.
|
boolean |
isDateFormatUnixTime()
Checks if the date format is configured to use Unix time.
|
boolean |
isEnabled(JSONWriter.Feature feature)
Checks if the specified feature is enabled in this context.
|
boolean |
isEnabled(long feature)
Checks if the specified feature mask is enabled in this context.
|
boolean |
isFormatyyyyMMddhhmmss19()
Checks if the date format is configured to use yyyy-MM-dd HH:mm:ss format (19 characters).
|
void |
setAfterFilter(AfterFilter afterFilter)
Sets the after filter for this context.
|
void |
setBeforeFilter(BeforeFilter beforeFilter)
Sets the before filter for this context.
|
void |
setContextNameFilter(ContextNameFilter contextNameFilter)
Sets the context name filter for this context.
|
void |
setContextValueFilter(ContextValueFilter contextValueFilter)
Sets the context value filter for this context.
|
void |
setDateFormat(java.lang.String dateFormat)
Sets the date format pattern for this context.
|
void |
setFeatures(long features)
Sets the features bitmask for this context.
|
void |
setLabelFilter(LabelFilter labelFilter)
Sets the label filter for this context.
|
void |
setMaxLevel(int maxLevel)
Sets the maximum nesting level allowed for this context.
|
void |
setNameFilter(NameFilter nameFilter)
Sets the name filter for this context.
|
void |
setPropertyFilter(PropertyFilter propertyFilter)
Sets the property filter for this context.
|
void |
setPropertyPreFilter(PropertyPreFilter propertyPreFilter)
Sets the property pre-filter for this context.
|
void |
setValueFilter(ValueFilter valueFilter)
Sets the value filter for this context.
|
void |
setZoneId(java.time.ZoneId zoneId)
Sets the ZoneId for this context.
|
static final java.time.ZoneId DEFAULT_ZONE_ID
public final ObjectWriterProvider provider
java.time.format.DateTimeFormatter dateFormatter
java.lang.String dateFormat
java.util.Locale locale
boolean dateFormatMillis
boolean dateFormatISO8601
boolean dateFormatUnixTime
boolean formatyyyyMMddhhmmss19
boolean formatHasDay
boolean formatHasHour
long features
java.time.ZoneId zoneId
int maxLevel
boolean hasFilter
PropertyPreFilter propertyPreFilter
PropertyFilter propertyFilter
NameFilter nameFilter
ValueFilter valueFilter
BeforeFilter beforeFilter
AfterFilter afterFilter
LabelFilter labelFilter
ContextValueFilter contextValueFilter
ContextNameFilter contextNameFilter
public Context(ObjectWriterProvider provider)
provider - the object writer provider to usejava.lang.IllegalArgumentException - if provider is nullpublic Context(JSONWriter.Feature... features)
features - the features to enablepublic Context(java.lang.String format,
JSONWriter.Feature... features)
format - the date format pattern to usefeatures - the features to enablepublic Context(ObjectWriterProvider provider, JSONWriter.Feature... features)
provider - the object writer provider to usefeatures - the features to enablejava.lang.IllegalArgumentException - if provider is nullpublic long getFeatures()
public void setFeatures(long features)
features - the features bitmask to setpublic boolean isEnabled(JSONWriter.Feature feature)
feature - the feature to checkpublic boolean isEnabled(long feature)
feature - the feature mask to checkpublic void config(JSONWriter.Feature... features)
features - the features to enablepublic void config(JSONWriter.Feature feature, boolean state)
feature - the feature to configurestate - true to enable the feature, false to disable itpublic void configFilter(Filter... filters)
filters - the filters to configurepublic <T> ObjectWriter<T> getObjectWriter(java.lang.Class<T> objectType)
T - the type of objects to be serializedobjectType - the class of objects to be serializedpublic <T> ObjectWriter<T> getObjectWriter(java.lang.reflect.Type objectType, java.lang.Class<T> objectClass)
T - the type of objects to be serializedobjectType - the type of objects to be serializedobjectClass - the class of objects to be serializedpublic ObjectWriterProvider getProvider()
public java.time.ZoneId getZoneId()
public void setZoneId(java.time.ZoneId zoneId)
zoneId - the ZoneId to setpublic java.lang.String getDateFormat()
public boolean isDateFormatMillis()
public boolean isDateFormatUnixTime()
public boolean isDateFormatISO8601()
public boolean isDateFormatHasDay()
public boolean isDateFormatHasHour()
public boolean isFormatyyyyMMddhhmmss19()
public java.time.format.DateTimeFormatter getDateFormatter()
public void setDateFormat(java.lang.String dateFormat)
dateFormat - the date format pattern to setpublic PropertyPreFilter getPropertyPreFilter()
public void setPropertyPreFilter(PropertyPreFilter propertyPreFilter)
propertyPreFilter - the property pre-filter to setpublic NameFilter getNameFilter()
public void setNameFilter(NameFilter nameFilter)
nameFilter - the name filter to setpublic ValueFilter getValueFilter()
public void setValueFilter(ValueFilter valueFilter)
valueFilter - the value filter to setpublic ContextValueFilter getContextValueFilter()
public void setContextValueFilter(ContextValueFilter contextValueFilter)
contextValueFilter - the context value filter to setpublic ContextNameFilter getContextNameFilter()
public void setContextNameFilter(ContextNameFilter contextNameFilter)
contextNameFilter - the context name filter to setpublic PropertyFilter getPropertyFilter()
public void setPropertyFilter(PropertyFilter propertyFilter)
propertyFilter - the property filter to setpublic AfterFilter getAfterFilter()
public void setAfterFilter(AfterFilter afterFilter)
afterFilter - the after filter to setpublic BeforeFilter getBeforeFilter()
public void setBeforeFilter(BeforeFilter beforeFilter)
beforeFilter - the before filter to setpublic LabelFilter getLabelFilter()
public void setLabelFilter(LabelFilter labelFilter)
labelFilter - the label filter to setpublic int getMaxLevel()
public void setMaxLevel(int maxLevel)
maxLevel - the maximum nesting level to set