From 2d91fce761f70ac4013083236ac142c92d34d7d1 Mon Sep 17 00:00:00 2001 From: Jon Sagara Date: Mon, 4 May 2026 18:32:58 -0700 Subject: [PATCH] Add Stream overloads to STJ Serialize and Deserialize. --- Directory.Build.props | 2 +- .../Json/SystemTextJson/STJsonHelper.cs | 89 ++++++++++++++++++- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index b06a2b9..44ca326 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ net8.0;net9.0;net10.0 - 5.4.4 + 5.4.5 5.4.0 5.4.0 Jon Sagara diff --git a/src/Sagara.Core/Json/SystemTextJson/STJsonHelper.cs b/src/Sagara.Core/Json/SystemTextJson/STJsonHelper.cs index 3ec61c2..06801a4 100644 --- a/src/Sagara.Core/Json/SystemTextJson/STJsonHelper.cs +++ b/src/Sagara.Core/Json/SystemTextJson/STJsonHelper.cs @@ -51,6 +51,35 @@ public static string Serialize(TValue value, bool camelCase = true, bool : JsonSerializer.Serialize(value, _serializePascalCaseMinify); } + /// + /// Convert the provided value to UTF-8 encoded JSON text and write it to the . + /// + /// The UTF-8 to write to. + /// The value to convert to JSON. + /// True to use camelCase property naming; false to use PascalCase. Default is true. + /// True to pretty print the JSON; false to minify it. Default is false. + public static void Serialize(Stream utf8Json, TValue value, bool camelCase = true, bool prettyPrint = false) + { + Check.ThrowIfNull(utf8Json); + + if (camelCase) + { + var camelCaseOptions = prettyPrint + ? _serializeCamelCasePrettyPrint + : _serializeCamelCaseMinify; + + JsonSerializer.Serialize(utf8Json, value, camelCaseOptions); + } + else + { + var pascalCaseOptions = prettyPrint + ? _serializePascalCasePrettyPrint + : _serializePascalCaseMinify; + + JsonSerializer.Serialize(utf8Json, value, pascalCaseOptions); + } + } + /// /// Asynchronously convert the provided value to UTF-8 encoded JSON text and write it to the . /// @@ -65,19 +94,19 @@ public static async Task SerializeAsync(Stream utf8Json, TValue value, b if (camelCase) { - var options = prettyPrint + var camelCaseOptions = prettyPrint ? _serializeCamelCasePrettyPrint : _serializeCamelCaseMinify; - await JsonSerializer.SerializeAsync(utf8Json, value, options, cancellationToken).ConfigureAwait(false); + await JsonSerializer.SerializeAsync(utf8Json, value, camelCaseOptions, cancellationToken).ConfigureAwait(false); } else { - var options = prettyPrint + var pascalCaseOptions = prettyPrint ? _serializePascalCasePrettyPrint : _serializePascalCaseMinify; - await JsonSerializer.SerializeAsync(utf8Json, value, options, cancellationToken).ConfigureAwait(false); + await JsonSerializer.SerializeAsync(utf8Json, value, pascalCaseOptions, cancellationToken).ConfigureAwait(false); } } @@ -98,6 +127,24 @@ public static string Serialize(TValue value, JsonSerializerOptions optio return JsonSerializer.Serialize(value, options); } + /// + /// Convert the value into a JSON string using the settings passed in by the caller. + /// + /// + /// This class's available configurations don't work for all cases. Allow the caller to completely customize + /// the serialization behavior. + /// + /// The UTF-8 to write to. + /// The value to convert to JSON. + /// The explicitly configured by the caller. + public static void Serialize(Stream utf8Json, TValue value, JsonSerializerOptions options) + { + Check.ThrowIfNull(utf8Json); + Check.ThrowIfNull(options); + + JsonSerializer.Serialize(utf8Json, value, options); + } + /// /// Asynchronously convert the value into a JSON string using the settings passed in by the caller. /// @@ -164,6 +211,40 @@ public static async Task SerializeAsync(Stream utf8Json, TValue value, J return JsonSerializer.Deserialize(json, options); } + /// + /// Reads the UTF-8 encoded text representing a single JSON value into a . + /// The Stream will be read to completion. + /// + /// JSON data to parse. + /// True to use case-insensitive property names; false to use + /// case-sensitive property names. Default is true. + /// The deserialized object of type T. + public static T? Deserialize(Stream utf8Json, bool caseInsensitivePropertyNames = true) + { + Check.ThrowIfNull(utf8Json); + + var options = caseInsensitivePropertyNames + ? _deserializeCaseInsensitive + : _deserializeCaseSensitive; + + return JsonSerializer.Deserialize(utf8Json, options); + } + + /// + /// Reads the UTF-8 encoded text representing a single JSON value into a . + /// The Stream will be read to completion. + /// + /// JSON data to parse. + /// The explicitly configured by the caller. + /// The deserialized object of type T. + public static T? Deserialize(Stream utf8Json, JsonSerializerOptions options) + { + Check.ThrowIfNull(utf8Json); + Check.ThrowIfNull(options); + + return JsonSerializer.Deserialize(utf8Json, options); + } + /// /// Asynchronously reads the UTF-8 encoded text representing a single JSON value into a . /// The Stream will be read to completion.