Json in UnrealScript

Our first example will demonstrate encoding and decoding of JSON data in UnrealScript. Here’s the full source code:

class JSONExample extends Object;

struct Response1
{
    var int Page;
    var array<string> Fruits;
};

struct Response2
{
    var int Page;
    var array<string> Fruits;
};

function ExampleJSON()
{
    local string JsonString;
    local JsonObject Json;
    local Response1 Res1;
    local Response2 Res2;
    local array<string> Fruits;

    // Encoding basic data types to JSON strings
    Json = new class'JsonObject';
    Json.SetBooleanValue("boolValue", true);
    Json.SetIntValue("intValue", 1);
    Json.SetFloatValue("floatValue", 2.34);
    Json.SetStringValue("stringValue", "gopher");

    `log(Json.EncodeJson());

    // Encoding arrays and maps
    Fruits.AddItem("apple");
    Fruits.AddItem("peach");
    Fruits.AddItem("pear");
    Json.SetStringArrayValue("fruits", Fruits);

    `log(Json.EncodeJson());

    // Encoding custom data types
    Res1.Page = 1;
    Res1.Fruits = Fruits;
    Json.SetObjectValue("response1", class'JsonObject'.static.EncodeObject(Res1));

    `log(Json.EncodeJson());

    // Decoding JSON data
    JsonString = "{\"page\": 1, \"fruits\": [\"apple\", \"peach\"]}";
    Json = class'JsonObject'.static.DecodeJson(JsonString);

    Res2.Page = Json.GetIntValue("page");
    Json.GetStringArrayValue("fruits", Res2.Fruits);

    `log("Page: " $ Res2.Page);
    `log("First fruit: " $ Res2.Fruits[0]);
}

This example demonstrates basic JSON operations in UnrealScript:

  1. We start by defining two structs, Response1 and Response2, which we’ll use to demonstrate encoding and decoding of custom types.

  2. The ExampleJSON function contains our main logic:

    • We create a new JsonObject to handle JSON operations.

    • We encode basic data types (boolean, integer, float, string) into JSON.

    • We demonstrate encoding arrays by creating an array of fruits and adding it to the JSON object.

    • We show how to encode custom data types by creating a Response1 object and encoding it into JSON.

    • Finally, we demonstrate JSON decoding by parsing a JSON string into a Response2 object.

  3. Throughout the example, we use `log to output the results, which is UnrealScript’s equivalent of printing to the console.

To run this example, you would typically include this class in your UnrealScript project and call the ExampleJSON function from somewhere in your game logic.

Note that UnrealScript’s JSON handling is more limited compared to Go’s. It doesn’t have built-in support for directly encoding/decoding between JSON and custom types, so we have to do more manual work to achieve similar functionality.

Also, UnrealScript doesn’t have a direct equivalent to Go’s map type, so we’ve omitted that part of the example. Instead, you can use the JsonObject to represent key-value pairs similar to a map.

Remember that exact syntax and available methods may vary depending on the specific version of UnrealScript and any custom JSON libraries you might be using in your Unreal Engine project.