aboutsummaryrefslogtreecommitdiff
path: root/src/helper.cpp
blob: bb09b2b56a744335b92f2d11028380f9ef0dc877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <rapidjson/document.h>

#include "ns_plugin.h"

#include "helper.h"

void SquirrelToJSON(
    rapidjson::Value* out_val,
    rapidjson::MemoryPoolAllocator<>& allocator,
    SQObject* obj_ptr
) {

    if (obj_ptr)
    {
        switch (obj_ptr->_Type)
        {
            case OT_BOOL:
                out_val->SetBool(obj_ptr->_VAL.asInteger);
                break;

            case OT_INTEGER:
                out_val->SetInt(obj_ptr->_VAL.asInteger);
                break;

            case OT_STRING:
                out_val->SetString(obj_ptr->_VAL.asString->_val, obj_ptr->_VAL.asString->length);
                break;

            case OT_ARRAY:
                out_val->SetArray();

                for (int i = 0; i < obj_ptr->_VAL.asArray->_usedSlots; ++i)
                {
                    rapidjson::Value n;
                    SquirrelToJSON(&n, allocator, &obj_ptr->_VAL.asArray->_values[i]);
                    out_val->PushBack(n, allocator);
                }
                break;

            case OT_TABLE:
                out_val->SetObject();

                for (int i = 0; i < obj_ptr->_VAL.asTable->_numOfNodes; ++i)
                {
                    auto node = &obj_ptr->_VAL.asTable->_nodes[i];
                    if (node->key._Type != OT_STRING)
                        continue;

                    rapidjson::Value k;
                    SquirrelToJSON(&k, allocator, &node->key);

                    rapidjson::Value v;
                    SquirrelToJSON(&v, allocator, &node->val);
                    out_val->AddMember(k, v, allocator);
                }
                break;

            default:
                break;
        }
    }
}