Class MessagePackString
- Namespace
- Nerdbank.MessagePack
- Assembly
- Nerdbank.MessagePack.dll
A .NET string together with its msgpack encoding parts, to optimize serialization of well-known, often seen strings such as property names used in a msgpack map object.
public class MessagePackString : IEquatable<MessagePackString>
- Inheritance
-
MessagePackString
- Implements
- Inherited Members
Remarks
This class is not a substitute for string interning. Activate string interning via the InternStrings property. This class avoids the need to encode/decode strings, which is even more efficient than interning strings, but it is meant only for a finite sized set of well-known strings.
Constructors
MessagePackString(string)
Initializes a new instance of the MessagePackString class.
public MessagePackString(string value)
Parameters
value
stringThe string to pre-encode for msgpack serialization.
Properties
MsgPack
Gets the msgpack encoded bytes of the string.
public ReadOnlyMemory<byte> MsgPack { get; }
Property Value
- ReadOnlyMemory<byte>
The msgpack encoded bytes are the UTF-8 encoded bytes of the string, prefixed with the msgpack encoding of the string length.
Remarks
The value of this property is suitable for providing to the WriteRaw(ReadOnlySpan<byte>) method.
Utf8
Gets the UTF-8 encoded bytes of the string.
public ReadOnlyMemory<byte> Utf8 { get; }
Property Value
Value
Gets the string value behind this instance.
public string Value { get; }
Property Value
Methods
Equals(MessagePackString?)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(MessagePackString? other)
Parameters
other
MessagePackStringAn object to compare with this object.
Returns
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
obj
objectThe object to compare with the current object.
Returns
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
IsMatch(ReadOnlySequence<byte>)
Checks whether a given UTF-8 encoded string matches the string in Value.
public bool IsMatch(ReadOnlySequence<byte> utf8String)
Parameters
utf8String
ReadOnlySequence<byte>The UTF-8 encoded string to test against.
Returns
Remarks
This method is allocation free.
IsMatch(ReadOnlySpan<byte>)
Checks whether a given UTF-8 encoded string matches the string in Value.
public bool IsMatch(ReadOnlySpan<byte> utf8String)
Parameters
utf8String
ReadOnlySpan<byte>The UTF-8 encoded string to test against.
Returns
Remarks
This method is allocation free.
TryRead(ref MessagePackReader)
Checks the string at the current reader position for equality with this string.
public bool TryRead(ref MessagePackReader reader)
Parameters
reader
MessagePackReaderThe reader. The reader's position will be advanced if and only if the next msgpack token is a matching string.
Returns
Remarks
No exception is thrown if the reader is positioned at a non-string token.
This method never allocates memory nor encodes/decodes strings.
Exceptions
- EndOfStreamException
Thrown if the reader has no more tokens, or the buffer contains an incomplete string token.