Skip to content

JsonConvert.DeserializeObject can throw several unexpected exceptions #1947

Open

Description

@Metalnem

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

  • ArgumentException
  • JsonSerializationException
  • JsonWriterException
  • NullReferenceException

Documentation doesn't say anything about this, but deserialization is definitely weird place for a JsonWriterException. ArgumentException and NullReferenceException should not be possible in my opinion (if the input strings are non-empty). Here is the complete program to reproduce all of this:

using System;

namespace Newtonsoft.Json.Run
{
  public class Program
  {
    public static void Main(string[] args)
    {
      try
      {
        JsonConvert.DeserializeObject("[{\"click\":\"X\",\"Close()\"://[{\"click\":\"Kpen()\"},{\"cOOOOOOOOOO\u007f]}}}");
      }
      catch (ArgumentException) { }

      try
      {
        JsonConvert.DeserializeObject("{\"menu\":{\"id\":1,\"val\":\"X\",\"pop\":{\"a\":[{\"click\":\"Open()\"},{\"click\":\"Close()\"}]}}");
      }
      catch (JsonSerializationException) { }

      try
      {
        JsonConvert.DeserializeObject("{\"menu\":{\"id\":1,\"val\":\"X\",\"pop\":{\"a\":[{\"clicT\":\"Open()\"},");
      }
      catch (JsonWriterException) { }

      try
      {
        JsonConvert.DeserializeObject("[new\0([");
      }
      catch (NullReferenceException) { }
    }
  }
}

Found via SharpFuzz.

Activity

  1. AaronJThompson commented on Nov 10, 2021

    @AaronJThompson

    The last 3 seem to have been fixed. However, the first one now throws the error:
    Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.
    Which I presume is still a bug

  2. Aws-amplify-cli-puppy commented on Oct 14, 2025

    @Aws-amplify-cli-puppy

    The last 3 seem to have been fixed. However, the first one now throws the error: Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject. Which I presume is still a bug

    https://linebackersudoku.github.io/unsophisticated73/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions