Skip to content

Commit 47d9c18

Browse files
committed
Move underlying JSON type definitions to separate header
fly::JsonTraits will be retired. Move the type definitions out of this header to their own header.
1 parent 6983a14 commit 47d9c18

16 files changed

+341
-341
lines changed

build/win/libfly/libfly.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
<ClInclude Include="..\..\..\fly\types\json\json_concepts.hpp" />
227227
<ClInclude Include="..\..\..\fly\types\json\json_exception.hpp" />
228228
<ClInclude Include="..\..\..\fly\types\json\json_traits.hpp" />
229+
<ClInclude Include="..\..\..\fly\types\json\json_types.hpp" />
229230
<ClInclude Include="..\..\..\fly\types\numeric\detail\endian_traits.hpp" />
230231
<ClInclude Include="..\..\..\fly\types\numeric\detail\literal_parser.hpp" />
231232
<ClInclude Include="..\..\..\fly\types\numeric\endian.hpp" />

build/win/libfly/libfly.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@
271271
<ClInclude Include="..\..\..\fly\types\json\json_traits.hpp">
272272
<Filter>types\json</Filter>
273273
</ClInclude>
274+
<ClInclude Include="..\..\..\fly\types\json\json_types.hpp">
275+
<Filter>types\json</Filter>
276+
</ClInclude>
274277
<ClInclude Include="..\..\..\fly\types\numeric\endian.hpp">
275278
<Filter>types\numeric</Filter>
276279
</ClInclude>

