Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>

<!-- NuGet -->
<Version>5.4.4</Version>
<Version>5.4.5</Version>
<AssemblyVersion>5.4.0</AssemblyVersion>
<FileVersion>5.4.0</FileVersion>
<Authors>Jon Sagara</Authors>
Expand Down
89 changes: 85 additions & 4 deletions src/Sagara.Core/Json/SystemTextJson/STJsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ public static string Serialize<TValue>(TValue value, bool camelCase = true, bool
: JsonSerializer.Serialize(value, _serializePascalCaseMinify);
}

/// <summary>
/// Convert the provided value to UTF-8 encoded JSON text and write it to the <see cref="Stream"/>.
/// </summary>
/// <param name="utf8Json">The UTF-8 <see cref="Stream"/> to write to.</param>
/// <param name="value">The value to convert to JSON.</param>
/// <param name="camelCase">True to use camelCase property naming; false to use PascalCase. Default is true.</param>
/// <param name="prettyPrint">True to pretty print the JSON; false to minify it. Default is false.</param>
public static void Serialize<TValue>(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);
}
}

/// <summary>
/// Asynchronously convert the provided value to UTF-8 encoded JSON text and write it to the <see cref="Stream"/>.
/// </summary>
Expand All @@ -65,19 +94,19 @@ public static async Task SerializeAsync<TValue>(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);
}
}

Expand All @@ -98,6 +127,24 @@ public static string Serialize<TValue>(TValue value, JsonSerializerOptions optio
return JsonSerializer.Serialize(value, options);
}

/// <summary>
/// Convert the value into a JSON string using the settings passed in by the caller.
/// </summary>
/// <remarks>
/// This class's available configurations don't work for all cases. Allow the caller to completely customize
/// the serialization behavior.
/// </remarks>
/// <param name="utf8Json">The UTF-8 <see cref="Stream"/> to write to.</param>
/// <param name="value">The value to convert to JSON.</param>
/// <param name="options">The <see cref="JsonSerializerOptions"/> explicitly configured by the caller.</param>
public static void Serialize<TValue>(Stream utf8Json, TValue value, JsonSerializerOptions options)
{
Check.ThrowIfNull(utf8Json);
Check.ThrowIfNull(options);

JsonSerializer.Serialize(utf8Json, value, options);
}

/// <summary>
/// Asynchronously convert the value into a JSON string using the settings passed in by the caller.
/// </summary>
Expand Down Expand Up @@ -164,6 +211,40 @@ public static async Task SerializeAsync<TValue>(Stream utf8Json, TValue value, J
return JsonSerializer.Deserialize<T>(json, options);
}

/// <summary>
/// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="T"/>.
/// The Stream will be read to completion.
/// </summary>
/// <param name="utf8Json">JSON data to parse.</param>
/// <param name="caseInsensitivePropertyNames">True to use case-insensitive property names; false to use
/// case-sensitive property names. Default is true.</param>
/// <returns>The deserialized object of type T.</returns>
public static T? Deserialize<T>(Stream utf8Json, bool caseInsensitivePropertyNames = true)
{
Check.ThrowIfNull(utf8Json);

var options = caseInsensitivePropertyNames
? _deserializeCaseInsensitive
: _deserializeCaseSensitive;

return JsonSerializer.Deserialize<T>(utf8Json, options);
}

/// <summary>
/// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="T"/>.
/// The Stream will be read to completion.
/// </summary>
/// <param name="utf8Json">JSON data to parse.</param>
/// <param name="options">The <see cref="JsonSerializerOptions"/> explicitly configured by the caller.</param>
/// <returns>The deserialized object of type T.</returns>
public static T? Deserialize<T>(Stream utf8Json, JsonSerializerOptions options)
{
Check.ThrowIfNull(utf8Json);
Check.ThrowIfNull(options);

return JsonSerializer.Deserialize<T>(utf8Json, options);
}

/// <summary>
/// Asynchronously reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="T"/>.
/// The Stream will be read to completion.
Expand Down