Json API

The AngelScript JSON API provides robust parsing and encoding of JSON directly into native AngelScript structures. JSON objects map to dictionary objects, using standard AngelScript types.

This API mirrors the Lua JSON API and is designed to behave identically where possible.


Overview

Available functions:

Function
Description

json_parse(text, result, error)

Parse JSON text into a dictionary

json_decode(text, result, error)

Alias of json_parse

json_stringify(value, json, error)

Convert a dictionary into a JSON string

json_encode(value, json, error)

Alias of json_stringify

All functions return true on success and false on failure, with detailed error messages.


JSON → Dictionary

bool json_parse(const string &in text, dictionary &out result, string &out error)

bool json_decode(const string &in text, dictionary &out result, string &out error)

Parses a JSON string and converts it into a nested AngelScript dictionary.

Parameters

Name
Type
Description

text

string

JSON text to parse

result

dictionary

Output dictionary filled with parsed values

error

string

Error message if the function fails

JSON → AngelScript Type Mapping

JSON
AngelScript

Object {}

dictionary (recursively nested)

String "text"

string

Number 123, 4.5

double

Boolean true/false

double (1.0/0.0)

null

field omitted

Array [...]

(currently skipped)

Example


Dictionary → JSON

bool json_stringify(const dictionary &in value, string &out json, string &out error)

bool json_encode(const dictionary &in value, string &out json, string &out error)

Converts a dictionary (and any nested dictionaries) into valid JSON text.

Parameters

Name
Type
Description

value

dictionary

The object to convert into JSON

json

string

Output JSON text

error

string

Error message if the function fails

AngelScript → JSON Type Mapping

AngelScript
JSON

double

number

string

string

bool

boolean

dictionary@

JSON object

null dictionary@

null

(any other type)

error

Example

Output:


Nesting Example

JSON objects automatically map to nested dictionaries:


Null Behavior

JSON null becomes nonexistent key:

Becomes:


Unsupported Types

The AngelScript dictionary can technically store any type, but the JSON stringifier only supports:

  • double

  • string

  • bool

  • dictionary@

Anything else causes:


Arrays

JSON arrays ([ ... ]) are currently skipped. If you need them, support can be added using array<T> or CScriptArray.


Full Roundtrip Example


Full API Test


Summary of Functions

Function
Description

json_parse(text, result, error)

Parse JSON into dictionary

json_decode(text, result, error)

Alias of parse

json_stringify(value, json, error)

Convert dictionary to JSON

json_encode(value, json, error)

Alias of stringify

Last updated