quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

C# - Empty array breaks deserialization

Open Sharpienero opened this issue 6 years ago • 0 comments

Hi all,

I searched the open tickets, but didn't find anything for this. If a json response comes in with an empty array, the deserialization throws an exception. I wouldn't consider this a critical bug, but it's a bug nonetheless. I've added some sample code so that this can easily be reproduced. This only happens when you use the array option, but the behavior works as intended when using the list option.

Example code:

var json = "{ \"searchRecords\" : [ ] }";
Search[] search = Search.FromJson(json);
Console.WriteLine(search);
namespace QuickType.Search
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Search
    {
        [JsonProperty("searchRecords")]
        public object[] searchRecords{ get; set; }
    }

    public partial class Search
    {
        public static Search FromJson(string json) => JsonConvert.DeserializeObject<Search>(json, QuickType.Search.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this Search self) => JsonConvert.SerializeObject(self, QuickType.Search.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters =
            {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}

gifv of error in example: https://i.imgur.com/0E2MXXU.gifv

I'm not sure if this intended behavior or not, but figured I'd report it just in case.

Sharpienero avatar Aug 19 '19 14:08 Sharpienero