AvroConvert icon indicating copy to clipboard operation
AvroConvert copied to clipboard

Serialize Struct with private fields

Open morlando016 opened this issue 3 years ago • 3 comments

Hello,

I was wondering if this project supports the Instant data type from the NodaTime library? I tried to generate a schema for a simple record that had an Instant property but when I went to serialize, I got an error stating that 'Instance property 'MinValue' is not defined for type 'NodaTime.Instant' (Parameter 'propertyName')'. It looks like the instant could not be serialized properly as the schema did not know about this MinValue property. Is there any way to support this data type? I am considering using this library and trying to prototype something with it.

The type I try to serialize:

public class Error
    {
        public int Id { get; set; }
        public string AppName { get; set; }
        public Instant Date { get; set; }
    }

The app code that throws an error:

            Error appError = new Error
            {
                AppName = "MyApplication",
                Id = 123,
                Date = SystemClock.Instance.GetCurrentInstant()
            };

            string avroSchema = AvroConvert.GenerateSchema(typeof(Error));

            var serialized = AvroConvert.SerializeHeadless(appError, avroSchema); // Error here

morlando016 avatar Apr 26 '22 18:04 morlando016

Hello,

Thank you for creating the issue. I will take a look at this.

Regards, Adrian

AdrianStrugala avatar Apr 28 '22 07:04 AdrianStrugala

Hello,

The issue seems to be more complex than expected. Instant is a struct holding its value as a private field. Additionally contains a couple of helper methods and other private fields.

My proposition as a workaround is to serialize the date to a string:

SystemClock.Instance.GetCurrentInstant().ToString()

I will try to figure out a fix, but it may take some time.

Regards, Adrian

AdrianStrugala avatar Apr 28 '22 08:04 AdrianStrugala

Hello,

The issue seems to be more complex than expected. Instant is a struct holding its value as a private field. Additionally contains a couple of helper methods and other private fields.

My proposition as a workaround is to serialize the date to a string:

SystemClock.Instance.GetCurrentInstant().ToString()

I will try to figure out a fix, but it may take some time.

Regards, Adrian

Yeah this is what I have done and actually may work better for what Ill need! Thanks for the response.

morlando016 avatar Apr 28 '22 14:04 morlando016

At the moment there is no plan for direct support of private fields serialization

AdrianStrugala avatar Feb 09 '23 12:02 AdrianStrugala