Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize: class empty but json has elements => throws #25

Closed
MaxHorstmann opened this issue Jun 19, 2014 · 0 comments
Closed

Deserialize: class empty but json has elements => throws #25

MaxHorstmann opened this issue Jun 19, 2014 · 0 comments

Comments

@MaxHorstmann
Copy link

Looks like Jil throws a DeserializationException when encountering a key in a child which should be empty. Example:

using System;
using Jil;
using Newtonsoft.Json;

namespace JsonTests
{
    class Response
    {
        public int id { get; set; }
        public Foo foo { get; set; }
    }

    class Foo {  /* nothing here .. yet */ }

    class Program 
    {
        static void Main(string[] args)
        {
            const string jsonString = "{ \"id\" : 17, \"foo\" : { \"bar\" : 17} }";

            var resultJsonNet = JsonConvert.DeserializeObject<Response>(jsonString); /* ok */
            Console.WriteLine(resultJsonNet.id);

            var resultJil = JSON.Deserialize<Response>(jsonString); /* throws */
            Console.WriteLine(resultJil.id);
        }
    }
}

Can easily be mitigated by removing the empty Foo on the parent. But IMO worth making it a bit more robust if feasible without perf impact. It's a fairly typical dev workflow to incrementally build out data model, starting with empty classes. So those exceptions can hurt productivity a bit when it's not clear where they're coming from.

kevin-montrose added a commit that referenced this issue Jun 19, 2014
kevin-montrose added a commit that referenced this issue Jun 19, 2014
kevin-montrose added a commit that referenced this issue Jul 10, 2014
kevin-montrose added a commit that referenced this issue Jul 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant