Skip to content

Change serializer settings to not send empty values, and deserialize empty collections #3381

@nohwnd

Description

@nohwnd

With our current serializer settings there is difference between sending null, and sending empty collection. The serializer will do "the right thing" and give you what you sent. So either a null or empty collection. If no-one touches the property, or does not re-assign it to null we will get empty list on the other side.

IMHO there is almost never a case where you want to know if the collection was null, or empty, and we should change our serializer settings to:

  1. not send empty or null values
  2. always populate collections with empty collection even if we receive null, unless opted out (and I cannot recall any place where we would want to do that)
using ConsoleDump;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

JsonDataSerializer.Instance.DeserializePayload<AAA>(
    JsonDataSerializer.Instance.DeserializeMessage(
        JsonDataSerializer.Instance.SerializePayload("mmm", new AAA())).Dump()).Dump();

JsonDataSerializer.Instance.DeserializePayload<BBB>(
    JsonDataSerializer.Instance.DeserializeMessage(
        JsonDataSerializer.Instance.SerializePayload("mmm", new BBB())).Dump()).Dump();



public class AAA
{
    public IList<string> Names { get; set; } = new List<string>();
}

public class BBB
{
    public IList<string> Names { get; set; }
}

image

Screenshot shows the message on the receiving side before deserialization of the payload (yellow) and after deserialization (green).


In the approach I propose, the payload for both AAA and BBB would be empty ({}) and Names would deserialize as empty collection for both AAA and BBB.

Originally posted by @nohwnd in #3349 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions