question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issues with structure serialization

See original GitHub issue

Thank 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:

  1. 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

  1. 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:

  1. 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 with JsonPropertyName when using different serializers in the projects.
        public class MemberNameSerialization
        {
            [DataMember (Name = "memberName")]
            public int A { get; set; } = 1;
        }
  1. It would be more convenient to use SetupExtensions if it returned JsonSerializerOptions

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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nsentinelcommented, Dec 21, 2019

Thank you very much!

0reactions
mcatanzariticommented, Dec 21, 2019

Implemented in 1.0.9

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Why is it wrong to serialize a struct?
So yes, your preference for serialization of a struct due to its increased performance is justified. Because oftentimes structs are ...
Read more >
Question - Array of struct serialization problem
Hello everyone, i have a serialization problem, i used an array of structs for my item list but now i have a problem,...
Read more >
Serialization
Serializing the data structure in an architecture-independent format means preventing the problems of byte ordering, memory layout, or simply different ways ...
Read more >
Serializer-specific struct serialization #968 - serde-rs/serde
Hello, (TL;DR at the bottom) Backstory I am currently trying to implement an ASN.1 serialization system for serde.
Read more >
Serializing Data Structures in C [closed]
C has no native support for serializing structures, so you're on your own. The first order approximation is (as stated in other replies)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found