Skip to content

Commit 8f142a4

Browse files
committed
Merge pull request #168 from tetengo/usage
Usage (cherry picked from commit 80956ae)
1 parent 0ae5939 commit 8f142a4

File tree

53 files changed

+2285
-23
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2285
-23
lines changed

Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ EXCLUDE_SYMBOLS =
987987
# that contain example code fragments that are included (see the \include
988988
# command).
989989

990-
EXAMPLE_PATH =
990+
EXAMPLE_PATH = library
991991

992992
# If the value of the EXAMPLE_PATH tag contains directories, you can use the
993993
# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp and

library/json/cpp/include/tetengo/json/0namespace.dox

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,16 @@
66

77
/*! \namespace tetengo::json
88
\brief A JSON library.
9+
10+
\section usage-json Usage
11+
12+
\subsection usage-json-parsing Parsing JSON
13+
14+
\subsubsection usage-json-parsing-cpp C++
15+
16+
\snippet json/test/src/usage_tetengo.json.parsing_cpp.cpp parsing
17+
18+
\subsubsection usage-json-parsing-c C
19+
20+
\snippet json/test/src/usage_tetengo.json.parsing_c.c parsing
921
*/

library/json/test/src/Makefile.am

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Automake Settings
22
# Copyright (C) 2019-2021 kaoru https://www.tetengo.org/
33

4-
headers =
4+
headers = \
5+
usage_tetengo.json.parsing_c.h \
6+
usage_tetengo.json.parsing_cpp.hpp
57

68
sources = \
79
master.cpp \
@@ -14,7 +16,10 @@ sources = \
1416
test_tetengo.json.line_counting_reader.cpp \
1517
test_tetengo.json.reader.cpp \
1618
test_tetengo.json.reader_iterator.cpp \
17-
test_tetengo.json.stream_reader.cpp
19+
test_tetengo.json.stream_reader.cpp \
20+
usage_tetengo.json.cpp \
21+
usage_tetengo.json.parsing_c.c \
22+
usage_tetengo.json.parsing_cpp.cpp
1823

1924
check_PROGRAMS = test_tetengo.json
2025

@@ -28,7 +33,7 @@ test_tetengo_json_LDADD = \
2833
${BOOST_UNIT_TEST_FRAMEWORK_LIB}
2934
test_tetengo_json_DEPENDENCIES = \
3035
${top_builddir}/library/json/c/src/libtetengo.json.la
31-
test_tetengo_json_SOURCES = ${sources}
36+
test_tetengo_json_SOURCES = ${headers} ${sources}
3237

