2011-06-21 20:26:32 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-06-21 20:26:32 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-20 14:11:28 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-06-21 20:26:32 +10:00
|
|
|
*
|
2018-07-05 17:46:02 +10:00
|
|
|
* Copyright 2010-2018 Danny Robson <danny@nerdcruft.net>
|
2011-06-21 20:26:32 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "tree.hpp"
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "except.hpp"
|
|
|
|
#include "flat.hpp"
|
2012-04-12 16:06:28 +10:00
|
|
|
|
2016-04-05 11:06:01 +10:00
|
|
|
#include "../debug.hpp"
|
|
|
|
#include "../io.hpp"
|
2018-07-05 17:46:02 +10:00
|
|
|
#include "../iterator.hpp"
|
2016-04-05 11:06:01 +10:00
|
|
|
#include "../maths.hpp"
|
2017-01-05 20:21:20 +11:00
|
|
|
#include "../stream.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2017-09-12 14:17:30 +10:00
|
|
|
#include "preprocessor.hpp"
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#include <algorithm>
|
2013-08-05 22:39:44 +10:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iomanip>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2017-01-05 20:21:20 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-08-28 21:06:04 +10:00
|
|
|
using json::tree::node;
|
|
|
|
using json::tree::object;
|
|
|
|
using json::tree::array;
|
|
|
|
using json::tree::string;
|
|
|
|
using json::tree::number;
|
|
|
|
using json::tree::boolean;
|
|
|
|
using json::tree::null;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2012-04-20 18:06:30 +10:00
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-11-16 11:42:20 +11:00
|
|
|
namespace util {
|
|
|
|
bool
|
|
|
|
is_integer (const json::tree::number &node)
|
|
|
|
{
|
2016-05-12 17:41:31 +10:00
|
|
|
using R = json::tree::number::repr_t;
|
|
|
|
|
|
|
|
switch (node.repr ()) {
|
|
|
|
case R::REAL:
|
|
|
|
return is_integer (node.real ());
|
|
|
|
|
|
|
|
case R::SINT:
|
|
|
|
case R::UINT:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unreachable ();
|
2015-11-16 11:42:20 +11:00
|
|
|
}
|
2012-04-20 18:06:30 +10:00
|
|
|
|
|
|
|
|
2015-11-16 11:42:20 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
is_integer (const json::tree::node &node)
|
|
|
|
{
|
2016-05-12 17:41:31 +10:00
|
|
|
return node.is_number () && is_integer (node.as_number ());
|
2015-11-16 11:42:20 +11:00
|
|
|
}
|
2012-04-20 18:06:30 +10:00
|
|
|
}
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-06-28 14:16:28 +10:00
|
|
|
template <typename T>
|
|
|
|
static
|
|
|
|
typename std::vector<json::flat::item<T>>::const_iterator
|
|
|
|
parse (typename std::vector<json::flat::item<T>>::const_iterator first,
|
|
|
|
typename std::vector<json::flat::item<T>>::const_iterator last,
|
2015-02-02 23:00:38 +11:00
|
|
|
std::unique_ptr<json::tree::node> &output);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-02-02 23:00:38 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-06-28 14:16:28 +10:00
|
|
|
template <typename T>
|
|
|
|
static
|
|
|
|
typename std::vector<json::flat::item<T>>::const_iterator
|
|
|
|
parse (typename std::vector<json::flat::item<T>>::const_iterator first,
|
|
|
|
typename std::vector<json::flat::item<T>>::const_iterator last,
|
2015-02-02 23:00:38 +11:00
|
|
|
json::tree::array &parent)
|
|
|
|
{
|
|
|
|
for (auto cursor = first; cursor != last; ) {
|
|
|
|
if (cursor->tag == json::flat::type::ARRAY_END)
|
|
|
|
return cursor + 1;
|
|
|
|
|
|
|
|
std::unique_ptr<json::tree::node> value;
|
2016-06-28 14:16:28 +10:00
|
|
|
cursor = ::parse<T> (cursor, last, value);
|
2015-02-02 23:00:38 +11:00
|
|
|
parent.insert (std::move (value));
|
|
|
|
}
|
|
|
|
|
|
|
|
unreachable ();
|
2012-04-26 18:18:09 +10:00
|
|
|
}
|
2012-04-12 14:08:41 +10:00
|
|
|
|
|
|
|
|
2015-02-02 23:00:38 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2016-06-28 14:16:28 +10:00
|
|
|
template <typename T>
|
|
|
|
static
|
|
|
|
typename std::vector<json::flat::item<T>>::const_iterator
|
|
|
|
parse (typename std::vector<json::flat::item<T>>::const_iterator first,
|
|
|
|
typename std::vector<json::flat::item<T>>::const_iterator last,
|
2015-02-02 23:00:38 +11:00
|
|
|
json::tree::object &parent)
|
|
|
|
{
|
|
|
|
for (auto cursor = first; cursor != last; ) {
|
|
|
|
if (cursor->tag == json::flat::type::OBJECT_END)
|
|
|
|
return cursor + 1;
|
2012-04-12 14:08:41 +10:00
|
|
|
|
2015-02-02 23:00:38 +11:00
|
|
|
CHECK_EQ (cursor->tag, json::flat::type::STRING);
|
2012-04-12 14:08:41 +10:00
|
|
|
|
2015-02-02 23:00:38 +11:00
|
|
|
std::string key (cursor->first + 1, cursor->last - 1);
|
|
|
|
++cursor;
|
|
|
|
|
|
|
|
std::unique_ptr<json::tree::node> val;
|
2016-06-28 14:16:28 +10:00
|
|
|
cursor = ::parse<T> (cursor, last, val);
|
2015-02-02 23:00:38 +11:00
|
|
|
|
|
|
|
parent.insert (key, std::move (val));
|
|
|
|
}
|
|
|
|
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-06-28 14:16:28 +10:00
|
|
|
template <typename T>
|
|
|
|
static
|
|
|
|
typename std::vector<json::flat::item<T>>::const_iterator
|
|
|
|
parse (typename std::vector<json::flat::item<T>>::const_iterator first,
|
|
|
|
typename std::vector<json::flat::item<T>>::const_iterator last,
|
2015-02-02 23:00:38 +11:00
|
|
|
std::unique_ptr<json::tree::node> &output)
|
|
|
|
{
|
|
|
|
CHECK (first != last);
|
|
|
|
CHECK (output.get () == nullptr);
|
|
|
|
|
2016-06-28 14:16:28 +10:00
|
|
|
using F = json::flat::type;
|
2016-05-12 17:41:31 +10:00
|
|
|
|
2015-02-02 23:00:38 +11:00
|
|
|
switch (first->tag) {
|
2016-06-28 14:16:28 +10:00
|
|
|
case F::NUL:
|
2015-02-02 23:00:38 +11:00
|
|
|
output.reset (new json::tree::null ());
|
|
|
|
return first + 1;
|
|
|
|
|
2016-06-28 14:16:28 +10:00
|
|
|
case F::BOOLEAN:
|
2015-02-02 23:00:38 +11:00
|
|
|
CHECK (*first->first == 't' || *first->first == 'f');
|
|
|
|
output.reset (new json::tree::boolean (*first->first == 't'));
|
|
|
|
return first + 1;
|
|
|
|
|
2016-06-28 14:16:28 +10:00
|
|
|
case F::STRING:
|
|
|
|
CHECK_NEQ (&*first->first, &*first->last);
|
2015-02-02 23:00:38 +11:00
|
|
|
output.reset (new json::tree::string (first->first + 1, first->last - 1));
|
|
|
|
return first + 1;
|
|
|
|
|
2016-06-28 14:16:28 +10:00
|
|
|
case F::INTEGER: {
|
|
|
|
T start = first->first;
|
|
|
|
bool negative = *start == '-';
|
|
|
|
if (negative)
|
|
|
|
++start;
|
|
|
|
|
|
|
|
T finish = start;
|
|
|
|
uintmax_t v = 0;
|
|
|
|
while (isdigit (*finish))
|
|
|
|
v *= 10, v += *finish - '0', ++finish;
|
2016-05-12 17:41:31 +10:00
|
|
|
|
2016-06-28 14:16:28 +10:00
|
|
|
if (finish == start || finish > first->last)
|
|
|
|
throw json::parse_error ("invalid integer");
|
|
|
|
|
|
|
|
if (negative)
|
|
|
|
output.reset (new json::tree::number (-intmax_t(v)));
|
|
|
|
else
|
2016-05-12 17:41:31 +10:00
|
|
|
output.reset (new json::tree::number (v));
|
|
|
|
|
|
|
|
return first + 1;
|
2016-06-28 14:16:28 +10:00
|
|
|
}
|
2016-05-12 17:41:31 +10:00
|
|
|
|
2016-06-28 14:16:28 +10:00
|
|
|
case F::REAL: {
|
|
|
|
std::string str (first->first, first->last);
|
|
|
|
output.reset (new json::tree::number (std::atof (str.c_str())));
|
2015-02-02 23:00:38 +11:00
|
|
|
return first + 1;
|
2016-06-28 14:16:28 +10:00
|
|
|
}
|
2015-02-02 23:00:38 +11:00
|
|
|
|
2016-06-28 14:16:28 +10:00
|
|
|
case F::ARRAY_BEGIN: {
|
2015-02-02 23:00:38 +11:00
|
|
|
auto value = std::make_unique<json::tree::array> ();
|
2016-06-28 14:16:28 +10:00
|
|
|
auto cursor = ::parse<T> (first + 1, last, *value);
|
2015-02-02 23:00:38 +11:00
|
|
|
output = std::move (value);
|
|
|
|
return cursor;
|
|
|
|
}
|
2012-04-12 14:08:41 +10:00
|
|
|
|
2016-06-28 14:16:28 +10:00
|
|
|
case F::OBJECT_BEGIN: {
|
2015-02-02 23:00:38 +11:00
|
|
|
auto value = std::make_unique<json::tree::object> ();
|
2016-06-28 14:16:28 +10:00
|
|
|
auto cursor = ::parse<T> (first + 1, last, *value);
|
2015-02-02 23:00:38 +11:00
|
|
|
output = std::move (value);
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
2016-06-28 14:16:28 +10:00
|
|
|
case F::UNKNOWN:
|
|
|
|
case F::OBJECT_END:
|
|
|
|
case F::ARRAY_END:
|
2015-02-02 23:00:38 +11:00
|
|
|
unreachable ();
|
2015-01-18 15:36:43 +11:00
|
|
|
}
|
2016-05-12 17:41:31 +10:00
|
|
|
|
|
|
|
unreachable ();
|
2015-02-02 23:00:38 +11:00
|
|
|
}
|
2012-04-12 14:08:41 +10:00
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-06-28 14:16:28 +10:00
|
|
|
template <typename T>
|
2015-02-02 23:00:38 +11:00
|
|
|
std::unique_ptr<json::tree::node>
|
2018-01-13 13:48:58 +11:00
|
|
|
json::tree::parse (const util::view<T> &src)
|
2015-02-02 23:00:38 +11:00
|
|
|
{
|
|
|
|
std::unique_ptr<json::tree::node> output;
|
2016-06-28 14:16:28 +10:00
|
|
|
auto data = json::flat::parse (src);
|
|
|
|
auto end = ::parse<T> (data.cbegin (), data.cend (), output);
|
2015-02-02 23:00:38 +11:00
|
|
|
|
|
|
|
CHECK (end == data.cend ());
|
|
|
|
(void)end;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2016-06-28 15:59:53 +10:00
|
|
|
#define INSTANTIATE(KLASS) \
|
|
|
|
template \
|
|
|
|
std::unique_ptr<json::tree::node> \
|
2018-01-13 13:48:58 +11:00
|
|
|
json::tree::parse (const util::view<KLASS>&);
|
2015-02-02 23:00:38 +11:00
|
|
|
|
2017-09-12 14:17:30 +10:00
|
|
|
MAP0(INSTANTIATE,
|
2016-06-28 15:59:53 +10:00
|
|
|
std::string::iterator,
|
|
|
|
std::string::const_iterator,
|
|
|
|
const char*,
|
|
|
|
char *
|
|
|
|
);
|
2016-02-11 14:53:43 +11:00
|
|
|
|
2016-06-28 15:59:53 +10:00
|
|
|
#undef INSTANTIATE
|
|
|
|
|
|
|
|
|
2016-06-28 16:57:25 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::unique_ptr<json::tree::node>
|
2016-10-07 19:48:42 +11:00
|
|
|
json::tree::parse (const std::experimental::filesystem::path &src)
|
2016-06-28 16:57:25 +10:00
|
|
|
{
|
2016-11-17 18:07:57 +11:00
|
|
|
const util::mapped_file data (src);
|
2018-05-10 12:52:01 +10:00
|
|
|
return parse (util::view{data}.cast<const char*> ());
|
2016-06-28 16:57:25 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-28 15:59:53 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
json::tree::write (const json::tree::node &node, std::ostream &os)
|
2016-02-11 14:53:43 +11:00
|
|
|
{
|
2016-06-28 15:59:53 +10:00
|
|
|
node.write (os);
|
2016-02-11 14:53:43 +11:00
|
|
|
}
|
|
|
|
|
2016-06-28 15:59:53 +10:00
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-04-19 16:41:10 +10:00
|
|
|
// Type conversion
|
|
|
|
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::object&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_object (void) const&
|
2015-03-18 15:54:13 +11:00
|
|
|
{ throw json::type_error ("node is not an object"); }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::array&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_array (void) const&
|
2015-03-18 15:54:13 +11:00
|
|
|
{ throw json::type_error ("node is not an array"); }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::string&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_string (void) const&
|
2015-03-18 15:54:13 +11:00
|
|
|
{ throw json::type_error ("node is not a string"); }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::number&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_number (void) const&
|
2015-03-18 15:54:13 +11:00
|
|
|
{ throw json::type_error ("node is not a number"); }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::boolean&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_boolean (void) const&
|
2015-03-18 15:54:13 +11:00
|
|
|
{ throw json::type_error ("node is not a boolean"); }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::null&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_null (void) const&
|
2015-03-18 15:50:07 +11:00
|
|
|
{ throw json::type_error ("node is not a null"); }
|
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-03-18 15:50:07 +11:00
|
|
|
json::tree::object&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_object (void)&
|
2015-03-18 15:50:07 +11:00
|
|
|
{ throw json::type_error ("node is not an object"); }
|
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-18 15:50:07 +11:00
|
|
|
json::tree::array&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_array (void)&
|
2015-03-18 15:50:07 +11:00
|
|
|
{ throw json::type_error ("node is not an array"); }
|
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-18 15:50:07 +11:00
|
|
|
json::tree::string&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_string (void)&
|
2015-03-18 15:50:07 +11:00
|
|
|
{ throw json::type_error ("node is not a string"); }
|
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-18 15:50:07 +11:00
|
|
|
json::tree::number&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_number (void)&
|
2015-03-18 15:50:07 +11:00
|
|
|
{ throw json::type_error ("node is not a number"); }
|
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-18 15:50:07 +11:00
|
|
|
json::tree::boolean&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_boolean (void)&
|
2015-03-18 15:50:07 +11:00
|
|
|
{ throw json::type_error ("node is not a boolean"); }
|
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-03-18 15:50:07 +11:00
|
|
|
json::tree::null&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_null (void)&
|
2015-03-18 15:50:07 +11:00
|
|
|
{ throw json::type_error ("node is not a null"); }
|
2012-04-13 11:15:08 +10:00
|
|
|
|
|
|
|
|
2015-02-16 23:34:50 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-03-23 18:41:07 +11:00
|
|
|
bool
|
|
|
|
json::tree::node::as_bool (void) const
|
|
|
|
{
|
|
|
|
return !!as_boolean ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-16 23:34:50 +11:00
|
|
|
float
|
|
|
|
json::tree::node::as_float (void) const
|
|
|
|
{
|
2016-05-12 17:41:31 +10:00
|
|
|
return static_cast<float> (as_number ().real ());
|
2015-02-16 23:34:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
double
|
|
|
|
json::tree::node::as_double (void) const
|
|
|
|
{
|
2016-05-12 17:41:31 +10:00
|
|
|
return as_number ().real ();
|
2015-02-16 23:34:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-05-12 17:41:31 +10:00
|
|
|
uintmax_t
|
2015-02-16 23:34:50 +11:00
|
|
|
json::tree::node::as_uint (void) const
|
|
|
|
{
|
2016-05-12 17:41:31 +10:00
|
|
|
return as_number ().uint ();
|
|
|
|
}
|
|
|
|
|
2015-02-16 23:34:50 +11:00
|
|
|
|
2016-05-12 17:41:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
intmax_t
|
|
|
|
json::tree::node::as_sint (void) const
|
|
|
|
{
|
|
|
|
return as_number ().sint ();
|
2015-02-16 23:34:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:48:01 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const char*
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::as_chars (void) const&
|
2015-03-18 15:48:01 +11:00
|
|
|
{
|
|
|
|
return as_string ().native ().c_str ();
|
|
|
|
}
|
|
|
|
|
2012-04-19 16:41:10 +10:00
|
|
|
|
2015-09-08 14:33:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-01-05 15:06:49 +11:00
|
|
|
namespace json::tree {
|
2016-10-11 21:44:25 +11:00
|
|
|
#if defined(__clang__)
|
|
|
|
#elif defined(__GNUC__)
|
2016-06-29 17:55:12 +10:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wuseless-cast"
|
2016-10-11 21:44:25 +11:00
|
|
|
#endif
|
2015-09-08 14:33:39 +10:00
|
|
|
template <>
|
|
|
|
bool node::as (void) const
|
|
|
|
{
|
|
|
|
return as_bool ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <>
|
|
|
|
float json::tree::node::as (void) const
|
|
|
|
{
|
|
|
|
return as_float ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <>
|
|
|
|
double
|
|
|
|
json::tree::node::as (void) const
|
|
|
|
{
|
|
|
|
return as_double ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-05-12 17:41:31 +10:00
|
|
|
template <>
|
|
|
|
uint32_t
|
|
|
|
json::tree::node::as (void) const
|
|
|
|
{
|
|
|
|
return static_cast<uint32_t> (as_uint ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <>
|
|
|
|
uint64_t
|
|
|
|
json::tree::node::as (void) const
|
|
|
|
{
|
|
|
|
return static_cast<uint64_t> (as_uint ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <>
|
|
|
|
int32_t
|
|
|
|
json::tree::node::as (void) const
|
|
|
|
{
|
|
|
|
return static_cast<int32_t> (as_sint ());
|
|
|
|
}
|
2015-09-08 14:33:39 +10:00
|
|
|
|
2016-05-12 17:41:31 +10:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <>
|
|
|
|
int64_t
|
|
|
|
json::tree::node::as (void) const
|
|
|
|
{
|
|
|
|
return static_cast<int64_t> (as_sint ());
|
|
|
|
}
|
2018-01-13 13:48:58 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <>
|
|
|
|
const std::string&
|
|
|
|
json::tree::node::as (void) const
|
|
|
|
{
|
|
|
|
return as_string ().native ();
|
|
|
|
}
|
2016-10-11 21:44:25 +11:00
|
|
|
#if defined(__clang__)
|
|
|
|
#elif defined(__GNUC__)
|
2016-06-29 17:55:12 +10:00
|
|
|
#pragma GCC diagnostic pop
|
2016-10-11 21:44:25 +11:00
|
|
|
#endif
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2015-09-08 14:33:39 +10:00
|
|
|
|
|
|
|
|
2015-08-28 21:06:04 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Global operatoers
|
2011-05-23 17:18:52 +10:00
|
|
|
bool
|
2015-02-02 21:22:38 +11:00
|
|
|
json::tree::node::operator!= (const node &rhs) const
|
2011-05-23 17:18:52 +10:00
|
|
|
{ return !(*this == rhs); }
|
|
|
|
|
|
|
|
|
2015-08-28 20:36:21 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool json::tree::node::operator==(const char *rhs) const
|
|
|
|
{
|
2012-04-19 16:41:54 +10:00
|
|
|
try {
|
2012-04-20 18:07:42 +10:00
|
|
|
return as_string ().native () == rhs;
|
2015-02-03 00:07:09 +11:00
|
|
|
} catch (const json::type_error&) {
|
2012-04-19 16:41:54 +10:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 17:05:40 +11:00
|
|
|
|
|
|
|
|
2015-03-18 15:52:33 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::node&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::operator[] (const std::string &key)&
|
2015-03-18 15:52:33 +11:00
|
|
|
{ return as_object ()[key]; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::node&
|
2016-05-12 17:41:31 +10:00
|
|
|
json::tree::node::operator[] (size_t idx)&
|
2015-03-18 15:52:33 +11:00
|
|
|
{ return as_array()[idx]; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::node&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::node::operator[] (const std::string &key) const&
|
2012-04-12 16:08:06 +10:00
|
|
|
{ return as_object ()[key]; }
|
2011-09-23 13:45:09 +10:00
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::node&
|
2016-05-12 17:41:31 +10:00
|
|
|
json::tree::node::operator[] (size_t idx) const&
|
2012-04-12 16:08:06 +10:00
|
|
|
{ return as_array()[idx]; }
|
2011-09-23 13:45:09 +10:00
|
|
|
|
2012-04-19 16:41:10 +10:00
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-04-19 16:41:10 +10:00
|
|
|
// Object
|
2015-02-02 21:22:38 +11:00
|
|
|
json::tree::object::~object ()
|
2012-04-12 16:09:49 +10:00
|
|
|
{ ; }
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-03-18 15:44:46 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::unique_ptr<json::tree::node>
|
|
|
|
json::tree::object::clone (void) const
|
|
|
|
{
|
|
|
|
auto obj = std::make_unique<json::tree::object> ();
|
|
|
|
|
|
|
|
for (auto &i: *this)
|
|
|
|
obj->insert (i.first, i.second->clone ());
|
|
|
|
|
|
|
|
return std::move (obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
bool
|
2018-07-05 17:46:02 +10:00
|
|
|
json::tree::object::operator== (const json::tree::object &rhs) const
|
2015-03-18 15:54:13 +11:00
|
|
|
{
|
2018-07-05 17:46:02 +10:00
|
|
|
if (rhs.size () != size ())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (auto const &[i,j]: util::zip (m_values, rhs.m_values)) {
|
|
|
|
if (i.first != j.first)
|
2011-05-23 17:18:52 +10:00
|
|
|
return false;
|
2018-07-05 17:46:02 +10:00
|
|
|
|
|
|
|
if (*i.second != *j.second)
|
2011-05-23 17:18:52 +10:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
void
|
2015-08-28 21:06:04 +10:00
|
|
|
json::tree::object::insert (const std::string &_key, std::unique_ptr<json::tree::node> &&value)
|
2015-03-18 15:54:13 +11:00
|
|
|
{
|
|
|
|
m_values[_key] = move(value);
|
|
|
|
}
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-03-18 15:40:16 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::node&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::object::operator[](const std::string &key)&
|
2015-03-18 15:40:16 +11:00
|
|
|
{
|
|
|
|
auto value = m_values.find (key);
|
|
|
|
if (value == m_values.end ())
|
|
|
|
throw json::key_error (key);
|
|
|
|
|
|
|
|
return *value->second;
|
|
|
|
}
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-03-18 15:40:16 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-02 21:22:38 +11:00
|
|
|
const json::tree::node&
|
2016-03-17 18:05:28 +11:00
|
|
|
json::tree::object::operator[](const std::string &key) const&
|
2015-03-18 15:40:16 +11:00
|
|
|
{
|
2011-05-23 17:18:52 +10:00
|
|
|
auto value = m_values.find (key);
|
2015-03-18 15:40:16 +11:00
|
|
|
if (value == m_values.end ())
|
|
|
|
throw json::key_error (key);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
return *value->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-04-20 18:09:23 +10:00
|
|
|
bool
|
2015-03-18 15:54:13 +11:00
|
|
|
json::tree::object::has (const std::string &key) const
|
|
|
|
{
|
2012-04-20 18:09:23 +10:00
|
|
|
return m_values.find (key) != m_values.end ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 16:00:21 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::object::const_iterator
|
|
|
|
json::tree::object::find (const std::string &key) const
|
|
|
|
{
|
|
|
|
return m_values.find (key);
|
2012-04-19 16:44:35 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-02 21:22:38 +11:00
|
|
|
json::tree::object::const_iterator
|
|
|
|
json::tree::object::begin (void) const
|
2012-04-19 16:44:35 +10:00
|
|
|
{ return m_values.begin (); }
|
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-02-02 21:22:38 +11:00
|
|
|
json::tree::object::const_iterator
|
|
|
|
json::tree::object::end (void) const
|
2012-04-19 16:44:35 +10:00
|
|
|
{ return m_values.end (); }
|
|
|
|
|
|
|
|
|
2015-03-18 16:00:21 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
size_t
|
|
|
|
json::tree::object::size (void) const
|
|
|
|
{
|
|
|
|
return m_values.size ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
json::tree::object::clear (void)
|
|
|
|
{
|
|
|
|
m_values.clear ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
json::tree::object::erase (const std::string &key)
|
|
|
|
{
|
|
|
|
auto pos = m_values.find (key);
|
|
|
|
if (pos == m_values.end ())
|
|
|
|
throw json::error ("erasing invalid key");
|
|
|
|
|
|
|
|
m_values.erase (key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
std::ostream&
|
2015-03-18 15:54:13 +11:00
|
|
|
json::tree::object::write (std::ostream &os) const
|
|
|
|
{
|
2012-04-12 14:09:33 +10:00
|
|
|
os << "{\n";
|
|
|
|
{
|
2015-08-28 21:06:04 +10:00
|
|
|
util::indenter raii(os);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2012-04-12 14:09:33 +10:00
|
|
|
for (auto i = m_values.begin (); i != m_values.end ();) {
|
|
|
|
os << '"' << i->first << "\" : " << *i->second;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2012-04-12 14:09:33 +10:00
|
|
|
if (++i != m_values.end ())
|
|
|
|
os << ",\n";
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
2012-04-12 14:09:33 +10:00
|
|
|
os << "\n}";
|
2011-05-23 17:18:52 +10:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-04-19 16:41:10 +10:00
|
|
|
// Array
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-02-02 21:22:38 +11:00
|
|
|
json::tree::array::~array()
|
|
|
|
{
|
|
|
|
m_values.clear ();
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-03-18 15:44:46 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::unique_ptr<json::tree::node>
|
|
|
|
json::tree::array::clone (void) const
|
|
|
|
{
|
|
|
|
auto ret = std::make_unique<array> ();
|
|
|
|
|
|
|
|
for (const auto &i: *this)
|
|
|
|
ret->insert (i.clone ());
|
|
|
|
|
|
|
|
return std::move (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
void
|
2015-08-28 21:06:04 +10:00
|
|
|
json::tree::array::insert (std::unique_ptr<json::tree::node> &&_value)
|
2015-02-02 21:22:38 +11:00
|
|
|
{
|
|
|
|
m_values.push_back (move (_value));
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
bool
|
2015-03-18 15:54:13 +11:00
|
|
|
json::tree::array::operator==(const json::tree::array &rhs) const
|
|
|
|
{
|
2018-07-05 17:46:02 +10:00
|
|
|
if (rhs.size () != size ())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (auto const &[i,j]: util::zip (m_values, rhs.m_values))
|
|
|
|
if (*i != *j)
|
|
|
|
return false;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 16:01:10 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
json::tree::array::operator== (const node &rhs) const
|
|
|
|
{
|
|
|
|
return rhs == *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
size_t
|
|
|
|
json::tree::array::size (void) const
|
|
|
|
{
|
|
|
|
return m_values.size ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:52:33 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::node&
|
2016-05-12 17:41:31 +10:00
|
|
|
json::tree::array::operator[] (size_t idx)&
|
2015-03-18 15:52:33 +11:00
|
|
|
{
|
|
|
|
return *m_values[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const json::tree::node&
|
2016-05-12 17:41:31 +10:00
|
|
|
json::tree::array::operator[] (size_t idx) const&
|
2015-03-18 15:52:33 +11:00
|
|
|
{
|
|
|
|
return *m_values[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:59:29 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::array::iterator
|
|
|
|
json::tree::array::begin (void)
|
|
|
|
{
|
|
|
|
return iterator (m_values.begin ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::array::iterator
|
|
|
|
json::tree::array::end (void)
|
|
|
|
{
|
|
|
|
return iterator (m_values.end ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::array::const_iterator
|
|
|
|
json::tree::array::begin (void) const
|
|
|
|
{
|
|
|
|
return const_iterator (m_values.begin ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::array::const_iterator
|
|
|
|
json::tree::array::end (void) const
|
|
|
|
{
|
|
|
|
return const_iterator (m_values.end ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::array::const_iterator
|
|
|
|
json::tree::array::cbegin (void) const
|
|
|
|
{
|
|
|
|
return const_iterator (m_values.cbegin ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::array::const_iterator
|
|
|
|
json::tree::array::cend (void) const
|
|
|
|
{
|
|
|
|
return const_iterator (m_values.cend ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:52:33 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
std::ostream&
|
2015-02-02 21:22:38 +11:00
|
|
|
json::tree::array::write (std::ostream &os) const {
|
2014-05-20 14:11:28 +10:00
|
|
|
os << "[\n";
|
2012-04-12 14:09:33 +10:00
|
|
|
{
|
2015-08-28 21:06:04 +10:00
|
|
|
util::indenter raii(os);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2012-04-12 14:09:33 +10:00
|
|
|
for (auto i = m_values.begin (); i != m_values.end (); ++i) {
|
|
|
|
(*i)->write (os);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2012-04-12 14:09:33 +10:00
|
|
|
if (i != m_values.end () - 1)
|
|
|
|
os << ",\n";
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
2012-04-12 14:09:33 +10:00
|
|
|
os << "\n]";
|
2011-05-23 17:18:52 +10:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-04-19 16:41:10 +10:00
|
|
|
// String
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-03-18 15:44:46 +11:00
|
|
|
std::unique_ptr<json::tree::node>
|
|
|
|
json::tree::string::clone (void) const
|
|
|
|
{
|
|
|
|
return std::make_unique<json::tree::string> (m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
std::ostream&
|
2015-03-18 15:54:13 +11:00
|
|
|
json::tree::string::write (std::ostream &os) const
|
|
|
|
{
|
2011-05-23 17:18:52 +10:00
|
|
|
os << '"' << m_value << '"';
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
bool
|
2015-03-18 15:54:13 +11:00
|
|
|
json::tree::string::operator== (const json::tree::string &rhs) const
|
2011-05-23 17:18:52 +10:00
|
|
|
{ return rhs.m_value == m_value; }
|
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-04-20 18:18:47 +10:00
|
|
|
bool
|
2015-03-18 15:54:13 +11:00
|
|
|
json::tree::string::operator== (const char *rhs) const
|
2012-04-20 18:18:47 +10:00
|
|
|
{ return rhs == m_value; }
|
|
|
|
|
|
|
|
|
2015-03-18 16:01:29 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
json::tree::string::operator== (const std::string &rhs) const
|
|
|
|
{ return rhs == m_value; }
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-04-19 16:41:10 +10:00
|
|
|
// Number
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-03-18 15:44:46 +11:00
|
|
|
std::unique_ptr<json::tree::node>
|
|
|
|
json::tree::number::clone (void) const
|
|
|
|
{
|
2016-05-12 17:41:31 +10:00
|
|
|
switch (m_repr) {
|
|
|
|
case REAL: return std::make_unique<json::tree::number> (m_value.r);
|
|
|
|
case SINT: return std::make_unique<json::tree::number> (m_value.s);
|
|
|
|
case UINT: return std::make_unique<json::tree::number> (m_value.u);
|
|
|
|
}
|
|
|
|
|
|
|
|
unreachable ();
|
2015-03-18 15:44:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
std::ostream&
|
2015-03-18 15:54:13 +11:00
|
|
|
json::tree::number::write (std::ostream &os) const
|
|
|
|
{
|
2017-01-05 20:21:20 +11:00
|
|
|
util::stream::scoped::precision p (os);
|
2016-05-12 17:41:31 +10:00
|
|
|
|
|
|
|
switch (m_repr) {
|
2017-01-05 20:21:20 +11:00
|
|
|
case REAL: return os << std::setprecision (std::numeric_limits<real_t>::digits10) << m_value.r;
|
|
|
|
case SINT: return os << std::setprecision (std::numeric_limits<sint_t>::digits10) << m_value.s;
|
|
|
|
case UINT: return os << std::setprecision (std::numeric_limits<uint_t>::digits10) << m_value.u;
|
2016-05-12 17:41:31 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
unreachable ();
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
bool
|
2016-05-12 17:41:31 +10:00
|
|
|
json::tree::number::operator ==(const json::tree::number &rhs) const {
|
|
|
|
if (repr () != rhs.repr ())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (repr ()) {
|
|
|
|
case REAL: return util::almost_equal (real (), rhs.real ());
|
|
|
|
case SINT: return util::almost_equal (sint (), rhs.sint ());
|
|
|
|
case UINT: return util::almost_equal (uint (), rhs.uint ());
|
|
|
|
}
|
|
|
|
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-20 16:02:08 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-05-12 17:41:31 +10:00
|
|
|
json::tree::number::real_t
|
|
|
|
json::tree::number::real (void) const
|
|
|
|
{
|
2016-09-20 16:02:08 +10:00
|
|
|
switch (repr ()) {
|
|
|
|
case REAL:
|
|
|
|
return m_value.r;
|
|
|
|
|
|
|
|
case UINT:
|
|
|
|
if (uint_t (real_t (m_value.u)) != m_value.u)
|
|
|
|
throw type_error ("number is not a real");
|
|
|
|
return m_value.u;
|
|
|
|
|
|
|
|
case SINT:
|
|
|
|
if (sint_t (real_t (m_value.s)) != m_value.s)
|
|
|
|
throw type_error ("number is not a real");
|
|
|
|
return m_value.s;
|
|
|
|
}
|
2016-05-12 17:41:31 +10:00
|
|
|
|
2016-09-20 16:02:08 +10:00
|
|
|
unreachable ();
|
2016-05-12 17:41:31 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::number::sint_t
|
|
|
|
json::tree::number::sint (void) const
|
|
|
|
{
|
2016-09-20 16:02:08 +10:00
|
|
|
switch (repr ()) {
|
|
|
|
case SINT:
|
|
|
|
return m_value.s;
|
|
|
|
|
|
|
|
case REAL:
|
2018-01-31 19:33:42 +11:00
|
|
|
if (!::util::equal (real_t (sint_t (m_value.r)), m_value.r))
|
2016-09-20 16:02:08 +10:00
|
|
|
throw type_error ("number is not a sint");
|
|
|
|
return sint_t (m_value.r);
|
|
|
|
|
|
|
|
case UINT:
|
|
|
|
if (uint_t (sint_t (m_value.u)) != m_value.u)
|
|
|
|
throw type_error ("number is not a sint");
|
|
|
|
return m_value.s;
|
|
|
|
}
|
2016-05-12 17:41:31 +10:00
|
|
|
|
2016-09-20 16:02:08 +10:00
|
|
|
unreachable ();
|
2016-05-12 17:41:31 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::number::uint_t
|
|
|
|
json::tree::number::uint (void) const
|
|
|
|
{
|
2016-09-20 16:02:08 +10:00
|
|
|
switch (repr ()) {
|
|
|
|
case UINT:
|
|
|
|
return m_value.u;
|
|
|
|
|
|
|
|
case REAL:
|
2018-01-31 19:33:42 +11:00
|
|
|
if (!::util::equal (real_t (uint_t (m_value.r)), m_value.r))
|
2016-09-20 16:02:08 +10:00
|
|
|
throw type_error ("number is not a uint");
|
|
|
|
return uint_t (m_value.r);
|
|
|
|
|
|
|
|
case SINT:
|
|
|
|
if (sint_t (uint_t (m_value.s)) != m_value.s)
|
|
|
|
throw type_error ("number is not a uint");
|
|
|
|
return m_value.s;
|
|
|
|
}
|
2016-05-12 17:41:31 +10:00
|
|
|
|
2016-09-20 16:02:08 +10:00
|
|
|
unreachable ();
|
2016-05-12 17:41:31 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-20 16:02:08 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-05-12 17:41:31 +10:00
|
|
|
json::tree::number::operator json::tree::number::real_t (void) const
|
|
|
|
{
|
|
|
|
return real ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::number::operator json::tree::number::sint_t (void) const
|
|
|
|
{
|
|
|
|
return sint ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
json::tree::number::operator json::tree::number::uint_t (void) const
|
|
|
|
{
|
|
|
|
return uint ();
|
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-04-19 16:41:10 +10:00
|
|
|
// Boolean
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-03-18 15:44:46 +11:00
|
|
|
std::unique_ptr<json::tree::node>
|
|
|
|
json::tree::boolean::clone (void) const
|
|
|
|
{
|
|
|
|
return std::make_unique<json::tree::boolean> (m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
std::ostream&
|
2015-03-18 15:54:13 +11:00
|
|
|
json::tree::boolean::write (std::ostream &os) const
|
|
|
|
{
|
2012-04-12 14:09:33 +10:00
|
|
|
os << (m_value ? "true" : "false");
|
2011-05-23 17:18:52 +10:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
bool
|
2015-02-02 21:22:38 +11:00
|
|
|
json::tree::boolean::operator ==(const json::tree::boolean &rhs) const
|
2011-05-23 17:18:52 +10:00
|
|
|
{ return rhs.m_value == m_value; }
|
|
|
|
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-04-19 16:41:10 +10:00
|
|
|
// Null
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-03-18 15:44:46 +11:00
|
|
|
std::unique_ptr<json::tree::node>
|
|
|
|
json::tree::null::clone (void) const
|
|
|
|
{
|
|
|
|
return std::make_unique<json::tree::null> ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
std::ostream&
|
2015-03-18 15:54:13 +11:00
|
|
|
json::tree::null::write (std::ostream &os) const
|
|
|
|
{
|
2011-05-23 17:18:52 +10:00
|
|
|
os << "null";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-08-28 21:06:04 +10:00
|
|
|
std::ostream&
|
|
|
|
json::tree::operator<< (std::ostream &os, const json::tree::node &n)
|
2012-04-12 14:09:33 +10:00
|
|
|
{ return n.write (os); }
|
2013-08-05 20:54:19 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// to_json
|
|
|
|
|
2017-01-05 15:06:49 +11:00
|
|
|
namespace json::tree {
|
2013-08-05 20:54:19 +10:00
|
|
|
template <>
|
|
|
|
std::unique_ptr<node>
|
|
|
|
io<bool>::serialise (const bool &b) {
|
|
|
|
return std::unique_ptr<node> (new boolean (b));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
std::unique_ptr<node>
|
2015-08-28 21:06:04 +10:00
|
|
|
io<std::nullptr_t>::serialise (const std::nullptr_t &) {
|
2013-08-05 20:54:19 +10:00
|
|
|
return std::unique_ptr<node> (new null ());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
std::unique_ptr<node>
|
|
|
|
io<std::string>::serialise (const std::string &s) {
|
|
|
|
return std::unique_ptr<node> (new string (s));
|
|
|
|
}
|
2013-08-05 22:39:34 +10:00
|
|
|
|
|
|
|
template <>
|
|
|
|
std::unique_ptr<node>
|
2016-05-12 17:41:31 +10:00
|
|
|
io<int32_t>::serialise (const int32_t &i) {
|
|
|
|
return std::unique_ptr<node> (new number (intmax_t {i}));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
std::unique_ptr<node>
|
|
|
|
io<int64_t>::serialise (const int64_t &i) {
|
|
|
|
return std::unique_ptr<node> (new number (intmax_t {i}));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
std::unique_ptr<node>
|
|
|
|
io<uint32_t>::serialise (const uint32_t &i) {
|
|
|
|
return std::unique_ptr<node> (new number (uintmax_t {i}));
|
2013-08-05 22:39:34 +10:00
|
|
|
}
|
|
|
|
|
2014-09-01 16:25:24 +10:00
|
|
|
template <>
|
|
|
|
std::unique_ptr<node>
|
2016-05-12 17:41:31 +10:00
|
|
|
io<uint64_t>::serialise (const uint64_t &i) {
|
|
|
|
return std::unique_ptr<node> (new number (uintmax_t {i}));
|
2014-09-01 16:25:24 +10:00
|
|
|
}
|
|
|
|
|
2013-08-05 22:39:34 +10:00
|
|
|
template <>
|
|
|
|
std::unique_ptr<node>
|
|
|
|
io<float>::serialise (const float &f) {
|
|
|
|
return std::unique_ptr<node> (new number (f));
|
|
|
|
}
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|