Skip to content

Commit

Permalink
fix: csharp newtonsoft serialization (#2083)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Aug 13, 2024
1 parent f40c346 commit faf4673
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ public partial class Root
get { return email; }
set { this.email = value; }
}
public string Serialize()
{
return JsonConvert.SerializeObject(this);
}
public static Root Deserialize(string json)
{
return JsonConvert.DeserializeObject<Root>(json);
}
}
public class RootConverter : JsonConverter<Root>
Expand Down
5 changes: 3 additions & 2 deletions src/generators/csharp/CSharpGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ export class CSharpGenerator extends AbstractGenerator<
? ''
: `${outputModel.dependencies.join('\n')}\n\n`;

const outputContent = `namespace ${completeModelOptionsToUse.namespace}
const outputContent = `${outputDependencies}
namespace ${completeModelOptionsToUse.namespace}
{
${FormatHelpers.indent(
outputDependencies + outputModel.result,
outputModel.result,
optionsToUse.indentation?.size,
optionsToUse.indentation?.type
)}
Expand Down
13 changes: 12 additions & 1 deletion src/generators/csharp/presets/NewtonsoftSerializerPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function renderDeserialize({
prop.property instanceof ConstrainedReferenceModel &&
prop.property.ref instanceof ConstrainedEnumModel
) {
toValue = `${prop.property.name}Extensions.To${prop.property.name}(jo["${prop.unconstrainedPropertyName}"].ToString())`;
toValue = `${prop.property.name}Extensions.To${prop.property.name}(jo["${prop.unconstrainedPropertyName}"].ToString()) ?? 0`;
}
if (prop.property.options.const) {
return undefined;
Expand Down Expand Up @@ -154,6 +154,17 @@ function renderDeserialize({
export const CSHARP_NEWTONSOFT_SERIALIZER_PRESET: CSharpPreset<CSharpOptions> =
{
class: {
additionalContent: ({ content, model, renderer }) => {
return renderer.indent(`${content}
public string Serialize()
{
return JsonConvert.SerializeObject(this);
}
public static ${model.name} Deserialize(string json)
{
return JsonConvert.DeserializeObject<${model.name}>(json);
}`);
},
self: ({ renderer, content, model }) => {
renderer.dependencyManager.addDependency('using Newtonsoft.Json;');
renderer.dependencyManager.addDependency('using Newtonsoft.Json.Linq;');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,11 @@ public static class StatesExtensions
`;
exports[`CSharpGenerator should render models and their dependencies 1`] = `
"namespace Test.Namespace
{
using System.Collections.Generic;
"using System.Collections.Generic;
namespace Test.Namespace
{
public partial class Address
{
private string streetName;
Expand Down Expand Up @@ -300,10 +301,11 @@ exports[`CSharpGenerator should render models and their dependencies 1`] = `
`;
exports[`CSharpGenerator should render models and their dependencies 2`] = `
"namespace Test.Namespace
{
using System.Collections.Generic;
"using System.Collections.Generic;
namespace Test.Namespace
{
public partial class OtherModel
{
private string? streetName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public partial class Test
get { return additionalProperties; }
set { this.additionalProperties = value; }
}
public string Serialize()
{
return JsonConvert.SerializeObject(this);
}
public static Test Deserialize(string json)
{
return JsonConvert.DeserializeObject<Test>(json);
}
}
public class TestConverter : JsonConverter<Test>
Expand All @@ -61,7 +70,7 @@ if(jo[\\"numberProp\\"] != null) {
value.NumberProp = jo[\\"numberProp\\"].ToObject<double?>(serializer);
}
if(jo[\\"enumProp\\"] != null) {
value.EnumProp = EnumTestExtensions.ToEnumTest(jo[\\"enumProp\\"].ToString());
value.EnumProp = EnumTestExtensions.ToEnumTest(jo[\\"enumProp\\"].ToString()) ?? 0;
}
if(jo[\\"objectProp\\"] != null) {
value.ObjectProp = jo[\\"objectProp\\"].ToObject<NestedTest?>(serializer);
Expand Down Expand Up @@ -178,6 +187,15 @@ public partial class NestedTest
get { return additionalProperties; }
set { this.additionalProperties = value; }
}
public string Serialize()
{
return JsonConvert.SerializeObject(this);
}
public static NestedTest Deserialize(string json)
{
return JsonConvert.DeserializeObject<NestedTest>(json);
}
}
public class NestedTestConverter : JsonConverter<NestedTest>
Expand Down

0 comments on commit faf4673

Please sign in to comment.