Class JsonObject
- Namespace
- Vintagestory.API.Datastructures
- Assembly
- VintagestoryAPI.dll
Elegant, yet somewhat inefficently designed (because wasteful with heap objects) wrapper class to abstract away the type-casting nightmare of JToken O.O
public class JsonObject : IReadOnlyCollection<JsonObject>, IEnumerable<JsonObject>, IEnumerable
- Inheritance
-
JsonObject
- Implements
- Inherited Members
- Extension Methods
Constructors
JsonObject(JToken)
Create a new instance of a JsonObject
public JsonObject(JToken token)
Parameters
token
JToken
JsonObject(JsonObject, bool)
Create a new instance of a JsonObject
public JsonObject(JsonObject original, bool unused)
Parameters
original
JsonObjectunused
boolOnly present so that the Constructor with a sole null parameter has an unambiguous signature
Properties
Count
Gets the number of elements in the collection.
public int Count { get; }
Property Value
- int
The number of elements in the collection.
Exists
True if the token is not null
public bool Exists { get; }
Property Value
this[string]
Access a tokens element with given key
public JsonObject this[string key] { get; }
Parameters
key
string
Property Value
Token
public virtual JToken Token { get; set; }
Property Value
- JToken
Methods
AsArray()
Turn the token into an array of JsonObjects
public JsonObject[] AsArray()
Returns
AsArray<T>(T[], string)
Turn the token into an array
public T[] AsArray<T>(T[] defaultValue = null, string defaultDomain = null)
Parameters
defaultValue
T[]If the conversion fails, this value is used instead
defaultDomain
string
Returns
- T[]
Type Parameters
T
AsBool(bool)
Turn the token into a boolean
public bool AsBool(bool defaultValue = false)
Parameters
defaultValue
boolIf the conversion fails, this value is used instead
Returns
AsDouble(double)
Turn the token into a double
public double AsDouble(double defaultValue = 0)
Parameters
defaultValue
doubleIf the conversion fails, this value is used instead
Returns
AsFloat(float)
Turn the token into a float
public float AsFloat(float defaultValue = 0)
Parameters
defaultValue
floatIf the conversion fails, this value is used instead
Returns
AsFloatArray(float[])
[Obsolete("Use AsArray<float>() instead")]
public float[] AsFloatArray(float[] defaultValue = null)
Parameters
defaultValue
float[]
Returns
- float[]
AsInt(int)
Turn the token into an integer
public int AsInt(int defaultValue = 0)
Parameters
defaultValue
intIf the conversion fails, this value is used instead
Returns
AsObject<T>(JsonSerializerSettings, T, string)
public T AsObject<T>(JsonSerializerSettings settings, T defaultValue, string domain = "game")
Parameters
settings
JsonSerializerSettingsdefaultValue
Tdomain
string
Returns
- T
Type Parameters
T
AsObject<T>(T)
Deserialize the token to an object of the specified type T
public T AsObject<T>(T defaultValue = default)
Parameters
defaultValue
T
Returns
- T
Type Parameters
T
AsObject<T>(T, string)
Deserialize the token to an object of the specified type T, with the specified domain for any AssetLocation which needs to be parsed
public T AsObject<T>(T defaultValue, string domain)
Parameters
defaultValue
Tdomain
string
Returns
- T
Type Parameters
T
AsString(string)
Turn the token into a string
public string AsString(string defaultValue = null)
Parameters
defaultValue
stringIf the conversion fails, this value is used instead
Returns
AsStringArray(string[], string)
[Obsolete("Use AsArray<string>() instead")]
public string[] AsStringArray(string[] defaultValue = null, string defaultDomain = null)
Parameters
Returns
- string[]
Clone()
Returns a deep clone
public JsonObject Clone()
Returns
FillPlaceHolder(string, string)
public virtual void FillPlaceHolder(string key, string value)
Parameters
FromJson(string)
public static JsonObject FromJson(string jsonCode)
Parameters
jsonCode
string
Returns
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<JsonObject> GetEnumerator()
Returns
- IEnumerator<JsonObject>
An enumerator that can be used to iterate through the collection.
IsArray()
True if the token is a JArray
public bool IsArray()
Returns
IsTrue(string)
Returns true if this object has the named bool attribute, and it is true
public bool IsTrue(string attrName)
Parameters
attrName
string
Returns
KeyExists(string)
True if the token has an element with given key
public bool KeyExists(string key)
Parameters
key
string
Returns
ToAttribute()
Turns the token into an IAttribute with all its child elements, if it has any.
Note: If you converting this to a tree attribute, a subsequent call to tree.GetInt() might not work because Newtonsoft.JSON seems to load integers as long, so use GetDecimal() or GetLong() instead. Similar things might happen with float<->double
public IAttribute ToAttribute()
Returns
ToPrimitiveArray<T>(JArray)
Turn a JArray into a primitive array
public static T[] ToPrimitiveArray<T>(JArray array)
Parameters
array
JArray
Returns
- T[]
Type Parameters
T
ToString()
Calls token.ToString()
public override string ToString()