parser returns an object

This commit is contained in:
Niels 2015-01-02 17:19:40 +01:00
parent 16c30cff8b
commit 18364aac85
2 changed files with 19 additions and 39 deletions

View file

@ -291,9 +291,7 @@ JSON::~JSON() noexcept
*/ */
JSON JSON::parse(const std::string& s) JSON JSON::parse(const std::string& s)
{ {
JSON j; return Parser(s).parse();
Parser(s).parse(j);
return j;
} }
/*! /*!
@ -302,9 +300,7 @@ JSON JSON::parse(const std::string& s)
*/ */
JSON JSON::parse(const char* s) JSON JSON::parse(const char* s)
{ {
JSON j; return Parser(s).parse();
Parser(s).parse(j);
return j;
} }
@ -1793,18 +1789,14 @@ JSON::Parser::Parser(std::istream& _is)
next(); next();
} }
/*! JSON JSON::Parser::parse()
@param[out] result the JSON to parse to
*/
void JSON::Parser::parse(JSON& result)
{ {
switch (_current) switch (_current)
{ {
case ('{'): case ('{'):
{ {
// explicitly set result to object to cope with {} // explicitly set result to object to cope with {}
result._type = value_type::object; JSON result(value_type::object);
result._value.object = new object_t;
next(); next();
@ -1820,7 +1812,7 @@ void JSON::Parser::parse(JSON& result)
expect(':'); expect(':');
// value // value
parse(result[std::move(key)]); result[std::move(key)] = parse();
} }
while (_current == ',' and next()); while (_current == ',' and next());
} }
@ -1828,14 +1820,13 @@ void JSON::Parser::parse(JSON& result)
// closing brace // closing brace
expect('}'); expect('}');
break; return result;
} }
case ('['): case ('['):
{ {
// explicitly set result to array to cope with [] // explicitly set result to array to cope with []
result._type = value_type::array; JSON result(value_type::array);
result._value.array = new array_t;
next(); next();
@ -1845,9 +1836,7 @@ void JSON::Parser::parse(JSON& result)
size_t element_count = 0; size_t element_count = 0;
do do
{ {
// add a dummy value and continue parsing at its position result.push_back(parse());
result += JSON();
parse(result[element_count++]);
} }
while (_current == ',' and next()); while (_current == ',' and next());
} }
@ -1855,37 +1844,30 @@ void JSON::Parser::parse(JSON& result)
// closing bracket // closing bracket
expect(']'); expect(']');
break; return result;
} }
case ('\"'): case ('\"'):
{ {
result._type = value_type::string; return JSON(parseString());
result._value.string = new string_t(std::move(parseString()));
break;
} }
case ('t'): case ('t'):
{ {
parseTrue(); parseTrue();
result._type = value_type::boolean; return JSON(true);
result._value.boolean = true;
break;
} }
case ('f'): case ('f'):
{ {
parseFalse(); parseFalse();
result._type = value_type::boolean; return JSON(false);
result._value.boolean = false;
break;
} }
case ('n'): case ('n'):
{ {
parseNull(); parseNull();
// nothing to do with result: is null by default return JSON();
break;
} }
default: default:
@ -1911,14 +1893,12 @@ void JSON::Parser::parse(JSON& result)
if (float_val == int_val) if (float_val == int_val)
{ {
// we would not lose precision -> int // we would not lose precision -> int
result._type = value_type::number; return JSON(int_val);
result._value.number = int_val;
} }
else else
{ {
// we would lose precision -> float // we would lose precision -> float
result._type = value_type::number_float; return JSON(float_val);
result._value.number_float = float_val;
} }
} }
catch (...) catch (...)

View file

@ -194,13 +194,13 @@ class JSON
/// read from stream /// read from stream
friend std::istream& operator>>(std::istream& i, JSON& j) friend std::istream& operator>>(std::istream& i, JSON& j)
{ {
Parser(i).parse(j); j = Parser(i).parse();
return i; return i;
} }
/// read from stream /// read from stream
friend std::istream& operator<<(JSON& j, std::istream& i) friend std::istream& operator<<(JSON& j, std::istream& i)
{ {
Parser(i).parse(j); j = Parser(i).parse();
return i; return i;
} }
@ -406,8 +406,8 @@ class JSON
// no copy assignment // no copy assignment
Parser& operator=(Parser) = delete; Parser& operator=(Parser) = delete;
/// parse into a given JSON object /// parse and return a JSON object
void parse(JSON&); JSON parse();
private: private:
/// read the next character, stripping whitespace /// read the next character, stripping whitespace