diff --git a/Lip.Tests/PackageManifestTests.cs b/Lip.Tests/PackageManifestTests.cs index 4710334..514c93f 100644 --- a/Lip.Tests/PackageManifestTests.cs +++ b/Lip.Tests/PackageManifestTests.cs @@ -399,4 +399,29 @@ public void FromBytes_InvalidVersion_ThrowsArgumentException() Assert.Throws("value", () => PackageManifest.FromBytes(bytes)); } + + [Fact] + public void ToBytes_MinimumInput_Serialized() + { + var manifest = new PackageManifest + { + FormatVersion = 3, + FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d", + Tooth = "", + Version = "1.0.0" + }; + + byte[] bytes = manifest.ToBytes(); + + string json = Encoding.UTF8.GetString(bytes); + + Assert.Equal(""" + { + "format_version": 3, + "format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d", + "tooth": "", + "version": "1.0.0" + } + """, json); + } } diff --git a/Lip/PackageManifest.cs b/Lip/PackageManifest.cs index 5e53d58..eb4a03a 100644 --- a/Lip/PackageManifest.cs +++ b/Lip/PackageManifest.cs @@ -216,6 +216,8 @@ public record VariantType private static readonly JsonSerializerOptions s_jsonSerializerOptions = new() { AllowTrailingCommas = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + IndentSize = 4, ReadCommentHandling = JsonCommentHandling.Skip, WriteIndented = true, }; @@ -274,4 +276,10 @@ public required string Version return manifest; } + + public byte[] ToBytes() + { + byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(this, s_jsonSerializerOptions); + return bytes; + } }