Method ParseJObject
ParseJObject(string)
Safely parses a JSON string into a Newtonsoft.Json.Linq.JObject, enforcing a maximum depth limit to mitigate risks associated with deeply nested or malicious JSON payloads.
Declaration
public static JObject ParseJObject(string json)
Parameters
Type | Name | Description |
---|---|---|
string | json | The JSON string to parse into a Newtonsoft.Json.Linq.JObject. |
Returns
Type | Description |
---|---|
JObject | A Newtonsoft.Json.Linq.JObject representing the root of the parsed JSON object. If the input is not a valid JSON object, an exception will be thrown. |
Remarks
This method utilizes a Newtonsoft.Json.JsonTextReader with an explicitly defined maximum depth, providing robust protection against excessively nested structures that could otherwise lead to stack overflow exceptions or performance degradation.
This is particularly important when parsing user-generated or externally sourced JSON content, where input structure cannot be guaranteed.
Example usage:
var obj = Utils.ParseJObject(jsonString);
var name = obj["user"]?["name"]?.Value<string>();
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Thrown when the |
JsonReaderException | Thrown if the input JSON is malformed, not an object, or exceeds the maximum allowed depth as defined by MaxDepth. |