-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetengo_json_jsonParser.cpp
More file actions
136 lines (113 loc) · 3.1 KB
/
Copy pathtetengo_json_jsonParser.cpp
File metadata and controls
136 lines (113 loc) · 3.1 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
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
/*! \file
\brief A JSON parser.
Copyright (C) 2019-2026 kaoru https://www.tetengo.org/
*/
#include <memory>
#include <stdexcept>
#include <utility>
#include <stddef.h>
#include <boost/preprocessor.hpp>
#include <boost/scope_exit.hpp>
#include <tetengo/json/element.h>
#include <tetengo/json/jsonParser.h>
#include <tetengo/json/json_parser.hpp>
#include <tetengo/json/reader.h>
#include "tetengo_json_element.hpp"
#include "tetengo_json_reader.hpp"
struct tetengo_json_jsonParser_tag
{
std::unique_ptr<tetengo::json::json_parser> p_cpp_parser;
mutable std::unique_ptr<const tetengo_json_element_t> p_current_element;
explicit tetengo_json_jsonParser_tag(std::unique_ptr<tetengo::json::json_parser>&& p_cpp_parser) :
p_cpp_parser{ std::move(p_cpp_parser) },
p_current_element{}
{}
};
size_t tetengo_json_jsonParser_defaultBufferCapacity(void)
{
try
{
return tetengo::json::json_parser::default_buffer_capacity();
}
catch (...)
{
return 0;
}
}
tetengo_json_jsonParser_t*
tetengo_json_jsonParser_create(tetengo_json_reader_t* const p_reader, const size_t buffer_capacity)
{
try
{
BOOST_SCOPE_EXIT(p_reader)
{
tetengo_json_reader_destroy(p_reader);
}
BOOST_SCOPE_EXIT_END;
if (!p_reader)
{
throw std::invalid_argument{ "p_reader is NULL." };
}
auto p_cpp_parser =
std::make_unique<tetengo::json::json_parser>(std::move(p_reader->move_cpp_reader()), buffer_capacity);
auto p_instance = std::make_unique<tetengo_json_jsonParser_t>(std::move(p_cpp_parser));
return p_instance.release();
}
catch (...)
{
return nullptr;
}
}
void tetengo_json_jsonParser_destroy(const tetengo_json_jsonParser_t* const p_parser)
{
try
{
const std::unique_ptr<const tetengo_json_jsonParser_t> p_instance{ p_parser };
}
catch (...)
{}
}
bool tetengo_json_jsonParser_hasNext(const tetengo_json_jsonParser_t* const p_parser)
{
try
{
if (!p_parser)
{
throw std::invalid_argument{ "p_parser is NULL." };
}
return p_parser->p_cpp_parser->has_next();
}
catch (...)
{
return false;
}
}
const tetengo_json_element_t* tetengo_json_jsonParser_peek(const tetengo_json_jsonParser_t* const p_parser)
{
try
{
if (!p_parser)
{
throw std::invalid_argument{ "p_parser is NULL." };
}
p_parser->p_current_element = std::make_unique<tetengo_json_element_t>(&p_parser->p_cpp_parser->peek());
return std::to_address(p_parser->p_current_element);
}
catch (...)
{
return nullptr;
}
}
void tetengo_json_jsonParser_next(tetengo_json_jsonParser_t* const p_parser)
{
try
{
if (!p_parser)
{
throw std::invalid_argument{ "p_parser is NULL." };
}
p_parser->p_cpp_parser->next();
}
catch (...)
{}
}