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

      • toJsonString

        @Nonnull
        public static <Message extends PMessage<Message>> java.lang.String toJsonString​(PMessageOrBuilder<Message> message)
        Prints a JSON string of the provided message.
        Type Parameters:
        Message - The message type.
        Parameters:
        message - The message to stringify.
        Returns:
        The resulting string.
      • toPrettyJsonString

        @Nonnull
        public static <Message extends PMessage<Message>> java.lang.String toPrettyJsonString​(PMessageOrBuilder<Message> message)
        Prints a pretty formatted JSON string of the provided message.
        Type Parameters:
        Message - The message type.
        Parameters:
        message - The message to stringify.
        Returns:
        The resulting string.
      • parseJsonString

        @Nonnull
        public static <Message extends PMessage<Message>> Message parseJsonString​(java.lang.String string,
                                                                                  PMessageDescriptor<Message> descriptor)
        Parses a JSON string, and makes exceptions unchecked.
        Type Parameters:
        Message - The message type.
        Parameters:
        string - The message string to parse.
        descriptor - The message descriptor.
        Returns:
        The parsed message.
      • serialize

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

        public <T extends PMessage<T>> int serialize​(@Nonnull
                                                     java.io.OutputStream output,
                                                     @Nonnull
                                                     PMessageOrBuilder<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.
        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>> int serialize​(@Nonnull
                                                     java.io.OutputStream output,
                                                     @Nonnull
                                                     PServiceCall<T> 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.
        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>> T deserialize​(@Nonnull
                                                     java.io.InputStream input,
                                                     @Nonnull
                                                     PMessageDescriptor<T> 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.
        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>> T deserialize​(@Nonnull
                                                     java.io.Reader input,
                                                     @Nonnull
                                                     PMessageDescriptor<T> type)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • deserialize

        @Nonnull
        public <T extends PMessage<T>> PServiceCall<T> 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.
        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.
      • verifyEndOfContent

        public void verifyEndOfContent​(@Nonnull
                                       java.io.InputStream input)
                                throws java.io.IOException
        Description copied from class: Serializer
        Verify that the input stream does not contain any more readable content and close the stream. Throws exception if unable to verify, or the streams contains any meaningful content.
        Specified by:
        verifyEndOfContent in class Serializer
        Parameters:
        input - The input stream to check.
        Throws:
        java.io.IOException - If unable to read from stream or stream contains meaningful content.
      • verifyEndOfContent

        public void verifyEndOfContent​(@Nonnull
                                       java.io.Reader input)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • 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.