fly/parser/ini_parser.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace fly::parser {
1313
//==================================================================================================
1414
std::optional<fly::Json> IniParser::parse_internal()
1515
{
16-
fly::Json values = JsonTraits::object_type();
16+
fly::Json values = json_object_type();
1717
fly::Json::iterator current;
1818

1919
std::string data;
@@ -38,8 +38,7 @@ std::optional<fly::Json> IniParser::parse_internal()
3838
{
3939
try
4040
{
41-
auto it =
42-
values.insert_or_assign(*std::move(section), JsonTraits::object_type());
41+
auto it = values.insert_or_assign(*std::move(section), json_object_type());
4342
current = it.first;
4443
}
4544
catch (const JsonException &ex)

fly/parser/json_parser.cpp

+26-29
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ namespace fly::parser {
1313

1414
namespace {
1515

16-
using JsonString = fly::JsonTraits::StringType;
17-
1816
inline bool
1917
is_feature_enabled(JsonParser::Features enabled_features, JsonParser::Features feature)
2018
{
@@ -102,7 +100,7 @@ std::optional<fly::Json> JsonParser::parse_object()
102100
{
103101
static constexpr const Token s_end_token = Token::CloseBrace;
104102

105-
fly::Json object = fly::JsonTraits::object_type();
103+
fly::Json object = fly::json_object_type();
106104
ParseState state;
107105

108106
// Discard the opening brace, which has already been peeked.
@@ -115,7 +113,7 @@ std::optional<fly::Json> JsonParser::parse_object()
115113
break;
116114
}
117115

118-
std::optional<fly::JsonTraits::string_type> key = parse_quoted_string();
116+
std::optional<fly::json_string_type> key = parse_quoted_string();
119117

120118
if (!key || (consume_token(Token::Colon) == ParseState::Invalid))
121119
{
@@ -140,7 +138,7 @@ std::optional<fly::Json> JsonParser::parse_array()
140138
{
141139
static constexpr const Token s_end_token = Token::CloseBracket;
142140

143-
fly::Json array = fly::JsonTraits::array_type();
141+
fly::Json array = fly::json_array_type();
144142
ParseState state;
145143

146144
// Discard the opening bracket, which has already been peeked.
@@ -191,9 +189,9 @@ JsonParser::ParseState JsonParser::state_for_object_or_array(Token end_token)
191189
}
192190

193191
//==================================================================================================
194-
std::optional<fly::JsonTraits::string_type> JsonParser::parse_quoted_string()
192+
std::optional<fly::json_string_type> JsonParser::parse_quoted_string()
195193
{
196-
fly::JsonTraits::string_type value;
194+
fly::json_string_type value;
197195
Token token;
198196

199197
if (consume_token(Token::Quote) == ParseState::Invalid)
@@ -203,14 +201,14 @@ std::optional<fly::JsonTraits::string_type> JsonParser::parse_quoted_string()
203201

204202
while ((token = get<Token>()) != Token::Quote)
205203
{
206-
value.push_back(static_cast<fly::JsonTraits::char_type>(token));
204+
value.push_back(static_cast<fly::json_char_type>(token));
207205

208206
if (token == Token::ReverseSolidus)
209207
{
210208
// Blindly ignore escaped symbols, the fly::Json class will check whether they are
211209
// valid. Just read at least one more symbol to prevent breaking out of the loop too
212210
// early if the next symbol is a quote.
213-
value.push_back(get<fly::JsonTraits::char_type>());
211+
value.push_back(get<fly::json_char_type>());
214212
}
215213
else if (token == Token::EndOfFile)
216214
{
@@ -224,7 +222,7 @@ std::optional<fly::JsonTraits::string_type> JsonParser::parse_quoted_string()
224222
//==================================================================================================
225223
std::optional<fly::Json> JsonParser::parse_value()
226224
{
227-
const fly::JsonTraits::string_type value = consume_value();
225+
const fly::json_string_type value = consume_value();
228226

229227
if (value == FLY_JSON_STR("true"))
230228
{
@@ -242,21 +240,22 @@ std::optional<fly::Json> JsonParser::parse_value()
242240
switch (validate_number(value))
243241
{
244242
case NumberType::SignedInteger:
245-
if (auto num = JsonString::convert<fly::JsonTraits::signed_type>(value); num)
243+
if (auto num = fly::JsonStringType::convert<fly::json_signed_integer_type>(value); num)
246244
{
247245
return *num;
248246
}
249247
break;
250248

251249
case NumberType::UnsignedInteger:
252-
if (auto num = JsonString::convert<fly::JsonTraits::unsigned_type>(value); num)
250+
if (auto num = fly::JsonStringType::convert<fly::json_unsigned_integer_type>(value);
251+
num)
253252
{
254253
return *num;
255254
}
256255
break;
257256

258257
case NumberType::FloatingPoint:
259-
if (auto num = JsonString::convert<fly::JsonTraits::float_type>(value); num)
258+
if (auto num = fly::JsonStringType::convert<fly::json_floating_point_type>(value); num)
260259
{
261260
return *num;
262261
}
@@ -306,9 +305,9 @@ JsonParser::ParseState JsonParser::consume_comma(Token end_token)
306305
}
307306

308307
//==================================================================================================
309-
fly::JsonTraits::string_type JsonParser::consume_value()
308+
fly::json_string_type JsonParser::consume_value()
310309
{
311-
fly::JsonTraits::string_type value;
310+
fly::json_string_type value;
312311

313312
auto keep_parsing = [this](Token token) -> bool
314313
{
@@ -328,7 +327,7 @@ fly::JsonTraits::string_type JsonParser::consume_value()
328327

329328
while (keep_parsing(peek<Token>()))
330329
{
331-
value.push_back(get<fly::JsonTraits::char_type>());
330+
value.push_back(get<fly::json_char_type>());
332331
}
333332

334333
return value;
@@ -418,9 +417,9 @@ JsonParser::ParseState JsonParser::consume_comment()
418417
}
419418

420419
//==================================================================================================
421-
JsonParser::NumberType JsonParser::validate_number(const fly::JsonTraits::string_type &value) const
420+
JsonParser::NumberType JsonParser::validate_number(const fly::json_string_type &value) const
422421
{
423-
const JsonString::view_type value_view = value;
422+
const fly::JsonStringType::view_type value_view = value;
424423

425424
const bool is_signed = !value_view.empty() && (value_view[0] == '-');
426425
const auto signless = value_view.substr(is_signed ? 1 : 0);
@@ -431,23 +430,22 @@ JsonParser::NumberType JsonParser::validate_number(const fly::JsonTraits::string
431430
}
432431

433432
const bool is_octal =
434-
(signless.size() > 1) && (signless[0] == '0') && JsonString::is_digit(signless[1]);
433+
(signless.size() > 1) && (signless[0] == '0') && fly::JsonStringType::is_digit(signless[1]);
435434

436-
if (!JsonString::is_digit(signless[0]) || is_octal)
435+
if (!fly::JsonStringType::is_digit(signless[0]) || is_octal)
437436
{
438437
return NumberType::Invalid;
439438
}
440439

441-
const fly::JsonTraits::string_type::size_type d = signless.find('.');
442-
const fly::JsonTraits::string_type::size_type e1 = signless.find('e');
443-
const fly::JsonTraits::string_type::size_type e2 = signless.find('E');
440+
const fly::json_string_type::size_type d = signless.find('.');
441+
const fly::json_string_type::size_type e1 = signless.find('e');
442+
const fly::json_string_type::size_type e2 = signless.find('E');
444443

445-
if (d != fly::JsonTraits::string_type::npos)
444+
if (d != fly::json_string_type::npos)
446445
{
447-
fly::JsonTraits::string_type::size_type end = signless.size();
446+
fly::json_string_type::size_type end = signless.size();
448447

449-
if ((e1 != fly::JsonTraits::string_type::npos) ||
450-
(e2 != fly::JsonTraits::string_type::npos))
448+
if ((e1 != fly::json_string_type::npos) || (e2 != fly::json_string_type::npos))
451449
{
452450
end = std::min(e1, e2);
453451
}
@@ -459,8 +457,7 @@ JsonParser::NumberType JsonParser::validate_number(const fly::JsonTraits::string
459457

460458
return NumberType::FloatingPoint;
461459
}
462-
else if (
463-
(e1 != fly::JsonTraits::string_type::npos) || (e2 != fly::JsonTraits::string_type::npos))
460+
else if ((e1 != fly::json_string_type::npos) || (e2 != fly::json_string_type::npos))
464461
{
465462
return NumberType::FloatingPoint;
466463
}

fly/parser/json_parser.hpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "fly/parser/parser.hpp"
44
#include "fly/types/json/json.hpp"
5-
#include "fly/types/json/json_traits.hpp"
65
#include "fly/types/string/string.hpp"
76

87
#include <cstdint>
@@ -46,9 +45,9 @@ class JsonParser : public Parser
4645
/**
4746
* ASCII codes for special JSON tokens.
4847
*/
49-
enum class Token : std::char_traits<fly::JsonTraits::char_type>::int_type
48+
enum class Token : std::char_traits<fly::json_char_type>::int_type
5049
{
51-
EndOfFile = std::char_traits<fly::JsonTraits::char_type>::eof(),
50+
EndOfFile = std::char_traits<fly::json_char_type>::eof(),
5251

5352
Tab = 0x09, // \t
5453
NewLine = 0x0a, // \n
@@ -152,7 +151,7 @@ class JsonParser : public Parser
152151
*
153152
* @return If successful, the parsed JSON string. Otherwise, an uninitialized value.
154153
*/
155-
std::optional<fly::JsonTraits::string_type> parse_quoted_string();
154+
std::optional<fly::json_string_type> parse_quoted_string();
156155

157156
/**
158157
* Parse a JSON number, boolean, or null value from the stream.
@@ -183,7 +182,7 @@ class JsonParser : public Parser
183182
*
184183
* @return The JSON value that was parsed as a string.
185184
*/
186-
fly::JsonTraits::string_type consume_value();
185+
fly::json_string_type consume_value();
187186

188187
/**
189188
* Extract all consecutive whitespace symbols and comments (if enabled in the feature set) from
@@ -213,7 +212,7 @@ class JsonParser : public Parser
213212
*
214213
* @return The interpreted JSON value type.
215214
*/
216-
NumberType validate_number(const fly::JsonTraits::string_type &value) const;
215+
NumberType validate_number(const fly::json_string_type &value) const;
217216

218217
/**
219218
* Check if a symbol is a whitespace symbol.

fly/types/json/detail/json_iterator.hpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "fly/traits/traits.hpp"
44
#include "fly/types/json/json_concepts.hpp"
55
#include "fly/types/json/json_exception.hpp"
6-
#include "fly/types/json/json_traits.hpp"
76
#include "fly/types/string/string.hpp"
87

98
#include <cmath>
@@ -68,16 +67,16 @@ class JsonIterator
6867
*/
6968
using object_iterator_type = std::conditional_t<
7069
is_const_iterator,
71-
JsonTraits::object_type::const_iterator,
72-
JsonTraits::object_type::iterator>;
70+
json_object_type::const_iterator,
71+
json_object_type::iterator>;
7372

7473
/**
7574
* Alias for an array iterator, depending on the Json type's constness.
7675
*/
7776
using array_iterator_type = std::conditional_t<
7877
is_const_iterator,
79-
JsonTraits::array_type::const_iterator,
80-
JsonTraits::array_type::iterator>;
78+
json_array_type::const_iterator,
79+
json_array_type::iterator>;
8180

8281
/**
8382
* Alias for the std::variant holding the iterator.
@@ -411,7 +410,7 @@ class JsonIterator
411410
* @throws JsonIteratorException If the Json instance is not an object.
412411
* @throws NullJsonException If the iterator is empty or past-the-end.
413412
*/
414-
const typename JsonTraits::object_type::key_type &key() const;
413+
const typename json_object_type::key_type &key() const;
415414

416415
/**
417416
* Retrieve a reference to the Json instance pointed to by this iterator.
@@ -814,11 +813,11 @@ auto JsonIterator<JsonType>::operator-(const JsonIterator &iterator) const -> di
814813

815814
//==================================================================================================
816815
template <typename JsonType>
817-
const typename JsonTraits::object_type::key_type &JsonIterator<JsonType>::key() const
816+
const typename json_object_type::key_type &JsonIterator<JsonType>::key() const
818817
{
819818
validate_iterator();
820819

821-
auto visitor = [this](const auto &it) -> const typename JsonTraits::object_type::key_type &
820+
auto visitor = [this](const auto &it) -> const typename json_object_type::key_type &
822821
{
823822
if constexpr (is_object_iterator<decltype(it)>)
824823
{

fly/types/json/detail/json_reverse_iterator.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3+
#include "fly/types/json/json_concepts.hpp"
34
#include "fly/types/json/json_exception.hpp"
4-
#include "fly/types/json/json_traits.hpp"
55

66
#include <cstddef>
77
#include <iterator>
@@ -213,7 +213,7 @@ class JsonReverseIterator : public std::reverse_iterator<JsonIterator>
213213
* @throws JsonIteratorException If the Json instance is not an object.
214214
* @throws NullJsonException If the iterator is empty or past-the-end.
215215
*/
216-
const typename JsonTraits::object_type::key_type &key() const;
216+
const typename json_object_type::key_type &key() const;
217217

218218
/**
219219
* Retrieve a reference to the Json instance pointed to by this iterator.
@@ -356,7 +356,7 @@ auto JsonReverseIterator<JsonIterator>::operator-(const JsonReverseIterator &oth
356356

357357
//==================================================================================================
358358
template <typename JsonIterator>
359-
const typename JsonTraits::object_type::key_type &JsonReverseIterator<JsonIterator>::key() const
359+
const typename json_object_type::key_type &JsonReverseIterator<JsonIterator>::key() const
360360
{
361361
try
362362
{

0 commit comments

Comments
 (0)