Hoyt's FORK of DemoIccMAX 2.1.17.hoyt
Documentation for Hoyt's FORK of DemoIccMAX
Loading...
Searching...
No Matches
IccJsonUtil.cpp
Go to the documentation of this file.
1/*
2 File: IccJsonUtil.cpp
3
4 Contains: Utility function with working with nlohmann/json objects
5
6 Version: V1
7
8 Copyright: (c) see below
9*/
10
11/*
12 * The ICC Software License, Version 0.2
13 *
14 *
15 * Copyright (c) 2003-2024 The International Color Consortium. All rights
16 * reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 *
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 *
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
28 * distribution.
29 *
30 * 3. In the absence of prior written permission, the names "ICC" and "The
31 * International Color Consortium" must not be used to imply that the
32 * ICC organization endorses or promotes products derived from this
33 * software.
34 *
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE INTERNATIONAL COLOR CONSORTIUM OR
40 * ITS CONTRIBUTING MEMBERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of the The International Color Consortium.
52 *
53 *
54 * Membership in the ICC is encouraged when this software is used for
55 * commercial purposes.
56 *
57 *
58 * For more information on The International Color Consortium, please
59 * see <http://www.color.org/>.
60 *
61 *
62 */
63
64 //////////////////////////////////////////////////////////////////////
65 // HISTORY:
66 //
67 // -Initial implementation by Max Derhak 1-11-2024
68 //
69 //////////////////////////////////////////////////////////////////////
70
71#include "IccJsonUtil.h"
72#include "IccUtil.h"
73#include <stdio.h>
74
75template <typename T>
76 std::string arrayToJson(T* a, int nCount)
77{
78 std::string str, num;
79 str += "[ ";
80
81 for (int i = 0; i < nCount; i++) {
82 if (i)
83 str += ", ";
84
85 num = std::to_string(a[i]);
86
87 str += num;
88 }
89 str += " ]";
90
91 return str;
92}
93
94template std::string arrayToJson<icUInt8Number>(icUInt8Number*, int);
95template std::string arrayToJson<icUInt16Number>(icUInt16Number*, int);
96template std::string arrayToJson<icUInt32Number>(icUInt32Number*, int);
97template std::string arrayToJson<icUInt64Number>(icUInt64Number*, int);
98template std::string arrayToJson<icInt8Number>(icInt8Number*, int);
99template std::string arrayToJson<icInt16Number>(icInt16Number*, int);
100template std::string arrayToJson<icInt32Number>(icInt32Number*, int);
101template std::string arrayToJson<icInt64Number>(icInt64Number*, int);
102
103template<typename T>
104std::string valueToJson(const char* name, T v)
105{
106 std::string str = std::string("\"") + name + "\": " + std::to_string(v);
107
108 return str;
109}
110
111template std::string valueToJson<icUInt8Number>(const char*, icUInt8Number);
112template std::string valueToJson<icUInt16Number>(const char*, icUInt16Number);
113template std::string valueToJson<icUInt32Number>(const char*, icUInt32Number);
114template std::string valueToJson<icUInt64Number>(const char*, icUInt64Number);
115template std::string valueToJson<unsigned int>(const char*, unsigned int);
116template std::string valueToJson<icInt8Number>(const char*, icInt8Number);
117template std::string valueToJson<icInt16Number>(const char*, icInt16Number);
118template std::string valueToJson<icInt32Number>(const char*, icInt32Number);
119template std::string valueToJson<icInt64Number>(const char*, icInt64Number);
120template std::string valueToJson<int>(const char*, int);
121template std::string valueToJson<float>(const char*, float);
122template std::string valueToJson<double>(const char*, double);
123
124
125std::string fixJsonString(const char* v)
126{
127 std::string rv;
128 for (; *v; v++) {
129 if (*v == '\\')
130 rv += "\\\\";
131 else
132 rv += *v;
133 }
134
135 return rv;
136}
137
138template<>
139std::string valueToJson(const char *name, const std::string &v)
140{
141 std::string str = std::string("\"") + name + "\": \"" + fixJsonString(v.c_str()) + "\"";
142 return str;
143}
144
145template<>
146std::string valueToJson(const char* name, const char* v)
147{
148 std::string str = std::string("\"") + name + "\": \"" + fixJsonString(v) + "\"";
149
150 return str;
151}
152
153template<>
154std::string valueToJson(const char* name, char* v)
155{
156 std::string str = std::string("\"") + name + "\": \"" + fixJsonString(v) + "\"";
157
158 return str;
159}
160
161std::string valueToJson(const char* name, const char* v, bool& bPreviousLine)
162{
163 std::string str;
164 if (v && *v) {
165 if (bPreviousLine)
166 str = std::string(", ") + valueToJson(name, v);
167 else
168 str = valueToJson(name, v);
169
170 bPreviousLine = true;
171 }
172 return str;
173}
174
175template <typename T>
176bool jsonToValue(const json& j, T& nValue)
177{
178 if (j.is_number()) {
179 nValue = j.get<T>();
180 return true;
181 }
182
183 return false;
184}
185
186template bool jsonToValue<icInt8Number>(const json&, icInt8Number&);
187template bool jsonToValue<int>(const json&, int&);
188template bool jsonToValue<unsigned int>(const json&, unsigned int&);
189template bool jsonToValue<icUInt8Number>(const json&, icUInt8Number&);
190template bool jsonToValue<icInt16Number>(const json&, icInt16Number&);
191template bool jsonToValue<icUInt16Number>(const json&, icUInt16Number&);
192template bool jsonToValue<icInt32Number>(const json&, icInt32Number&);
193template bool jsonToValue<icUInt32Number>(const json&, icUInt32Number&);
194template bool jsonToValue<icInt64Number>(const json&, icInt64Number&);
195template bool jsonToValue<icUInt64Number>(const json&, icUInt64Number&);
196template bool jsonToValue<float>(const json&, float&);
197template bool jsonToValue<double>(const json&, double&);
198
199bool jsonToString(const json& j, std::string& value)
200{
201 if (j.is_string()) {
202 value = j.get<std::string>();
203 return true;
204 }
205
206 return false;
207}
208
209template<>
210bool jsonToValue(const json& j, bool& value)
211{
212 if (j.is_boolean()) {
213 value = j.get<bool>();
214 return true;
215 }
216 else if (j.is_number_float()) {
217 // don't want to check for 0 due to floating point messiness. If the value is greater than 0.5, then
218 // it's definitely not 0.
219 value = j.get<float>() > 0.5;
220 return true;
221 }
222 else if (j.is_number_integer()) {
223 value = j.get<int>() != 0;
224 return true;
225 }
226 else if (j.is_number_unsigned()) {
227 value = j.get<unsigned>() != 0;
228 return true;
229 }
230
231 return false;
232}
233
234
235template<>
236bool jsonToValue(const json& j, std::string&value)
237{
238 return jsonToString(j, value);
239}
240
241template <typename T>
242bool jsonToArray(const json& v, T* vals, int n)
243{
244 if (v.is_array()) {
245 int nValid = 0;
246 auto e = v.begin();
247 for (int i = 0; e != v.end() && i < n; e++, i++) {
248 if (e->is_number()) {
249 vals[i] = e->get<T>();
250 nValid++;
251 }
252 }
253 return nValid == v.size();
254 }
255
256 return false;
257}
258
259
260template bool jsonToArray<icInt8Number>(const json&, icInt8Number*, int);
261template bool jsonToArray<icUInt8Number>(const json&, icUInt8Number*, int);
262template bool jsonToArray<icInt16Number>(const json&, icInt16Number*, int);
263template bool jsonToArray<icUInt16Number>(const json&, icUInt16Number*, int);
264template bool jsonToArray<icInt32Number>(const json&, icInt32Number*, int);
265template bool jsonToArray<icUInt32Number>(const json&, icUInt32Number*, int);
266template bool jsonToArray<icInt64Number>(const json&, icInt64Number*, int);
267template bool jsonToArray<icUInt64Number>(const json&, icUInt64Number*, int);
268template bool jsonToArray<float>(const json&, float*, int);
269template bool jsonToArray<double>(const json&, double*, int);
270
271bool jsonToArray(const json& v, std::vector<std::string>& vals)
272{
273 if (v.is_array()) {
274 size_t nSize = v.size();
275 int nValid = 0;
276 vals.resize(nSize);
277 auto e = v.begin();
278 for (int i = 0; e != v.end(); e++, i++) {
279 if (e->is_string()) {
280 vals[i] = e->get<std::string>();
281 nValid++;
282 }
283 else {
284 vals[i] = "";
285 }
286 }
287 return nValid == nSize;
288 }
289
290 return false;
291}
292
293bool jsonToList(const json& v, std::list<std::string>& vals)
294{
295 if (v.is_array()) {
296 size_t nSize = v.size();
297 int nValid = 0;
298 auto e = v.begin();
299 for (int i = 0; e != v.end(); e++, i++) {
300 if (e->is_string()) {
301 vals.push_back(e->get<std::string>());
302 nValid++;
303 }
304 }
305 return nValid == nSize;
306 }
307
308 return false;
309}
310
311template <typename T>
312bool jsonToArray(const json& v, std::vector<T>& a)
313{
314 if (v.is_array()) {
315 size_t nSize = v.size();
316 int nValid = 0;
317 a.resize(nSize);
318 auto e = v.begin();
319 for (int i = 0; e != v.end(); e++, i++) {
320 if (e->is_number()) {
321 a[i] = e->get<T>();
322 nValid++;
323 }
324 else {
325 a[i] = 0;
326 }
327 }
328 return nValid == v.size();
329 }
330
331 return false;
332}
333
334template bool jsonToArray<icInt8Number>(const json&, std::vector<icInt8Number>&);
335template bool jsonToArray<icUInt8Number>(const json&, std::vector < icUInt8Number>&);
336template bool jsonToArray<icInt16Number>(const json&, std::vector < icInt16Number>&);
337template bool jsonToArray<icUInt16Number>(const json&, std::vector < icUInt16Number>&);
338template bool jsonToArray<icInt32Number>(const json&, std::vector < icInt32Number>&);
339template bool jsonToArray<icUInt32Number>(const json&, std::vector < icUInt32Number>&);
340template bool jsonToArray<icInt64Number>(const json&, std::vector < icInt64Number>&);
341template bool jsonToArray<icUInt64Number>(const json&, std::vector < icUInt64Number>&);
342template bool jsonToArray<float>(const json&, std::vector<float>&);
343template bool jsonToArray<double>(const json&, std::vector<double>&);
344
345
346bool jsonToCStr(const json& j, char* str, int nSize)
347{
348 if (j.is_string() && nSize>0) {
349 std::string val = j.get<std::string>();
350 strncpy(str, val.c_str(), nSize);
351 str[nSize - 1] = 0;
352 return true;
353 }
354
355 return false;
356}
357
359{
360 std::string str;
361 if (jsonToValue(j, str)) {
362 if (str.size())
363 sig = (icColorSpaceSignature)icGetSigVal(str.c_str());
364 else
366
367 return true;
368 }
369 return false;
370}
371
372bool saveJsonAs(const json& j, const char* szFname, int indent)
373{
374 if (!j.is_object())
375 return false;
376
377 bool rv = false;
378
379 FILE* f;
380
381 if (szFname && szFname[0])
382 f = fopen(szFname, "wb");
383 else
384 f = stdout;
385
386 if (f) {
387 std::string str = j.dump(indent);
388
389 if (fwrite((void*)(str.c_str()), 1, (unsigned long)str.size(), f) == (long)str.size())
390 rv = true;
391
392 if (f!=stdout)
393 fclose(f);
394 }
395
396 return rv;
397}
398
399bool loadJsonFrom(json& j, const char* szFname)
400{
401 bool rv = false;
402 j.clear();
403
404 FILE* f = fopen(szFname, "rb");
405 if (f) {
406 fseek(f, 0, SEEK_END);
407 unsigned long flen = ftell(f);
408 fseek(f, 0, SEEK_SET);
409 char* buf = (char*)malloc(flen+1);
410
411 if (buf) {
412 buf[flen] = 0;
413 if (fread(buf, 1, flen, f) == flen) {
414 try {
415 j = json::parse(buf);
416 rv = true;
417 }
418 catch (...) {
419 }
420 }
421 free(buf);
422 }
423 fclose(f);
424 }
425
426 return rv;
427}
icArraySignature sig
template bool jsonToValue< icUInt16Number >(const json &, icUInt16Number &)
template std::string arrayToJson< icUInt32Number >(icUInt32Number *, int)
bool loadJsonFrom(json &j, const char *szFname)
bool jsonToArray(const json &v, T *vals, int n)
template bool jsonToValue< int >(const json &, int &)
template bool jsonToArray< icInt32Number >(const json &, icInt32Number *, int)
template std::string valueToJson< double >(const char *, double)
template bool jsonToValue< icUInt64Number >(const json &, icUInt64Number &)
template bool jsonToArray< icInt16Number >(const json &, icInt16Number *, int)
template std::string arrayToJson< icUInt16Number >(icUInt16Number *, int)
std::string valueToJson(const char *name, T v)
template std::string valueToJson< int >(const char *, int)
template std::string arrayToJson< icInt32Number >(icInt32Number *, int)
template std::string valueToJson< icUInt8Number >(const char *, icUInt8Number)
template bool jsonToValue< icInt32Number >(const json &, icInt32Number &)
bool jsonToList(const json &v, std::list< std::string > &vals)
template bool jsonToArray< icUInt8Number >(const json &, icUInt8Number *, int)
template std::string arrayToJson< icInt16Number >(icInt16Number *, int)
template std::string arrayToJson< icUInt8Number >(icUInt8Number *, int)
template bool jsonToValue< float >(const json &, float &)
template bool jsonToValue< icInt64Number >(const json &, icInt64Number &)
bool jsonToColorSpace(const json &j, icColorSpaceSignature &sig)
template bool jsonToArray< icInt8Number >(const json &, icInt8Number *, int)
std::string arrayToJson(T *a, int nCount)
template bool jsonToValue< icInt8Number >(const json &, icInt8Number &)
std::string fixJsonString(const char *v)
template std::string arrayToJson< icInt64Number >(icInt64Number *, int)
template bool jsonToArray< float >(const json &, float *, int)
template std::string arrayToJson< icUInt64Number >(icUInt64Number *, int)
template std::string valueToJson< unsigned int >(const char *, unsigned int)
bool jsonToValue(const json &j, T &nValue)
template bool jsonToValue< double >(const json &, double &)
template std::string valueToJson< float >(const char *, float)
template std::string valueToJson< icInt16Number >(const char *, icInt16Number)
template bool jsonToArray< icInt64Number >(const json &, icInt64Number *, int)
template bool jsonToArray< icUInt64Number >(const json &, icUInt64Number *, int)
template std::string valueToJson< icUInt16Number >(const char *, icUInt16Number)
template bool jsonToValue< unsigned int >(const json &, unsigned int &)
bool saveJsonAs(const json &j, const char *szFname, int indent)
template bool jsonToArray< icUInt16Number >(const json &, icUInt16Number *, int)
template std::string valueToJson< icInt32Number >(const char *, icInt32Number)
template bool jsonToArray< double >(const json &, double *, int)
template bool jsonToValue< icInt16Number >(const json &, icInt16Number &)
template std::string valueToJson< icInt64Number >(const char *, icInt64Number)
template std::string valueToJson< icInt8Number >(const char *, icInt8Number)
template bool jsonToValue< icUInt8Number >(const json &, icUInt8Number &)
template std::string arrayToJson< icInt8Number >(icInt8Number *, int)
bool jsonToCStr(const json &j, char *str, int nSize)
template std::string valueToJson< icUInt32Number >(const char *, icUInt32Number)
template bool jsonToArray< icUInt32Number >(const json &, icUInt32Number *, int)
template std::string valueToJson< icUInt64Number >(const char *, icUInt64Number)
template bool jsonToValue< icUInt32Number >(const json &, icUInt32Number &)
bool jsonToString(const json &j, std::string &value)
icUInt32Number icGetSigVal(const icChar *pBuf)
Definition IccUtil.cpp:1258
File: IccUtil.h.
unsigned int icUInt32Number
unsigned char icUInt8Number
Number definitions.
unsigned short icUInt16Number
long icInt32Number
char icInt8Number
Signed numbers.
short icInt16Number
icColorSpaceSignature
Color Space Signatures.
icUInt32Number icUInt64Number[2]
#define icSigUnknownData
icInt32Number icInt64Number[2]