Package org.apache.juneau.marshall.json
Class JsonSerializerSession
java.lang.Object
org.apache.juneau.marshall.ContextSession
org.apache.juneau.marshall.MarshallingSession
org.apache.juneau.marshall.MarshallingTraverseSession
org.apache.juneau.marshall.serializer.SerializerSession
org.apache.juneau.marshall.serializer.WriterSerializerSession
org.apache.juneau.marshall.json.JsonSerializerSession
- All Implemented Interfaces:
BeanSession,ConverterSession,ArrayRecordWritable,RecordWritable,TokenWritable
- Direct Known Subclasses:
JcsSerializerSession,Json5SerializerSession,JsonlSerializerSession,JsonSchemaSerializerSession
public class JsonSerializerSession
extends WriterSerializerSession
implements TokenWritable, ArrayRecordWritable
Session object that lives for the duration of a single use of
JsonSerializer.
Notes:
- This class is not thread safe and is typically discarded after one use.
See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class.static final classConcrete default builder leaf for the non-subclassedcreate()path (CRTP terminal). -
Field Summary
Fields inherited from class org.apache.juneau.marshall.MarshallingTraverseSession
indentFields inherited from class org.apache.juneau.marshall.MarshallingSession
NAME_PROPERTY_NAME -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedJsonSerializerSession(JsonSerializerSession.Builder<?> builder) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionstatic JsonSerializerSession.Builder<?>create(JsonSerializer ctx) Creates a new builder for this object.protected voiddoWrite(SerializerPipe out, Object o) Serializes a POJO to the specified pipe.protected JsonClassMetagetJsonClassMeta(ClassMeta<?> cm) Returns the language-specific metadata on the specified class.protected JsonWriter<?>Converts the specified output target object to anJsonWriter.final booleanAdd"_type" properties when needed.protected final booleanPrefix solidus'/' characters with escapes.protected booleanWhether the token writer emits unquoted (simple) attribute names.protected JsonWriter<?>writeAnything(JsonWriter<?> out, Object o, ClassMeta<?> eType, String attrName, BeanPropertyMeta pMeta) Workhorse method.writeArrayRecords(Object output) Streaming array-elementRecordWriterbacked byJsonTokenWriter.protected SerializerWriter<?>writeBeanMap(JsonWriter<?> out, BeanMap<?> m, String typeName) protected StringSerializes an object to a JSON string using this session's configuration.protected SerializerWriter<?>writeMap(JsonWriter<?> out, Map m, ClassMeta<?> type) writeTokens(Object output) Opens a low-level push generator that emits JSON one structural event at a time, bound to this session's live config.Methods inherited from class org.apache.juneau.marshall.serializer.WriterSerializerSession
create, createPipe, getMaxIndent, getQuoteChar, getStreamCharset, isUseWhitespace, isWriterSerializer, properties, write, writeToStringMethods inherited from class org.apache.juneau.marshall.serializer.SerializerSession
addVarBean, canIgnoreValue, canIgnoreValue, create, createBeanTypeNameProperty, createDefaultVarResolverSession, forEachEntry, forEachEntry, forEachStreamableEntry, generalize, getBeanTypeName, getCalendarFormat, getDateFormat, getDurationFormat, getExpectedRootType, getJavaMethod, getListener, getListener, getLocaleFormat, getPeriodFormat, getResponseHeaders, getSchema, getTemporalFormat, getTimeZoneFormat, getUriContext, getUriRelativity, getUriResolution, getUriResolver, getVarResolver, handleThrown, isAddRootType, isKeepNullProperties, isNonDefault, isSortCollections, isSortMaps, isTrimEmptyCollections, isTrimEmptyMaps, isTrimStrings, onBeanGetterException, onError, push2, relativizeUri, resolve, resolveUri, sort, sort, sort, swap, toList, toListFromStreamable, toString, trim, unwrapSupplier, write, writeCalendar, writeDate, writeDuration, writePeriod, writeTemporalMethods inherited from class org.apache.juneau.marshall.MarshallingTraverseSession
getInitialDepth, getLastLocation, getMaxDepth, getOptionalType, getOptionalValue, getStack, isDetectRecursions, isIgnoreRecursions, isOptional, isRoot, pop, push, setCurrentClass, setCurrentProperty, willExceedDepth, willRecurseMethods inherited from class org.apache.juneau.marshall.MarshallingSession
addWarning, convertToMemberType, convertToMemberType, convertToMemberType, convertToType, convertToType, convertToType, convertToType, create, get, getActiveView, getAnnotationProvider, getArgsClassMeta, getBeanClassVisibility, getBeanConstructorVisibility, getBeanDictionary, getBeanFieldVisibility, getBeanMeta, getBeanMethodVisibility, getBeanRegistry, getBeanTypePropertyName, getBeanTypePropertyName, getClassLoader, getClassMeta, getClassMeta, getClassMetaForObject, getClassMetaForObject, getLocale, getMarshallingContext, getMediaType, getNotBeanClasses, getNotBeanPackagesNames, getPropertyNamer, getSwaps, getTimeZone, getTimeZoneId, isBean, isBean, isBeanMapPutReturnsOldValue, isBeansRequireDefaultConstructor, isBeansRequireSerializable, isBeansRequireSettersForGetters, isBeansRequireSomeProperties, isFindFluentSetters, isIgnoreInvocationExceptionsOnGetters, isIgnoreInvocationExceptionsOnSetters, isIgnoreMissingSetters, isIgnoreUnknownBeanProperties, isIgnoreUnknownNullBeanProperties, isPropertyInActiveView, isRecordComponentOrder, isUnsortedProperties, isUseInterfaceProxies, isUseJavaBeanIntrospector, isValidateSchema, newBean, newBean, newBeanMap, newBeanMap, newGenericList, newGenericMap, newGenericMap, object, parseToList, parseToMap, string, toArray, toBeanMap, toBeanMap, toBeanMapMethods inherited from class org.apache.juneau.marshall.ContextSession
checkForWarnings, getContext, getSessionProperties, getWarnings, isDebug, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.juneau.marshall.stream.ArrayRecordWritable
isArrayRecordStreaming, writeArrayRecordsMethods inherited from interface org.apache.juneau.commons.conversion.ConverterSession
getMethods inherited from interface org.apache.juneau.marshall.stream.RecordWritable
isRecordStreamingMethods inherited from interface org.apache.juneau.marshall.stream.TokenWritable
writeRecords
-
Constructor Details
-
JsonSerializerSession
Constructor.- Parameters:
builder- The builder for this object.
-
-
Method Details
-
create
Creates a new builder for this object.- Parameters:
ctx- The context creating this session.
Cannot benull .- Returns:
- A new builder.
-
writeBeanMap
protected SerializerWriter<?> writeBeanMap(JsonWriter<?> out, BeanMap<?> m, String typeName) throws SerializeException - Throws:
SerializeException
-
writeMap
protected SerializerWriter<?> writeMap(JsonWriter<?> out, Map m, ClassMeta<?> type) throws SerializeException - Throws:
SerializeException
-
doWrite
Description copied from class:SerializerSessionSerializes a POJO to the specified pipe.This method should NOT close the context object.
The default implementation of this method simply calls
Serializer.doWrite(SerializerSession,SerializerPipe,Object).- Overrides:
doWritein classSerializerSession- Parameters:
out- Where to send the output from the serializer.o- The object to serialize.- Throws:
IOException- Thrown by underlying stream.SerializeException- Problem occurred trying to serialize object.
-
getJsonClassMeta
Returns the language-specific metadata on the specified class.- Parameters:
cm- The class to return the metadata on.- Returns:
- The metadata.
-
getJsonWriter
Converts the specified output target object to anJsonWriter.- Parameters:
out- The output target object.
Must not benull .- Returns:
- The output target object wrapped in an
JsonWriter.
-
isAddBeanTypes
Description copied from class:SerializerSessionAdd"_type" properties when needed.- Overrides:
isAddBeanTypesin classSerializerSession- Returns:
true if"_type" properties added to beans if their type cannot be inferred through reflection.- See Also:
-
isEscapeSolidus
Prefix solidus'/' characters with escapes.- Returns:
true if solidus (e.g. slash) characters should be escaped.- See Also:
-
isSimpleAttrs
Whether the token writer emits unquoted (simple) attribute names.- Returns:
false for RFC-8259 strict JSON; JSON5 overrides totrue .
-
writeTokens
Opens a low-level push generator that emits JSON one structural event at a time, bound to this session's live config.The writer is purely structural — object swaps and
@Schemaannotations are not applied at this layer. SeeTokenWriterfor the contract. The higher-levelTokenWriter.object(Object)bridge walks an arbitrary POJO viaPojoWalkerand DOES honor the databind settings (keepNullProperties ,trimEmptyMaps ,trimEmptyCollections ,sortMaps ,sortCollections ,trimStrings ).- Specified by:
writeTokensin interfaceTokenWritable- Parameters:
output- The output. AcceptsWriterorOutputStream.- Returns:
- A new
JsonTokenWriter. - Throws:
IOException- If the output type is not supported or could not be opened.
-
writeArrayRecords
Streaming array-elementRecordWriterbacked byJsonTokenWriter. Memory is O(1) in the array length — elements are emitted as they arrive.- Specified by:
writeArrayRecordsin interfaceArrayRecordWritable- Parameters:
output- The output.- Returns:
- A new element-streamed
RecordWriter. - Throws:
IOException- If a problem occurred opening the underlying output.
-
writeAnything
protected JsonWriter<?> writeAnything(JsonWriter<?> out, Object o, ClassMeta<?> eType, String attrName, BeanPropertyMeta pMeta) throws SerializeException Workhorse method. Determines the type of object, and then calls the appropriate type-specific serialization method.- Parameters:
out- The output writer.
Must not benull .o- The object to serialize.
Can benull (written as the JSON literal"null" ).eType- The expected type.
Can benull (defaults toObject).attrName- The attribute name.pMeta- The bean property currently being serialized.
Can benull (the bean-property-level dictionary lookup and URI detection are skipped).- Returns:
- The same writer passed in.
- Throws:
SerializeException- General serialization error occurred.
-
writeJson
Serializes an object to a JSON string using this session's configuration.Protected extension hook that subclasses can call or override when they need to serialize a value to JSON (e.g. for embedding a nested JSON fragment).
- Parameters:
o- The object to serialize.- Returns:
- The serialized object.
- Throws:
Exception- Error occurred.
-