- removed m_final member
- parse_internal now takes a reference to a basic_json object to fill
with data
- header is automatically regenerated if re2c file is changed
This commit is contained in:
Niels 2015-04-26 21:43:47 +02:00
parent f09df96742
commit f874b5f0f8
3 changed files with 476 additions and 825 deletions

View File

@ -16,7 +16,9 @@ json_unit: test/unit.cpp src/json.hpp test/catch.hpp
$(CXX) -std=c++11 $(CXXFLAGS) $(FLAGS) $(CPPFLAGS) -I src -I test $< $(LDFLAGS) -o $@
# create scanner with re2c
re2c: src/json.hpp.re2c
re2c: src/json.hpp
src/json.hpp: src/json.hpp.re2c
$(RE2C) -b -s -i --no-generation-date $< | $(SED) '1d' > src/json.hpp
# static analyser

File diff suppressed because it is too large Load Diff

View File

@ -416,13 +416,12 @@ class basic_json
// the initializer list could describe an object
bool is_object = true;
// check if each element is an array with two elements whose first element
// is a string
// check if each element is an array with two elements whose first
// element is a string
for (const auto& element : init)
{
if ((element.m_final and element.m_type == value_t::array)
or (element.m_type != value_t::array or element.size() != 2
or element[0].m_type != value_t::string))
if ((element.m_type != value_t::array or element.size() != 2
or element[0].m_type != value_t::string))
{
// we found an element that makes it impossible to use the
// initializer list as object
@ -434,9 +433,6 @@ class basic_json
// adjust type if type deduction is not wanted
if (not type_deduction)
{
// mark this object's type as final
m_final = true;
// if array is wanted, do not create an object though possible
if (manual_type == value_t::array)
{
@ -758,6 +754,12 @@ class basic_json
return m_type;
}
/// return the type of the object (implicit)
inline operator value_t() const noexcept
{
return m_type;
}
// return whether value is null
inline bool is_null() const noexcept
{
@ -794,12 +796,6 @@ class basic_json
return m_type == value_t::string;
}
/// return the type of the object (implicit)
inline operator value_t() const noexcept
{
return m_type;
}
private:
//////////////////////
// value conversion //
@ -1083,6 +1079,7 @@ class basic_json
throw std::runtime_error("cannot use [] with " + type_name());
}
// fill gaps with null values
for (size_t i = m_value.array->size(); i <= idx; ++i)
{
m_value.array->push_back(basic_json());
@ -1520,6 +1517,7 @@ class basic_json
{
case (value_t::null):
{
// null values are empty
return true;
}
@ -1551,6 +1549,7 @@ class basic_json
{
case (value_t::null):
{
// null values are empty
return 0;
}
@ -1582,6 +1581,7 @@ class basic_json
{
case (value_t::null):
{
// null values are empty
return 0;
}
@ -2258,9 +2258,6 @@ class basic_json
/// the type of the current element
value_t m_type = value_t::null;
/// whether the type of JSON object may change later
bool m_final = false;
/// the value of the current element
json_value m_value = {};
@ -3850,7 +3847,8 @@ class basic_json
/// public parser interface
inline basic_json parse()
{
basic_json result = parse_internal();
basic_json result;
parse_internal(result);
expect(lexer::token_type::end_of_input);
@ -3859,14 +3857,17 @@ class basic_json
private:
/// the actual parser
inline basic_json parse_internal()
inline void parse_internal(basic_json& pos)
{
switch (last_token)
{
case (lexer::token_type::begin_object):
{
// explicitly set result to object to cope with {}
basic_json result(value_t::object);
pos.m_type = value_t::object;
AllocatorType<object_t> alloc;
pos.m_value.object = alloc.allocate(1);
alloc.construct(pos.m_value.object);
// read next token
get_token();
@ -3875,7 +3876,7 @@ class basic_json
if (last_token == lexer::token_type::end_object)
{
get_token();
return result;
return;
}
// otherwise: parse key-value pairs
@ -3897,21 +3898,25 @@ class basic_json
// parse and add value
get_token();
result.m_value.object->emplace(key, parse_internal());
auto it = pos.m_value.object->emplace(key, nullptr);
parse_internal(it.first->second);
}
while (last_token == lexer::token_type::value_separator);
// closing }
expect(lexer::token_type::end_object);
get_token();
return result;
get_token();
return;
}
case (lexer::token_type::begin_array):
{
// explicitly set result to object to cope with []
basic_json result(value_t::array);
pos.m_type = value_t::array;
AllocatorType<array_t> alloc;
pos.m_value.array = alloc.allocate(1);
alloc.construct(pos.m_value.array);
// read next token
get_token();
@ -3920,7 +3925,7 @@ class basic_json
if (last_token == lexer::token_type::end_array)
{
get_token();
return result;
return;
}
// otherwise: parse values
@ -3933,40 +3938,51 @@ class basic_json
}
// parse and add value
result.m_value.array->emplace_back(parse_internal());
auto it = pos.m_value.array->insert(pos.m_value.array->end(), nullptr);
parse_internal(*it);
}
while (last_token == lexer::token_type::value_separator);
// closing ]
expect(lexer::token_type::end_array);
get_token();
return result;
get_token();
return;
}
case (lexer::token_type::literal_null):
{
get_token();
return basic_json(nullptr);
return;
}
case (lexer::token_type::value_string):
{
const auto s = m_lexer.get_string();
pos.m_type = value_t::string;
AllocatorType<string_t> alloc;
pos.m_value.string = alloc.allocate(1);
alloc.construct(pos.m_value.string, m_lexer.get_string());
get_token();
return basic_json(s);
return;
}
case (lexer::token_type::literal_true):
{
pos.m_type = value_t::boolean;
pos.m_value.boolean = true;
get_token();
return basic_json(true);
return;
}
case (lexer::token_type::literal_false):
{
pos.m_type = value_t::boolean;
pos.m_value.boolean = false;
get_token();
return basic_json(false);
return;
}
case (lexer::token_type::value_number):
@ -3981,20 +3997,23 @@ class basic_json
m_lexer.get_token() + " is not a number");
}
get_token();
// check if conversion loses precision
const auto int_val = static_cast<number_integer_t>(float_val);
if (approx(float_val, static_cast<number_float_t>(int_val)))
{
// we basic_json not lose precision -> return int
return basic_json(int_val);
pos.m_type = value_t::number_integer;
pos.m_value.number_integer = int_val;
}
else
{
// we would lose precision -> returnfloat
return basic_json(float_val);
pos.m_type = value_t::number_float;
pos.m_value.number_float = float_val;
}
get_token();
return;
}
default: