question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw errors on blank strings or empty objects when checking for missing member handling

See original GitHub issue

Source/destination types

 public class MyObj
 {
     public string Prop { get; set; }
 }

Source/destination JSON

(blank)
OR
{}

Expected behavior

An exception to be thrown

Actual behavior

Returned null value

Steps to reproduce

using Newtonsoft.Json;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var settings = new JsonSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Error
            };

            //unexpected
            JsonConvert.DeserializeObject<MyObj>("", settings); //returns null
            JsonConvert.DeserializeObject<MyObj>("{}", settings); //returns null

            //expected
            JsonConvert.DeserializeObject<MyObj>(null, settings); //throws System.ArgumentNullException
            JsonConvert.DeserializeObject<MyObj>("[]", settings); //throws Newtonsoft.Json.JsonSerializationException
            JsonConvert.DeserializeObject<MyObj>("{'Test': 'Test'}", settings); //throws Newtonsoft.Json.JsonSerializationException
            JsonConvert.DeserializeObject<MyObj>("{'Prop': 'Test'}", settings); //returns MyObj instance
        }
    }

    public class MyObj
    {
        public string Prop { get; set; }
    }
}

This is a minor issue I can code around, but it seemed like a bug.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
alex-valchukcommented, Apr 22, 2019

I’m interested in throwing serialization exceptions in cases when a user provides json with a key (for a not required property) for which a relevant value=“” is supposed to be a complex object e.g:

// invalid json - exception should be thrown
{
    "user_info": ""
}
// valid json - no exceptions:
{
    "user_info": {
         "first_name": "John",
         "last_name": "Smith"
    }
}

Does anybody know how it can be achieved?

0reactions
vladdcommented, Mar 30, 2020

@alex-valchuk Your example can be further simplified to deserializing "" (i. e, an text containing a string) into an instance of UserInfo class. JsonConvert.DeserializeObject<UserInfo>("\"\"") returns null instead of throwing an exception. I believe this to be a bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Catch empty String. Which exception to use?
First throw a RuntimeException on Valuable: public Valueables(String name){ //name.trim().length() == 0 if(name == null || name.
Read more >
Returning null or a empty value/throw exception? [duplicate]
This question already has answers here:​​ Instead of returning null default values (0 or empty string or empty object) should be returned or...
Read more >
How to detect for empty field in excel and throw an error ...
i am now use the “String.IsNullOrEmpty(row(“DELIVERY DATE”).ToString)” to check one by one. currently it can only detect for one row and throw ...
Read more >
JavaScript Check Empty String – Checking Null or ...
"This is an empty string!" Note: If the value is null, this will throw an error because the length property does not work...
Read more >
Handling JSON null and empty arrays and objects
Handling null and empty arrays and objects used in JSON data is described. ... If the property is not defined as nillable, it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found