-
Notifications
You must be signed in to change notification settings - Fork 80
/
fnc_encodeJSON.sqf
99 lines (76 loc) · 2.18 KB
/
fnc_encodeJSON.sqf
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "\x\alive\addons\x_lib\script_component.hpp"
/* ----------------------------------------------------------------------------
Function: ALiVE_fnc_encodeJSON
Description:
Encodes any SQF value to a JSON string.
Parameters:
SQF data [any]
Returns:
JSON [string]
Attributes:
N/A
Examples:
N/A
See Also:
Author:
Naught
---------------------------------------------------------------------------- */
if (isNil "_this") exitWith {"undefined"}; // Return
switch (typeName _this) do
{
case "SCALAR": {format["%1", _this]}; // Return
case "BOOL": {format["%1", _this]}; // Return
case "OBJECT":
{
if (isNull _this) exitWith {"null"}; // Return
'{' + // Return object format
'"type": "object",' +
'"netId": ' + (netId _this) +
'}'
};
case "GROUP":
{
if (isNull _this) exitWith {"null"}; // Return
'{' + // Return object format
'"type": "group",' +
'"netId": ' + (netId _this) +
'}'
};
case "STRING":
{
private ["_output"];
_strArr = toArray(_this);
if (22 in _strArr) then // Escape double quotes (UTF-8 )
{
private ["_output", "_offset"];
_output = [22];
_offset = 1;
{ // forEach
if (_x in [22,92]) then
{
_output set [(_forEachIndex + _offset), 92]; // Escape with backslash (UTF-8 \)
_offset = _offset + 1;
};
_output set [(_forEachIndex + _offset), _x];
} forEach toArray(_this);
_output pushback 22;
toString(_output) // Return
}
else
{
("""" + _this + """") // Return
};
};
case "ARRAY":
{
private ["_output"];
_output = "[";
{ // forEach
if (_forEachIndex > 0) then {_output = _output + ","};
_output = _output + (_x call ALiVE_fnc_encodeJSON);
} forEach _this;
(_output + "]") // Return
};
// For all other types, just convert to string
default {str(_this) call ALiVE_fnc_encodeJSON};
};