Class HjsonSerializer
- All Implemented Interfaces:
Function<Object,,String> ThrowingFunction<Object,,String> HjsonMetaProvider,RecordWritable
Media types
Handles
Produces
Description
Hjson is a syntax extension of JSON designed for human-friendly configuration files and hand-edited data.
This serializer produces output that is valid Hjson and can be parsed back by HjsonParser.
The conversion is as follows:
-
Maps (e.g.
HashMaps,TreeMaps) are converted to Hjson objects. -
Collections (e.g.
HashSets,LinkedLists) and Java arrays are converted to Hjson arrays. -
Stringsare serialized as quoteless values when they contain only safe characters. Strings that would be misinterpreted as booleans, null, or numbers are automatically double-quoted. -
Numbers(e.g.Integer,Long,Double) are converted to Hjson numbers. -
Booleansare converted totrueorfalse. -
nullvalues are converted to Hjsonnull. - Beans are converted to Hjson objects.
The types above are considered "Hjson-primitive" object types.
Any non-Hjson-primitive object types are transformed into Hjson-primitive object types through
ObjectSwaps associated through the
MarshallingContext.Builder.swaps(Class...) method.
Several default transforms are provided for transforming Dates, Enums, Iterators, etc.
Example:
Example output - readable mode (Map of name/age):
{ name: Alice age: 30 }
Example output - compact mode:
{name:Alice,age:30}
Notes:
- String values equal to
true ,false ,null , or parseable as numbers are automatically serialized in double quotes to prevent parse ambiguity. - Empty strings are always serialized as
"" . - Root-level objects always include enclosing braces
{} by default. UseHjsonSerializer.Builder.omitRootBraces(boolean)to suppress them. - In compact mode, strings containing newlines are serialized as double-quoted strings
with JSON escape sequences rather than multiline
''' blocks. - Format-specific field-level annotations (
@Hjson) are supported but intentionally minimal in the initial implementation. - This class is thread safe and reusable.
See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class org.apache.juneau.marshall.serializer.WriterSerializer
WriterSerializer.DefaultBuilderNested classes/interfaces inherited from class org.apache.juneau.marshall.serializer.Serializer
Serializer.Null -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final HjsonSerializerDefault serializer (readable).static final HjsonSerializerCompact serializer (single line).protected final booleanOmit root object braces.protected final booleanUse multiline ''' for strings with newlines.protected final booleanUse newlines instead of commas between members.protected final booleanUse quoteless keys when safe.protected final booleanUse quoteless strings for simple values.Fields inherited from class org.apache.juneau.marshall.serializer.WriterSerializer
maxIndent, quoteChar, quoteCharOverride, useWhitespaceFields inherited from class org.apache.juneau.marshall.serializer.Serializer
accept, addBeanTypes, addRootType, keepNullProperties, listener, nonDefault, produces, sortCollections, sortMaps, trimEmptyCollections, trimEmptyMaps, trimStringsFields inherited from class org.apache.juneau.marshall.MarshallingContextable
marshallingContextFields inherited from class org.apache.juneau.marshall.Context
CONTEXT_APPLY_FILTER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncopy()Creates a builder from this context object.static HjsonSerializer.Buildercreate()Creates a new builder.Create a session builder based on the properties defined on this context.Returns the Hjson-specific metadata on the specified bean property.getHjsonClassMeta(ClassMeta<?> cm) Returns the Hjson-specific metadata on the specified class.booleanStreaming-quality signal queryable before opening a cursor.Returns the properties on this bean as a map for debugging.writeRecords(Object output) Convenience delegator that opens aRecordWriterover the output using default session arguments (mirrorsWriterSerializer.write(Object)).Methods inherited from class org.apache.juneau.marshall.serializer.WriterSerializer
applyThrows, getMaxIndent, getQuoteChar, getSession, getStreamCharset, isUseWhitespace, isWriterSerializer, println, quoteChar, toString, writeMethods inherited from class org.apache.juneau.marshall.serializer.Serializer
createSerializerBuilder, doWrite, forEachAcceptMediaType, getListener, getMediaTypeRanges, getPrimaryMediaType, getResponseContentType, getResponseHeaders, getUriContext, getUriRelativity, getUriResolution, isAddBeanTypes, isAddRootType, isKeepNullProperties, isNonDefault, isSortCollections, isSortMaps, isTrimEmptyCollections, isTrimEmptyMaps, isTrimStrings, write, writeToStringMethods inherited from class org.apache.juneau.marshall.MarshallingTraverseContext
getDetectRecursions, getIgnoreRecursions, getInitialDepth, getMaxDepth, isDetectRecursions, isIgnoreRecursions, shouldDetectRecursionsMethods inherited from class org.apache.juneau.marshall.MarshallingContextable
getDurationFormat, getMarshallingContext, getPeriodFormatMethods inherited from class org.apache.juneau.marshall.Context
createBuilder, getAnnotationProvider, init, isDebug, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.juneau.commons.function.ThrowingFunction
apply
-
Field Details
-
useMultilineStrings
Use multiline ''' for strings with newlines. -
useQuotelessStrings
Use quoteless strings for simple values. -
useQuotelessKeys
Use quoteless keys when safe. -
omitRootBraces
Omit root object braces. -
useNewlineSeparators
Use newlines instead of commas between members. -
DEFAULT
Default serializer (readable). -
DEFAULT_COMPACT
Compact serializer (single line).
-
-
Constructor Details
-
HjsonSerializer
Constructor.- Parameters:
builder- The builder.
-
-
Method Details
-
create
Creates a new builder.- Returns:
- A new builder.
-
createSession
Description copied from class:ContextCreate a session builder based on the properties defined on this context.Use this method for creating sessions where you want to override basic settings. Otherwise, use
Context.getSession()directly.- Overrides:
createSessionin classWriterSerializer- Returns:
- A new session builder.
-
copy
Description copied from class:ContextCreates a builder from this context object.Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
- Overrides:
copyin classWriterSerializer- Returns:
- A new Builder object.
-
getHjsonBeanPropertyMeta
Description copied from interface:HjsonMetaProviderReturns the Hjson-specific metadata on the specified bean property.- Specified by:
getHjsonBeanPropertyMetain interfaceHjsonMetaProvider- Parameters:
bpm- The bean property to return the metadata on.- Returns:
- The metadata.
-
getHjsonClassMeta
Description copied from interface:HjsonMetaProviderReturns the Hjson-specific metadata on the specified class.- Specified by:
getHjsonClassMetain interfaceHjsonMetaProvider- Parameters:
cm- The class to return the metadata on.- Returns:
- The metadata.
-
properties
Description copied from class:ContextReturns the properties on this bean as a map for debugging.- Overrides:
propertiesin classWriterSerializer- Returns:
- The properties on this bean as a map for debugging.
-
writeRecords
Convenience delegator that opens aRecordWriterover the output using default session arguments (mirrorsWriterSerializer.write(Object)).The real implementation lives on
HjsonSerializerSession.writeRecords(Object). Callers that need request-derived configuration should callcreateSession()and invokeHjsonSerializerSession.writeRecords(Object)on the built session instead.- Specified by:
writeRecordsin interfaceRecordWritable- Parameters:
output- The output.- Returns:
- A new
RecordWriter. - Throws:
IOException- If a problem occurred opening the underlying output.
-
isRecordStreaming
Description copied from interface:RecordWritableStreaming-quality signal queryable before opening a cursor.- Specified by:
isRecordStreamingin interfaceRecordWritable- Returns:
true ifRecordWritable.writeRecords(Object)returns an O(1)-memory streaming cursor;false for buffered (whole-value) formats backed byRecordAdapter.
-