OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
json_diff Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Utils/json_diff.hpp>

Collaboration diagram for json_diff:

Public Member Functions

bool load_compatible_settings (std::string const &type, std::string const &version)
int all2diff (nlohmann::json const &in, nlohmann::json &out)
int diff2all (nlohmann::json const &in, nlohmann::json &out)
int all2diff_base_reset (nlohmann::json const &base)
int diff2all_base_reset (nlohmann::json &base)
void compare_print (nlohmann::json &a, nlohmann::json &b)
bool is_need_request ()

Private Member Functions

int diff_objects (nlohmann::json const &in, nlohmann::json &out, nlohmann::json const &base)
int restore_objects (nlohmann::json const &in, nlohmann::json &out, nlohmann::json const &base)
int restore_append_objects (nlohmann::json const &in, nlohmann::json &out)
void merge_objects (nlohmann::json const &in, nlohmann::json &out)

Private Attributes

std::string printer_type
std::string printer_version = "00.00.00.00"
nlohmann::json settings_base
nlohmann::json full_message
nlohmann::json diff2all_base
nlohmann::json all2diff_base
int decode_error_count = 0

Detailed Description

Member Function Documentation

◆ all2diff()

int json_diff::all2diff ( nlohmann::json const & in,
nlohmann::json & out )
115{
116 int ret = 0;
117 if (all2diff_base.empty()) {
118 all2diff_base = in;
119 out = in;
120 BOOST_LOG_TRIVIAL(trace) << "json_c diff base reinit";
121 return 0;
122 }
123
124 ret = diff_objects(in, out, all2diff_base);
125 if (ret != 0) {
126 BOOST_LOG_TRIVIAL(trace) << "json_c diff no new info";
127 }
128 all2diff_base = in;
129 return 0;
130}
nlohmann::json all2diff_base
Definition json_diff.hpp:19
int diff_objects(nlohmann::json const &in, nlohmann::json &out, nlohmann::json const &base)
Definition json_diff.cpp:21

References all2diff_base, and diff_objects().

Here is the call graph for this function:

◆ all2diff_base_reset()

int json_diff::all2diff_base_reset ( nlohmann::json const & base)
73{
74 BOOST_LOG_TRIVIAL(trace) << "all2diff_base_reset";
75 all2diff_base = base;
76 return 0;
77}

References all2diff_base.

◆ compare_print()

void json_diff::compare_print ( nlohmann::json & a,
nlohmann::json & b )
253{
254 for (auto& e: a.items()) {
255 if (!b.contains(e.key()) ) { BOOST_LOG_TRIVIAL(trace) << "json_c compare loss " << e.key()
256 << " type: " << e.value().type_name();
257 }
258 if (e.value() != b[e.key()]) {
259 BOOST_LOG_TRIVIAL(trace) << "json_c compare not equal: key: " << e.key()
260 << " value: " << e.value();
261 BOOST_LOG_TRIVIAL(trace) << "json_c compare vs value "
262 << " vs value: " << b[e.key()];
263
264 }
265 }
266 return;
267}

◆ diff2all()

int json_diff::diff2all ( nlohmann::json const & in,
nlohmann::json & out )
233{
234 if (!diff2all_base.empty()) {
235 int ret = restore_objects(in, out, diff2all_base);
236 if (ret < 0) {
237 BOOST_LOG_TRIVIAL(trace) << "json_c restore failed";
239 return ret;
240 }
241 } else {
242 BOOST_LOG_TRIVIAL(trace) << "json_c restore base empty";
244 return -1;
245 }
246 restore_append_objects(in, out);
247 diff2all_base = out;
249 return 0;
250}
int decode_error_count
Definition json_diff.hpp:20
int restore_objects(nlohmann::json const &in, nlohmann::json &out, nlohmann::json const &base)
Definition json_diff.cpp:132
nlohmann::json diff2all_base
Definition json_diff.hpp:18
int restore_append_objects(nlohmann::json const &in, nlohmann::json &out)
Definition json_diff.cpp:189

References decode_error_count, diff2all_base, restore_append_objects(), and restore_objects().

Here is the call graph for this function:

◆ diff2all_base_reset()

