What exceptions does Newtonsoft.Json.DeserializeObject throw?

Error processing SSI file

Answers

  1. Maverick

    • 2015/7/17

    JSON.NET defines the following exceptions:

    • JsonException
      • JsonReaderException
      • JsonSerializationException
      • JsonWriterException
      • JsonSchemaException

    Serialization or deserialization errors will typically result in a JsonSerializationException.

  2. Nathanael

    • 2018/3/21

    The error event is raised whenever an exception is thrown while serializing or deserializing JSON. Like all settings found on JsonSerializer, it can also be set 

  3. Neil

    • 2015/2/24

    The exception depends on what actually failed, for example, I took some valid JSON and removed the closing '}' and it threw a JsonSerializationException. Yet if I change it to be not JSON like at all (i.e. just a word or even HTML), it threw JsonReaderException. –

  4. Jaden

    • 2019/7/27

    The exception thrown when an error occurs during JSON serialization or deserialization.

  5. Grassi

    • 2018/5/18

    Its JsonReaderException.

    Check below1

    And it can be handled easily 2

  6. Demetrius

    • 2016/8/10

    I am trying to use Newtonsoft.Json to serialize an object for http post request and after that to deserialize the response into a list of objects. The Newtonsoft.Json SerializeObject and DeserializeObject are not working and returning null (variables json and items in code below). They do not throw any exception. The code is as follows:

  7. Harvey

    • 2019/7/4

    Serialization or deserialization errors will typically result in a JsonSerializationException . Note that Json.NET's error handling documentation shows a 

  8. Kase

    • 2015/12/16

    JsonConvert.DeserializeObject can throw several unexpected exceptions (JsonReaderException is the one that is usually expected). These are: ArgumentException JsonSerializationException JsonWriterException NullReferenceException Documenta

  9. Guillermo

    • 2017/5/21

    Note that Json.NET's error handling documentation shows a strategy for the API user to deal with errors by handling error events rather than directly catching exceptions. This makes sense when you consider that perhaps only one item in an array may fail to deserialize, and you might want to handle this in a more granular fashion than one monolithic exception for the entire set.

    This answer addresses the "want to handle them" part of your question without getting at the "what exceptions" part. As another answer shows, all Json.NET exceptions inherit from JsonException Class, so catching this would be a nice fail-safe. However, it seems that if you want to really understand what caused an exception to be thrown, you would need to read its Message property, not handle based on the Exception type, as the different types seem to be more oriented on the action you are performing than the error category. In the following example code, the args.ErrorContext.Error is an instance of Exception.

    Example code from the documentation:

    List<string> errors = new List<string>();
    
    List<DateTime> c = JsonConvert.DeserializeObject<List<DateTime>>(@"[
          '2009-09-09T00:00:00Z',
          'I am not a date and will error!',
          [
            1
          ],
          '1977-02-20T00:00:00Z',
          null,
          '2000-12-01T00:00:00Z'
        ]",
        new JsonSerializerSettings
        {
            Error = delegate(object sender, ErrorEventArgs args)
            {
                errors.Add(args.ErrorContext.Error.Message);
                args.ErrorContext.Handled = true;
            },
            Converters = { new IsoDateTimeConverter() }
        });
    
    // 2009-09-09T00:00:00Z
    // 1977-02-20T00:00:00Z
    // 2000-12-01T00:00:00Z
    
    // The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
    // Unexpected token parsing date. Expected String, got StartArray.
    // Cannot convert null value to System.DateTime.
    
  10. Lucian

    • 2016/1/8

    JsonConvert.DeserializeObject can throw several unexpected exceptions #1947 using System; namespace Newtonsoft.Json.

  11. Damien

    • 2015/12/28

    Hi, When calling JsonConvert.DeserializeObject with an invalid array as input an exception should be thrown, bu this is not the case. However, JArray.Parse does throw an exception.

  12. Phillip

    • 2017/11/14

    [Solution trouvée!] JSON.NET définit les exceptions suivantes: JsonException JsonReaderException JsonSerializationException JsonWriterException 

  13. Joshua

    • 2015/3/27

    This sample uses the P:Newtonsoft.Json.JsonSerializerSettings.Error event to ignore the exceptions thrown from the invalid date strings.

  14. Drew

    • 2019/2/4

    During deserialization, Newtonsoft.Json ignores comments in the JSON by default. The System.Text.Json default is to throw exceptions for 

  15. Layne

    • 2020/8/30

    The OnErrorAttribute works much like the other .NET serialization attributes that Json.NET supports. To use it you simply place the attribute on a method that takes the correct parameters: a StreamingContext and an ErrorContext.

  16. Blaze

    • 2016/10/30

    Because you are trying to make the library do something that it is not intended to do; it is illegal and not safe to convert the values from 

Comments are closed.

More Posts