ISource2ModJSON

Reading JSON without the plugin owning a JSON library.

Source2Mod parses; a plugin walks the result through this interface. Nothing library-specific crosses the boundary, so a plugin does not have to vendor a matching JSON implementation or agree with Source2Mod on its ABI.

Interface id ISource2ModJSON001 (SOURCE2MOD_JSON_INTERFACE) · Global g_pSource2ModJSON · Header source2mod/ISource2ModJSON.h

Methods

Parse

virtual ISource2ModJSONDocument* Parse(const char* pszText, int nLength = -1) = 0;

Parses text into a document the caller then owns.

Never returns null: on a parse error the document reports IsValid() false and its root reads as null, so walking it is safe without checking.

ParameterTypeDefaultDescription
pszTextconst char*JSON text.
nLengthint-1Length, or -1 to read up to the terminator.

Returns ISource2ModJSONDocument* — A document; call Release() on it when done.

ISource2ModJSONValue

One node of a parsed document: an object, array, or scalar.

Nodes are owned by the document they came from and stay valid until it is released. Every accessor is total -- asking a string for its integer value, or an object for element 3, returns the default you passed rather than throwing.

IsNull

virtual bool IsNull() const = 0;

IsObject

virtual bool IsObject() const = 0;

IsArray

virtual bool IsArray() const = 0;

IsString

virtual bool IsString() const = 0;

IsNumber

virtual bool IsNumber() const = 0;

IsBool

virtual bool IsBool() const = 0;

Has

virtual bool Has(const char* pszKey) const = 0;

Whether this object has the key. False for anything but an object.

ParameterType
pszKeyconst char*

Get

virtual const ISource2ModJSONValue* Get(const char* pszKey) const = 0;

The value at a key, or null when absent or not an object.

ParameterType
pszKeyconst char*

Count

virtual int Count() const = 0;

Elements in an array, or members in an object. Zero otherwise.

At

virtual const ISource2ModJSONValue* At(int nIndex) const = 0;

The element at an index, or null when out of range or not an array.

ParameterType
nIndexint

KeyAt

virtual const char* KeyAt(int nIndex) const = 0;

The key of the member at an index, for walking an object. Null when out of range or not an object.

ParameterType
nIndexint

AsString

virtual const char* AsString(const char* pszDefault = "") const = 0;

This node's value, or pszDefault when it is not a string.

The pointer stays valid as long as the document does.

ParameterTypeDefault
pszDefaultconst char*""

AsInt64

virtual int64_t AsInt64(int64_t nDefault = 0) const = 0;
ParameterTypeDefault
nDefaultint64_t0

AsInt

virtual int AsInt(int nDefault = 0) const = 0;
ParameterTypeDefault
nDefaultint0

AsDouble

virtual double AsDouble(double flDefault = 0.0) const = 0;
ParameterTypeDefault
flDefaultdouble0.0

AsFloat

virtual float AsFloat(float flDefault = 0.0f) const = 0;
ParameterTypeDefault
flDefaultfloat0.0f

AsBool

virtual bool AsBool(bool bDefault = false) const = 0;
ParameterTypeDefault
bDefaultboolfalse

GetString

virtual const char* GetString(const char* pszKey, const char* pszDefault = "") const = 0;

Shorthand for Get(key) then the matching As*, with the default when the key is missing or the wrong type.

ParameterTypeDefault
pszKeyconst char*
pszDefaultconst char*""

GetInt64

virtual int64_t GetInt64(const char* pszKey, int64_t nDefault = 0) const = 0;
ParameterTypeDefault
pszKeyconst char*
nDefaultint64_t0

GetInt

virtual int GetInt(const char* pszKey, int nDefault = 0) const = 0;
ParameterTypeDefault
pszKeyconst char*
nDefaultint0

GetDouble

virtual double GetDouble(const char* pszKey, double flDefault = 0.0) const = 0;
ParameterTypeDefault
pszKeyconst char*
flDefaultdouble0.0

GetFloat

virtual float GetFloat(const char* pszKey, float flDefault = 0.0f) const = 0;
ParameterTypeDefault
pszKeyconst char*
flDefaultfloat0.0f

GetBool

virtual bool GetBool(const char* pszKey, bool bDefault = false) const = 0;
ParameterTypeDefault
pszKeyconst char*
bDefaultboolfalse

Dump

virtual const char* Dump(int nIndent = -1) const = 0;

This node serialised back to text.

ParameterTypeDefaultDescription
nIndentint-1Spaces per level, or -1 for the compact form.

Returns const char* — Text owned by the document; valid until it is released.

ISource2ModJSONDocument

A parsed document, and the root node it owns.

Release() invalidates every node reached through it.

IsValid

virtual bool IsValid() const = 0;

Whether the text parsed. A failed parse still returns a document, whose root reads as null, so a caller can skip the check.

GetError

virtual const char* GetError() const = 0;

The parser's complaint, or an empty string when IsValid().

Root

virtual const ISource2ModJSONValue* Root() const = 0;

Release

virtual void Release() = 0;

Frees the document and everything reached through it.

On this page