int json_diff::diff2all_base_reset ( nlohmann::json & base)
278{
279 BOOST_LOG_TRIVIAL(trace) << "diff2all_base_reset";
280 full_message = base;
281 if (!settings_base.empty()) {
283 }
284 diff2all_base = base;
285 return 0;
286}
nlohmann::json settings_base
Definition json_diff.hpp:15
nlohmann::json full_message
Definition json_diff.hpp:16
void merge_objects(nlohmann::json const &in, nlohmann::json &out)
Definition json_diff.cpp:217

References diff2all_base, full_message, merge_objects(), and settings_base.

Referenced by load_compatible_settings().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ diff_objects()

int json_diff::diff_objects ( nlohmann::json const & in,
nlohmann::json & out,
nlohmann::json const & base )
private
22{
23 for (auto& el: in.items()) {
24 if (el.value().empty()) {
25 //BBL_LOG_INFO("json_c diff empty key: " << el.key());
26 continue;
27 }
28
29 if (!base.contains(el.key()) ) {
30 out[el.key()] = el.value();
31 BOOST_LOG_TRIVIAL(trace) << "json_c diff new key: " << el.key()
32 << " type: " << el.value().type_name()
33 << " value: " << el.value();
34
35 continue;
36 }
37
38 if (el.value().type() != base[el.key()].type()) {
39 out[el.key()] = el.value();
40 BOOST_LOG_TRIVIAL(trace) << "json_c diff type changed"
41 << " key: " << el.key() << " value: " << el.value().dump()
42 << " last value: " << base[el.key()].dump();
43 continue;
44 }
45
46
47 if (el.value().is_object()) {
48 json recur_out;
49 int recur_ret = diff_objects(
50 el.value(), recur_out, base[el.key()]);
51 if (recur_ret == 0) {
52 out[el.key()] = recur_out;
53 }
54 continue;
55 }
56
57 if (el.value() != base[el.key()]) {
58 out[el.key()] = el.value();
59 BOOST_LOG_TRIVIAL(trace) << "json_c diff value changed"
60 << " key: " << el.key() << " value: " << el.value().dump()
61 << " last value: " << base[el.key()].dump();
62 continue;
63 }
64 }
65
66 if (out.empty())
67 return 1;
68
69 return 0;
70}
nlohmann::json json
Definition AssembleList.cpp:42

References diff_objects().

Referenced by all2diff(), and diff_objects().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_need_request()

bool json_diff::is_need_request ( )
270{
271 if (decode_error_count > 5) {
272 return true;
273 }
274 return false;
275}

References decode_error_count.

◆ load_compatible_settings()

bool json_diff::load_compatible_settings ( std::string const & type,
std::string const & version )
80{
81 // Reload on empty type and version
82 if (!type.empty() || !version.empty()) {
83 std::string type2 = type.empty() ? printer_type : type;
84 std::string version2 = version.empty() ? printer_version : version;
85 if (type2 == printer_type && version2 == printer_version)
86 return false;
87 printer_type = type2;
88 printer_version = version2;
89 }
90 settings_base.clear();
91 std::string config_file = Slic3r::data_dir() + "/printers/" + printer_type + ".json";
92 boost::nowide::ifstream json_file(config_file.c_str());
93 try {
94 json versions;
95 if (json_file.is_open()) {
96 json_file >> versions;
97 for (auto iter = versions.begin(); iter != versions.end(); ++iter) {
98 if (iter.key() > printer_version)
99 break;
101 }
102 if (!full_message.empty())
104 return true;
105 } else {
106 BOOST_LOG_TRIVIAL(error) << "load_compatible_settings failed, file = " << config_file;
107 }
108 } catch (...) {
109 BOOST_LOG_TRIVIAL(error) << "load_compatible_settings failed, file = " << config_file;
110 }
111 return false;
112}
int diff2all_base_reset(nlohmann::json &base)
Definition json_diff.cpp:277
std::string printer_type
Definition json_diff.hpp:13
std::string printer_version
Definition json_diff.hpp:14
const std::string & data_dir()
Definition utils.cpp:365

References Slic3r::data_dir(), diff2all_base_reset(), full_message, merge_objects(), printer_type, printer_version, and settings_base.

