save progress
This commit is contained in:
@@ -20,7 +20,14 @@ namespace StellaOps.Canonical.Json;
|
||||
/// </remarks>
|
||||
public static class CanonJson
|
||||
{
|
||||
private static readonly JsonWriterOptions CanonWriterOptions = new()
|
||||
private static readonly JsonSerializerOptions DefaultSerializerOptions = new()
|
||||
{
|
||||
WriteIndented = false,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
};
|
||||
|
||||
private static readonly JsonWriterOptions DefaultWriterOptions = new()
|
||||
{
|
||||
Indented = false,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
@@ -62,16 +69,11 @@ public static class CanonJson
|
||||
/// <returns>UTF-8 encoded canonical JSON bytes.</returns>
|
||||
public static byte[] Canonicalize<T>(T obj)
|
||||
{
|
||||
var json = JsonSerializer.SerializeToUtf8Bytes(obj, new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = false,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
});
|
||||
var json = JsonSerializer.SerializeToUtf8Bytes(obj, DefaultSerializerOptions);
|
||||
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new Utf8JsonWriter(ms, CanonWriterOptions);
|
||||
using var writer = new Utf8JsonWriter(ms, DefaultWriterOptions);
|
||||
|
||||
WriteElementSorted(doc.RootElement, writer);
|
||||
writer.Flush();
|
||||
@@ -92,7 +94,7 @@ public static class CanonJson
|
||||
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new Utf8JsonWriter(ms, CanonWriterOptions);
|
||||
using var writer = new Utf8JsonWriter(ms, CreateWriterOptions(options));
|
||||
|
||||
WriteElementSorted(doc.RootElement, writer);
|
||||
writer.Flush();
|
||||
@@ -107,15 +109,50 @@ public static class CanonJson
|
||||
/// <returns>UTF-8 encoded canonical JSON bytes.</returns>
|
||||
public static byte[] CanonicalizeParsedJson(ReadOnlySpan<byte> jsonBytes)
|
||||
{
|
||||
using var doc = JsonDocument.Parse(jsonBytes.ToArray());
|
||||
var reader = new Utf8JsonReader(jsonBytes, isFinalBlock: true, state: default);
|
||||
using var doc = JsonDocument.ParseValue(ref reader);
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new Utf8JsonWriter(ms, CanonWriterOptions);
|
||||
using var writer = new Utf8JsonWriter(ms, DefaultWriterOptions);
|
||||
|
||||
WriteElementSorted(doc.RootElement, writer);
|
||||
writer.Flush();
|
||||
return ms.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Canonicalizes raw JSON bytes using a custom encoder for output.
|
||||
/// </summary>
|
||||
/// <param name="jsonBytes">UTF-8 encoded JSON bytes.</param>
|
||||
/// <param name="encoder">Encoder to use for output escaping.</param>
|
||||
/// <returns>UTF-8 encoded canonical JSON bytes.</returns>
|
||||
public static byte[] CanonicalizeParsedJson(ReadOnlySpan<byte> jsonBytes, JavaScriptEncoder encoder)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(encoder);
|
||||
|
||||
var reader = new Utf8JsonReader(jsonBytes, isFinalBlock: true, state: default);
|
||||
using var doc = JsonDocument.ParseValue(ref reader);
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new Utf8JsonWriter(ms, new JsonWriterOptions
|
||||
{
|
||||
Indented = false,
|
||||
Encoder = encoder
|
||||
});
|
||||
|
||||
WriteElementSorted(doc.RootElement, writer);
|
||||
writer.Flush();
|
||||
return ms.ToArray();
|
||||
}
|
||||
|
||||
private static JsonWriterOptions CreateWriterOptions(JsonSerializerOptions? options)
|
||||
{
|
||||
var encoder = options?.Encoder ?? DefaultWriterOptions.Encoder;
|
||||
return new JsonWriterOptions
|
||||
{
|
||||
Indented = false,
|
||||
Encoder = encoder
|
||||
};
|
||||
}
|
||||
|
||||
private static void WriteElementSorted(JsonElement el, Utf8JsonWriter w)
|
||||
{
|
||||
switch (el.ValueKind)
|
||||
@@ -198,16 +235,11 @@ public static class CanonJson
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(version);
|
||||
|
||||
var json = JsonSerializer.SerializeToUtf8Bytes(obj, new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = false,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
});
|
||||
var json = JsonSerializer.SerializeToUtf8Bytes(obj, DefaultSerializerOptions);
|
||||
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new Utf8JsonWriter(ms, CanonWriterOptions);
|
||||
using var writer = new Utf8JsonWriter(ms, DefaultWriterOptions);
|
||||
|
||||
WriteElementVersioned(doc.RootElement, writer, version);
|
||||
writer.Flush();
|
||||
@@ -230,7 +262,7 @@ public static class CanonJson
|
||||
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new Utf8JsonWriter(ms, CanonWriterOptions);
|
||||
using var writer = new Utf8JsonWriter(ms, CreateWriterOptions(options));
|
||||
|
||||
WriteElementVersioned(doc.RootElement, writer, version);
|
||||
writer.Flush();
|
||||
@@ -249,6 +281,11 @@ public static class CanonJson
|
||||
// Write remaining properties sorted
|
||||
foreach (var prop in el.EnumerateObject().OrderBy(p => p.Name, StringComparer.Ordinal))
|
||||
{
|
||||
if (string.Equals(prop.Name, CanonVersion.VersionFieldName, StringComparison.Ordinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
w.WritePropertyName(prop.Name);
|
||||
WriteElementSorted(prop.Value, w);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user