Skip to content

Commit

Permalink
add impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jul 27, 2024
1 parent 9f9fc78 commit f5afbee
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/generators/csharp/presets/JsonSerializerPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,28 @@ ${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

0 comments on commit f5afbee

Please sign in to comment.