Class StreamUtils
Provides the framing of JSON messages on a stream: a 4-byte little-endian length, then that many bytes of UTF-8 JSON. The readers limit the payload; the writers do not.
public static class StreamUtils
- Inheritance
-
StreamUtils
- Inherited Members
Fields
MaxMessageLength
The readers' default largest payload, in bytes, after the 4-byte length: 512 KB; a payload of exactly it is accepted.
public const int MaxMessageLength = 524288
Field Value
Methods
ObjectToJsonBuffer(object)
Frames an object as a JSON message, by its runtime type.
public static Memory<byte> ObjectToJsonBuffer(object obj)
Parameters
objobjectThe object.
Returns
ObjectToJsonBuffer(object, JsonTypeInfo)
Frames an object as a JSON message, with its type's metadata.
public static Memory<byte> ObjectToJsonBuffer(object obj, JsonTypeInfo jsonTypeInfo)
Parameters
objobjectThe object.
jsonTypeInfoJsonTypeInfoThe type's metadata.
Returns
ObjectToJsonBuffer<T>(T, JsonTypeInfo<T>)
Frames an object as a JSON message, with its metadata.
public static Memory<byte> ObjectToJsonBuffer<T>(T obj, JsonTypeInfo<T> jsonTypeInfo)
Parameters
objTThe object.
jsonTypeInfoJsonTypeInfo<T>The type's metadata.
Returns
Type Parameters
TThe object's type.
ReadMessageAsync(Stream, int, CancellationToken)
Reads a framed message's text; it rejects a frame whose next four bytes are HTTP.
public static Task<string> ReadMessageAsync(Stream stream, int maxLength, CancellationToken cancellationToken)
Parameters
streamStreamThe stream.
maxLengthintThe largest message accepted, in bytes.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Exceptions
- FormatException
The length is negative, or over
maxLength.- Exception
The length is zero.
- EndOfStreamException
The stream ends early.
- UnauthorizedAccessException
The stream's next four bytes are
HTTP.- OperationCanceledException
cancellationTokenwas canceled.- DecoderFallbackException
The message's bytes are no UTF-8.
ReadMessageAsync(Stream, CancellationToken)
Reads a framed message's text, of at most MaxMessageLength.
public static Task<string> ReadMessageAsync(Stream stream, CancellationToken cancellationToken)
Parameters
streamStreamThe stream.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Exceptions
- FormatException
The length is negative, or over the largest.
- Exception
The length is zero.
- EndOfStreamException
The stream ends early.
- UnauthorizedAccessException
The stream's next four bytes are
HTTP.- OperationCanceledException
cancellationTokenwas canceled.- DecoderFallbackException
The message's bytes are no UTF-8.
ReadObjectAsync<T>(Stream, int, CancellationToken)
Reads a framed JSON message as an object.
public static Task<T> ReadObjectAsync<T>(Stream stream, int maxLength, CancellationToken cancellationToken)
Parameters
streamStreamThe stream.
maxLengthintThe largest message accepted, in bytes.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<T>
A task whose result is the object.
Type Parameters
TThe object's type.
Exceptions
- FormatException
The length is negative, or over
maxLength.- Exception
The length is zero, or the deserialization returns null.
- EndOfStreamException
The stream ends early.
- JsonException
The message is no JSON of the type, as when its bytes are no UTF-8.
- UnauthorizedAccessException
The stream's next four bytes are
HTTP.- OperationCanceledException
cancellationTokenwas canceled.
ReadObjectAsync<T>(Stream, JsonTypeInfo<T>, int, CancellationToken)
Reads a framed JSON message as an object with its metadata.
public static Task<T> ReadObjectAsync<T>(Stream stream, JsonTypeInfo<T> jsonTypeInfo, int maxLength, CancellationToken cancellationToken)
Parameters
streamStreamThe stream.
jsonTypeInfoJsonTypeInfo<T>The type's metadata.
maxLengthintThe largest message accepted, in bytes.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<T>
A task whose result is the object.
Type Parameters
TThe object's type.
Exceptions
- FormatException
The length is negative, or over
maxLength.- Exception
The length is zero, or the deserialization returns null.
- EndOfStreamException
The stream ends early.
- JsonException
The message is no JSON of the type, as when its bytes are no UTF-8.
- UnauthorizedAccessException
The stream's next four bytes are
HTTP.- OperationCanceledException
cancellationTokenwas canceled.
ReadObjectAsync<T>(Stream, JsonTypeInfo<T>, CancellationToken)
Reads a framed JSON message as an object with its metadata, of at most MaxMessageLength.
public static Task<T> ReadObjectAsync<T>(Stream stream, JsonTypeInfo<T> jsonTypeInfo, CancellationToken cancellationToken)
Parameters
streamStreamThe stream.
jsonTypeInfoJsonTypeInfo<T>The type's metadata.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<T>
A task whose result is the object.
Type Parameters
TThe object's type.
Exceptions
- FormatException
The length is negative, or over the largest.
- Exception
The length is zero, or the deserialization returns null.
- EndOfStreamException
The stream ends early.
- JsonException
The message is no JSON of the type, as when its bytes are no UTF-8.
- UnauthorizedAccessException
The stream's next four bytes are
HTTP.- OperationCanceledException
cancellationTokenwas canceled.
ReadObjectAsync<T>(Stream, CancellationToken)
Reads a framed JSON message as an object, of at most MaxMessageLength.
public static Task<T> ReadObjectAsync<T>(Stream stream, CancellationToken cancellationToken)
Parameters
streamStreamThe stream.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<T>
A task whose result is the object.
Type Parameters
TThe object's type.
Exceptions
- FormatException
The length is negative, or over the largest.
- Exception
The length is zero, or the deserialization returns null.
- EndOfStreamException
The stream ends early.
- JsonException
The message is no JSON of the type, as when its bytes are no UTF-8.
- UnauthorizedAccessException
The stream's next four bytes are
HTTP.- OperationCanceledException
cancellationTokenwas canceled.
ReadObject<T>(Stream, int)
Reads a framed JSON message as an object, synchronously.
public static T ReadObject<T>(Stream stream, int maxLength = 524288)
Parameters
Returns
- T
The object.
Type Parameters
TThe object's type.
Exceptions
- FormatException
The length is negative, or over
maxLength.- Exception
The length is zero, or the deserialization returns null.
- EndOfStreamException
The stream ends early.
- JsonException
The message is no JSON of the type, as when its bytes are no UTF-8.
WriteObject(Stream, object)
Writes an object as a framed JSON message, synchronously.
public static void WriteObject(Stream stream, object obj)
Parameters
WriteObjectAsync(Stream, object, JsonTypeInfo, CancellationToken)
Writes an object as a framed JSON message, with its type's metadata.
public static ValueTask WriteObjectAsync(Stream stream, object obj, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
Parameters
streamStreamThe stream.
objobjectThe object.
jsonTypeInfoJsonTypeInfoThe type's metadata.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- ValueTask
A task that represents the asynchronous operation.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
WriteObjectAsync(Stream, object, CancellationToken)
Writes an object as a framed JSON message, by its runtime type.
public static ValueTask WriteObjectAsync(Stream stream, object obj, CancellationToken cancellationToken)
Parameters
streamStreamThe stream.
objobjectThe object.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- ValueTask
A task that represents the asynchronous operation.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
WriteObjectAsync<T>(Stream, T, JsonTypeInfo<T>, CancellationToken)
Writes an object as a framed JSON message, with its metadata.
public static ValueTask WriteObjectAsync<T>(Stream stream, T obj, JsonTypeInfo<T> jsonTypeInfo, CancellationToken cancellationToken)
Parameters
streamStreamThe stream.
objTThe object.
jsonTypeInfoJsonTypeInfo<T>The type's metadata.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- ValueTask
A task that represents the asynchronous operation.
Type Parameters
TThe object's type.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.