JObject

Inherits:
Object
Namespace:
BonaJson

Description

Abstract base class that all subsequent Json nodes inherits from. Is written in a generic manner so the application seldom has to typecheck or convert a JObject into their derived types in order the retrieve their data or be operated upon, like adding new elements or iterate over (in the cases of object collections or arrays). All the derived types possible methods are visible. Any method supported by some derived types but not others will throw a MethodNotSupported exception when called from an unsupported type.

Has static methods that will take Json-formatted strings and convert them into JObject trees and can thereby be seen as an entry point into the Json parsing process.

Public Methods

Name Type Description
Value () T If the JObject is a derivate of JValueObject, tries to retrieve the value contained in the node converted to type T.
Deserialize () T Tried to deserialize a JObject to an instance of T using reflection.
GetAsFloat () Single Workaround for an error causing crashes when Value is called where T is a float.
Add (Object element) JObject Adds any object to an array. Uses the other add methods to make a JObject of the parameter.
Add (JObject element) JObject Adds a JObject to an array.
Add (String name,JObject child) JObject Adds a JObject to an object collection.
Add (String name,Object element) JObject Adds any object to an object collection with the specified key. Uses the other add methods to make a JObject of the parameter.
Remove (JObject element) Boolean Removes an object from an array.
Remove (String name) Boolean Removes the object with the specified key from an object collection.
GetEnumerator () IEnumerator`1 Used internally by .NET to allow iteration over JObjectCollections and JObject arrays.
PrettyPrint () String Return a human-readable formatted Json string.
AddNodeToPrettyPrint (PrettyPrintData data) Void Used internally when using PrettyPrint().

Static Methods

Name Type Description
Parse (String jsonData) JObject Serialized a string into a JObject
Serialize (Object o) JObject Uses reflection to serialize any object to a JObject tree using reflection.

Public Properties

Examples

Parse a Json formatted sting to a JObject.
var jsonString = "{Data: {1, 2, 3, 4} }";

// Takes the string and parses it into an JObkect (JObjectCollection in this case)
var jObject = JObject.Parse(jsonString);

// This object can now be operated upon
foreach(var element in jObject["Data"]){
    // The Value<T>() method gets the value from the JObject and tries to convert it into the desired type. You need to keep track of what type is expected. In this case we created a list of Ints so all element's values can be extracted as Int64.
    Console.WriteLine(element.Value<Int64>());
}            
Converting from a JObject back into a string is not harder than using C#'s inbuilt ToString() method.
// Lets create a JObject with some data
var jObject = new JObjectArray();
jObject.Add(1);
jObject.Add(2);
jObject.Add(3);

// To convert this to a flat Json-formatted string, use C#'s ToString() method.
// This sting not contains the text "[1, 2, 3]"
var jObjectString = jObject.ToString();

// To instead get a (more) human readable format, use the PrettyPrint() method instead.
// This string now contains the text
// "[
//     1,
//     2,
//     3,
// ]"
var jOjectPrerryPrint = jObject.PrettyPrint();