forked from squix78/json-streaming-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementValue.h
More file actions
102 lines (84 loc) · 1.9 KB
/
ElementValue.h
File metadata and controls
102 lines (84 loc) · 1.9 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
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
#include <Arduino.h>
union Variant {
bool boolValue;
float numValue;
const char* stringValue;
};
struct ElementValue {
private:
static const int Type_Null = 0;
static const int Type_Int = 1;
static const int Type_Float = 2;
static const int Type_String = 3;
static const int Type_Bool = 4;
Variant data;
int type;
public:
ElementValue with(float value) {
data.numValue = value;
type = Type_Float;
return *this;
}
ElementValue with(long value) {
data.numValue = value;
type = Type_Int;
return *this;
}
ElementValue with(bool value) {
data.boolValue = value;
type = Type_Bool;
return *this;
}
ElementValue with(const char* value) {
data.stringValue = value;
type = Type_String;
return *this;
}
ElementValue with() {
type = Type_Null;
return *this;
}
bool getBool() {
return data.boolValue;
}
const char* getString() {
return data.stringValue;
}
float getFloat() {
return data.numValue;
}
long getInt() {
return (long)data.numValue;
}
bool isInt() {
return type == Type_Int;
}
bool isFloat() {
return type == Type_Float;
}
bool isString() {
return type == Type_String;
}
bool isBool() {
return type == Type_Bool;
}
bool isNull() {
return type == Type_Null;
}
char* toString(char* buffer) {
if(isInt()) {
sprintf(buffer, "%d", getInt());
} else if(isFloat()) {
sprintf(buffer, "%f", getFloat());
} else if(isString()) {
sprintf(buffer, "\"%s\"", getString());
} else if(isBool()) {
strcpy(buffer, getBool() ? "true" : "false");
} else if(isNull()) {
strcpy(buffer, "null");
} else {
strcpy(buffer, "?");
}
return buffer;
}
};