Class PrettySerializer
- java.lang.Object
-
- net.morimekta.providence.serializer.Serializer
-
- net.morimekta.providence.serializer.PrettySerializer
-
public class PrettySerializer extends Serializer
Pretty printer that can print message content for easily reading and debugging. This is a write only format used in stringifying messages.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MEDIA_TYPE
-
Fields inherited from class net.morimekta.providence.serializer.Serializer
DEFAULT_STRICT
-
-
Constructor Summary
Constructors Constructor Description PrettySerializer()
PrettySerializer(boolean strict)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
binaryProtocol()
PrettySerializer
compact()
Make a PrettySerializer that generates content similar to the PMessage asString methods.PrettySerializer
config()
Make a PrettySerializer that generates content similar to what the ProvidenceConfig reads.<Message extends PMessage<Message>>
Messagedeserialize(java.io.InputStream input, PMessageDescriptor<Message> descriptor)
Deserialize a message.<Message extends PMessage<Message>>
PServiceCall<Message>deserialize(java.io.InputStream input, PService service)
Deserialize a service call.java.lang.String
mediaType()
static <Message extends PMessage<Message>>
MessageparseDebugString(java.lang.String string, PMessageDescriptor<Message> descriptor)
Parses a pretty formatted string, and makes exceptions unchecked.<Message extends PMessage<Message>>
intserialize(java.io.OutputStream out, PMessageOrBuilder<Message> message)
Serialize a message.<Message extends PMessage<Message>>
intserialize(java.io.OutputStream out, PServiceCall<Message> call)
Serialize a service call.PrettySerializer
string()
Make a PrettySerializer that generates content similar to the PMessage toString methods.static <Message extends PMessage<Message>>
java.lang.StringtoDebugString(PMessageOrBuilder<Message> message)
Prints a pretty formatted string that is optimized for diffing (mainly for testing and debugging).void
verifyEndOfContent(java.io.InputStream input)
Verify that the input stream does not contain any more readable content and close the stream.-
Methods inherited from class net.morimekta.providence.serializer.Serializer
isRequestCallType, toString
-
-
-
-
Field Detail
-
MEDIA_TYPE
public static final java.lang.String MEDIA_TYPE
- See Also:
- Constant Field Values
-
-
Method Detail
-
toDebugString
@Nonnull public static <Message extends PMessage<Message>> java.lang.String toDebugString(PMessageOrBuilder<Message> message)
Prints a pretty formatted string that is optimized for diffing (mainly for testing and debugging).- Type Parameters:
Message
- The message type.- Parameters:
message
- The message to stringify.- Returns:
- The resulting string.
-
parseDebugString
@Nonnull public static <Message extends PMessage<Message>> Message parseDebugString(java.lang.String string, PMessageDescriptor<Message> descriptor)
Parses a pretty formatted 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.
-
compact
public PrettySerializer compact()
Make a PrettySerializer that generates content similar to the PMessage asString methods. The output of this has very little whitespace, so can be pretty difficult to read. It's similar to thestring()
variant, but without the qualified name prefix.- Returns:
- Compact pretty serializer.
-
string
public PrettySerializer string()
Make a PrettySerializer that generates content similar to the PMessage toString methods. The output of this has very little whitespace, so can be pretty difficult to read. It prefixes the message with the root message qualified name, as anyPMessage
.toString() would expect.- Returns:
- String pretty serializer.
-
config
public PrettySerializer config()
Make a PrettySerializer that generates content similar to what the ProvidenceConfig reads. It will not make use of references or anything fancy though.- Returns:
- Config-like pretty serializer.
-
serialize
public <Message extends PMessage<Message>> int serialize(@Nonnull java.io.OutputStream out, @Nonnull PMessageOrBuilder<Message> message)
Description copied from class:Serializer
Serialize a message.- Specified by:
serialize
in classSerializer
- Type Parameters:
Message
- The message type.- Parameters:
out
- The output stream to write to.message
- The message to write.- Returns:
- The number of bytes written to the output stream.
-
serialize
public <Message extends PMessage<Message>> int serialize(@Nonnull java.io.OutputStream out, @Nonnull PServiceCall<Message> call)
Description copied from class:Serializer
Serialize a service call.- Specified by:
serialize
in classSerializer
- Type Parameters:
Message
- The message type contained in the service call.- Parameters:
out
- The output stream to write to.call
- The service call to write.- Returns:
- The number of bytes written to the output stream.
-
deserialize
@Nonnull public <Message extends PMessage<Message>> PServiceCall<Message> 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 classSerializer
- Type Parameters:
Message
- 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.
-
deserialize
@Nonnull public <Message extends PMessage<Message>> Message deserialize(@Nonnull java.io.InputStream input, @Nonnull PMessageDescriptor<Message> descriptor) throws java.io.IOException
Description copied from class:Serializer
Deserialize a message.- Specified by:
deserialize
in classSerializer
- Type Parameters:
Message
- The message type.- Parameters:
input
- The input stream to read from.descriptor
- The descriptor of the message to read.- Returns:
- The message read.
- Throws:
java.io.IOException
- If reading or deserialization failed.
-
binaryProtocol
public boolean binaryProtocol()
- Specified by:
binaryProtocol
in classSerializer
- 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 classSerializer
- Parameters:
input
- The input stream to check.- Throws:
java.io.IOException
- If unable to read from stream or stream contains meaningful content.
-
mediaType
@Nonnull public java.lang.String mediaType()
- Specified by:
mediaType
in classSerializer
- Returns:
- Media type of the content generated by the serializer. Can be used for Content-Type and Accept HTTP headers, MIME type etc.
-
-