Skip to content

Commit

Permalink
Json converter for complex value object uses converters instead of re…
Browse files Browse the repository at this point in the history
…ader/writer

* this is approach is better because it supports custom converters
  • Loading branch information
PawelGerr committed Dec 7, 2023
1 parent 4cc5e0e commit 6c704ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,9 @@ public sealed class ValueObjectJsonConverter : global::System.Text.Json.Serializ
for (var i = 0; i < _assignableInstanceFieldsAndProperties.Count; i++)
{
var memberInfo = _assignableInstanceFieldsAndProperties[i];
var needsConverter = GenerateWriteValue(null, memberInfo);

if (needsConverter)
{
_sb.Append(@"
private readonly global::System.Text.Json.Serialization.JsonConverter<").Append(memberInfo.TypeFullyQualifiedWithNullability).Append("> _").Append(memberInfo.ArgumentName.Raw).Append("Converter;");
}

_sb.Append(@"
private readonly global::System.Text.Json.Serialization.JsonConverter<").Append(memberInfo.TypeFullyQualifiedWithNullability).Append("> _").Append(memberInfo.ArgumentName.Raw).Append(@"Converter;
private readonly string _").Append(memberInfo.ArgumentName.Raw).Append("PropertyName;");
}

Expand All @@ -70,15 +64,9 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o
for (var i = 0; i < _assignableInstanceFieldsAndProperties.Count; i++)
{
var memberInfo = _assignableInstanceFieldsAndProperties[i];
var needsConverter = GenerateWriteValue(null, memberInfo);

if (needsConverter)
{
_sb.Append(@"
this._").Append(memberInfo.ArgumentName.Raw).Append("Converter = (global::System.Text.Json.Serialization.JsonConverter<").Append(memberInfo.TypeFullyQualifiedWithNullability).Append(">)global::Thinktecture.JsonSerializerOptionsExtensions.GetCustomValueObjectMemberConverter(options, typeof(").Append(memberInfo.TypeFullyQualified).Append("));");
}

_sb.Append(@"
this._").Append(memberInfo.ArgumentName.Raw).Append("Converter = (global::System.Text.Json.Serialization.JsonConverter<").Append(memberInfo.TypeFullyQualifiedWithNullability).Append(">)global::Thinktecture.JsonSerializerOptionsExtensions.GetCustomValueObjectMemberConverter(options, typeof(").Append(memberInfo.TypeFullyQualified).Append(@"));
this._").Append(memberInfo.ArgumentName.Raw).Append("PropertyName = namingPolicy?.ConvertName(\"").Append(memberInfo.Name).Append(@""") ?? """).Append(memberInfo.Name).Append(@""";");
}

Expand Down Expand Up @@ -142,11 +130,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o

_sb.Append("(comparer.Equals(propName, this._").Append(memberInfo.ArgumentName.Raw).Append(@"PropertyName))
{
").Append(memberInfo.ArgumentName.Escaped).Append(" = ");

GenerateReadValue(_sb, memberInfo);

_sb.Append(@";
").Append(memberInfo.ArgumentName.Escaped).Append(" = this._").Append(memberInfo.ArgumentName.Raw).Append("Converter.Read(ref reader, typeof(").Append(memberInfo.TypeFullyQualified).Append(@"), options);
}");
}

Expand Down Expand Up @@ -220,11 +204,7 @@ public override void Write(global::System.Text.Json.Utf8JsonWriter writer, ").Ap
_sb.Append("writer.WritePropertyName(this._").Append(memberInfo.ArgumentName.Raw).Append(@"PropertyName);
");

_sb.Append(" ");

GenerateWriteValue(_sb, memberInfo);

_sb.Append(@"
_sb.Append(" this._").Append(memberInfo.ArgumentName.Raw).Append("Converter.Write(writer, ").Append(memberInfo.ArgumentName.Raw).Append(@"PropertyValue, options);
}");
}

Expand Down Expand Up @@ -255,71 +235,4 @@ public override bool CanConvert(global::System.Type typeToConvert)
}
");
}

private static bool GenerateWriteValue(StringBuilder? sb, InstanceMemberInfo memberInfo)
{
var command = memberInfo.SpecialType switch
{
SpecialType.System_Boolean => "WriteBooleanValue",

SpecialType.System_String => "WriteStringValue",
SpecialType.System_DateTime => "WriteStringValue",

_ => null
};

if (command is null)
{
if (memberInfo.TypeFullyQualified == "global::System.Guid")
{
command = "WriteStringValue";
}
else if (memberInfo.TypeFullyQualified == "global::System.DateTimeOffset")
{
command = "WriteStringValue";
}
else
{
sb?.Append("this._").Append(memberInfo.ArgumentName.Raw).Append("Converter.Write(writer, ").Append(memberInfo.ArgumentName.Raw).Append("PropertyValue, options);");

return true;
}
}

sb?.Append("writer.").Append(command).Append("(").Append(memberInfo.ArgumentName.Raw).Append("PropertyValue);");

return false;
}

private static void GenerateReadValue(StringBuilder sb, InstanceMemberInfo memberInfo)
{
var command = memberInfo.SpecialType switch
{
SpecialType.System_Boolean => "GetBoolean",

SpecialType.System_String => "GetString",
SpecialType.System_DateTime => "GetDateTime",

_ => null
};

if (command is null)
{
if (memberInfo.TypeFullyQualified == "global::System.Guid")
{
command = "GetGuid";
}
else if (memberInfo.TypeFullyQualified == "global::System.DateTimeOffset")
{
command = "GetDateTimeOffset";
}
else
{
sb.Append("this._").Append(memberInfo.ArgumentName.Raw).Append("Converter.Read(ref reader, typeof(").Append(memberInfo.TypeFullyQualified).Append("), options)");
return;
}
}

sb.Append("reader.").Append(command).Append("()");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ partial class TestValueObject
{
public sealed class ValueObjectJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::Thinktecture.Tests.TestValueObject>
{
private readonly global::System.Text.Json.Serialization.JsonConverter<string> _referenceFieldConverter;
private readonly string _referenceFieldPropertyName;
private readonly global::System.Text.Json.Serialization.JsonConverter<int> _structPropertyConverter;
private readonly string _structPropertyPropertyName;
Expand All @@ -158,6 +159,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o

var namingPolicy = options.PropertyNamingPolicy;

this._referenceFieldConverter = (global::System.Text.Json.Serialization.JsonConverter<string>)global::Thinktecture.JsonSerializerOptionsExtensions.GetCustomValueObjectMemberConverter(options, typeof(string));
this._referenceFieldPropertyName = namingPolicy?.ConvertName("ReferenceField") ?? "ReferenceField";
this._structPropertyConverter = (global::System.Text.Json.Serialization.JsonConverter<int>)global::Thinktecture.JsonSerializerOptionsExtensions.GetCustomValueObjectMemberConverter(options, typeof(int));
this._structPropertyPropertyName = namingPolicy?.ConvertName("StructProperty") ?? "StructProperty";
Expand Down Expand Up @@ -195,7 +197,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o

if (comparer.Equals(propName, this._referenceFieldPropertyName))
{
referenceField = reader.GetString();
referenceField = this._referenceFieldConverter.Read(ref reader, typeof(string), options);
}
else if (comparer.Equals(propName, this._structPropertyPropertyName))
{
Expand Down Expand Up @@ -236,7 +238,7 @@ public override void Write(global::System.Text.Json.Utf8JsonWriter writer, globa
if(!ignoreNullValues || referenceFieldPropertyValue is not null)
{
writer.WritePropertyName(this._referenceFieldPropertyName);
writer.WriteStringValue(referenceFieldPropertyValue);
this._referenceFieldConverter.Write(writer, referenceFieldPropertyValue, options);
}
var structPropertyPropertyValue = value.StructProperty;

Expand Down Expand Up @@ -315,6 +317,7 @@ partial struct TestValueObject
{
public sealed class ValueObjectJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::Thinktecture.Tests.TestValueObject>
{
private readonly global::System.Text.Json.Serialization.JsonConverter<string> _referenceFieldConverter;
private readonly string _referenceFieldPropertyName;
private readonly global::System.Text.Json.Serialization.JsonConverter<int> _structPropertyConverter;
private readonly string _structPropertyPropertyName;
Expand All @@ -328,6 +331,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o

var namingPolicy = options.PropertyNamingPolicy;

this._referenceFieldConverter = (global::System.Text.Json.Serialization.JsonConverter<string>)global::Thinktecture.JsonSerializerOptionsExtensions.GetCustomValueObjectMemberConverter(options, typeof(string));
this._referenceFieldPropertyName = namingPolicy?.ConvertName("ReferenceField") ?? "ReferenceField";
this._structPropertyConverter = (global::System.Text.Json.Serialization.JsonConverter<int>)global::Thinktecture.JsonSerializerOptionsExtensions.GetCustomValueObjectMemberConverter(options, typeof(int));
this._structPropertyPropertyName = namingPolicy?.ConvertName("StructProperty") ?? "StructProperty";
Expand Down Expand Up @@ -365,7 +369,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o

if (comparer.Equals(propName, this._referenceFieldPropertyName))
{
referenceField = reader.GetString();
referenceField = this._referenceFieldConverter.Read(ref reader, typeof(string), options);
}
else if (comparer.Equals(propName, this._structPropertyPropertyName))
{
Expand Down Expand Up @@ -406,7 +410,7 @@ public override void Write(global::System.Text.Json.Utf8JsonWriter writer, globa
if(!ignoreNullValues || referenceFieldPropertyValue is not null)
{
writer.WritePropertyName(this._referenceFieldPropertyName);
writer.WriteStringValue(referenceFieldPropertyValue);
this._referenceFieldConverter.Write(writer, referenceFieldPropertyValue, options);
}
var structPropertyPropertyValue = value.StructProperty;

Expand Down Expand Up @@ -485,6 +489,7 @@ partial struct TestValueObject
{
public sealed class ValueObjectJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::Thinktecture.Tests.TestValueObject>
{
private readonly global::System.Text.Json.Serialization.JsonConverter<string> _referenceFieldConverter;
private readonly string _referenceFieldPropertyName;
private readonly global::System.Text.Json.Serialization.JsonConverter<int> _structPropertyConverter;
private readonly string _structPropertyPropertyName;
Expand All @@ -498,6 +503,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o

var namingPolicy = options.PropertyNamingPolicy;

this._referenceFieldConverter = (global::System.Text.Json.Serialization.JsonConverter<string>)global::Thinktecture.JsonSerializerOptionsExtensions.GetCustomValueObjectMemberConverter(options, typeof(string));
this._referenceFieldPropertyName = namingPolicy?.ConvertName("ReferenceField") ?? "ReferenceField";
this._structPropertyConverter = (global::System.Text.Json.Serialization.JsonConverter<int>)global::Thinktecture.JsonSerializerOptionsExtensions.GetCustomValueObjectMemberConverter(options, typeof(int));
this._structPropertyPropertyName = namingPolicy?.ConvertName("StructProperty") ?? "StructProperty";
Expand Down Expand Up @@ -535,7 +541,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o

if (comparer.Equals(propName, this._referenceFieldPropertyName))
{
referenceField = reader.GetString();
referenceField = this._referenceFieldConverter.Read(ref reader, typeof(string), options);
}
else if (comparer.Equals(propName, this._structPropertyPropertyName))
{
Expand Down Expand Up @@ -576,7 +582,7 @@ public override void Write(global::System.Text.Json.Utf8JsonWriter writer, globa
if(!ignoreNullValues || referenceFieldPropertyValue is not null)
{
writer.WritePropertyName(this._referenceFieldPropertyName);
writer.WriteStringValue(referenceFieldPropertyValue);
this._referenceFieldConverter.Write(writer, referenceFieldPropertyValue, options);
}
var structPropertyPropertyValue = value.StructProperty;

Expand Down Expand Up @@ -650,6 +656,7 @@ partial struct TestValueObject
{
public sealed class ValueObjectJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::TestValueObject>
{
private readonly global::System.Text.Json.Serialization.JsonConverter<string> _referenceFieldConverter;
private readonly string _referenceFieldPropertyName;
private readonly global::System.Text.Json.Serialization.JsonConverter<int> _structPropertyConverter;
private readonly string _structPropertyPropertyName;
Expand All @@ -663,6 +670,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o

var namingPolicy = options.PropertyNamingPolicy;

this._referenceFieldConverter = (global::System.Text.Json.Serialization.JsonConverter<string>)global::Thinktecture.JsonSerializerOptionsExtensions.GetCustomValueObjectMemberConverter(options, typeof(string));
this._referenceFieldPropertyName = namingPolicy?.ConvertName("ReferenceField") ?? "ReferenceField";
this._structPropertyConverter = (global::System.Text.Json.Serialization.JsonConverter<int>)global::Thinktecture.JsonSerializerOptionsExtensions.GetCustomValueObjectMemberConverter(options, typeof(int));
this._structPropertyPropertyName = namingPolicy?.ConvertName("StructProperty") ?? "StructProperty";
Expand Down Expand Up @@ -700,7 +708,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o

if (comparer.Equals(propName, this._referenceFieldPropertyName))
{
referenceField = reader.GetString();
referenceField = this._referenceFieldConverter.Read(ref reader, typeof(string), options);
}
else if (comparer.Equals(propName, this._structPropertyPropertyName))
{
Expand Down Expand Up @@ -741,7 +749,7 @@ public override void Write(global::System.Text.Json.Utf8JsonWriter writer, globa
if(!ignoreNullValues || referenceFieldPropertyValue is not null)
{
writer.WritePropertyName(this._referenceFieldPropertyName);
writer.WriteStringValue(referenceFieldPropertyValue);
this._referenceFieldConverter.Write(writer, referenceFieldPropertyValue, options);
}
var structPropertyPropertyValue = value.StructProperty;

Expand Down

0 comments on commit 6c704ee

Please sign in to comment.