This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
JSONUtils.h
156 lines (121 loc) · 3.51 KB
/
JSONUtils.h
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//==============================================================================
/**
@file JSONUtils.h
@brief Utility methods for JSON parser from N.Lohmann
https://github.com/nlohmann/json
@author T. Schnitzler, W.Hamm
@note
**/
//==============================================================================
#pragma once
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "json.hpp"
using json = nlohmann::json;
class JSONUtils
{
public:
//! Get object by name
static bool GetObjectByName(const json& inJSON, const std::string& inName, json& outObject)
{
// Check desired value exists
json::const_iterator iter(inJSON.find(inName));
if (iter == inJSON.end())
return false;
// Check value is an array
if (!iter->is_object())
return false;
// Assign value
outObject = *iter;
return true;
}
//! Get array by name
static bool GetArrayByName(const json& inJSON, const std::string& inName, json& outArray)
{
// Check desired value exists
json::const_iterator iter(inJSON.find(inName));
if (iter == inJSON.end())
return false;
// Check value is an array
if (!iter->is_array())
return false;
// Assign value
outArray = *iter;
return true;
}
//! Get string by name
static std::string GetStringByName(const json& inJSON, const std::string& inName, const std::string& defaultValue = "")
{
// Check desired value exists
json::const_iterator iter(inJSON.find(inName));
if (iter == inJSON.end())
return defaultValue;
// Check value is a string
if (!iter->is_string())
return defaultValue;
// Return value
return *iter;
}
//! Get string
static std::string GetString(const json& j, const std::string& defaultString = "")
{
// Check value is a string
if (!j.is_string())
return defaultString;
return j;
}
//! Get bool by name
static bool GetBoolByName(const json& inJSON, const std::string& inName, bool defaultValue = false)
{
// Check desired value exists
json::const_iterator iter(inJSON.find(inName));
if (iter == inJSON.end())
return defaultValue;
// Check value is a bool
if (!iter->is_boolean())
return defaultValue;
// Return value
return *iter;
}
//! Get integer by name
static int GetIntByName(const json& inJSON, const std::string& inName, int defaultValue = 0)
{
// Check desired value exists
json::const_iterator iter(inJSON.find(inName));
if (iter == inJSON.end())
return defaultValue;
// Check value is an integer
if (!iter->is_number_integer())
return defaultValue;
// Return value
return *iter;
}
//! Get unsigned integer by name
static unsigned int GetUnsignedIntByName(const json& inJSON, const std::string& inName, unsigned int defaultValue = 0)
{
// Check desired value exists
json::const_iterator iter(inJSON.find(inName));
if (iter == inJSON.end())
return defaultValue;
// Check value is an unsigned integer
if (!iter->is_number_unsigned())
return defaultValue;
// Return value
return *iter;
}
//! Get float by name
static float GetFloatByName(const json& inJSON, const std::string& inName, float defaultValue = 0.0)
{
// Check desired value exists
json::const_iterator iter(inJSON.find(inName));
if (iter == inJSON.end())
return defaultValue;
// Check value is an integer
if (!iter->is_number_float())
if (!iter->is_number_float() && !iter->is_number_integer())
return defaultValue;
// Return value
return *iter;
}
};