Class JsonObject
Generic, mutable data structure used to represent JavaScript Objects.
Namespace: Unity.Serialization.Json
Syntax
public sealed class JsonObject
Remarks
Useful when you need to modify free-form JSON documents not represented by a C# type known at compile time.
For performance reasons, using JsonObject is not recommended when you de-serialize JSON into strongly-typed instances. For example, if you only need to read (not write) free-form JSON, use SerializedObjectReader directly.
The JsonObject is compatible with read-only visitors (see
Constructors
JsonObject()
Creates a Null object.
Declaration
public JsonObject()
JsonObject(Boolean)
Creates a Boolean object.
Declaration
public JsonObject(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| Boolean | value | Initial boolean value. |
JsonObject(Double)
Creates a Number object.
Declaration
public JsonObject(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | value | Initial numeric value. |
JsonObject(String)
Creates a String object.
Declaration
public JsonObject(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| String | value | Initial string value. |
JsonObject(JsonDataType)
Creates an object of the given type.
Declaration
public JsonObject(JsonDataType dataType)
Parameters
| Type | Name | Description |
|---|---|---|
| JsonDataType | dataType | Object type |
JsonObject(SerializedObjectView)
Creates a mutable object from the given immutable serialized object view.
Declaration
public JsonObject(SerializedObjectView view)
Parameters
| Type | Name | Description |
|---|---|---|
| SerializedObjectView | view | Immutable view from which this constructor initializes this instance. |
JsonObject(SerializedValueView)
Creates a mutable object from the given immutable serialized value view.
Declaration
public JsonObject(SerializedValueView view)
Parameters
| Type | Name | Description |
|---|---|---|
| SerializedValueView | view | Immutable view from which this constructor initializes this instance. |
Properties
BooleanValue
Gets or sets the object boolean value.
Declaration
public bool BooleanValue { get; set; }
Property Value
| Type | Description |
|---|---|
| Boolean |
Remarks
This property is only valid on objects of type Boolean.
DataType
Current data type.
Declaration
public JsonDataType DataType { get; }
Property Value
| Type | Description |
|---|---|
| JsonDataType |
Item[Int32]
Gets or sets an array object by index.
Declaration
public JsonObject this[int key] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | key | The member index. |
Property Value
| Type | Description |
|---|---|
| JsonObject |
Remarks
This indexer is only valid on objects of type Array.
Item[String]
Gets or sets an object member by name.
Declaration
public JsonObject this[string key] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| String | key | The member name. |
Property Value
| Type | Description |
|---|---|
| JsonObject |
Remarks
This indexer is only valid on objects of type Object.
NumberValue
Gets or sets the object numeric value.
Declaration
public double NumberValue { get; set; }
Property Value
| Type | Description |
|---|---|
| Double |
Remarks
This property is only valid on objects of type Number.
StringValue
Gets or sets the object string value.
Declaration
public string StringValue { get; set; }
Property Value
| Type | Description |
|---|---|
| String |
Remarks
This property is only valid on objects of type String.
Methods
ChangeType(JsonDataType)
Changes this object's data type. When the type changes, the underlying data resets.
Declaration
public bool ChangeType(JsonDataType newType)
Parameters
| Type | Name | Description |
|---|---|---|
| JsonDataType | newType | New object data type. |
Returns
| Type | Description |
|---|---|
| Boolean | True if the type changed, False otherwise. |
DeserializeFromString(String)
Creates and returns an object from the given JSON string.
Declaration
public static JsonObject DeserializeFromString(string json)
Parameters
| Type | Name | Description |
|---|---|---|
| String | json | JSON string to deserialize. |
Returns
| Type | Description |
|---|---|
| JsonObject | The created object. |
Remove(String)
Removes a member from the current object.
Declaration
public bool Remove(string key)
Parameters
| Type | Name | Description |
|---|---|---|
| String | key | The name of the member to remove. |
Returns
| Type | Description |
|---|---|
| Boolean | True if the object was found and removed, False otherwise. |
Remarks
This method is only valid on objects of type Object.
Serialize(JsonVisitor)
Serializes this object to a JSON string.
Declaration
public string Serialize(JsonVisitor visitor = null)
Parameters
| Type | Name | Description |
|---|---|---|
| JsonVisitor | visitor | Optional visitor used for serialization. |
Returns
| Type | Description |
|---|---|
| String | The created JSON string. |
TryGetValue(String, out JsonObject)
Attempts to retrieve the object member value by name.
Declaration
public bool TryGetValue(string key, out JsonObject value)
Parameters
| Type | Name | Description |
|---|---|---|
| String | key | The member name. |
| JsonObject | value | The found value, or null. |
Returns
| Type | Description |
|---|---|
| Boolean | True if the value was found and returned, False otherwise. |
Remarks
This method is only valid on objects of type Object.
Operators
Implicit(Boolean to JsonObject)
Implicit conversion from Boolean to JsonObject.
Declaration
public static implicit operator JsonObject(bool rhs)
Parameters
| Type | Name | Description |
|---|---|---|
| Boolean | rhs |
Returns
| Type | Description |
|---|---|
| JsonObject |
Implicit(Double to JsonObject)
Implicit conversion from Double to JsonObject.
Declaration
public static implicit operator JsonObject(double rhs)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | rhs |
Returns
| Type | Description |
|---|---|
| JsonObject |
Implicit(String to JsonObject)
Implicit conversion from String to JsonObject.
Declaration
public static implicit operator JsonObject(string rhs)
Parameters
| Type | Name | Description |
|---|---|---|
| String | rhs |
Returns
| Type | Description |
|---|---|
| JsonObject |