forked from pure-data/pure-data
/
ceammc_json.cpp
106 lines (89 loc) · 3.65 KB
/
ceammc_json.cpp
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
100
101
102
103
104
105
106
/*****************************************************************************
* Copyright 2020 Serge Poltavsky. All rights reserved.
*
* This file may be distributed under the terms of GNU Public License version
* 3 (GPL v3) as defined by the Free Software Foundation (FSF). A copy of the
* license should have been included with this file, or the project in which
* this file belongs to. You may also find the details of GPL v3 at:
* http://www.gnu.org/licenses/gpl-3.0.txt
*
* If you have any questions regarding the use of this file, feel free to
* contact the author of this file, or the owner of the project in which
* this file belongs to.
*****************************************************************************/
#include "ceammc_json.h"
#include "datatype_dict.h"
#include "datatype_mlist.h"
#include "datatype_string.h"
#include "json/json.hpp"
namespace ceammc {
namespace json {
static nlohmann::json to_json_struct(const Atom& a, const JsonWriteOpts& opt);
static nlohmann::json to_json_struct(const AtomList& l, const JsonWriteOpts& opt);
static nlohmann::json to_json_struct(const DataTypeDict& dict, const JsonWriteOpts& opt);
static nlohmann::json to_json_struct(const DataTypeString& str, const JsonWriteOpts& opt);
static nlohmann::json to_json_struct(const AtomList& l, const JsonWriteOpts& opt)
{
nlohmann::json arr = nlohmann::json::array();
for (const Atom& a : l)
arr.push_back(to_json_struct(a, opt));
return arr;
}
static nlohmann::json to_json_struct(const DataTypeDict& dict, const JsonWriteOpts& opt)
{
auto obj = nlohmann::json::object();
for (auto& kv : dict) {
auto key = kv.first->s_name;
auto& value = kv.second;
if (opt.compressSingleList && value.isA<Atom>())
obj[key] = to_json_struct(value.asT<Atom>(), opt);
else
obj[key] = to_json_struct(value, opt);
}
return obj;
}
static nlohmann::json to_json_struct(const DataTypeString& str, const JsonWriteOpts& opt)
{
return nlohmann::json(str.str());
}
static nlohmann::json to_json_struct(const Atom& a, const JsonWriteOpts& opt)
{
if (a.isSymbol())
return nlohmann::json(a.asSymbol()->s_name);
else if (a.isInteger())
return nlohmann::json(a.asInt());
else if (a.isFloat())
return nlohmann::json(a.asFloat());
else if (a.isA<DataTypeMList>())
return to_json_struct(a.asD<DataTypeMList>()->data(), opt);
else if (a.isA<DataTypeString>())
return to_json_struct(*a.asD<DataTypeString>(), opt);
else if (a.isA<DataTypeDict>())
return to_json_struct(*a.asD<DataTypeDict>(), opt);
else if (a.isData())
return nlohmann::json::parse(a.asData()->toJsonString());
else
return nlohmann::json();
}
std::string to_json_string(const Atom& a, const JsonWriteOpts& opt)
{
return to_json_struct(a, opt).dump(opt.indent);
}
std::string to_json_string(const AtomList& l, const JsonWriteOpts& opt)
{
return to_json_struct(l, opt).dump(opt.indent);
}
std::string to_json_string(const DataTypeString& str, const JsonWriteOpts& opt)
{
return to_json_struct(str, opt).dump(opt.indent);
}
std::string to_json_string(const DataTypeMList& ml, const JsonWriteOpts& opt)
{
return to_json_struct(ml.data(), opt).dump(opt.indent);
}
std::string to_json_string(const DataTypeDict& dict, const JsonWriteOpts& opt)
{
return to_json_struct(dict, opt).dump(opt.indent);
}
}
}