4
Copyright (c) 2021 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo
9
#include "simodo/variable/json/Serialization.h"
10
#include "simodo/variable/json/parser/JsonRdp.h"
11
#include "simodo/inout/convert/functions.h"
12
#include "simodo/inout/token/FileStream.h"
13
#include "simodo/inout/token/RefBufferStream.h"
14
#include "simodo/variable/json/Rpc.h"
19
namespace simodo::variable
23
class StringReporter: public inout::Reporter_abstract
25
mutable std::string _message;
28
virtual void report(const inout::SeverityLevel level,
29
const inout::Location & ,
30
const std::string & briefly,
31
const std::string & atlarge) const override
33
_message += inout::getSeverityLevelName(level) + briefly;
35
_message += " (" + atlarge + ")\n";
38
std::string message() const { return _message; }
41
void printJson(const Value & value, std::ostream & out, int level, bool compress, bool skip_empty_parameters)
46
out << (value.getBool() ? "true" : "false");
49
out << value.getInt();
51
case ValueType::Float:
52
out << value.getFloat(); // clearNumberFractionalPart
54
case ValueType::String:
56
std::u16string str = value.getString();
57
inout::replaceAll(str,u"\\",u"\\\\");
58
inout::replaceAll(str,u"\"",u"\\\"");
59
out << "\"" << inout::toU8(str) << "\"";
63
// case ValueType::Undefined:
66
case ValueType::Object:
68
const std::shared_ptr<Object> object = value.getObject();
77
for(const Variable & var : object->variables()) {
78
// if (var.type() == ValueType::Null)
80
if (level <= 1 && skip_empty_parameters && var.type() == ValueType::Null)
90
for(int i=0; i < level; ++i)
94
out << "\"" << inout::toU8(var.name()) << "\" : ";
96
out << "\"" << inout::toU8(var.name()) << "\":";
98
printJson(var.value(), out, level+1, compress, skip_empty_parameters);
104
if (!compress) // -V581
105
for(int i=0; i < level-1; ++i)
111
case ValueType::Array:
113
std::shared_ptr<Array> array = value.getArray();
122
for(const Value & v : array->values())
131
for(int i=0; i < level; ++i)
134
printJson(v, out, level+1, compress, skip_empty_parameters);
140
if (!compress) // -V581
141
for(int i=0; i < level-1; ++i)
148
out << "\"unrecognizable type of value: " + getValueTypeName(value.type())+ "\"";
154
std::string loadJson(const std::string &file_name, Value &value)
156
StringReporter reporter;
157
JsonRdp parser(reporter, file_name, value);
160
bool ok = parser.parse();
163
result = reporter.message();
168
std::string loadJson(std::istream &in, Value &value, const std::string &file_name)
170
inout::InputStream stream(in);
171
StringReporter reporter;
172
JsonRdp parser(reporter, file_name, value);
175
bool ok = parser.parse(stream);
178
result = reporter.message();
183
bool saveJson(const Value &value, std::ostream &out, bool compress, bool skip_empty_parameters)
188
printJson(value, out, 1, compress, skip_empty_parameters);
195
bool saveJson(const Value &value, const std::string &file_name, bool compress, bool skip_empty_parameters)
197
std::ofstream out(file_name);
202
return saveJson(value, out, compress, skip_empty_parameters);
205
bool saveJson(const JsonRpc & rpc, std::ostream & out, bool compress, bool skip_empty_parameters)
207
return saveJson(rpc.value(), out, compress, skip_empty_parameters);
210
Value fromJson(const std::string & json_string)
212
std::istringstream is(json_string);
213
inout::InputStream input(is);
215
StringReporter reporter;
216
JsonRdp parser(reporter,{},res);
223
Value fromJson(const std::u16string & json_string)
225
return fromJson(inout::toU8(json_string));
228
std::string toJson(const Value & value, bool compress, bool skip_empty_parameters)
230
std::ostringstream os;
231
saveJson(value, os, compress, skip_empty_parameters);