3338
TESTS = ${check_PROGRAMS}
3439

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*! \file
2+
\brief The usage test of tetengo::json.
3+
4+
Copyright (C) 2019-2021 kaoru https://www.tetengo.org/
5+
*/
6+
7+
8+
#include <boost/preprocessor.hpp>
9+
#include <boost/test/unit_test.hpp>
10+
11+
#include "usage_tetengo.json.parsing_c.h"
12+
#include "usage_tetengo.json.parsing_cpp.hpp"
13+
14+
15+
BOOST_AUTO_TEST_SUITE(usagetest_tetengo)
16+
BOOST_AUTO_TEST_SUITE(json)
17+
18+
19+
BOOST_AUTO_TEST_CASE(parsing)
20+
{
21+
BOOST_TEST_PASSPOINT();
22+
23+
usage_tetengo::json::parsing();
24+
usage_tetengo_json_parsing();
25+
}
26+
27+
28+
BOOST_AUTO_TEST_SUITE_END()
29+
BOOST_AUTO_TEST_SUITE_END()
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*! \file
2+
\brief The usage of JSON parsing.
3+
4+
Copyright (C) 2019-2021 kaoru https://www.tetengo.org/
5+
*/
6+
7+
#include "usage_tetengo.json.parsing_c.h"
8+
9+
/* [parsing] */
10+
#include <assert.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <string.h>
14+
15+
#include <tetengo/json/element.h>
16+
#include <tetengo/json/jsonParser.h>
17+
#include <tetengo/json/reader.h>
18+
19+
20+
int make_json_file(const char* text, const char* path);
21+
const char* to_string(const tetengo_json_element_t* p_element);
22+
23+
void usage_tetengo_json_parsing()
24+
{
25+
/* clang-format off */
26+
static const char* const json_text =
27+
"{\n"
28+
" \"hoge\": 42,\n"
29+
" \"fuga\": [ \"foo\", \"bar\" ]\n"
30+
"}\n";
31+
/* clang-format on */
32+
static const char* const json_file_path = "jsonParser_sample.json";
33+
if (!make_json_file(json_text, json_file_path))
34+
{
35+
return;
36+
}
37+
{
38+
/* Creates a reader from a file path. */
39+
tetengo_json_reader_t* const p_reader = tetengo_json_reader_createStreamReader(
40+
json_file_path, tetengo_json_reader_streamReaderDefaultBufferCapacity());
41+
42+
/* Creates a JSON parser */
43+
tetengo_json_jsonParser_t* const p_parser =
44+
tetengo_json_jsonParser_create(p_reader, tetengo_json_jsonParser_defaultBufferCapacity());
45+
46+
/* Iteration. */
47+
char element_list_string[384] = { 0 };
48+
while (tetengo_json_jsonParser_hasNext(p_parser))
49+
{
50+
/* Obtains the current element. */
51+
const tetengo_json_element_t* const p_element = tetengo_json_jsonParser_peek(p_parser);
52+
53+
strcat(element_list_string, to_string(p_element));
54+
55+
/* Moves to the next element. */
56+
tetengo_json_jsonParser_next(p_parser);
57+
}
58+
59+
{
60+
/* clang-format off */
61+
static const char* const expected =
62+
"object:open:\n"
63+
"member:open:name=hoge:\n"
64+
"number:42\n"
65+
"member:close:\n"
66+
"member:open:name=fuga:\n"
67+
"array:open:\n"
68+
"string:foo\n"
69+
"string:bar\n"
70+
"array:close:\n"
71+
"member:close:\n"
72+
"object:close:\n";
73+
/* clang-format on */
74+
assert(strcmp(element_list_string, expected) == 0);
75+
}
76+
77+
/* Destroys the JSON parser. The reader inside is also destroyed. */
78+
tetengo_json_jsonParser_destroy(p_parser);
79+
}
80+
remove(json_file_path);
81+
}
82+
83+
int make_json_file(const char* const text, const char* const path)
84+
{
85+
FILE* const stream = fopen(path, "w");
86+
if (!stream)
87+
{
88+
fprintf(stderr, "Can't create a file: %s", path);
89+
return 0;
90+
}
91+
fputs(text, stream);
92+
fclose(stream);
93+
return 1;
94+
}
95+
96+
const char* to_string(const tetengo_json_element_t* const p_element)
97+
{
98+
static char result[32] = { 0 };
99+
result[0] = '\0';
100+
{
101+
/* Obtains the element type name. */
102+
const tetengo_json_element_type_t* const p_type = tetengo_json_element_type(p_element);
103+
if (p_type->name == tetengo_json_element_typeName_string())
104+
{
105+
strcat(result, "string:");
106+
}
107+
else if (p_type->name == tetengo_json_element_typeName_number())
108+
{
109+
strcat(result, "number:");
110+
}
111+
else if (p_type->name == tetengo_json_element_typeName_boolean())
112+
{
113+
strcat(result, "boolean:");
114+
}
115+
else if (p_type->name == tetengo_json_element_typeName_null())
116+
{
117+
strcat(result, "null:");
118+
}
119+
else if (p_type->name == tetengo_json_element_typeName_object())
120+
{
121+
strcat(result, "object:");
122+
}
123+
else if (p_type->name == tetengo_json_element_typeName_member())
124+
{
125+
strcat(result, "member:");
126+
}
127+
else
128+
{
129+
assert(p_type->name == tetengo_json_element_typeName_array());
130+
strcat(result, "array:");
131+
}
132+
133+
/* Obtains the element type category. */
134+
if (p_type->category == tetengo_json_element_typeCategory_primitive())
135+
{
136+
}
137+
else if (p_type->category == tetengo_json_element_typeCategory_structureOpen())
138+
{
139+
strcat(result, "open:");
140+
}
141+
else
142+
{
143+
assert(p_type->category == tetengo_json_element_typeCategory_structureClose());
144+
strcat(result, "close:");
145+
}
146+
147+
{
148+
/* Obtains the element attributes. */
149+
const size_t attribute_count = tetengo_json_element_attributeKeys(p_element, NULL);
150+
if (attribute_count > 0)
151+
{
152+
const char** const p_keys = (const char**)malloc(attribute_count * sizeof(const char*));
153+
if (!p_keys)
154+
{
155+
fprintf(stderr, "Failed to allocate a memory.");
156+
return result;
157+
}
158+
tetengo_json_element_attributeKeys(p_element, p_keys);
159+
{
160+
size_t i = 0;
161+
for (i = 0; i < attribute_count; ++i)
162+
{
163+
strcat(result, p_keys[i]);
164+
strcat(result, "=");
165+
strcat(result, tetengo_json_element_attributeValueOf(p_element, p_keys[i]));
166+
strcat(result, ":");
167+
}
168+
}
169+
free((void*)p_keys);
170+
}
171+
}
172+
173+
/* Obtains the element value. */
174+
strcat(result, tetengo_json_element_value(p_element));
175+
}
176+
strcat(result, "\n");
177+
return result;
178+
}
179+
/* [parsing] */
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*! \file
2+
\brief The usage of JSON parsing.
3+
4+
Copyright (C) 2019-2021 kaoru https://www.tetengo.org/
5+
*/
6+
7+
8+
#if !defined(USAGETETENGO_JSON_PARSING_C_H)
9+
#define USAGETETENGO_JSON_PARSING_C_H
10+
11+
12+
#if defined(__cplusplus)
13+
extern "C" {
14+
#endif
15+
16+
void usage_tetengo_json_parsing();
17+
18+
19+
#if defined(__cplusplus)
20+
}
21+
#endif
22+
23+
24+
#endif

0 commit comments

Comments
 (0)