Json in AngelScript

Our program demonstrates JSON encoding and decoding in AngelScript, including handling of basic data types and custom structures.

import json;

// We'll use these two classes to demonstrate encoding and
// decoding of custom types below.
class Response1 {
    int page;
    array<string> fruits;
}

// Only public properties will be encoded/decoded in JSON.
class Response2 {
    int page;
    array<string> fruits;
}

void main() {
    // First we'll look at encoding basic data types to
    // JSON strings. Here are some examples for atomic values.
    string bolB = json::encode(true);
    print(bolB);

    string intB = json::encode(1);
    print(intB);

    string fltB = json::encode(2.34);
    print(fltB);

    string strB = json::encode("gopher");
    print(strB);

    // And here are some for arrays and dictionaries, which encode
    // to JSON arrays and objects as you'd expect.
    array<string> slcD = {"apple", "peach", "pear"};
    string slcB = json::encode(slcD);
    print(slcB);

    dictionary mapD;
    mapD["apple"] = 5;
    mapD["lettuce"] = 7;
    string mapB = json::encode(mapD);
    print(mapB);

    // The JSON package can automatically encode your
    // custom data types. It will only include public
    // properties in the encoded output.
    Response1 res1D;
    res1D.page = 1;
    res1D.fruits = {"apple", "peach", "pear"};
    string res1B = json::encode(res1D);
    print(res1B);

    // AngelScript doesn't have built-in support for JSON field tags,
    // so the output will use the property names as-is.
    Response2 res2D;
    res2D.page = 1;
    res2D.fruits = {"apple", "peach", "pear"};
    string res2B = json::encode(res2D);
    print(res2B);

    // Now let's look at decoding JSON data into AngelScript
    // values. Here's an example for a generic data structure.
    string byt = '{"num":6.13,"strs":["a","b"]}';

    // We need to provide a variable where the JSON
    // package can put the decoded data. This
    // dictionary will hold a map of strings to any data types.
    dictionary dat;
    json::decode(byt, dat);
    print(dat);

    // In order to use the values in the decoded dictionary,
    // we'll need to convert them to their appropriate type.
    // For example here we convert the value in 'num' to
    // the expected double type.
    double num = double(dat["num"]);
    print(num);

    // Accessing nested data requires a series of
    // conversions.
    array<string> strs = array<string>(dat["strs"]);
    string str1 = strs[0];
    print(str1);

    // We can also decode JSON into custom data types.
    // This has the advantages of adding additional
    // type-safety to our programs.
    string str = '{"page": 1, "fruits": ["apple", "peach"]}';
    Response2 res;
    json::decode(str, res);
    print(res.page);
    print(res.fruits[0]);

    // In AngelScript, we don't have direct streaming to
    // standard output, but we can achieve similar results
    // by encoding to a string and then printing it.
    dictionary d;
    d["apple"] = 5;
    d["lettuce"] = 7;
    string encoded = json::encode(d);
    print(encoded);
}

To run the program, save it as json_example.as and use your AngelScript interpreter:

$ angelscript json_example.as
true
1
2.34
"gopher"
["apple","peach","pear"]
{"apple":5,"lettuce":7}
{"page":1,"fruits":["apple","peach","pear"]}
{"page":1,"fruits":["apple","peach","pear"]}
{"num":6.13,"strs":["a","b"]}
6.13
a
1
apple
{"apple":5,"lettuce":7}

This example covers the basics of JSON in AngelScript. Note that the exact syntax and available functions may vary depending on the specific JSON library implementation for AngelScript that you’re using.