forked from azadkuh/gason--
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
94 lines (78 loc) · 2.18 KB
/
main.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
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
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include "jsonbuilder.hpp"
///////////////////////////////////////////////////////////////////////////////
/* sample1.json is going to be made.
{
"array": [
0,
1,
2
],
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "Hello World"
}
*/
///////////////////////////////////////////////////////////////////////////////
class JSonGason
{
public:
static bool doTest1() {
char buffer[257] = {0};
return build(buffer, 256, "./sample1-w.json");
}
static bool doTestOverflow() {
char buffer[65] = {0};
return build(buffer, 64, "./sample1-w-overflow.json");
}
protected:
static bool build(char *buffer, size_t capacity,
const char* fileName) {
gason::JSonBuilder doc(buffer, capacity);
// this sample JSon at least requires a buffer with 119 bytes.
doc.startObject()
.startArray("array")
.addValue(0)
.addValue(1)
.addValue(2)
.endArray()
.addValue("boolean", true)
.addNull("null")
.addValue("number", 123)
.startObject("object")
.addValue("a", "b")
.addValue("c", "d")
.addValue("e", "f")
.endObject()
.addValue("string", "Hello World")
.endObject();
if ( !doc.isBufferAdequate() ) {
puts("warning: the buffer is small and the output json is not valid.");
}
FILE* fp = fopen(fileName, "w+t");
if ( fp == 0 ) {
puts("failed opening sample1-w.json to write");
return false;
}
// hold json data through the test
fwrite(buffer, strlen(buffer), 1, fp);
fclose(fp);
return true;
}
};
///////////////////////////////////////////////////////////////////////////////
int main(int , char **)
{
JSonGason::doTest1();
JSonGason::doTestOverflow();
return 0;
}
///////////////////////////////////////////////////////////////////////////////