-
-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathJsonUtils.cpp
More file actions
71 lines (65 loc) · 1.83 KB
/
Copy pathJsonUtils.cpp
File metadata and controls
71 lines (65 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*---------------------------------------------------------*\
| JsonUtils.cpp |
| |
| JSON utility functions |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include "JsonUtils.h"
bool JsonUtils::JsonGetBool(nlohmann::json& val, std::string key, bool dft)
{
if((val.contains(key)) && (val[key].type() == nlohmann::json::value_t::boolean))
{
return((bool)val[key]);
}
else
{
return(dft);
}
}
int JsonUtils::JsonGetInt(nlohmann::json& val, std::string key, int dft)
{
if((val.contains(key)) &&
((val[key].type() == nlohmann::json::value_t::number_integer) ||
(val[key].type() == nlohmann::json::value_t::number_unsigned) ||
(val[key].type() == nlohmann::json::value_t::number_float)))
{
return((int)val[key]);
}
else
{
return(dft);
}
}
std::string JsonUtils::JsonGetString(nlohmann::json& val, std::string key, std::string dft, bool allow_empty)
{
if((val.contains(key)) && (val[key].type() == nlohmann::json::value_t::string))
{
std::string ret_val = (std::string)val[key];
if((!allow_empty) && (ret_val == ""))
{
return(dft);
}
else
{
return(ret_val);
}
}
else
{
return(dft);
}
}
bool JsonUtils::JsonParse(std::string json_string, nlohmann::json& json_ref)
{
try
{
json_ref = nlohmann::json::parse(json_string);
return(true);
}
catch(...)
{
return(false);
}
}