Here is the call graph for this function:

◆ merge_objects()

void json_diff::merge_objects ( nlohmann::json const & in,
nlohmann::json & out )
private
218{
219 for (auto &el : in.items()) {
220 if (!out.contains(el.key())) {
221 out[el.key()] = el.value();
222 continue;
223 }
224 if (el.value().is_object()) {
225 merge_objects(el.value(), out[el.key()]);
226 continue;
227 }
228 out[el.key()] = el.value();
229 }
230}

References merge_objects().

Referenced by diff2all_base_reset(), load_compatible_settings(), and merge_objects().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ restore_append_objects()

int json_diff::restore_append_objects ( nlohmann::json const & in,
nlohmann::json & out )
private
190{
191 /*a new element comming, but be recoreded in base
192 need be added to output*/
193 for (auto& el: in.items()) {
194
195 if (!out.contains(el.key()) ) {
196 BOOST_LOG_TRIVIAL(trace) << "json_c append new " << el.key()
197 << " type: " << el.value().type_name()
198 << " value: " << el.value();
199 out[el.key()] = el.value();
200 continue;
201 }
202
203 if (el.value().is_object()) {
204 int recur_ret =
205 restore_append_objects(el.value(), out[el.key()]);
206 if (recur_ret != 0) {
207 BOOST_LOG_TRIVIAL(trace) << "json_c append obj failed"
208 << " key: " << el.key()
209 << " value: " << el.value();
210 return recur_ret;
211 }
212 }
213 }
214 return 0;
215}

References restore_append_objects().

Referenced by diff2all(), and restore_append_objects().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ restore_objects()

int json_diff::restore_objects ( nlohmann::json const & in,
nlohmann::json & out,
nlohmann::json const & base )
private
133{
134 json jout;
135
136 for (auto& el: base.items()) {
137 /*element not in input json,
138 use base element to restore*/
139 if (!in.contains(el.key()) ) {
140 out[el.key()] = el.value();
141 /*
142 BBL_LOG_INFO("json_c restore compressed key " << el.key()
143 << " type: " << el.value().type_name()
144 << " value: " << el.value());
145 */
146 continue;
147 }
148
149 /*element in both base and input, but json type changed
150 use input to restore*/
151 if (el.value().type() != in[el.key()].type() ){
152 out[el.key()] = in[el.key()];
153 BOOST_LOG_TRIVIAL(trace) << "json_c restore type changed"
154 << " key: " << el.key() << " value: "
155 << in[el.key()].dump()
156 << " last value: " << el.value().dump();
157 continue;
158 }
159
160 /*element in both base and input, it is a object
161 recursive until basic type*/
162 if (el.value().is_object()) {
163 json recur_out;
164 int recur_ret = restore_objects(
165 in[el.key()], recur_out, el.value());
166 if (recur_ret == 0) {
167 out[el.key()] = recur_out;
168 }
169 continue;
170 }
171
172 /*element in both base and input, but value changed
173 use input to restore*/
174 if (el.value() != in[el.key()]) {
175 out[el.key()] = in[el.key()];
176 continue;
177 }
178 /*element in both base and input,value is same
179 use base to restore*/
180 out[el.key()] = el.value();
181 }
182
183 if (out.empty())
184 return -1;
185
186 return 0;
187}

References restore_objects().

Referenced by diff2all(), and restore_objects().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ all2diff_base

nlohmann::json json_diff::all2diff_base
private

Referenced by all2diff(), and all2diff_base_reset().

◆ decode_error_count

int json_diff::decode_error_count = 0
private

Referenced by diff2all(), and is_need_request().

◆ diff2all_base

nlohmann::json json_diff::diff2all_base
private

Referenced by diff2all(), and diff2all_base_reset().

◆ full_message

nlohmann::json json_diff::full_message
private

◆ printer_type

std::string json_diff::printer_type
private

◆ printer_version

std::string json_diff::printer_version = "00.00.00.00"
private

◆ settings_base

nlohmann::json json_diff::settings_base
private

The documentation for this class was generated from the following files:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Utils/json_diff.hpp
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Utils/json_diff.cpp