blob: da7d228459e957ec250cee12eda171f295dbf4a1 (
plain) (
blame)
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 "json.h"
#include "wrapper.h"
#include <library/cpp/json/json_value.h>
using namespace NJson;
void NLua::PushJsonValue(TLuaStateHolder* state, const TJsonValue& json) {
// each recursive call will explicitly push only a single element to stack relying on subcalls to reserve stack space for themselves
// I.e. for a map {"a": "b"} the first call will ensure stack space for create_table, then call PushJsonValue for the string,
// this PushJsonValue will ensure stack space for the string. Thus only a single ensure_stack at the start of the function is enough.
state->ensure_stack(1);
switch (json.GetType()) {
case JSON_UNDEFINED:
ythrow yexception() << "cannot push undefined json value";
case JSON_NULL:
state->push_nil();
break;
case JSON_BOOLEAN:
state->push_bool(json.GetBoolean());
break;
case JSON_INTEGER:
state->push_number(json.GetInteger());
break;
case JSON_UINTEGER:
state->push_number(json.GetUInteger());
break;
case JSON_DOUBLE:
state->push_number(json.GetDouble());
break;
case JSON_STRING:
state->push_string(json.GetString());
break;
case JSON_MAP:
state->create_table();
for (const auto& pair : json.GetMap()) {
PushJsonValue(state, pair.second); // Recursive call tests for stack space on its own
state->set_field(-2, pair.first.data());
}
break;
case JSON_ARRAY: {
state->create_table();
int index = 1; // lua arrays start from 1
for (const auto& element : json.GetArray()) {
PushJsonValue(state, element); // Recursive call tests for stack space on its own, no need to double check
state->rawseti(-2, index++);
}
break;
}
default:
ythrow yexception() << "Unexpected json value type";
}
}
|