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:
 
 {
   "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.DataList bytesToList​(byte[] input)  
    com.linkedin.data.DataMap bytesToMap​(byte[] input)  
    java.lang.String getStringEncoding()  
    byte[] listToBytes​(com.linkedin.data.DataList list)  
    java.lang.String listToString​(com.linkedin.data.DataList list)  
    byte[] mapToBytes​(com.linkedin.data.DataMap map)  
    java.lang.String mapToString​(com.linkedin.data.DataMap map)  
    com.linkedin.data.DataList readList​(java.io.InputStream in)  
    com.linkedin.data.DataList readList​(java.io.Reader in)  
    com.linkedin.data.DataMap readMap​(java.io.InputStream in)  
    com.linkedin.data.DataMap readMap​(java.io.Reader in)  
    com.linkedin.data.DataList stringToList​(java.lang.String input)  
    com.linkedin.data.DataMap stringToMap​(java.lang.String input)  
    void writeList​(com.linkedin.data.DataList list, java.io.OutputStream out)  
    void writeList​(com.linkedin.data.DataList list, java.io.Writer out)  
    void writeMap​(com.linkedin.data.DataMap map, java.io.OutputStream out)  
    void writeMap​(com.linkedin.data.DataMap map, java.io.Writer out)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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:
      mapToBytes in interface com.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:
      writeMap in interface com.linkedin.data.codec.DataCodec
      Throws:
      java.io.IOException
    • bytesToMap

      public com.linkedin.data.DataMap bytesToMap​(byte[] input) throws java.io.IOException
      Specified by:
      bytesToMap in interface com.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:
      readMap in interface com.linkedin.data.codec.DataCodec
      Throws:
      java.io.IOException
    • listToBytes

      public byte[] listToBytes​(com.linkedin.data.DataList list) throws java.io.IOException
      Specified by:
      listToBytes in interface com.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:
      writeList in interface com.linkedin.data.codec.DataCodec
      Throws:
      java.io.IOException
    • bytesToList

      public com.linkedin.data.DataList bytesToList​(byte[] input) throws java.io.IOException
      Specified by:
      bytesToList in interface com.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:
      readList in interface com.linkedin.data.codec.DataCodec
      Throws:
      java.io.IOException
    • getStringEncoding

      public java.lang.String getStringEncoding()
      Specified by:
      getStringEncoding in interface com.linkedin.data.codec.TextDataCodec
    • mapToString

      public java.lang.String mapToString​(com.linkedin.data.DataMap map) throws java.io.IOException
      Specified by:
      mapToString in interface com.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:
      writeMap in interface com.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:
      stringToList in interface com.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:
      readMap in interface com.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:
      writeList in interface com.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:
      stringToMap in interface com.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:
      listToString in interface com.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:
      readList in interface com.linkedin.data.codec.TextDataCodec
      Throws:
      java.io.IOException