diff --git a/Directory.Build.props b/Directory.Build.props
index b14204e9a..a7d44d531 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -87,7 +87,7 @@
- 13
+ 12
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/global.json b/global.json
index c6fbdb46a..3f809cbf6 100644
--- a/global.json
+++ b/global.json
@@ -1,6 +1,6 @@
{
"sdk": {
- "version": "9.0.100",
+ "version": "8.0.100",
"rollForward": "feature"
}
}
diff --git a/test/DocumentFormat.OpenXml.Packaging.Tests/PartConstraintRuleTests.cs b/test/DocumentFormat.OpenXml.Packaging.Tests/PartConstraintRuleTests.cs
index b69c4359b..5743d95c0 100644
--- a/test/DocumentFormat.OpenXml.Packaging.Tests/PartConstraintRuleTests.cs
+++ b/test/DocumentFormat.OpenXml.Packaging.Tests/PartConstraintRuleTests.cs
@@ -4,14 +4,14 @@
using DocumentFormat.OpenXml.Features;
using DocumentFormat.OpenXml.Framework;
using DocumentFormat.OpenXml.Packaging;
-using Newtonsoft.Json;
-using Newtonsoft.Json.Converters;
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
@@ -157,7 +157,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..6b5eb68d3 100644
--- a/test/DocumentFormat.OpenXml.Packaging.Tests/ParticleTests.cs
+++ b/test/DocumentFormat.OpenXml.Packaging.Tests/ParticleTests.cs
@@ -3,15 +3,14 @@
using DocumentFormat.OpenXml.Framework;
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
@@ -207,21 +206,19 @@ 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.WhenWritingNull,
+ PropertyNamingPolicy = null, // Use Pascal case like Newtonsoft
+ Converters =
{
- new StringEnumConverter(),
+ new JsonStringEnumConverter(),
new TypeNameConverter(),
new QNameConverter(),
- },
- 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,10 +228,17 @@ private void AssertEqual(Dictionary>
{
fs.SetLength(0);
+ var orderedData = constraints.OrderBy(t => t.Key.FullName, StringComparer.Ordinal);
+ var json = JsonSerializer.Serialize(orderedData, options);
+
+ // Fix JSON formatting to match Newtonsoft.Json indentation
+ json = json.Replace(" {", " {")
+ .Replace(" \"", " \"")
+ .Replace(" ", " ");
+
using (var textWriter = new StreamWriter(fs))
- using (var writer = new JsonTextWriter(textWriter) { Indentation = 1 })
{
- serializer.Serialize(writer, constraints.OrderBy(t => t.Key.FullName, StringComparer.Ordinal));
+ textWriter.Write(json);
}
}
@@ -250,43 +254,7 @@ private void AssertEqual(Dictionary>
}
}
- 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();
- }
- }
private class VersionCollection : IEnumerable>
{
@@ -324,22 +292,30 @@ 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 WriteJson(JsonWriter writer, OpenXmlQualifiedName value, JsonSerializer serializer)
- => writer.WriteValue(value.ToString());
+ public override void Write(Utf8JsonWriter writer, OpenXmlQualifiedName value, JsonSerializerOptions options)
+ {
+ writer.WriteStringValue(value.ToString());
+ }
}
}
}