Skip to content

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

Closed as not planned

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 Jakub Jareš (@nohwnd) in #3349 (comment)

Activity

  1. Evangelink commented on Feb 17, 2022

    @Evangelink
    Member

    Nullability would help in our code to prevent us from setting null but we cannot control outside of our boundaries (e.g. user or serializer could set null). The only real safety would be to either prevent public setter or to have a backing field and check we don't pass null.

    public class CCC
    {
        private IList<string> _names = new List<string>();
        public IList<string> Names 
        {
            get => _names;
            set => _names = value ?? new List<string>();
        }
    }
  2. nohwnd commented on Jul 8, 2024

    @nohwnd
    MemberAuthor

    This is a new feature and won't be implemented, we are focusing on adding new features to Testing.Platform instead. https://aka.ms/testingplatform

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to 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