Hoyt's FORK of DemoIccMAX 2.1.17.hoyt
Documentation for Hoyt's FORK of DemoIccMAX
Loading...
Searching...
No Matches
IccJsonUtil.cpp File Reference
#include "IccJsonUtil.h"
#include "IccUtil.h"
#include <stdio.h>
+ 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< icInt32Number > (const json &, icInt32Number &)
 
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 &)
 
template bool jsonToValue< unsigned int > (const json &, unsigned 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)
 
template std::string valueToJson< int > (const char *, int)
 
template std::string valueToJson< unsigned int > (const char *, unsigned int)
 

Function Documentation

◆ arrayToJson()

template<typename T >
std::string arrayToJson ( T * a,
int nCount )

Definition at line 76 of file IccJsonUtil.cpp.

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}

◆ 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)

Definition at line 125 of file IccJsonUtil.cpp.

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}

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 )

Definition at line 271 of file IccJsonUtil.cpp.

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}

◆ jsonToArray() [2/3]

template<typename T >
bool jsonToArray ( const json & v,
std::vector< T > & a )

Definition at line 312 of file IccJsonUtil.cpp.

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}

◆ jsonToArray() [3/3]

template<typename T >
bool jsonToArray ( const json & v,
T * vals,
int n )

Definition at line 242 of file IccJsonUtil.cpp.

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}

Referenced by CIccCfgDataEntry::fromJson().

+ Here is the caller graph for this function:

◆ 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 )

Definition at line 358 of file IccJsonUtil.cpp.

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}
icArraySignature sig
bool jsonToValue(const json &j, T &nValue)
icUInt32Number icGetSigVal(const icChar *pBuf)
Definition IccUtil.cpp:1258
icColorSpaceSignature
Color Space Signatures.
#define icSigUnknownData

References icGetSigVal(), icSigUnknownData, jsonToValue(), and sig.

Referenced by CIccCfgColorData::fromJson(), and CIccCfgDataApply::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 )

Definition at line 346 of file IccJsonUtil.cpp.

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}

◆ jsonToList()

bool jsonToList ( const json & v,
std::list< std::string > & vals )

Definition at line 293 of file IccJsonUtil.cpp.

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}

Referenced by CIccCfgDataEntry::fromJson().

+ Here is the caller graph for this function:

◆ jsonToString()

bool jsonToString ( const json & j,
std::string & value )

Definition at line 199 of file IccJsonUtil.cpp.

200{
201 if (j.is_string()) {
202 value = j.get<std::string>();
203 return true;
204 }
205
206 return false;
207}

Referenced by jsonToValue().

+ Here is the caller graph for this function:

◆ jsonToValue() [1/3]

template<>
bool jsonToValue ( const json & j,
bool & value )

Definition at line 210 of file IccJsonUtil.cpp.

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}

◆ jsonToValue() [2/3]

template<>
bool jsonToValue ( const json & j,
std::string & value )

Definition at line 236 of file IccJsonUtil.cpp.

237{
238 return jsonToString(j, value);
239}
bool jsonToString(const json &j, std::string &value)

References jsonToString().

+ Here is the call graph for this function:

◆ jsonToValue() [3/3]

template<typename T >
bool jsonToValue ( const json & j,
T & nValue )

Definition at line 176 of file IccJsonUtil.cpp.

177{
178 if (j.is_number()) {
179 nValue = j.get<T>();
180 return true;
181 }
182
183 return false;
184}

Referenced by jsonToColorSpace().

+ 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< icInt32Number >()

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

◆ 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 &  )

◆ jsonToValue< unsigned int >()

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

◆ loadJsonFrom()

bool loadJsonFrom ( json & j,
const char * szFname )

Definition at line 399 of file IccJsonUtil.cpp.

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}

Referenced by main(), and main().

+ Here is the caller graph for this function:

◆ saveJsonAs()

bool saveJsonAs ( const json & j,
const char * szFname,
int indent )

Definition at line 372 of file IccJsonUtil.cpp.

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}

Referenced by main().

+ Here is the caller graph for this function:

◆ valueToJson() [1/5]

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

Definition at line 154 of file IccJsonUtil.cpp.

155{
156 std::string str = std::string("\"") + name + "\": \"" + fixJsonString(v) + "\"";
157
158 return str;
159}
std::string fixJsonString(const char *v)

References fixJsonString().

+ Here is the call graph for this function:

◆ valueToJson() [2/5]

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

Definition at line 146 of file IccJsonUtil.cpp.

147{
148 std::string str = std::string("\"") + name + "\": \"" + fixJsonString(v) + "\"";
149
150 return str;
151}

References fixJsonString().

+ Here is the call graph for this function:

◆ valueToJson() [3/5]

std::string valueToJson ( const char * name,
const char * v,
bool & bPreviousLine )

Definition at line 161 of file IccJsonUtil.cpp.

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}
std::string valueToJson(const char *name, T v)

References valueToJson().

+ Here is the call graph for this function:

◆ valueToJson() [4/5]

template<>
std::string valueToJson ( const char * name,
const std::string & v )

Definition at line 139 of file IccJsonUtil.cpp.

140{
141 std::string str = std::string("\"") + name + "\": \"" + fixJsonString(v.c_str()) + "\"";
142 return str;
143}

References fixJsonString().

+ Here is the call graph for this function:

◆ valueToJson() [5/5]

template<typename T >
std::string valueToJson ( const char * name,
T v )

Definition at line 104 of file IccJsonUtil.cpp.

105{
106 std::string str = std::string("\"") + name + "\": " + std::to_string(v);
107
108 return str;
109}

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  )

◆ valueToJson< int >()

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

◆ valueToJson< unsigned int >()

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