Skip to content

Commit

Permalink
feat: add ToBytes() for manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Jan 5, 2025
1 parent 4c1883a commit 282c7fd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Lip.Tests/PackageManifestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,29 @@ public void FromBytes_InvalidVersion_ThrowsArgumentException()

Assert.Throws<ArgumentException>("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);
}
}
8 changes: 8 additions & 0 deletions Lip/PackageManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -274,4 +276,10 @@ public required string Version

return manifest;
}

public byte[] ToBytes()
{
byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(this, s_jsonSerializerOptions);

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64, ubuntu-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64, ubuntu-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64, ubuntu-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (linux-x64, ubuntu-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (osx-arm64, macos-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (osx-arm64, macos-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (osx-arm64, macos-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (osx-arm64, macos-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (osx-x64, macos-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (osx-x64, macos-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm64, ubuntu-22.04)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm64, ubuntu-22.04)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (osx-x64, macos-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (osx-x64, macos-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm64, ubuntu-22.04)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (linux-arm64, ubuntu-22.04)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (win-x64, windows-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (win-x64, windows-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (win-x64, windows-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (win-x64, windows-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (win-arm64, windows-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (win-arm64, windows-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (win-arm64, windows-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 282 in Lip/PackageManifest.cs

View workflow job for this annotation

GitHub Actions / build (win-arm64, windows-latest)

Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.
return bytes;
}
}

0 comments on commit 282c7fd

Please sign in to comment.