Package org.coursera.pegasus
Class TypedDefinitionCodec
java.lang.Object
org.coursera.pegasus.TypedDefinitionCodec
- All Implemented Interfaces:
com.linkedin.data.codec.DataCodec,com.linkedin.data.codec.TextDataCodec
Extends existing Pegasus codecs to support for the "typedDefinition" and "flatTypedDefinition"
serializations originally used in our Play! JSON classes. (See
JsonFormats.typedDefinitionFormat() and JsonFormats.flatTypedDefinitionFormat() in infra-services
for the Play! JSON implementation).
WARNING: This codec filters out unrecognized fields (any fields in the data that are not defined in the schema) when reading and writing data by default. It is only when this codec is used exclusively for reading and writing of data that passthrough is guaranteed to propagate data in unrecognized fields correctly, in which case the 'passthroughEnabled' may be safely set to true.
Usage: In order to indicate a Pegasus union should be serialized using "typedDefinition" or "flatTypedDefinition", a typeref must be defined for the union and annotated with a property defining the mapping between the "memberKeys" of the Pegasus union and the "typeNames" of the typed definition format.
For example, the Peagsus schema:
For a "flatTypedDefinition", instead do:
"memberKeys" referencing types in the same namespace as the typeref may use simple names. Fully qualified names are only required when referencing types in other namespaces.
WARNING: This codec filters out unrecognized fields (any fields in the data that are not defined in the schema) when reading and writing data by default. It is only when this codec is used exclusively for reading and writing of data that passthrough is guaranteed to propagate data in unrecognized fields correctly, in which case the 'passthroughEnabled' may be safely set to true.
Usage: In order to indicate a Pegasus union should be serialized using "typedDefinition" or "flatTypedDefinition", a typeref must be defined for the union and annotated with a property defining the mapping between the "memberKeys" of the Pegasus union and the "typeNames" of the typed definition format.
For example, the Peagsus schema:
{
"name": "ExampleTypedDefinition",
"type": "typeref",
"ref": [ "org.example.TextEntry", "org.example.MultipleChoice" ],
"typedDefinition": {
"org.example.TextEntry": "textEntry",
"org.example.MultipleChoice": "multipleChoice"
}
}
defines a "typedDefinition" union where the "memberKeys" of "org.example.TextEntry" and
"org.example.MultipleChoice" map to the "typeNames" of "textEntry" and "multipleChoice".
For a "flatTypedDefinition", instead do:
{
"name": "ExampleTypedDefinition",
"type": "typeref",
"ref": [ "org.example.TextEntry", "org.example.MultipleChoice" ],
"flatTypedDefinition": {
"org.example.TextEntry": "textEntry",
"org.example.MultipleChoice": "multipleChoice"
}
}
"memberKeys" referencing types in the same namespace as the typeref may use simple names. Fully qualified names are only required when referencing types in other namespaces.
-
Constructor Summary
ConstructorsConstructorDescriptionTypedDefinitionCodec(com.linkedin.data.schema.DataSchema schema, com.linkedin.data.codec.TextDataCodec underlying) TypedDefinitionCodec(com.linkedin.data.schema.DataSchema schema, com.linkedin.data.codec.TextDataCodec underlying, Boolean passthroughEnabled) -
Method Summary
Modifier and TypeMethodDescriptioncom.linkedin.data.DataListbytesToList(byte[] input) com.linkedin.data.DataMapbytesToMap(byte[] input) byte[]listToBytes(com.linkedin.data.DataList list) listToString(com.linkedin.data.DataList list) byte[]mapToBytes(com.linkedin.data.DataMap map) mapToString(com.linkedin.data.DataMap map) com.linkedin.data.DataListreadList(InputStream in) com.linkedin.data.DataListcom.linkedin.data.DataMapreadMap(InputStream in) com.linkedin.data.DataMapcom.linkedin.data.DataListstringToList(String input) com.linkedin.data.DataMapstringToMap(String input) voidwriteList(com.linkedin.data.DataList list, OutputStream out) voidvoidwriteMap(com.linkedin.data.DataMap map, OutputStream out) void
-
Constructor Details
-
TypedDefinitionCodec
public TypedDefinitionCodec(com.linkedin.data.schema.DataSchema schema, com.linkedin.data.codec.TextDataCodec underlying) - Parameters:
schema- provides the schema of the data to serialize.underlying- provides the codec to use to serialize/deserialize. Typically this is [[JacksonDataCodec]], [[PrettyPrinterJacksonDataCodec]] or similar.
-
TypedDefinitionCodec
public TypedDefinitionCodec(com.linkedin.data.schema.DataSchema schema, com.linkedin.data.codec.TextDataCodec underlying, Boolean passthroughEnabled) - Parameters:
schema- provides the schema of the data to serialize.underlying- provides the codec to use to serialize/deserialize. Typically this is [[JacksonDataCodec]], [[PrettyPrinterJacksonDataCodec]] or similar.passthroughEnabled- configures if passthrough of recognized fields is enabled. Only enable this when reading and writing data exclusively with this codec.
-
-
Method Details
-
mapToBytes
- Specified by:
mapToBytesin interfacecom.linkedin.data.codec.DataCodec- Throws:
IOException
-
writeMap
- Specified by:
writeMapin interfacecom.linkedin.data.codec.DataCodec- Throws:
IOException
-
bytesToMap
- Specified by:
bytesToMapin interfacecom.linkedin.data.codec.DataCodec- Throws:
IOException
-
readMap
- Specified by:
readMapin interfacecom.linkedin.data.codec.DataCodec- Throws:
IOException
-
listToBytes
- Specified by:
listToBytesin interfacecom.linkedin.data.codec.DataCodec- Throws:
IOException
-
writeList
- Specified by:
writeListin interfacecom.linkedin.data.codec.DataCodec- Throws:
IOException
-
bytesToList
- Specified by:
bytesToListin interfacecom.linkedin.data.codec.DataCodec- Throws:
IOException
-
readList
- Specified by:
readListin interfacecom.linkedin.data.codec.DataCodec- Throws:
IOException
-
getStringEncoding
- Specified by:
getStringEncodingin interfacecom.linkedin.data.codec.TextDataCodec
-
mapToString
- Specified by:
mapToStringin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
IOException
-
writeMap
- Specified by:
writeMapin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
IOException
-
stringToList
- Specified by:
stringToListin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
IOException
-
readMap
- Specified by:
readMapin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
IOException
-
writeList
- Specified by:
writeListin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
IOException
-
stringToMap
- Specified by:
stringToMapin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
IOException
-
listToString
- Specified by:
listToStringin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
IOException
-
readList
- Specified by:
readListin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
IOException
-