diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/CSharpJsonSerializerGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/CSharpJsonSerializerGeneratorTests.cs new file mode 100644 index 000000000..75c24f36b --- /dev/null +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/CSharpJsonSerializerGeneratorTests.cs @@ -0,0 +1,85 @@ +using System; +using Xunit; + +namespace NJsonSchema.CodeGeneration.CSharp.Tests +{ + public class CSharpJsonSerializerGeneratorTests + { + [Fact] + public void When_using_SytemTextJson_with_JsonConverters_GenerateJsonSerializerParameterCode_generates_correctly() + { + //// Arrange + var additionalJsonConverters = new string[] { "AdditionalConverter1", "AdditionalConverter2" }; + var settings = new CSharpGeneratorSettings + { + JsonLibrary = CSharpJsonLibrary.SystemTextJson, + JsonConverters = new string[] { "CustomConverter1", "CustomConverter2" } + }; + + //// Act + var output = CSharpJsonSerializerGenerator.GenerateJsonSerializerParameterCode(settings, additionalJsonConverters); + Console.WriteLine(output); + + //// Assert + Assert.Equal(", System.Text.Json.JsonSerializerOptions(); var converters = new System.Text.Json.Serialization.JsonConverter[] { new CustomConverter1(), new CustomConverter2(), new AdditionalConverter1(), new AdditionalConverter2() }", output); + } + + [Fact] + public void When_using_NewtonsoftJson_with_JsonConverters_GenerateJsonSerializerParameterCode_generates_correctly() + { + //// Arrange + var additionalJsonConverters = new string[] { "AdditionalConverter1", "AdditionalConverter2" }; + var settings = new CSharpGeneratorSettings + { + JsonLibrary = CSharpJsonLibrary.NewtonsoftJson, + JsonConverters = new string[] { "CustomConverter1", "CustomConverter2" } + }; + + //// Act + var output = CSharpJsonSerializerGenerator.GenerateJsonSerializerParameterCode(settings, additionalJsonConverters); + Console.WriteLine(output); + + //// Assert + Assert.Equal(", new Newtonsoft.Json.JsonConverter[] { new CustomConverter1(), new CustomConverter2(), new AdditionalConverter1(), new AdditionalConverter2() }", output); + } + + [Fact] + public void When_using_SytemTextJson_with_JsonSerializerSettingsOrOptionsTransformationMethod_GenerateJsonSerializerParameterCode_generates_correctly() + { + //// Arrange + var settings = new CSharpGeneratorSettings + { + JsonLibrary = CSharpJsonLibrary.SystemTextJson, + JsonSerializerSettingsTransformationMethod = "TestJsonSerializerSettingsTransformationMethod" + }; + + //// Act + var output = CSharpJsonSerializerGenerator.GenerateJsonSerializerParameterCode(settings, null); + Console.WriteLine(output); + + //// Assert + Assert.Equal(", TestJsonSerializerSettingsTransformationMethod(System.Text.Json.JsonSerializerOptions())", output); + } + + [Fact] + public void When_using_NewtonsoftJson_with_HandleReferences_and_JsonConverters_and_JsonSerializerSettingsTransformationMethod_GenerateJsonSerializerParameterCode_generates_correctly() + { + //// Arrange + var additionalJsonConverters = new string[] { "AdditionalConverter1", "AdditionalConverter2" }; + var settings = new CSharpGeneratorSettings + { + JsonLibrary = CSharpJsonLibrary.NewtonsoftJson, + HandleReferences = true, + JsonConverters = new string[] { "CustomConverter1", "CustomConverter2" }, + JsonSerializerSettingsTransformationMethod = "TestJsonSerializerSettingsTransformationMethod", + }; + + //// Act + var output = CSharpJsonSerializerGenerator.GenerateJsonSerializerParameterCode(settings, additionalJsonConverters); + Console.WriteLine(output); + + //// Assert + Assert.Equal(", TestJsonSerializerSettingsTransformationMethod(new Newtonsoft.Json.JsonSerializerSettings { PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All, Converters = new Newtonsoft.Json.JsonConverter[] { new CustomConverter1(), new CustomConverter2(), new AdditionalConverter1(), new AdditionalConverter2() } })", output); + } + } +} diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs index 070e80c59..4646585a6 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs @@ -57,7 +57,7 @@ public CSharpGeneratorSettings() /// Gets or sets the .NET namespace of the generated types (default: MyNamespace). public string Namespace { get; set; } - /// Gets or sets a value indicating whether a required property must be defined in JSON + /// Gets or sets a value indicating whether a required property must be defined in JSON /// (sets Required.Always when the property is required) (default: true). public bool RequiredPropertiesMustBeDefined { get; set; } @@ -121,7 +121,7 @@ public CSharpGeneratorSettings() /// Gets or sets a value indicating whether to use preserve references handling (All) in the JSON serializer (default: false). public bool HandleReferences { get; set; } - /// Gets or sets the name of a static method which is called to transform the JsonSerializerSettings used in the generated ToJson()/FromJson() methods (default: null). + /// Gets or sets the name of a static method which is called to transform the JsonSerializerSettings (for Newtonsoft.Json) or the JsonSerializerOptions (for System.Text.Json) used in the generated ToJson()/FromJson() methods (default: null). public string JsonSerializerSettingsTransformationMethod { get; set; } /// Gets or sets a value indicating whether to render ToJson() and FromJson() methods (default: true). diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpJsonSerializerGenerator.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpJsonSerializerGenerator.cs index 25b0399fe..b4baebd65 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpJsonSerializerGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpJsonSerializerGenerator.cs @@ -23,47 +23,78 @@ public static string GenerateJsonSerializerParameterCode(CSharpGeneratorSettings var jsonConverters = (settings.JsonConverters ?? new string[0]).Concat(additionalJsonConverters ?? new string[0]).ToList(); var hasJsonConverters = jsonConverters.Any(); - var useSettingsTransformationMethod = !string.IsNullOrEmpty(settings.JsonSerializerSettingsTransformationMethod); - return settings.JsonLibrary == CSharpJsonLibrary.SystemTextJson ? - string.Empty : // TODO(system.text.json): What to do here? - GenerateForNewtonsoftJson(settings, jsonConverters, hasJsonConverters, useSettingsTransformationMethod); + return GenerateForJsonLibrary(settings, jsonConverters, hasJsonConverters); } - private static string GenerateForNewtonsoftJson(CSharpGeneratorSettings settings, List jsonConverters, bool hasJsonConverters, bool useSettingsTransformationMethod) + private static string GenerateForJsonLibrary(CSharpGeneratorSettings settings, List jsonConverters, bool hasJsonConverters) { - if (settings.HandleReferences || useSettingsTransformationMethod) - { - // TODO(system.text.json): Also support System.Text.Json - return ", " + - (useSettingsTransformationMethod ? settings.JsonSerializerSettingsTransformationMethod + "(" : string.Empty) + - "new Newtonsoft.Json.JsonSerializerSettings { " + - (settings.HandleReferences - ? "PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All" - : string.Empty) + - (hasJsonConverters - ? (settings.HandleReferences ? ", " : string.Empty) + "Converters = " + GenerateConverters(jsonConverters) - : string.Empty) + - " }" + - (useSettingsTransformationMethod ? ")" : string.Empty); - } - else + var useSettingsTransformationMethod = !string.IsNullOrEmpty(settings.JsonSerializerSettingsTransformationMethod); + switch (settings.JsonLibrary) { - if (hasJsonConverters) - { - return ", " + GenerateConverters(jsonConverters); - } - else - { + case CSharpJsonLibrary.NewtonsoftJson: + if (settings.HandleReferences || useSettingsTransformationMethod) + { + return ", " + + (useSettingsTransformationMethod ? settings.JsonSerializerSettingsTransformationMethod + "(" : string.Empty) + + "new Newtonsoft.Json.JsonSerializerSettings { " + + (settings.HandleReferences + ? "PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All" + : string.Empty) + + (hasJsonConverters + ? (settings.HandleReferences ? ", " : string.Empty) + "Converters = " + GenerateConverters(jsonConverters, settings.JsonLibrary) + : string.Empty) + + " }" + + (useSettingsTransformationMethod ? ")" : string.Empty); + } + else + { + if (hasJsonConverters) + { + return ", " + GenerateConverters(jsonConverters, settings.JsonLibrary); + } + else + { + return string.Empty; + } + } + + case CSharpJsonLibrary.SystemTextJson: + // TODO: add more conditions? + if (useSettingsTransformationMethod || hasJsonConverters) + { + return ", " + + (useSettingsTransformationMethod ? settings.JsonSerializerSettingsTransformationMethod + "(" : string.Empty) + + "System.Text.Json.JsonSerializerOptions()" + + (useSettingsTransformationMethod ? ")" : string.Empty) + + (hasJsonConverters + ? "; var converters = " + GenerateConverters(jsonConverters, settings.JsonLibrary) + : string.Empty); + } + else + { + return string.Empty; + } + + default: // TODO: possibly add more json converters return string.Empty; - } } } - private static string GenerateConverters(List jsonConverters) + private static string GenerateConverters(List jsonConverters, CSharpJsonLibrary jsonLibrary) { if (jsonConverters.Any()) { - return "new Newtonsoft.Json.JsonConverter[] { " + string.Join(", ", jsonConverters.Select(c => "new " + c + "()")) + " }"; + switch (jsonLibrary) + { + case CSharpJsonLibrary.NewtonsoftJson: + return "new Newtonsoft.Json.JsonConverter[] { " + string.Join(", ", jsonConverters.Select(c => "new " + c + "()")) + " }"; + + case CSharpJsonLibrary.SystemTextJson: + return "new System.Text.Json.Serialization.JsonConverter[] { " + string.Join(", ", jsonConverters.Select(c => "new " + c + "()")) + " }"; + + default: // TODO: possibly add more json converters + return string.Empty; + } } else {