Class RecordCodec<T>
- Type Parameters:
T- the type this codec can encode and decode.
- All Implemented Interfaces:
Codec<T>, RawJsonCodec<T>, SchemaConvertable<T>, ValidatableCodec<T>
Codec for immutable types such as record classes.
Unlike BuilderCodec, this codec never mutates the target instance. Each field is decoded
into a constructor argument array in the order the fields were appended, then the type is built
once by invoking the supplied constructor. The same fields are read back through their accessors
when encoding.
Build one with builder(Class); each append refines the builder type so the final
build is checked against the constructor, for example .build(Point::new). Append
order must match the constructor parameter order. Pass a Validator to append to
attach a single field validator, or a RecordField.Options consumer for multiple
validators, documentation, or metadata. Records with more than eight components continue with
RecordCodec.RawBuilder and finish with RecordCodec.AbstractBuilder.buildRaw(Function).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA step in the typed builder chain for aRecordCodec.static final classstatic final classstatic final classstatic final classstatic final classstatic final classstatic final classstatic final classstatic final classstatic final classThe untyped tail of the builder chain, used once a record has more components than the typedBuilder0throughBuilder8steps cover. -
Field Summary
Fields inherited from interface Codec
BASE64_PATTERN, BOOLEAN, BSON_DOCUMENT, BYTE, BYTE_ARRAY, DOUBLE, DOUBLE_ARRAY, DURATION, DURATION_SECONDS, FLOAT, FLOAT_ARRAY, INSTANT, INT_ARRAY, INTEGER, LOG_LEVEL, LONG, LONG_ARRAY, NULLABLE_BOOLEAN, PATH, SHORT, STRING, STRING_ARRAY, UUID_BINARY, UUID_STRING -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> RecordCodec.Builder0<T> Creates a typed builder for aRecordCodec.decodeJson(RawJsonReader reader, ExtraInfo extraInfo) org.bson.BsonDocumentgetType()static <T> RecordCodec.RawBuilder<T> rawBuilder(Class<T> type) Starts an untyped builder for records with more components than the typed chain supports.toSchema(SchemaContext context) Returns a schema object that represents this codec as JSON Schema.toString()voidvoidvalidateDefaults(ExtraInfo extraInfo, Set<Codec<?>> tested) Methods inherited from interface RawJsonCodec
decodeJsonMethods inherited from interface SchemaConvertable
toSchema
-
Method Details
-
getType
-
getDocumentation
-
decode
-
encode
-
decodeJson
- Specified by:
decodeJsonin interfaceCodec<T>- Specified by:
decodeJsonin interfaceRawJsonCodec<T>- Throws:
IOException
-
validate
- Specified by:
validatein interfaceValidatableCodec<T>
-
validateDefaults
- Specified by:
validateDefaultsin interfaceValidatableCodec<T>
-
toSchema
Description copied from interface:SchemaConvertableReturns a schema object that represents this codec as JSON Schema.- Specified by:
toSchemain interfaceSchemaConvertable<T>- Parameters:
context- A context to use to assist with creating the schema.- Returns:
- the generated schema.
-
toString
-
builder
Creates a typed builder for aRecordCodec. Append fields withRecordCodec.Builder0.append(KeyedCodec, Function)in constructor parameter order, then finish with the matchingbuildoverload (for example.build(Point::new)). For records with more than eight components, userawBuilder(Class).- Parameters:
type- the class of the record type.- Returns:
- a fresh builder with no fields appended yet.
-
rawBuilder
Starts an untyped builder for records with more components than the typed chain supports. Finish withRecordCodec.AbstractBuilder.buildRaw(Function). Preferbuilder(Class)for records with eight or fewer components to keep compile-time constructor arity checking.- Parameters:
type- the class of the record type.- Returns:
- a fresh untyped builder with no fields appended yet.
-