Class JsonSerializer


  • public class JsonSerializer
    extends Serializer
    Compact JSON serializer. This uses the most compact type-safe JSON format allowable. There are two optional variants switching the struct field ID between numeric ID and field name.

    Format is like this:

     {
         "id":value,
         "structId":{ ... },
         "listId":[value1,value2],
         "mapId":{"id1":value1,"id2":value2}
     }
     
    But without formatting spaces. The formatted JSON can be read normally. Binary fields are base64 encoded.

    This format supports 'compact' struct formatting. A compact struct is formatted as a list with fields in order from 1 to N. E.g.:

     ["tag",5,6.45]
     
    is equivalent to:
     {"1":"tag","2":5,"3":6.45}
     
    • Constructor Detail

      • JsonSerializer

        public JsonSerializer()
      • JsonSerializer

        public JsonSerializer​(boolean strict)
    • Method Detail

      • serialize

        public <T extends PMessage<T,F>,F extends PField> void serialize​(@Nonnull
                                                                         java.io.PrintWriter output,
                                                                         @Nonnull
                                                                         T message)
                                                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • serialize

        public <T extends PMessage<T,F>,F extends PField> int serialize​(@Nonnull
                                                                        java.io.OutputStream output,
                                                                        @Nonnull
                                                                        T message)
                                                                 throws java.io.IOException
        Description copied from class: Serializer
        Serialize a message.
        Specified by:
        serialize in class Serializer
        Type Parameters:
        T - The message type.
        F - The message field type.
        Parameters:
        output - The output stream to write to.
        message - The message to write.
        Returns:
        The number of bytes written to the output stream.
        Throws:
        java.io.IOException - If writing or serialization failed.
      • serialize

        public <T extends PMessage<T,F>,F extends PField> int serialize​(@Nonnull
                                                                        java.io.OutputStream output,
                                                                        @Nonnull
                                                                        PServiceCall<T,F> call)
                                                                 throws java.io.IOException
        Description copied from class: Serializer
        Serialize a service call.
        Specified by:
        serialize in class Serializer
        Type Parameters:
        T - The message type contained in the service call.
        F - The message field type.
        Parameters:
        output - The output stream to write to.
        call - The service call to write.
        Returns:
        The number of bytes written to the output stream.
        Throws:
        java.io.IOException - If writing or serialization failed.
      • deserialize

        @Nonnull
        public <T extends PMessage<T,TF>,TF extends PField> T deserialize​(@Nonnull
                                                                          java.io.InputStream input,
                                                                          @Nonnull
                                                                          PMessageDescriptor<T,TF> type)
                                                                   throws java.io.IOException
        Description copied from class: Serializer
        Deserialize a message.
        Specified by:
        deserialize in class Serializer
        Type Parameters:
        T - The message type.
        TF - The message field type.
        Parameters:
        input - The input stream to read from.
        type - The descriptor of the message to read.
        Returns:
        The message read.
        Throws:
        java.io.IOException - If reading or deserialization failed.
      • deserialize

        public <T extends PMessage<T,TF>,TF extends PField> T deserialize​(@Nonnull
                                                                          java.io.Reader input,
                                                                          @Nonnull
                                                                          PMessageDescriptor<T,TF> type)
                                                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • deserialize

        @Nonnull
        public <T extends PMessage<T,F>,F extends PFieldPServiceCall<T,F> deserialize​(@Nonnull
                                                                                        java.io.InputStream input,
                                                                                        @Nonnull
                                                                                        PService service)
                                                                                 throws java.io.IOException
        Description copied from class: Serializer
        Deserialize a service call.
        Specified by:
        deserialize in class Serializer
        Type Parameters:
        T - The message type contained in the returned service call.
        F - The message field type.
        Parameters:
        input - The input stream to read from.
        service - The service definition to read for.
        Returns:
        The service call read.
        Throws:
        java.io.IOException - If reading or deserialization failed.
      • binaryProtocol

        public boolean binaryProtocol()
        Specified by:
        binaryProtocol in class Serializer
        Returns:
        True if the protocol of the serializer is "binary" a.k.a. unreadable to most humans.
      • mediaType

        @Nonnull
        public java.lang.String mediaType()
        Specified by:
        mediaType in class Serializer
        Returns:
        Media type of the content generated by the serializer. Can be used for Content-Type and Accept HTTP headers, MIME type etc.