Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

C# SimpleJsonWriter incorrectly serializes non-nullable field set to null #417

@chwarr

Description

@chwarr

Given the following .bond file

namespace bug;

struct NonNullableStringField
{
  0: string sf;
}

The following C# code should fail to serialize as sf is null.

namespace bug
{
    using System;
    using System.IO;

    using Bond;
    using Bond.Protocols;

    internal static class Program
    {
        private static void Main()
        {
            var obj = new NonNullableStringField
            {
                sf = null
            };

            var ser = new Serializer<SimpleJsonWriter>(typeof (NonNullableStringField));

            var stream = new StringWriter();
            var jw = new SimpleJsonWriter(stream);

            ser.Serialize(obj, jw);
            jw.Flush();

            Console.WriteLine(stream.ToString());
        }
    }
}

Instead, the following is output.

{"sf":null}

Other types than string, like sub-struct fields, are also affected.

SimpleXmlWriter is also affected. CompactBinaryWriter, FastBinaryWriter, and SimpleBinaryWriter all fail with a NullReferenceException, as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions