title: How to get JSON body from the stream in C# efficiently?
body: <p>I am using Utf8 json library to deserialize my JSON using <code>DeserializeAsync</code> method of <code>JsonSerializer</code> class. And sometimes I am seeing it is throwing exception as -</p>
  <pre><code>Arithmetic operation resulted in an overflow.
  </code></pre>
  <p>So it looks like my JSON data has a value that's too large to fit in one of our object's properties causing this overflow exception. Below is the code I got where I am trying to print the json which is bad and causing this <code>Arithmetic Overflow</code> error but it's not logging that JSON whenever this exception happens.</p>
  <pre><code>using (var content = httpResponseMessage.Content)
  {
      if (content == null) return (default(T), statusCode);
  
      using (var responseStream = await httpResponseMessage.Content.ReadAsStreamAsync())
      {
          try
          {
              deserializedValue = await JsonSerializer.DeserializeAsync&lt;T&gt;(responseStream, formatResolver);
          }
          catch (Exception ex)
          {
              var bodyString = (ex as JsonParsingException)?.GetUnderlyingByteArrayUnsafe();
              var error = (bodyString != null) ? $&quot;Bad json: {Encoding.UTF8.GetString(bodyString)}&quot; : &quot;Cannot Deserialize JSON&quot;;
              logger.logError(error, ex.Message, &quot;Deserialization Exception&quot;, ex.StackTrace, (int)statusCode);
              return (default(T), HttpStatusCode.BadRequest);
          }
      }
  }
  </code></pre>
  <p>For some reason <code>bodyString</code> is coming as null whenever this exception happens and that's why my condition is not getting evaluated to get the JSON body. My understanding was that it will throw <code>JsonParsingException</code> but looks like it is throwing some other exception.</p>
  <p>Is there any way to get JSON body whenever this exception happens? or maybe write this in a better way to get JSON whenever this exception happens efficiently?</p>