Table of Contents

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 JsonObject
unused bool

Only 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

bool

this[string]

Access a tokens element with given key

public JsonObject this[string key] { get; }

Parameters

key string

Property Value

JsonObject

Token

public virtual JToken Token { get; set; }

Property Value

JToken

Methods

AsArray()

Turn the token into an array of JsonObjects

public JsonObject[] AsArray()

Returns

JsonObject[]

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 bool

If the conversion fails, this value is used instead

Returns

bool

AsDouble(double)

Turn the token into a double

public double AsDouble(double defaultValue = 0)

Parameters

defaultValue double

If the conversion fails, this value is used instead

Returns

double

AsFloat(float)

Turn the token into a float

public float AsFloat(float defaultValue = 0)

Parameters

defaultValue float

If the conversion fails, this value is used instead

Returns

float

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 int

If the conversion fails, this value is used instead

Returns

int

AsObject<T>(JsonSerializerSettings, T, string)

public T AsObject<T>(JsonSerializerSettings settings, T defaultValue, string domain = "game")

Parameters

settings JsonSerializerSettings
defaultValue T
domain 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 T
domain string

Returns

T

Type Parameters

T

AsString(string)

Turn the token into a string

public string AsString(string defaultValue = null)

Parameters

defaultValue string

If the conversion fails, this value is used instead

Returns

string

AsStringArray(string[], string)

[Obsolete("Use AsArray<string>() instead")]
public string[] AsStringArray(string[] defaultValue = null, string defaultDomain = null)

Parameters

defaultValue string[]
defaultDomain string

Returns

string[]

Clone()

Returns a deep clone

public JsonObject Clone()

Returns

JsonObject

FillPlaceHolder(string, string)

public virtual void FillPlaceHolder(string key, string value)

Parameters

key string
value string

FromJson(string)

public static JsonObject FromJson(string jsonCode)

Parameters

jsonCode string

Returns

JsonObject

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

bool

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

bool

KeyExists(string)

True if the token has an element with given key

public bool KeyExists(string key)

Parameters

key string

Returns

bool

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

IAttribute

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()

Returns

string