Issues with structure serialization
See original GitHub issueThank you very much for developing a library that eliminates the current limitations of System.Text.Json
! I wanted to do it myself, but in the process of looking for options, I found your solution.
I’ve had a few issues using the library at the moment, and since you’ve already started working in this direction, you might want to solve them.
In the examples below, JsonHelpers.DefaultOptions.DefaultJsonFormat
includes setting extensions for options.
Using fields in a class works fine, but there are two similar cases with broken ones:
- The use of primitive fields in the structure:
public struct FieldSerializationStruct
{
public int A { get; set; }
public int B;
public FieldSerializationStruct (int a, int b)
{
A = a;
B = b;
}
}
string jsonSerialized = JsonSerializer.Serialize(new FieldSerializationStruct(1, 2), JsonHelpers.DefaultOptions.DefaultJsonFormat);
In this case, field B will be simply skipped
- A class with a nested structure (a field or property does not matter):
public class FieldSerializationClassWithStruct
{
public int A { get; set; } = 1;
public FieldSerializationStruct B = new FieldSerializationStruct(2,3);
}
string jsonSerialized = JsonSerializer.Serialize(new FieldSerializationClassWithStruct(), JsonHelpers.DefaultOptions.DefaultJsonFormat);
In this case, an exception occurs:
System.NullReferenceException: Object reference not set to an instance of an object.
at Dahomey.Json.Serialization.Converters.MemberConverter`2.Write (Utf8JsonWriter writer, Object obj, JsonSerializerOptions options)
at Dahomey.Json.Serialization.Converters.ObjectConverter`1.Write (Utf8JsonWriter writer, T value, JsonSerializerOptions options)
at System.Text.Json.JsonPropertyInfoNotNullable`4.OnWrite (WriteStackFrame & current, Utf8JsonWriter writer)
at System.Text.Json.JsonPropertyInfo.Write (WriteStack & state, Utf8JsonWriter writer)
at System.Text.Json.JsonSerializer.Write (Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack & state)
at System.Text.Json.JsonSerializer.WriteCore (Utf8JsonWriter writer, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCore (PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCoreString (Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize [TValue] (TValue value, JsonSerializerOptions options)
For “standard” fields like Guid
, decimal
it works correctly.
I would also like to note two things:
- It would be great if the library supported the use of the
DataMember
attribute to set the name so that you don’t have to duplicate attributes withJsonPropertyName
when using different serializers in the projects.
public class MemberNameSerialization
{
[DataMember (Name = "memberName")]
public int A { get; set; } = 1;
}
- It would be more convenient to use
SetupExtensions
if it returnedJsonSerializerOptions
For example, it would be possible to write the initialization in one line:
JsonSerializerOptions IndentedJsonSerializerOptions =
new JsonSerializerOptions{WriteIndented = true}.SetupExtensions();
Thanks in advance!
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Thank you very much!
Implemented in 1.0.9