Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: csharp json serialization #2081

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading