IccMAX 2.1.27
Color Profile Tools
Loading...
Searching...
No Matches
IccJsonUtil.cpp File Reference
#include "IccJsonUtil.h"
#include "IccUtil.h"
#include <stdio.h>
#include <string>
#include <sstream>
+ Include dependency graph for IccJsonUtil.cpp:

Go to the source code of this file.

Functions

template<typename T >
std::string arrayToJson (T *a, int nCount)
 
template std::string arrayToJson< icInt16Number > (icInt16Number *, int)
 
template std::string arrayToJson< icInt32Number > (icInt32Number *, int)
 
template std::string arrayToJson< icInt64Number > (icInt64Number *, int)
 
template std::string arrayToJson< icInt8Number > (icInt8Number *, int)
 
template std::string arrayToJson< icUInt16Number > (icUInt16Number *, int)
 
template std::string arrayToJson< icUInt32Number > (icUInt32Number *, int)
 
template std::string arrayToJson< icUInt64Number > (icUInt64Number *, int)
 
template std::string arrayToJson< icUInt8Number > (icUInt8Number *, int)
 
std::string fixJsonString (const char *v)
 
bool jsonToArray (const json &v, std::vector< std::string > &vals)
 
template<typename T >
bool jsonToArray (const json &v, std::vector< T > &a)
 
template<typename T >
bool jsonToArray (const json &v, T *vals, int n)
 
template bool jsonToArray< double > (const json &, double *, int)
 
template bool jsonToArray< double > (const json &, std::vector< double > &)
 
template bool jsonToArray< float > (const json &, float *, int)
 
template bool jsonToArray< float > (const json &, std::vector< float > &)
 
template bool jsonToArray< icInt16Number > (const json &, icInt16Number *, int)
 
template bool jsonToArray< icInt16Number > (const json &, std::vector< icInt16Number > &)
 
template bool jsonToArray< icInt32Number > (const json &, icInt32Number *, int)
 
template bool jsonToArray< icInt32Number > (const json &, std::vector< icInt32Number > &)
 
template bool jsonToArray< icInt64Number > (const json &, icInt64Number *, int)
 
template bool jsonToArray< icInt64Number > (const json &, std::vector< icInt64Number > &)
 
template bool jsonToArray< icInt8Number > (const json &, icInt8Number *, int)
 
template bool jsonToArray< icInt8Number > (const json &, std::vector< icInt8Number > &)
 
template bool jsonToArray< icUInt16Number > (const json &, icUInt16Number *, int)
 
template bool jsonToArray< icUInt16Number > (const json &, std::vector< icUInt16Number > &)
 
template bool jsonToArray< icUInt32Number > (const json &, icUInt32Number *, int)
 
template bool jsonToArray< icUInt32Number > (const json &, std::vector< icUInt32Number > &)
 
template bool jsonToArray< icUInt64Number > (const json &, icUInt64Number *, int)
 
template bool jsonToArray< icUInt64Number > (const json &, std::vector< icUInt64Number > &)
 
template bool jsonToArray< icUInt8Number > (const json &, icUInt8Number *, int)
 
template bool jsonToArray< icUInt8Number > (const json &, std::vector< icUInt8Number > &)
 
bool jsonToColorSpace (const json &j, icColorSpaceSignature &sig)
 
bool jsonToCStr (const json &j, char *str, int nSize)
 
bool jsonToList (const json &v, std::list< std::string > &vals)
 
bool jsonToString (const json &j, std::string &value)
 
template<>
bool jsonToValue (const json &j, bool &value)
 
template<>
bool jsonToValue (const json &j, std::string &value)
 
template<typename T >
bool jsonToValue (const json &j, T &nValue)
 
template bool jsonToValue< double > (const json &, double &)
 
template bool jsonToValue< float > (const json &, float &)
 
template bool jsonToValue< icInt16Number > (const json &, icInt16Number &)
 
template bool jsonToValue< icInt64Number > (const json &, icInt64Number &)
 
template bool jsonToValue< icInt8Number > (const json &, icInt8Number &)
 
template bool jsonToValue< icUInt16Number > (const json &, icUInt16Number &)
 
template bool jsonToValue< icUInt32Number > (const json &, icUInt32Number &)
 
template bool jsonToValue< icUInt64Number > (const json &, icUInt64Number &)
 
template bool jsonToValue< icUInt8Number > (const json &, icUInt8Number &)
 
template bool jsonToValue< int > (const json &, int &)
 
bool loadJsonFrom (json &j, const char *szFname)
 
bool saveJsonAs (const json &j, const char *szFname, int indent)
 
template<>
std::string valueToJson (const char *name, char *v)
 
template<>
std::string valueToJson (const char *name, const char *v)
 
std::string valueToJson (const char *name, const char *v, bool &bPreviousLine)
 
template<>
std::string valueToJson (const char *name, const std::string &v)
 
template<typename T >
std::string valueToJson (const char *name, T v)
 
template std::string valueToJson< double > (const char *, double)
 
template std::string valueToJson< float > (const char *, float)
 
template std::string valueToJson< icInt16Number > (const char *, icInt16Number)
 
template std::string valueToJson< icInt32Number > (const char *, icInt32Number)
 
template std::string valueToJson< icInt64Number > (const char *, icInt64Number)
 
template std::string valueToJson< icInt8Number > (const char *, icInt8Number)
 
template std::string valueToJson< icUInt16Number > (const char *, icUInt16Number)
 
template std::string valueToJson< icUInt32Number > (const char *, icUInt32Number)
 
template std::string valueToJson< icUInt64Number > (const char *, icUInt64Number)
 
template std::string valueToJson< icUInt8Number > (const char *, icUInt8Number)
 

Function Documentation

◆ arrayToJson()

template<typename T >
std::string arrayToJson ( T *  a,
int  nCount 
)
80{
81 std::string str, num;
82 str += "[ ";
83
84 for (int i = 0; i < nCount; i++) {
85 if (i)
86 str += ", ";
87
88 num = std::to_string(a[i]);
89
90 str += num;
91 }
92 str += " ]";
93
94 return str;
95}

◆ arrayToJson< icInt16Number >()

template std::string arrayToJson< icInt16Number > ( icInt16Number ,
int   
)

◆ arrayToJson< icInt32Number >()

template std::string arrayToJson< icInt32Number > ( icInt32Number ,
int   
)

◆ arrayToJson< icInt64Number >()

template std::string arrayToJson< icInt64Number > ( icInt64Number ,
int   
)

◆ arrayToJson< icInt8Number >()

template std::string arrayToJson< icInt8Number > ( icInt8Number ,
int   
)

◆ arrayToJson< icUInt16Number >()

template std::string arrayToJson< icUInt16Number > ( icUInt16Number ,
int   
)

◆ arrayToJson< icUInt32Number >()

template std::string arrayToJson< icUInt32Number > ( icUInt32Number ,
int   
)

◆ arrayToJson< icUInt64Number >()

template std::string arrayToJson< icUInt64Number > ( icUInt64Number ,
int   
)

◆ arrayToJson< icUInt8Number >()

template std::string arrayToJson< icUInt8Number > ( icUInt8Number ,
int   
)

◆ fixJsonString()

std::string fixJsonString ( const char *  v)
134{
135 std::string rv;
136 for (; *v; v++) {
137 if (*v == '\\')
138 rv += "\\\\";
139 else
140 rv += *v;
141 }
142
143 return rv;
144}

Referenced by valueToJson(), valueToJson(), and valueToJson().

+ Here is the caller graph for this function:

◆ jsonToArray() [1/3]

bool jsonToArray ( const json v,
std::vector< std::string > &  vals 
)
293{
294 if (v.is_array()) {
295 size_t nSize = v.size();
296 int nValid = 0;
297 vals.resize(nSize);
298 auto e = v.begin();
299 for (int i = 0; e != v.end(); e++, i++) {
300 if (e->is_string()) {
301 vals[i] = e->get<std::string>();
302 nValid++;
303 }
304 else {
305 vals[i] = "";
306 }
307 }
308 return nValid == nSize;
309 }
310
311 return false;
312}

◆ jsonToArray() [2/3]

template<typename T >
bool jsonToArray ( const json v,
std::vector< T > &  a 
)
334{
335 if (v.is_array()) {
336 size_t nSize = v.size();
337 int nValid = 0;
338 a.resize(nSize);
339 auto e = v.begin();
340 for (int i = 0; e != v.end(); e++, i++) {
341 if (e->is_number()) {
342 a[i] = e->get<T>();
343 nValid++;
344 }
345 else {
346 a[i] = 0;
347 }
348 }
349 return nValid == v.size();
350 }
351
352 return false;
353}

Referenced by CIccCfgDataEntry::fromJson().

+ Here is the caller graph for this function:

◆ jsonToArray() [3/3]

template<typename T >
bool jsonToArray ( const json v,
T *  vals,
int  n 
)
264{
265 if (v.is_array()) {
266 int nValid = 0;
267 auto e = v.begin();
268 for (int i = 0; e != v.end() && i < n; e++, i++) {
269 if (e->is_number()) {
270 vals[i] = e->get<T>();
271 nValid++;
272 }
273 }
274 return nValid == v.size();
275 }
276
277 return false;
278}

◆ jsonToArray< double >() [1/2]

template bool jsonToArray< double > ( const json ,
double *  ,
int   
)

◆ jsonToArray< double >() [2/2]

template bool jsonToArray< double > ( const json ,
std::vector< double > &   
)

◆ jsonToArray< float >() [1/2]

template bool jsonToArray< float > ( const json ,
float *  ,
int   
)

◆ jsonToArray< float >() [2/2]

template bool jsonToArray< float > ( const json ,
std::vector< float > &   
)

◆ jsonToArray< icInt16Number >() [1/2]

template bool jsonToArray< icInt16Number > ( const json ,
icInt16Number ,
int   
)

◆ jsonToArray< icInt16Number >() [2/2]

template bool jsonToArray< icInt16Number > ( const json ,
std::vector< icInt16Number > &   
)

◆ jsonToArray< icInt32Number >() [1/2]

template bool jsonToArray< icInt32Number > ( const json ,
icInt32Number ,
int   
)

◆ jsonToArray< icInt32Number >() [2/2]

template bool jsonToArray< icInt32Number > ( const json ,
std::vector< icInt32Number > &   
)

◆ jsonToArray< icInt64Number >() [1/2]

template bool jsonToArray< icInt64Number > ( const json ,
icInt64Number ,
int   
)

◆ jsonToArray< icInt64Number >() [2/2]

template bool jsonToArray< icInt64Number > ( const json ,
std::vector< icInt64Number > &   
)

◆ jsonToArray< icInt8Number >() [1/2]

template bool jsonToArray< icInt8Number > ( const json ,
icInt8Number ,
int   
)

◆ jsonToArray< icInt8Number >() [2/2]

template bool jsonToArray< icInt8Number > ( const json ,
std::vector< icInt8Number > &   
)

◆ jsonToArray< icUInt16Number >() [1/2]

template bool jsonToArray< icUInt16Number > ( const json ,
icUInt16Number ,
int   
)

◆ jsonToArray< icUInt16Number >() [2/2]

template bool jsonToArray< icUInt16Number > ( const json ,
std::vector< icUInt16Number > &   
)

◆ jsonToArray< icUInt32Number >() [1/2]

template bool jsonToArray< icUInt32Number > ( const json ,
icUInt32Number ,
int   
)

◆ jsonToArray< icUInt32Number >() [2/2]

template bool jsonToArray< icUInt32Number > ( const json ,
std::vector< icUInt32Number > &   
)

◆ jsonToArray< icUInt64Number >() [1/2]

template bool jsonToArray< icUInt64Number > ( const json ,
icUInt64Number ,
int   
)

◆ jsonToArray< icUInt64Number >() [2/2]

template bool jsonToArray< icUInt64Number > ( const json ,
std::vector< icUInt64Number > &   
)

◆ jsonToArray< icUInt8Number >() [1/2]

template bool jsonToArray< icUInt8Number > ( const json ,
icUInt8Number ,
int   
)

◆ jsonToArray< icUInt8Number >() [2/2]

template bool jsonToArray< icUInt8Number > ( const json ,
std::vector< icUInt8Number > &   
)

◆ jsonToColorSpace()

bool jsonToColorSpace ( const json j,
icColorSpaceSignature sig 
)
380{
381 std::string str;
382 if (jsonToValue(j, str)) {
383 if (str.size())
384 sig = (icColorSpaceSignature)icGetSigVal(str.c_str());
385 else
386 sig = icSigUnknownData;
387
388 return true;
389 }
390 return false;
391}
icColorSpaceSignature
Definition icProfileHeader.h:843
#define icSigUnknownData
Definition icProfileHeader.h:903
ICCPROFLIB_API icUInt32Number icGetSigVal(const icChar *pBuf)
Definition IccUtil.cpp:1258
bool jsonToValue(const json &j, T &nValue)
Definition IccJsonUtil.cpp:200

References icGetSigVal(), and jsonToValue().

Referenced by CIccCfgDataApply::fromJson(), and CIccCfgColorData::fromJson().

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

◆ jsonToCStr()

bool jsonToCStr ( const json j,
char *  str,
int  nSize 
)
368{
369 if (j.is_string() && nSize>0) {
370 std::string val = j.get<std::string>();
371 strncpy(str, val.c_str(), nSize);
372 str[nSize - 1] = 0;
373 return true;
374 }
375
376 return false;
377}

◆ jsonToList()

bool jsonToList ( const json v,
std::list< std::string > &  vals 
)
315{
316 if (v.is_array()) {
317 size_t nSize = v.size();
318 int nValid = 0;
319 auto e = v.begin();
320 for (int i = 0; e != v.end(); e++, i++) {
321 if (e->is_string()) {
322 vals.push_back(e->get<std::string>());
323 nValid++;
324 }
325 }
326 return nValid == nSize;
327 }
328
329 return false;
330}

Referenced by CIccCfgDataEntry::fromJson().

+ Here is the caller graph for this function:

◆ jsonToString()

bool jsonToString ( const json j,
std::string &  value 
)
221{
222 if (j.is_string()) {
223 value = j.get<std::string>();
224 return true;
225 }
226
227 return false;
228}

Referenced by jsonToValue().

+ Here is the caller graph for this function:

◆ jsonToValue() [1/3]

template<>
bool jsonToValue ( const json j,
bool &  value 
)
232{
233 if (j.is_boolean()) {
234 value = j.get<bool>();
235 return true;
236 }
237 else if (j.is_number_float()) {
238 // don't want to check for 0 due to floating point messiness. If the value is greater than 0.5, then
239 // it's definitely not 0.
240 value = j.get<float>() > 0.5;
241 return true;
242 }
243 else if (j.is_number_integer()) {
244 value = j.get<int>() != 0;
245 return true;
246 }
247 else if (j.is_number_unsigned()) {
248 value = j.get<unsigned>() != 0;
249 return true;
250 }
251
252 return false;
253}

◆ jsonToValue() [2/3]

template<>
bool jsonToValue ( const json j,
std::string &  value 
)
258{
259 return jsonToString(j, value);
260}
bool jsonToString(const json &j, std::string &value)
Definition IccJsonUtil.cpp:220

References jsonToString().

+ Here is the call graph for this function:

◆ jsonToValue() [3/3]

template<typename T >
bool jsonToValue ( const json j,
T &  nValue 
)
201{
202 if (j.is_number()) {
203 nValue = j.get<T>();
204 return true;
205 }
206
207 return false;
208}

Referenced by CIccCfgDataApply::fromJson(), CIccCfgImageApply::fromJson(), CIccCfgCreateLink::fromJson(), CIccCfgProfile::fromJson(), CIccCfgDataEntry::fromJson(), CIccCfgColorData::fromJson(), jsonToColorSpace(), jsonToValue(), jsonToValue(), and jsonToValue().

+ Here is the caller graph for this function:

◆ jsonToValue< double >()

template bool jsonToValue< double > ( const json ,
double &   
)

◆ jsonToValue< float >()

template bool jsonToValue< float > ( const json ,
float &   
)

◆ jsonToValue< icInt16Number >()

template bool jsonToValue< icInt16Number > ( const json ,
icInt16Number  
)

◆ jsonToValue< icInt64Number >()

template bool jsonToValue< icInt64Number > ( const json ,
icInt64Number  
)

◆ jsonToValue< icInt8Number >()

template bool jsonToValue< icInt8Number > ( const json ,
icInt8Number  
)

◆ jsonToValue< icUInt16Number >()

template bool jsonToValue< icUInt16Number > ( const json ,
icUInt16Number  
)

◆ jsonToValue< icUInt32Number >()

template bool jsonToValue< icUInt32Number > ( const json ,
icUInt32Number  
)

◆ jsonToValue< icUInt64Number >()

template bool jsonToValue< icUInt64Number > ( const json ,
icUInt64Number  
)

◆ jsonToValue< icUInt8Number >()

template bool jsonToValue< icUInt8Number > ( const json ,
icUInt8Number  
)

◆ jsonToValue< int >()

template bool jsonToValue< int > ( const json ,
int &   
)

◆ loadJsonFrom()

bool loadJsonFrom ( json j,
const char *  szFname 
)
421{
422 bool rv = false;
423 j.clear();
424
425 FILE* f = fopen(szFname, "rb");
426 if (f) {
427 fseek(f, 0, SEEK_END);
428 unsigned long flen = ftell(f);
429 fseek(f, 0, SEEK_SET);
430 char* buf = (char*)malloc(flen+1);
431
432 if (buf) {
433 buf[flen] = 0;
434 if (fread(buf, 1, flen, f) == flen) {
435 try {
436 j = json::parse(buf);
437 rv = true;
438 }
439 catch (...) {
440 }
441 }
442 free(buf);
443 }
444 fclose(f);
445 }
446
447 return rv;
448}

Referenced by main(), and main().

+ Here is the caller graph for this function:

◆ saveJsonAs()

bool saveJsonAs ( const json j,
const char *  szFname,
int  indent 
)
394{
395 if (!j.is_object())
396 return false;
397
398 bool rv = false;
399
400 FILE* f;
401
402 if (szFname && szFname[0])
403 f = fopen(szFname, "wb");
404 else
405 f = stdout;
406
407 if (f) {
408 std::string str = j.dump(indent);
409
410 if (fwrite((void*)(str.c_str()), 1, (unsigned long)str.size(), f) == (long)str.size())
411 rv = true;
412
413 if (f!=stdout)
414 fclose(f);
415 }
416
417 return rv;
418}

Referenced by main().

+ Here is the caller graph for this function:

◆ valueToJson() [1/5]

template<>
std::string valueToJson ( const char *  name,
char *  v 
)
172{
173 if (!name) {
174 throw std::invalid_argument("name cannot be null");
175 }
176 if (!v) {
177 return "\"" + std::string(name) + "\": null";
178 }
179 return "\"" + std::string(name) + "\": \"" + fixJsonString(v) + "\"";
180}
std::string fixJsonString(const char *v)
Definition IccJsonUtil.cpp:133

References fixJsonString().

+ Here is the call graph for this function:

◆ valueToJson() [2/5]

template<>
std::string valueToJson ( const char *  name,
const char *  v 
)
159{
160 if (!name) {
161 throw std::invalid_argument("name cannot be null");
162 }
163 if (!v) {
164 return "\"" + std::string(name) + "\": null";
165 }
166 return "\"" + std::string(name) + "\": \"" + fixJsonString(v) + "\"";
167}

References fixJsonString().

+ Here is the call graph for this function:

◆ valueToJson() [3/5]

std::string valueToJson ( const char *  name,
const char *  v,
bool &  bPreviousLine 
)
184{
185 if (!name) {
186 throw std::invalid_argument("name cannot be null");
187 }
188 if (v && *v) {
189 std::string str = valueToJson(name, v);
190 if (bPreviousLine) {
191 return ", " + str;
192 }
193 bPreviousLine = true;
194 return str;
195 }
196 return {};
197}
std::string valueToJson(const char *name, T v)
Definition IccJsonUtil.cpp:110

References valueToJson().

+ Here is the call graph for this function:

◆ valueToJson() [4/5]

template<>
std::string valueToJson ( const char *  name,
const std::string &  v 
)
149{
150 if (!name) {
151 throw std::invalid_argument("name cannot be null");
152 }
153 return "\"" + std::string(name) + "\": \"" + fixJsonString(v.c_str()) + "\"";
154}

References fixJsonString().

+ Here is the call graph for this function:

◆ valueToJson() [5/5]

template<typename T >
std::string valueToJson ( const char *  name,
v 
)
111{
112 // Safeguard for nullptr name
113 if (!name) {
114 throw std::invalid_argument("name cannot be null");
115 }
116
117 return std::string("\"") + name + "\": " + std::to_string(v);
118}

Referenced by valueToJson().

+ Here is the caller graph for this function:

◆ valueToJson< double >()

template std::string valueToJson< double > ( const char *  ,
double   
)

◆ valueToJson< float >()

template std::string valueToJson< float > ( const char *  ,
float   
)

◆ valueToJson< icInt16Number >()

template std::string valueToJson< icInt16Number > ( const char *  ,
icInt16Number   
)

◆ valueToJson< icInt32Number >()

template std::string valueToJson< icInt32Number > ( const char *  ,
icInt32Number   
)

◆ valueToJson< icInt64Number >()

template std::string valueToJson< icInt64Number > ( const char *  ,
icInt64Number   
)

◆ valueToJson< icInt8Number >()

template std::string valueToJson< icInt8Number > ( const char *  ,
icInt8Number   
)

◆ valueToJson< icUInt16Number >()

template std::string valueToJson< icUInt16Number > ( const char *  ,
icUInt16Number   
)

◆ valueToJson< icUInt32Number >()

template std::string valueToJson< icUInt32Number > ( const char *  ,
icUInt32Number   
)

◆ valueToJson< icUInt64Number >()

template std::string valueToJson< icUInt64Number > ( const char *  ,
icUInt64Number   
)

◆ valueToJson< icUInt8Number >()

template std::string valueToJson< icUInt8Number > ( const char *  ,
icUInt8Number   
)