Skip to content

Commit

Permalink
fix: csharp json serialization (#2081)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Aug 13, 2024
1 parent 55ea213 commit 6288bc6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Root>(json, deserializeOptions);
}
}
internal class RootConverter : JsonConverter<Root>
Expand Down
19 changes: 18 additions & 1 deletion src/generators/csharp/presets/JsonSerializerPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CSharpOptions> = {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Test>(json, deserializeOptions);
}
}
internal class TestConverter : JsonConverter<Test>
Expand Down Expand Up @@ -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<NestedTest>(json, deserializeOptions);
}
}
internal class NestedTestConverter : JsonConverter<NestedTest>
Expand Down

0 comments on commit 6288bc6

Please sign in to comment.