From 6288bc6ca36325e265aa26c93de2c02be4c115c2 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 13 Aug 2024 10:30:18 +0200 Subject: [PATCH] fix: csharp json serialization (#2081) --- .../__snapshots__/index.spec.ts.snap | 16 ++++++++++ .../csharp/presets/JsonSerializerPreset.ts | 19 ++++++++++- .../JsonSerializerPreset.spec.ts.snap | 32 +++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/examples/csharp-generate-json-serializer/__snapshots__/index.spec.ts.snap b/examples/csharp-generate-json-serializer/__snapshots__/index.spec.ts.snap index 6c6e5716ea..ff01c4943d 100644 --- a/examples/csharp-generate-json-serializer/__snapshots__/index.spec.ts.snap +++ b/examples/csharp-generate-json-serializer/__snapshots__/index.spec.ts.snap @@ -12,6 +12,22 @@ public partial class Root get { return email; } set { this.email = value; } } + + + public string Serialize() + { + return this.Serialize(null); + } + public string Serialize(JsonSerializerOptions options = null) + { + return JsonSerializer.Serialize(this, options); + } + public static Root Deserialize(string json) + { + var deserializeOptions = new JsonSerializerOptions(); + deserializeOptions.Converters.Add(new RootConverter()); + return JsonSerializer.Deserialize(json, deserializeOptions); + } } internal class RootConverter : JsonConverter diff --git a/src/generators/csharp/presets/JsonSerializerPreset.ts b/src/generators/csharp/presets/JsonSerializerPreset.ts index 845d1eaa5b..d160775e0c 100644 --- a/src/generators/csharp/presets/JsonSerializerPreset.ts +++ b/src/generators/csharp/presets/JsonSerializerPreset.ts @@ -204,12 +204,29 @@ ${renderer.indent(deserializeProperties, 4)} } /** - * Preset which adds `serialize` and `deserialize` functions to class. + * Preset which adds `Serialize` and `Deserialize` functions to the class. * * @implements {CSharpPreset} */ export const CSHARP_JSON_SERIALIZER_PRESET: CSharpPreset = { class: { + additionalContent({ renderer, content, model }) { + const supportFunctions = `public string Serialize() +{ + return this.Serialize(null); +} +public string Serialize(JsonSerializerOptions options = null) +{ + return JsonSerializer.Serialize(this, options); +} +public static ${model.type} Deserialize(string json) +{ + var deserializeOptions = new JsonSerializerOptions(); + deserializeOptions.Converters.Add(new ${model.name}Converter()); + return JsonSerializer.Deserialize<${model.type}>(json, deserializeOptions); +}`; + return `${content}\n${renderer.indent(supportFunctions)}`; + }, self({ renderer, model, content }) { renderer.dependencyManager.addDependency('using System.Text.Json;'); renderer.dependencyManager.addDependency( diff --git a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap index e9f50d637c..454d2c0e82 100644 --- a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap +++ b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap @@ -45,6 +45,22 @@ public partial class Test get { return additionalProperties; } set { this.additionalProperties = value; } } + + + public string Serialize() + { + return this.Serialize(null); + } + public string Serialize(JsonSerializerOptions options = null) + { + return JsonSerializer.Serialize(this, options); + } + public static Test Deserialize(string json) + { + var deserializeOptions = new JsonSerializerOptions(); + deserializeOptions.Converters.Add(new TestConverter()); + return JsonSerializer.Deserialize(json, deserializeOptions); + } } internal class TestConverter : JsonConverter @@ -223,6 +239,22 @@ public partial class NestedTest get { return additionalProperties; } set { this.additionalProperties = value; } } + + + public string Serialize() + { + return this.Serialize(null); + } + public string Serialize(JsonSerializerOptions options = null) + { + return JsonSerializer.Serialize(this, options); + } + public static NestedTest Deserialize(string json) + { + var deserializeOptions = new JsonSerializerOptions(); + deserializeOptions.Converters.Add(new NestedTestConverter()); + return JsonSerializer.Deserialize(json, deserializeOptions); + } } internal class NestedTestConverter : JsonConverter