Hoyt's FORK of DemoIccMAX 2.1.17.hoyt
Documentation for Hoyt's FORK of DemoIccMAX
Loading...
Searching...
No Matches
IccJsonUtil.h File Reference
#include <nlohmann/json.hpp>
#include "..\..\..\IccProfLib\IccDefs.h"
+ Include dependency graph for IccJsonUtil.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<class T >
std::string arrayToJson (T *a, int nCount)
 
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 > &vals)
 
template<typename T >
bool jsonToArray (const json &v, T *vals, int n)
 
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 &nValue)
 
template<>
bool jsonToValue (const json &j, std::string &nValue)
 
template<typename T >
bool jsonToValue (const json &j, T &nValue)
 
bool loadJsonFrom (json &j, const char *szFname)
 
bool saveJsonAs (const json &j, const char *szFname, int indent=1)
 
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 &)
 
template<>
std::string valueToJson (const char *name, double v)
 
template<>
std::string valueToJson (const char *name, float v)
 
template<typename T >
std::string valueToJson (const char *name, T v)
 
std::string valueToJsonUuid (const char *name, const char *v, bool bBinary=false)
 

Function Documentation

◆ arrayToJson()

template<class 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}

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

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:

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

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

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:

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

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/7]

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/7]

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/7]

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/7]

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/7]

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

◆ valueToJson() [6/7]

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

◆ valueToJson() [7/7]

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:

◆ valueToJsonUuid()

std::string valueToJsonUuid ( const char * name,
const char * v,
bool bBinary = false )