Throw errors on blank strings or empty objects when checking for missing member handling
See original GitHub issueSource/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:
- Created 5 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
Does anybody know how it can be achieved?
@alex-valchuk Your example can be further simplified to deserializing
""
(i. e, an text containing a string) into an instance ofUserInfo
class.JsonConvert.DeserializeObject<UserInfo>("\"\"")
returnsnull
instead of throwing an exception. I believe this to be a bug.