-1

While deserializing some JSON into a C# object, I started getting a System.OverflowException for one of my Int32 properties: Value was either too large or too small. I was able to save the offending json to a file to inspect it further and reproduced the error in a unit test. There should really never be a value this big or small coming in; I need to find the specific JSON attribute with this large value (to make the underlying property long). The problem is there are roughly 5000 lines to sort through.

From my research, Visual Studio Find RegExs don't seem like a good fit for this case as their don't seem to be greater than or less than operators. What is the best way to search this file for values greater than 2147483647?

Sample Json:

"id": "pmanc",
"operation": "UPDATE",
"ResourceIAQStats": {
  "resourceId": "pmanc",
  "resourceName": "Peter Manc",
  "resourceState": 7,
  "durationInStateMillis": 113888,
  "nHandledContacts": 68,
  "nPresentedContacts": 68,
  "avgTalkDuration": 181324,

Edit

Here is my C# method for parsing the JSON, the Overflow Exception is occurring when attempting to deserialize the JArray Children to the generic object. Newtonsoft does not specify which object (or property) was too large.

    public async Task<List<T>> GetStatsAsync<T>(string testJson)
    {
        var requestResult = JArray.Parse(testJson);
        return new List<T>(requestResult.Children()
            .Select(jo => jo[typeof(T).Name].ToObject<T>()));
    }
slashNburn
  • 464
  • 4
  • 17
  • Why not just spit out other information about the object, like ID? It'll log the ID before it gets to the exception and then you'll have your culprit. – Brandon Miller Dec 29 '17 at 21:40
  • @BrandonMiller: Doing that might require modifying library code. – Ben Voigt Dec 29 '17 at 21:42
  • @BenVoigt I'm not sure what you mean. You could mock something like that up in a console app in about 10 minutes or less. Just iterate over your objects one by one and try to deserialize it. At the very least, you'll at least get to the object right before the offending one. – Brandon Miller Dec 29 '17 at 21:45
  • Are you getting the error from Newtonsoft? Usually the offender is listed somewhere in the exception. Are you capturing the inner exceptions? – TyCobb Dec 29 '17 at 21:45
  • @BrandonMiller: For that, you need to write a parser capable of separating JSON into its individual objects... – Ben Voigt Dec 29 '17 at 21:46
  • @BenVoigt https://stackoverflow.com/questions/16045569/how-to-access-elements-of-a-jarray – Brandon Miller Dec 29 '17 at 21:48
  • @BrandonMiller: So your suggestion is to go ahead and use the library, but Deserialize as a JObject instead of the strongly typed C# class? Seems like a reasonable approach. Not sure your earlier comments actually conveyed that, however. – Ben Voigt Dec 29 '17 at 21:51
  • @BenVoigt Kind of, but with this approach you can allow yourself to deserialize down to a single property on an object instead of trying to deserialize the entire object as a whole. JArray.Children() is just an Enumerable of JToken. – Brandon Miller Dec 29 '17 at 21:56

2 Answers2

2

Can you search for \d{10} on the assumption that you don't have a lot of other sequences of 10+ digits?

BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
-1

I don't understand why you think you have to do this in Visual Studio. It should be trivial (less than 10 lines) of Javascript to recurse through a JSON object, collecting a path and printing the path any time the value is greater than your threshold.

You will find this Q&A useful: Iterate through object properties

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720