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
public class TypedDefinitionCodec
extends java.lang.Object
implements 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
Constructors Constructor Description TypedDefinitionCodec(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, java.lang.Boolean passthroughEnabled) -
Method Summary
Modifier and Type Method Description com.linkedin.data.DataListbytesToList(byte[] input)com.linkedin.data.DataMapbytesToMap(byte[] input)java.lang.StringgetStringEncoding()byte[]listToBytes(com.linkedin.data.DataList list)java.lang.StringlistToString(com.linkedin.data.DataList list)byte[]mapToBytes(com.linkedin.data.DataMap map)java.lang.StringmapToString(com.linkedin.data.DataMap map)com.linkedin.data.DataListreadList(java.io.InputStream in)com.linkedin.data.DataListreadList(java.io.Reader in)com.linkedin.data.DataMapreadMap(java.io.InputStream in)com.linkedin.data.DataMapreadMap(java.io.Reader in)com.linkedin.data.DataListstringToList(java.lang.String input)com.linkedin.data.DataMapstringToMap(java.lang.String input)voidwriteList(com.linkedin.data.DataList list, java.io.OutputStream out)voidwriteList(com.linkedin.data.DataList list, java.io.Writer out)voidwriteMap(com.linkedin.data.DataMap map, java.io.OutputStream out)voidwriteMap(com.linkedin.data.DataMap map, java.io.Writer out)
-
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, java.lang.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
public byte[] mapToBytes(com.linkedin.data.DataMap map) throws java.io.IOException- Specified by:
mapToBytesin interfacecom.linkedin.data.codec.DataCodec- Throws:
java.io.IOException
-
writeMap
public void writeMap(com.linkedin.data.DataMap map, java.io.OutputStream out) throws java.io.IOException- Specified by:
writeMapin interfacecom.linkedin.data.codec.DataCodec- Throws:
java.io.IOException
-
bytesToMap
public com.linkedin.data.DataMap bytesToMap(byte[] input) throws java.io.IOException- Specified by:
bytesToMapin interfacecom.linkedin.data.codec.DataCodec- Throws:
java.io.IOException
-
readMap
public com.linkedin.data.DataMap readMap(java.io.InputStream in) throws java.io.IOException- Specified by:
readMapin interfacecom.linkedin.data.codec.DataCodec- Throws:
java.io.IOException
-
listToBytes
public byte[] listToBytes(com.linkedin.data.DataList list) throws java.io.IOException- Specified by:
listToBytesin interfacecom.linkedin.data.codec.DataCodec- Throws:
java.io.IOException
-
writeList
public void writeList(com.linkedin.data.DataList list, java.io.OutputStream out) throws java.io.IOException- Specified by:
writeListin interfacecom.linkedin.data.codec.DataCodec- Throws:
java.io.IOException
-
bytesToList
public com.linkedin.data.DataList bytesToList(byte[] input) throws java.io.IOException- Specified by:
bytesToListin interfacecom.linkedin.data.codec.DataCodec- Throws:
java.io.IOException
-
readList
public com.linkedin.data.DataList readList(java.io.InputStream in) throws java.io.IOException- Specified by:
readListin interfacecom.linkedin.data.codec.DataCodec- Throws:
java.io.IOException
-
getStringEncoding
public java.lang.String getStringEncoding()- Specified by:
getStringEncodingin interfacecom.linkedin.data.codec.TextDataCodec
-
mapToString
public java.lang.String mapToString(com.linkedin.data.DataMap map) throws java.io.IOException- Specified by:
mapToStringin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
java.io.IOException
-
writeMap
public void writeMap(com.linkedin.data.DataMap map, java.io.Writer out) throws java.io.IOException- Specified by:
writeMapin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
java.io.IOException
-
stringToList
public com.linkedin.data.DataList stringToList(java.lang.String input) throws java.io.IOException- Specified by:
stringToListin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
java.io.IOException
-
readMap
public com.linkedin.data.DataMap readMap(java.io.Reader in) throws java.io.IOException- Specified by:
readMapin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
java.io.IOException
-
writeList
public void writeList(com.linkedin.data.DataList list, java.io.Writer out) throws java.io.IOException- Specified by:
writeListin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
java.io.IOException
-
stringToMap
public com.linkedin.data.DataMap stringToMap(java.lang.String input) throws java.io.IOException- Specified by:
stringToMapin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
java.io.IOException
-
listToString
public java.lang.String listToString(com.linkedin.data.DataList list) throws java.io.IOException- Specified by:
listToStringin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
java.io.IOException
-
readList
public com.linkedin.data.DataList readList(java.io.Reader in) throws java.io.IOException- Specified by:
readListin interfacecom.linkedin.data.codec.TextDataCodec- Throws:
java.io.IOException
-