using System.Text; using System.Text.Json; namespace StellaOps.Canonical.Json; /// /// Canonical JSON serialization with deterministic hashing. /// Produces bit-identical output across environments for proof replay. /// /// /// Key guarantees: /// /// Object keys are sorted alphabetically (Ordinal comparison) /// No whitespace or formatting variations /// Consistent number formatting /// UTF-8 encoding without BOM /// /// public static partial class CanonJson { /// /// Serializes an object to a canonical JSON string. /// Object keys are recursively sorted using Ordinal comparison. /// /// The type to serialize. /// The object to serialize. /// Canonical JSON string. public static string Serialize(T obj) { var bytes = Canonicalize(obj); return Encoding.UTF8.GetString(bytes); } /// /// Serializes an object to a canonical JSON string using custom serializer options. /// Object keys are recursively sorted using Ordinal comparison. /// /// The type to serialize. /// The object to serialize. /// JSON serializer options to use for initial serialization. /// Canonical JSON string. public static string Serialize(T obj, JsonSerializerOptions options) { var bytes = Canonicalize(obj, options); return Encoding.UTF8.GetString(bytes); } }