diff --git a/Directory.Packages.props b/Directory.Packages.props index 0c02da37d..ead377ede 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -15,7 +15,6 @@ - diff --git a/gen/DocumentFormat.OpenXml.Generator.Models/Converters/QualifiedNameConverter.cs b/gen/DocumentFormat.OpenXml.Generator.Models/Converters/QualifiedNameConverter.cs index 16a5febd1..144055fd3 100644 --- a/gen/DocumentFormat.OpenXml.Generator.Models/Converters/QualifiedNameConverter.cs +++ b/gen/DocumentFormat.OpenXml.Generator.Models/Converters/QualifiedNameConverter.cs @@ -2,25 +2,26 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using DocumentFormat.OpenXml.Generator.Models; -using Newtonsoft.Json; +using System; +using System.Text.Json; +using System.Text.Json.Serialization; namespace DocumentFormat.OpenXml.Generator.Converters; internal class QualifiedNameConverter : JsonConverter { - public override QName? ReadJson(JsonReader reader, Type objectType, QName? existingValue, bool hasExistingValue, JsonSerializer serializer) + public override QName? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - if (reader.TokenType != JsonToken.String) + if (reader.TokenType != JsonTokenType.String) { throw new InvalidOperationException("QName must be encoded as a string"); } - var str = serializer.Deserialize(reader) ?? string.Empty; - + var str = reader.GetString() ?? string.Empty; return QName.Parse(str); } - public override void WriteJson(JsonWriter writer, QName? value, JsonSerializer serializer) + public override void Write(Utf8JsonWriter writer, QName value, JsonSerializerOptions options) { throw new NotImplementedException(); } diff --git a/gen/DocumentFormat.OpenXml.Generator.Models/Converters/TypedQNameConverter.cs b/gen/DocumentFormat.OpenXml.Generator.Models/Converters/TypedQNameConverter.cs index acecdcdbc..70456f10b 100644 --- a/gen/DocumentFormat.OpenXml.Generator.Models/Converters/TypedQNameConverter.cs +++ b/gen/DocumentFormat.OpenXml.Generator.Models/Converters/TypedQNameConverter.cs @@ -2,20 +2,22 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using DocumentFormat.OpenXml.Generator.Models; -using Newtonsoft.Json; +using System; +using System.Text.Json; +using System.Text.Json.Serialization; namespace DocumentFormat.OpenXml.Generator.Converters; internal class TypedQNameConverter : JsonConverter { - public override TypedQName? ReadJson(JsonReader reader, Type objectType, TypedQName? existingValue, bool hasExistingValue, JsonSerializer serializer) + public override TypedQName? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - if (reader.TokenType != JsonToken.String) + if (reader.TokenType != JsonTokenType.String) { throw new InvalidOperationException("TypedQName must be encoded as a string"); } - var str = serializer.Deserialize(reader) ?? string.Empty; + var str = reader.GetString() ?? string.Empty; var split = str.Split('/'); if (split.Length != 2) @@ -26,7 +28,7 @@ internal class TypedQNameConverter : JsonConverter return new TypedQName(QName.Parse(split[0]), QName.Parse(split[1])); } - public override void WriteJson(JsonWriter writer, TypedQName? value, JsonSerializer serializer) + public override void Write(Utf8JsonWriter writer, TypedQName value, JsonSerializerOptions options) { throw new NotImplementedException(); } diff --git a/gen/DocumentFormat.OpenXml.Generator.Models/DocumentFormat.OpenXml.Generator.Models.csproj b/gen/DocumentFormat.OpenXml.Generator.Models/DocumentFormat.OpenXml.Generator.Models.csproj index d5ebe540a..6a11c3d57 100644 --- a/gen/DocumentFormat.OpenXml.Generator.Models/DocumentFormat.OpenXml.Generator.Models.csproj +++ b/gen/DocumentFormat.OpenXml.Generator.Models/DocumentFormat.OpenXml.Generator.Models.csproj @@ -9,7 +9,7 @@ DocumentFormat.OpenXml.Generator - + diff --git a/gen/DocumentFormat.OpenXml.Generator.Models/OpenXmlGeneratorDataSource.cs b/gen/DocumentFormat.OpenXml.Generator.Models/OpenXmlGeneratorDataSource.cs index 9ff0ef930..869fa3849 100644 --- a/gen/DocumentFormat.OpenXml.Generator.Models/OpenXmlGeneratorDataSource.cs +++ b/gen/DocumentFormat.OpenXml.Generator.Models/OpenXmlGeneratorDataSource.cs @@ -4,25 +4,25 @@ using DocumentFormat.OpenXml.Generator.Converters; using DocumentFormat.OpenXml.Generator.Models; using DocumentFormat.OpenXml.Generator.Schematron; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; using System.Collections.Immutable; +using System.Text.Json; +using System.Text.Json.Serialization; namespace DocumentFormat.OpenXml.Generator; public record OpenXmlGeneratorDataSource { - private static readonly JsonSerializerSettings _settings = new() + private static readonly JsonSerializerOptions _options = new() { Converters = { - new StringEnumConverter(), + new JsonStringEnumConverter(), new QualifiedNameConverter(), new TypedQNameConverter(), }, }; - public static T? Deserialize(string? content) => content is null ? default : JsonConvert.DeserializeObject(content, _settings); + public static T? Deserialize(string? content) => content is null ? default : JsonSerializer.Deserialize(content, _options); public ImmutableArray KnownNamespaces { get; init; } = ImmutableArray.Create(); diff --git a/gen/DocumentFormat.OpenXml.Generator/SourceGenerator.targets b/gen/DocumentFormat.OpenXml.Generator/SourceGenerator.targets index 048cd0fe8..6bc09548c 100644 --- a/gen/DocumentFormat.OpenXml.Generator/SourceGenerator.targets +++ b/gen/DocumentFormat.OpenXml.Generator/SourceGenerator.targets @@ -7,10 +7,7 @@ - - - - + diff --git a/test/Directory.Build.targets b/test/Directory.Build.targets index f77cf4244..84f6c225c 100644 --- a/test/Directory.Build.targets +++ b/test/Directory.Build.targets @@ -18,7 +18,6 @@ - diff --git a/test/DocumentFormat.OpenXml.Framework.Tests/TestUtility.cs b/test/DocumentFormat.OpenXml.Framework.Tests/TestUtility.cs index 49c147202..c968a1834 100644 --- a/test/DocumentFormat.OpenXml.Framework.Tests/TestUtility.cs +++ b/test/DocumentFormat.OpenXml.Framework.Tests/TestUtility.cs @@ -4,8 +4,10 @@ using System; using System.IO; using System.Reflection; +using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Serialization; +using Xunit; namespace DocumentFormat.OpenXml.Framework.Tests { @@ -13,6 +15,7 @@ internal static class TestUtility { private static readonly JsonSerializerOptions _options = new() { + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, Converters = { new OpenXmlNamespaceConverter(), @@ -22,6 +25,17 @@ internal static class TestUtility WriteIndented = true, }; + public static void ValidateJsonFileContentsAreEqual(Stream stream1, Stream stream2) + { + using var reader1 = new StreamReader(stream1); + using var reader2 = new StreamReader(stream2); + + var expected = reader1.ReadToEnd().Replace("\r\n", "\n"); + var actual = reader2.ReadToEnd().Replace("\r\n", "\n"); + + Assert.Equal(expected, actual); + } + #nullable enable public static T? Deserialize(string name) diff --git a/test/DocumentFormat.OpenXml.Packaging.Tests/ITestOutputHelperExtenstions.cs b/test/DocumentFormat.OpenXml.Packaging.Tests/ITestOutputHelperExtenstions.cs index 920e5df48..1a3c8c77b 100644 --- a/test/DocumentFormat.OpenXml.Packaging.Tests/ITestOutputHelperExtenstions.cs +++ b/test/DocumentFormat.OpenXml.Packaging.Tests/ITestOutputHelperExtenstions.cs @@ -1,9 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using DocumentFormat.OpenXml.Framework; using DocumentFormat.OpenXml.Framework.Tests; -using System; using System.IO; using Xunit; @@ -11,13 +9,15 @@ namespace DocumentFormat.OpenXml.Tests { internal static class ITestOutputHelperExtenstions { - public static void WriteObjectToTempFile(this ITestOutputHelper output, string name, T obj) + public static string WriteObjectToTempFile(this ITestOutputHelper output, string name, T obj) { var tmp = Path.GetTempFileName(); output.WriteLine($"Wrote {name} to temp path {tmp}"); File.WriteAllText(tmp, TestUtility.Serialize(obj)); + + return tmp; } } } diff --git a/test/DocumentFormat.OpenXml.Packaging.Tests/PartConstraintRuleTests.cs b/test/DocumentFormat.OpenXml.Packaging.Tests/PartConstraintRuleTests.cs index b69c4359b..0e6532c08 100644 --- a/test/DocumentFormat.OpenXml.Packaging.Tests/PartConstraintRuleTests.cs +++ b/test/DocumentFormat.OpenXml.Packaging.Tests/PartConstraintRuleTests.cs @@ -3,15 +3,17 @@ using DocumentFormat.OpenXml.Features; using DocumentFormat.OpenXml.Framework; +using DocumentFormat.OpenXml.Framework.Tests; using DocumentFormat.OpenXml.Packaging; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; +using DocumentFormat.OpenXml.Packaging.Tests; using NSubstitute; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization; using Xunit; namespace DocumentFormat.OpenXml.Tests @@ -86,10 +88,9 @@ public void ValidatePart(Type partType) } Assert.NotNull(expectedConstraints.Parts); -#if DEBUG + _output.WriteObjectToTempFile("expected constraints", expectedConstraints.Parts.OrderBy(p => p.RelationshipType)); _output.WriteObjectToTempFile("actual constraints", constraints.Rules.OrderBy(p => p.RelationshipType).Select(p => new PartConstraintRule2(p))); -#endif Assert.Equal( expectedConstraints.Parts.OrderBy(p => p.RelationshipType), @@ -119,7 +120,13 @@ public void ExportData() }) .OrderBy(d => d.Name, StringComparer.Ordinal); - _output.WriteObjectToTempFile("typed parts", result); + var output = _output.WriteObjectToTempFile("typed parts", result); + + using (var expectedStream = typeof(ParticleTests).GetTypeInfo().Assembly.GetManifestResourceStream("DocumentFormat.OpenXml.Packaging.Tests.data.PartConstraintData.json")) + using (var actualStream = new FileStream(output, FileMode.Open, FileAccess.Read)) + { + TestUtility.ValidateJsonFileContentsAreEqual(expectedStream!, actualStream); + } } public static IEnumerable GetOpenXmlParts() => GetParts().Select(p => new[] { p }); @@ -134,9 +141,7 @@ private static IEnumerable GetParts() => typeof(SpreadsheetDocument) private static OpenXmlPart InitializePart(Type type) { -#nullable disable - var part = (OpenXmlPart)Activator.CreateInstance(type, true); -#nullable enable + var part = (OpenXmlPart)Activator.CreateInstance(type, true)!; var appType = Substitute.For(); appType.Type.Returns(ApplicationType.None); @@ -148,7 +153,7 @@ private static OpenXmlPart InitializePart(Type type) private static ConstraintData GetConstraintData(OpenXmlPart part) => _cachedConstraintData.Value[part.GetType().FullName!]; - private static Lazy> _cachedConstraintData = new Lazy>(() => + private static readonly Lazy> _cachedConstraintData = new(() => { var names = typeof(PartConstraintRuleTests).GetTypeInfo().Assembly.GetManifestResourceNames(); @@ -157,7 +162,15 @@ private static OpenXmlPart InitializePart(Type type) using (var reader = new StreamReader(stream!)) { #nullable disable - return JsonConvert.DeserializeObject(reader.ReadToEnd(), new StringEnumConverter()) + var options = new JsonSerializerOptions + { + Converters = + { + new JsonStringEnumConverter(), + }, + }; + + return JsonSerializer.Deserialize(reader.ReadToEnd(), options) .ToDictionary(t => t.Name, StringComparer.Ordinal); #nullable enable } diff --git a/test/DocumentFormat.OpenXml.Packaging.Tests/ParticleTests.cs b/test/DocumentFormat.OpenXml.Packaging.Tests/ParticleTests.cs index c04911d81..427c1f808 100644 --- a/test/DocumentFormat.OpenXml.Packaging.Tests/ParticleTests.cs +++ b/test/DocumentFormat.OpenXml.Packaging.Tests/ParticleTests.cs @@ -2,16 +2,16 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using DocumentFormat.OpenXml.Framework; +using DocumentFormat.OpenXml.Framework.Tests; using DocumentFormat.OpenXml.Validation.Schema; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using Newtonsoft.Json.Serialization; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization; using Xunit; namespace DocumentFormat.OpenXml.Packaging.Tests @@ -178,9 +178,7 @@ public void ValidateExpectedParticles() if (constructor is not null) { -#nullable disable - var element = (OpenXmlElement)Activator.CreateInstance(type); -#nullable enable + var element = (OpenXmlElement)Activator.CreateInstance(type)!; if (version.AtLeast(element!.InitialVersion)) { @@ -207,21 +205,20 @@ public void ValidateExpectedParticles() private void AssertEqual(Dictionary> constraints) { - var settings = new JsonSerializerSettings + var options = new JsonSerializerOptions { - Formatting = Formatting.Indented, - Converters = new JsonConverter[] + WriteIndented = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + PropertyNamingPolicy = null, + Converters = { - new StringEnumConverter(), + new JsonStringEnumConverter(), new TypeNameConverter(), new QNameConverter(), + new ParticleConstraintConverter(), }, - ContractResolver = new OccursDefaultResolver(), - NullValueHandling = NullValueHandling.Ignore, - DefaultValueHandling = DefaultValueHandling.Ignore, }; - var serializer = JsonSerializer.Create(settings); var tmp = Path.GetTempFileName(); _output.WriteLine($"Writing output to {tmp}"); @@ -231,60 +228,22 @@ private void AssertEqual(Dictionary> { fs.SetLength(0); - using (var textWriter = new StreamWriter(fs)) - using (var writer = new JsonTextWriter(textWriter) { Indentation = 1 }) + var orderedData = constraints.OrderBy(t => t.Key.FullName, StringComparer.Ordinal); + + using var writer = new Utf8JsonWriter(fs, new JsonWriterOptions { - serializer.Serialize(writer, constraints.OrderBy(t => t.Key.FullName, StringComparer.Ordinal)); - } + Indented = true, + IndentSize = 1, + }); + + JsonSerializer.Serialize(writer, orderedData, options); } using (var expectedStream = typeof(ParticleTests).GetTypeInfo().Assembly.GetManifestResourceStream("DocumentFormat.OpenXml.Packaging.Tests.data.Particles.json")) - using (var expectedStreamReader = new StreamReader(expectedStream!)) using (var actualStream = File.OpenRead(tmp)) - using (var actualStreamReader = new StreamReader(actualStream)) { - var expected = expectedStreamReader.ReadToEnd().Replace("\r\n", "\n"); - var actual = actualStreamReader.ReadToEnd().Replace("\r\n", "\n"); - - Assert.Equal(expected, actual); - } - } - - private class OccursDefaultResolver : DefaultContractResolver - { - protected override JsonContract CreateContract(Type objectType) - { - // CompositeParticle implements IEnumerable to enable collection initializers, but we want it to serialize as if it were just the object - if (objectType == typeof(CompositeParticle)) - { - return CreateObjectContract(objectType); - } - - return base.CreateContract(objectType); - } - - protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) - { - var properties = base.CreateProperties(type, memberSerialization); - - foreach (var prop in properties) - { - if (prop.PropertyName == nameof(ParticleConstraint.MinOccurs) || prop.PropertyName == nameof(ParticleConstraint.MaxOccurs)) - { - prop.DefaultValue = 1; - } - else if (prop.PropertyName == nameof(ParticleConstraint.Version) || prop.PropertyName == nameof(CompositeParticle.RequireFilter)) - { - prop.Ignored = true; - } - else if (prop.PropertyName == nameof(CompositeParticle.ChildrenParticles)) - { - prop.PropertyType = typeof(IEnumerable); - prop.ShouldSerialize = c => ((CompositeParticle)c).ChildrenParticles.Any(); - } - } - - return properties.OrderBy(p => p.PropertyName).ToList(); + Assert.NotNull(expectedStream); + TestUtility.ValidateJsonFileContentsAreEqual(expectedStream, actualStream); } } @@ -324,22 +283,123 @@ public void Add(FileFormatVersions key, T value) IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } - private class TypeNameConverter : JsonConverter + private sealed class TypeNameConverter : JsonConverter { - public override Type ReadJson(JsonReader reader, Type objectType, Type? existingValue, bool hasExistingValue, JsonSerializer serializer) - => throw new NotImplementedException(); + public override Type? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + throw new NotImplementedException(); + } - public override void WriteJson(JsonWriter writer, Type? value, JsonSerializer serializer) - => serializer.Serialize(writer, value!.FullName); + public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.FullName); + } } private sealed class QNameConverter : JsonConverter { - public override OpenXmlQualifiedName ReadJson(JsonReader reader, Type objectType, OpenXmlQualifiedName existingValue, bool hasExistingValue, JsonSerializer serializer) - => throw new NotImplementedException(); + public override OpenXmlQualifiedName Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + throw new NotImplementedException(); + } + + public override void Write(Utf8JsonWriter writer, OpenXmlQualifiedName value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString()); + } + } + + private sealed class ParticleConstraintConverter : JsonConverter + { + public override ParticleConstraint? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + throw new NotImplementedException(); + } - public override void WriteJson(JsonWriter writer, OpenXmlQualifiedName value, JsonSerializer serializer) - => writer.WriteValue(value.ToString()); + public override void Write(Utf8JsonWriter writer, ParticleConstraint value, JsonSerializerOptions options) + { + writer.WriteStartObject(); + + // Get all public properties from the type + var type = value.GetType(); + var allProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanRead && p.GetIndexParameters().Length == 0); + + var propertiesToWrite = new List<(string Name, object? Value, bool ShouldWrite)>(); + + foreach (var prop in allProperties) + { + var propName = prop.Name; + var propValue = prop.GetValue(value); + + // Ignore certain properties + if (propName == nameof(ParticleConstraint.Version) || + propName == "RequireFilter" || + propName == "ParticleValidator" || + propName == "UnboundedMaxOccurs" || + propName == "CanOccursMoreThanOne") + { + continue; + } + + // Handle MinOccurs and MaxOccurs - only include if not default value of 1 + if (propName == nameof(ParticleConstraint.MinOccurs) || propName == nameof(ParticleConstraint.MaxOccurs)) + { + if (propValue is int intValue && intValue != 1) + { + propertiesToWrite.Add((propName, propValue, true)); + } + + continue; + } + + // Handle ChildrenParticles - only include if not empty + if (propName == "ChildrenParticles") + { + if (value is CompositeParticle composite && composite.ChildrenParticles.Any()) + { + propertiesToWrite.Add((propName, composite.ChildrenParticles, true)); + } + + continue; + } + + // Handle NamespaceValue - only include if not the default (Any = 0) + if (propName == "NamespaceValue") + { + if (propValue != null && Convert.ToInt32(propValue) != 0) + { + propertiesToWrite.Add((propName, propValue, true)); + } + + continue; + } + + // For ElementParticle, don't include ParticleType + if (value is ElementParticle && propName == nameof(ParticleConstraint.ParticleType)) + { + continue; + } + + // Include all other properties + if (propValue != null) + { + propertiesToWrite.Add((propName, propValue, true)); + } + } + + // Sort properties alphabetically and write them + foreach (var (name, propValue, shouldWrite) in propertiesToWrite.OrderBy(p => p.Name)) + { + if (shouldWrite && propValue != null) + { + writer.WritePropertyName(name); + JsonSerializer.Serialize(writer, propValue, propValue.GetType(), options); + } + } + + writer.WriteEndObject(); + } } } } diff --git a/test/DocumentFormat.OpenXml.Packaging.Tests/data/PartConstraintData.json b/test/DocumentFormat.OpenXml.Packaging.Tests/data/PartConstraintData.json index 4997cc6fd..c64282e3e 100644 --- a/test/DocumentFormat.OpenXml.Packaging.Tests/data/PartConstraintData.json +++ b/test/DocumentFormat.OpenXml.Packaging.Tests/data/PartConstraintData.json @@ -811,6 +811,16 @@ "TargetPath": "externalReferences", "Parts": [] }, + { + "Name": "DocumentFormat.OpenXml.Packaging.FeaturePropertyBagsPart", + "ContentType": "application/vnd.ms-excel.featurepropertybag+xml", + "IsContentTypeFixed": true, + "RelationshipType": "http://schemas.microsoft.com/office/2022/11/relationships/FeaturePropertyBag", + "TargetFileExtension": ".xml", + "TargetName": "featurePropertyBag", + "TargetPath": "featurePropertyBag", + "Parts": [] + }, { "Name": "DocumentFormat.OpenXml.Packaging.FontPart", "ContentType": null, @@ -4235,15 +4245,5 @@ "TargetName": "sig", "TargetPath": "_xmlsignatures", "Parts": [] - }, - { - "Name": "DocumentFormat.OpenXml.Packaging.FeaturePropertyBagsPart", - "ContentType": "application/vnd.ms-excel.featurepropertybag+xml", - "IsContentTypeFixed": true, - "RelationshipType": "http://schemas.microsoft.com/office/2022/11/relationships/FeaturePropertyBag", - "TargetFileExtension": ".xml", - "TargetName": "featurePropertyBag", - "TargetPath": "featurePropertyBag", - "Parts": [] } ] \ No newline at end of file