public class SurfSerializer extends Object
This serializer recognizes and can serialize the following type for SURF categories of resources:
byte[]ByteBufferCharacterCodePointCharacterEmailAddressBigInteger (serialized as decimal)BigDecimal (serialized as decimal)Number (including Integer, Long, and Double)CharSequence (including String)TelephoneNumberDate (serialized as instant)InstantMonthDayLocalDateLocalDateTimeLocalTimeOffsetDateTimeOffsetTimeYearYearMonthZonedDateTimeThis serializer is meant to be used once for generating a single SURF document. It should not be used to serialize multiple documents, as it maintains serialization state.
The serializer should be released after use so as not to leak memory of parsed resources when resources are present with tags/IDs and/or generate aliases.
This implementation is not thread safe.
| Modifier and Type | Field and Description |
|---|---|
static String |
GENERATED_ALIAS_PREFIX
The prefix used when generating aliases.
|
| Constructor and Description |
|---|
SurfSerializer() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
decreaseIndentLevel()
Decreases the indention level.
|
protected String |
determineAliasForResource(Object resource)
Determines an alias to use with a resource, generating one if the graph requires it.
|
protected void |
discoverResourceReferences(Object resource)
Discovers whether there are references to the given resource and recursively all nested resources.
|
protected void |
formatIndent(Appendable appendable)
Appends indention characters at the appropriate level if formatting is enabled.
|
protected boolean |
formatNewLine(Appendable appendable)
Separates lines by adding a line separation character sequence if formatting is enabled.
|
Optional<String> |
getAliasForResource(Object resource)
Returns any alias associated with a resource.
|
CharSequence |
getIndentSequence()
Returns the sequence of characters used for each indention level.
|
CharSequence |
getLineSeparator()
Returns the sequence of characters used to separate lines.
|
protected Closeable |
increaseIndentLevel()
Increases the indention level.
|
boolean |
isFormatted()
Returns whether this serializer will format the document with additional whitespace and newlines.
|
boolean |
isSequenceSeparatorRequired()
Whether separators will always be added between sequence items even if newlines are present.
|
protected boolean |
isSerialized(Object resource)
Determines whether a given resource has already been serialized.
|
void |
serialize(Appendable appendable,
Object root)
Serializes a SURF resource graph to a writer.
|
String |
serialize(Object root)
Serializes a SURF resource graph to a string.
|
void |
serialize(OutputStream outputStream,
Object root)
Serializes a SURF resource graph to an output stream.
|
static void |
serializeBinary(Appendable appendable,
byte[] bytes)
Serializes a binary literal along with its delimiter from an array of bytes.
|
static void |
serializeBinary(Appendable appendable,
ByteBuffer byteBuffer)
Serializes a binary literal along with its delimiter from a byte buffer.
|
static void |
serializeBoolean(Appendable appendable,
boolean bool)
Serializes a Boolean.
|
static void |
serializeCharacter(Appendable appendable,
int codePoint)
Serializes a character surrounded by character delimiters.
|
static void |
serializeCharacterCodePoint(Appendable appendable,
char delimiter,
int codePoint)
Serializes a character as content, without any delimiters.
|
void |
serializeDescription(Appendable appendable,
SurfObject surfObject)
Serializes a SURF resource description.
|
static void |
serializeEmailAddress(Appendable appendable,
com.globalmentor.net.EmailAddress emailAddress)
Serializes an email address along with its delimiter.
|
static void |
serializeIri(Appendable appendable,
URI iri)
Serializes an IRI along with its delimiters.
|
void |
serializeList(Appendable appendable,
List<?> list)
Serializes a SURF list.
|
void |
serializeMap(Appendable appendable,
Map<?,?> map)
Serializes a SURF map.
|
static void |
serializeNumber(Appendable appendable,
Number number)
Serializes a number along with its delimiter if should be represented as a decimal.
|
void |
serializeObject(Appendable appendable,
SurfObject surfObject)
Serializes a SURF object representation without the following description.
|
static void |
serializeRegularExpression(Appendable appendable,
Pattern regularExpression)
Serializes a regular expression along with its delimiters.
|
void |
serializeResource(Appendable appendable,
Object resource)
Serializes a SURF resource to a writer.
|
protected <I> void |
serializeSequence(Appendable appendable,
Iterable<I> sequence,
com.globalmentor.io.function.IOBiConsumer<Appendable,I> itemSerializer)
Serializes a general SURF sequence (such as a list).
|
void |
serializeSet(Appendable appendable,
Set<?> set)
Serializes a SURF set.
|
static void |
serializeString(Appendable appendable,
CharSequence charSequence)
Serializes a string surrounded by string delimiters.
|
static void |
serializeTelephoneNumber(Appendable appendable,
com.globalmentor.itu.TelephoneNumber telephoneNumber)
Serializes a telephone number along with its delimiter.
|
static void |
serializeTemporal(Appendable appendable,
TemporalAccessor temporal)
Serializes a temporal literal along with its delimiter.
|
static void |
serializeUuid(Appendable appendable,
UUID uuid)
Serializes a UUID along with its delimiter.
|
void |
setAliasForResource(Object resource,
String alias)
Associates an alias with a resource.
|
void |
setFormatted(boolean formatted)
Sets whether the serialization should be formatted.
|
void |
setIndentSequence(CharSequence indentSequence)
Set the sequence of characters used for each indention level.
|
void |
setLineSeparator(CharSequence lineSeparator)
Sets the sequence of characters used to separate lines.
|
void |
setSequenceSeparatorRequired(boolean sequenceSeparatorRequired)
Sets whether separators will always be added between sequence items even if newlines are present.
|
protected boolean |
setSerialized(Object resource)
Sets a resource has having been serialized.
|
public static final String GENERATED_ALIAS_PREFIX
public boolean isFormatted()
This implementation defaults to no formatting.
public void setFormatted(boolean formatted)
formatted - Whether this serializer will format the document with additional whitespace and newlines.public CharSequence getIndentSequence()
This implementation defaults to the horizontal tab character.
public void setIndentSequence(@Nonnull CharSequence indentSequence)
indentSequence - The character(s) to use for indention.protected Closeable increaseIndentLevel()
Closeable.close() is called.protected void formatIndent(@Nonnull Appendable appendable) throws IOException
If formatting is turned off, no content will be added.
appendable - The appendable to which SURF data should be appended.IOException - If there was an error writing the indent.isFormatted(),
getIndentSequence()protected void decreaseIndentLevel()
public CharSequence getLineSeparator()
This implementation defaults to the platform-dependent line separator for the current system.
System.lineSeparator()public void setLineSeparator(@Nonnull CharSequence lineSeparator)
lineSeparator - The character(s) to use for line endings.System.lineSeparator()protected boolean formatNewLine(@Nonnull Appendable appendable) throws IOException
If formatting is turned off, no content will be added.
appendable - The appendable to which SURF data should be appended.IOException - If there was an error writing the line separator.isFormatted(),
getLineSeparator()public boolean isSequenceSeparatorRequired()
This implementation defaults to not adding sequence separators if not needed.
public void setSequenceSeparatorRequired(boolean sequenceSeparatorRequired)
sequenceSeparatorRequired - Whether sequence separators will always be added even when options.protected void discoverResourceReferences(@Nonnull Object resource)
This method should not be called more than once for any resource graph, or all resources in the graph will be marked as having references.
resource - The resource graph for which references should be discovered.public Optional<String> getAliasForResource(@Nonnull Object resource)
resource - The resource with which an alias may be associated.public void setAliasForResource(@Nonnull Object resource, @Nonnull String alias)
Aliases are not allowed to be set for objects with tags or IDs, which themselves serve as labels.
resource - The resource to associate with an alias.alias - The alias to associate with the resource.NullPointerException - if the given resource and/or alias is null.IllegalArgumentException - if the given alias is not a valid name token.IllegalArgumentException - if an alias is given for a SurfObject with a tag or an ID.protected String determineAliasForResource(@Nonnull Object resource)
SurfObject with an a tag or an ID, as that serves as a label.resource - The resource with which an alias may be associated, or null if there is no alias.protected boolean isSerialized(@Nonnull Object resource)
resource - The resource to check.true if the resource has already been serialized.protected boolean setSerialized(@Nonnull Object resource)
resource - The resource to record as serialized.public String serialize(@Nonnull @Nullable Object root) throws IOException
This method discovers resource references to that aliases may be generated as needed. This record of resource references is reset after serialization, but any generated aliases remain. This allows the same serializer to be used multiple times for the same graph, with the same aliases being used.
This is a convenience method that delegates to serialize(Appendable, Object).
root - The root SURF resource, or null if there is no resource to serialize.IOException - If there was an error writing the SURF data.public void serialize(@Nonnull OutputStream outputStream, @Nullable Object root) throws IOException
This method discovers resource references to that aliases may be generated as needed. This record of resource references is reset after serialization, but any generated aliases remain. This allows the same serializer to be used multiple times for the same graph, with the same aliases being used.
outputStream - The output stream to receive SURF data.root - The root SURF resource, or null if there is no resource to serialize.IOException - If there was an error writing the SURF data.public void serialize(@Nonnull Appendable appendable, @Nullable Object root) throws IOException
All references to the resources in the graph must have already been discovered if aliases need to be generated.
appendable - The appendable to which SURF data should be appended.root - The root SURF resource, or null if there is no resource to serialize.NullPointerException - if the given appendable is null.IOException - If there was an error writing the SURF data.public void serializeResource(@Nonnull Appendable appendable, @Nullable Object resource) throws IOException
All references to the resources in the graph must have already been discovered if aliases need to be generated.
appendable - The appendable to which SURF data should be appended.resource - The SURF resource to serialize to serialize.NullPointerException - if the given appendable and/or resource is null.IOException - If there was an error appending the SURF data.public void serializeObject(@Nonnull Appendable appendable, @Nonnull SurfObject surfObject) throws IOException
All references to the resources in the graph must have already been discovered if aliases need to be generated.
appendable - The appendable to which SURF data should be appended.surfObject - The information to be serialized as a SURF object.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.OBJECT_BEGINpublic void serializeDescription(@Nonnull Appendable appendable, @Nonnull SurfObject surfObject) throws IOException
All references to the resources in the graph must have already been discovered if aliases need to be generated.
appendable - The appendable to which SURF data should be appended.surfObject - The SURF object with the description to be serialized.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.DESCRIPTION_BEGIN,
SURF.DESCRIPTION_ENDpublic static void serializeBinary(@Nonnull Appendable appendable, @Nonnull byte[] bytes) throws IOException
appendable - The appendable to which SURF data should be appended.bytes - The information to be serialized as a SURF binary literal.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.BINARY_BEGINpublic static void serializeBinary(@Nonnull Appendable appendable, @Nonnull ByteBuffer byteBuffer) throws IOException
appendable - The appendable to which SURF data should be appended.byteBuffer - The information to be serialized as a SURF binary literal.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.BINARY_BEGINpublic static void serializeBoolean(@Nonnull Appendable appendable, @Nonnull boolean bool) throws IOException
appendable - The appendable to which SURF data should be appended.bool - The Boolean value to be serialized as a SURF Boolean.NullPointerException - if the given reader is null.IllegalArgumentException - if the given code point is not a valid Unicode code point.IOException - if there is an error appending to the appendable.public static void serializeCharacter(@Nonnull Appendable appendable, @Nonnull int codePoint) throws IOException
appendable - The appendable to which SURF data should be appended.codePoint - The Unicode code point to be serialized as a SURF character.NullPointerException - if the given reader is null.IllegalArgumentException - if the given code point is not a valid Unicode code point.IOException - if there is an error appending to the appendable.SURF.CHARACTER_DELIMITER,
serializeCharacterCodePoint(Appendable, char, int)public static void serializeCharacterCodePoint(@Nonnull Appendable appendable, char delimiter, int codePoint) throws IOException, com.globalmentor.io.ParseIOException
This implementation does not escape the solidus (slash) character '/', which is not required to be escaped.
appendable - The appendable to which SURF data should be appended.delimiter - The delimiter that surrounds the character and which should be escaped.codePoint - The code point to serialize.NullPointerException - if the given appendable is null.IOException - if there is an error appending to the appender.com.globalmentor.io.ParseIOException - if a control character was represented, if the character is not escaped correctly, or the reader has no more characters before the
current character is completely parsed.SURF.CHARACTER_REQUIRED_ESCAPED_CHARACTERSpublic static void serializeEmailAddress(@Nonnull Appendable appendable, @Nonnull com.globalmentor.net.EmailAddress emailAddress) throws IOException
appendable - The appendable to which SURF data should be appended.emailAddress - The information to be serialized as a SURF email address.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.EMAIL_ADDRESS_BEGINpublic static void serializeIri(@Nonnull Appendable appendable, @Nonnull URI iri) throws IOException
This implementation serializes an IRI using a SURF IRI short form if possible.
appendable - The appendable to which SURF data should be appended.iri - The information to be serialized as a SURF IRI.NullPointerException - if the given reader is null.IllegalArgumentException - if the given IRI is not a true, absolute IRI with a scheme.IOException - if there is an error appending to the appendable.SURF.IRI_BEGINpublic static void serializeNumber(@Nonnull Appendable appendable, @Nonnull Number number) throws IOException
This implementation represents the following types as SURF decimal:
appendable - The appendable to which SURF data should be appended.number - The information to be serialized as a SURF number.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.NUMBER_DECIMAL_BEGINpublic static void serializeRegularExpression(@Nonnull Appendable appendable, @Nonnull Pattern regularExpression) throws IOException
appendable - The appendable to which SURF data should be appended.regularExpression - The information to be serialized as a SURF regular expression.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.REGULAR_EXPRESSION_DELIMITER,
SURF.REGULAR_EXPRESSION_ESCAPEpublic static void serializeString(@Nonnull Appendable appendable, @Nonnull CharSequence charSequence) throws IOException
appendable - The appendable to which SURF data should be appended.charSequence - The information to be serialized as a SURF string.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.STRING_DELIMITER,
serializeCharacterCodePoint(Appendable, char, int)public static void serializeTelephoneNumber(@Nonnull Appendable appendable, @Nonnull com.globalmentor.itu.TelephoneNumber telephoneNumber) throws IOException
appendable - The appendable to which SURF data should be appended.telephoneNumber - The information to be serialized as a SURF telephone number.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.IllegalArgumentException - if the given telephone number is not in global form.SURF.TELEPHONE_NUMBER_BEGIN,
TelephoneNumber.isGlobal()public static void serializeTemporal(@Nonnull Appendable appendable, @Nonnull TemporalAccessor temporal) throws IOException
appendable - The appendable to which SURF data should be appended.temporal - The information to be serialized as a SURF temporal.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.TEMPORAL_BEGINpublic static void serializeUuid(@Nonnull Appendable appendable, @Nonnull UUID uuid) throws IOException
appendable - The appendable to which SURF data should be appended.uuid - The information to be serialized as a SURF UUID.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.UUID_BEGINpublic void serializeList(@Nonnull Appendable appendable, @Nonnull List<?> list) throws IOException
All references to the resources in the graph must have already been discovered if aliases need to be generated.
appendable - The appendable to which SURF data should be appended.list - The information to be serialized as a SURF list.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.LIST_BEGIN,
SURF.LIST_ENDpublic void serializeMap(@Nonnull Appendable appendable, @Nonnull Map<?,?> map) throws IOException
All references to the resources in the graph must have already been discovered if aliases need to be generated.
appendable - The appendable to which SURF data should be appended.map - The information to be serialized as a SURF map.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.MAP_BEGIN,
SURF.MAP_END,
SURF.MAP_KEY_DELIMITER,
SURF.ENTRY_KEY_VALUE_DELIMITERpublic void serializeSet(@Nonnull Appendable appendable, @Nonnull Set<?> set) throws IOException
All references to the resources in the graph must have already been discovered if aliases need to be generated.
appendable - The appendable to which SURF data should be appended.set - The information to be serialized as a SURF set.NullPointerException - if the given reader is null.IOException - if there is an error appending to the appendable.SURF.SET_BEGIN,
SURF.SET_ENDprotected <I> void serializeSequence(@Nonnull Appendable appendable, @Nonnull Iterable<I> sequence, @Nonnull com.globalmentor.io.function.IOBiConsumer<Appendable,I> itemSerializer) throws IOException
IOBiConsumer.accept(Object, Object) is called, passing the output
Appendable along with the item to be serialized. The item serialization strategy will return false to indicate that there are no
further items.I - The type of item in the sequence.appendable - The appendable to which SURF data should be appended.sequence - An iterable representing the sequence to serialize.itemSerializer - The serialization strategy, which is passed the Appendable to use for serialization, along with each item to serialize.NullPointerException - if the given appendable and/or item serializer is null.IOException - if there is an error appending to the appender.Copyright © 2007–2017 GlobalMentor, Inc.. All rights reserved.