-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_config.cpp
58 lines (45 loc) · 1.95 KB
/
validate_config.cpp
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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "main/validate_json.h"
// g++ validate_config.cpp main/validate_json.cpp && ./a.out
#define JSON_CONFIG_BUF_SIZE (16*1024)
StaticJsonDocument<JSON_CONFIG_BUF_SIZE> configuration;
StaticJsonDocument<JSON_CONFIG_BUF_SIZE> configurationCopy;
StaticJsonDocument<JSON_CONFIG_BUF_SIZE> schema;
void printError(const DeserializationError err) {
if (err == DeserializationError::Ok) {puts("Reading ok"); return;}
if (err == DeserializationError::EmptyInput) puts("Empty input");
else if (err == DeserializationError::IncompleteInput) puts("Incomplete input");
else if (err == DeserializationError::InvalidInput) puts("Invalid input");
else if (err == DeserializationError::NoMemory) puts("No memory (configuration too long)");
else if (err == DeserializationError::TooDeep) puts("Configuration too deep (so it must be invalid)");
else puts("Cannot save configuration because no.");
}
int main() {
char buf[JSON_CONFIG_BUF_SIZE];
int size;
FILE *fptr;
fptr = fopen("resources/default_config.json", "r");
size = fread(buf, 1, JSON_CONFIG_BUF_SIZE-1, fptr);
fclose(fptr);
buf[size] = 0;
printError(deserializeJson(configuration, String(buf)));
configuration.garbageCollect();
configurationCopy = configuration;
fptr = fopen("resources/config.schema.json", "r");
size = fread(buf, 1, JSON_CONFIG_BUF_SIZE-1, fptr);
fclose(fptr);
buf[size] = 0;
printError(deserializeJson(schema, String(buf)));
schema.garbageCollect();
puts(validateJson(configuration, schema).c_str());
configuration.remove("wifi");
configuration["filters"]["inputFilters"]["hue"].remove(8);
puts(validateJson(configuration, schema, configurationCopy).c_str());
if (configuration.containsKey("wifi"))
puts("Key restored");
if (configuration["filters"]["inputFilters"]["hue"].size() == 9)
puts("Array restored");
return 0;
}