summaryrefslogtreecommitdiffstats
path: root/src/json_spirit
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/json_spirit
parentInitial commit. (diff)
downloadceph-upstream/16.2.11+ds.tar.xz
ceph-upstream/16.2.11+ds.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/json_spirit')
-rw-r--r--src/json_spirit/CMakeLists.txt4
-rw-r--r--src/json_spirit/json_spirit.h18
-rw-r--r--src/json_spirit/json_spirit_error_position.h54
-rw-r--r--src/json_spirit/json_spirit_reader.cpp137
-rw-r--r--src/json_spirit/json_spirit_reader.h62
-rw-r--r--src/json_spirit/json_spirit_reader_template.h661
-rw-r--r--src/json_spirit/json_spirit_stream_reader.h70
-rw-r--r--src/json_spirit/json_spirit_utils.h63
-rw-r--r--src/json_spirit/json_spirit_value.cpp8
-rw-r--r--src/json_spirit/json_spirit_value.h584
-rw-r--r--src/json_spirit/json_spirit_writer.cpp96
-rw-r--r--src/json_spirit/json_spirit_writer.h63
-rw-r--r--src/json_spirit/json_spirit_writer_options.h30
-rw-r--r--src/json_spirit/json_spirit_writer_template.h384
14 files changed, 2234 insertions, 0 deletions
diff --git a/src/json_spirit/CMakeLists.txt b/src/json_spirit/CMakeLists.txt
new file mode 100644
index 000000000..b3b5ce2e6
--- /dev/null
+++ b/src/json_spirit/CMakeLists.txt
@@ -0,0 +1,4 @@
+add_library(json_spirit STATIC
+ json_spirit_reader.cpp
+ json_spirit_writer.cpp)
+target_link_libraries(json_spirit common_utf8)
diff --git a/src/json_spirit/json_spirit.h b/src/json_spirit/json_spirit.h
new file mode 100644
index 000000000..d574af5b8
--- /dev/null
+++ b/src/json_spirit/json_spirit.h
@@ -0,0 +1,18 @@
+#ifndef JSON_SPIRIT
+#define JSON_SPIRIT
+
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include "json_spirit_value.h"
+#include "json_spirit_reader.h"
+#include "json_spirit_writer.h"
+#include "json_spirit_utils.h"
+
+#endif
diff --git a/src/json_spirit/json_spirit_error_position.h b/src/json_spirit/json_spirit_error_position.h
new file mode 100644
index 000000000..e6f63bbc6
--- /dev/null
+++ b/src/json_spirit/json_spirit_error_position.h
@@ -0,0 +1,54 @@
+#ifndef JSON_SPIRIT_ERROR_POSITION
+#define JSON_SPIRIT_ERROR_POSITION
+
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include <string>
+
+namespace json_spirit
+{
+ // An Error_position exception is thrown by the "read_or_throw" functions below on finding an error.
+ // Note the "read_or_throw" functions are around 3 times slower than the standard functions "read"
+ // functions that return a bool.
+ //
+ struct Error_position
+ {
+ Error_position();
+ Error_position( unsigned int line, unsigned int column, const std::string& reason );
+ bool operator==( const Error_position& lhs ) const;
+ unsigned int line_;
+ unsigned int column_;
+ std::string reason_;
+ };
+
+ inline Error_position::Error_position()
+ : line_( 0 )
+ , column_( 0 )
+ {
+ }
+
+ inline Error_position::Error_position( unsigned int line, unsigned int column, const std::string& reason )
+ : line_( line )
+ , column_( column )
+ , reason_( reason )
+ {
+ }
+
+ inline bool Error_position::operator==( const Error_position& lhs ) const
+ {
+ if( this == &lhs ) return true;
+
+ return ( reason_ == lhs.reason_ ) &&
+ ( line_ == lhs.line_ ) &&
+ ( column_ == lhs.column_ );
+ }
+}
+
+#endif
diff --git a/src/json_spirit/json_spirit_reader.cpp b/src/json_spirit/json_spirit_reader.cpp
new file mode 100644
index 000000000..268aceac4
--- /dev/null
+++ b/src/json_spirit/json_spirit_reader.cpp
@@ -0,0 +1,137 @@
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#include "json_spirit_reader.h"
+#include "json_spirit_reader_template.h"
+
+using namespace json_spirit;
+
+#ifdef JSON_SPIRIT_VALUE_ENABLED
+ bool json_spirit::read( const std::string& s, Value& value )
+ {
+ return read_string( s, value );
+ }
+
+ void json_spirit::read_or_throw( const std::string& s, Value& value )
+ {
+ read_string_or_throw( s, value );
+ }
+
+ bool json_spirit::read( std::istream& is, Value& value )
+ {
+ return read_stream( is, value );
+ }
+
+ void json_spirit::read_or_throw( std::istream& is, Value& value )
+ {
+ read_stream_or_throw( is, value );
+ }
+
+ bool json_spirit::read( std::string::const_iterator& begin, std::string::const_iterator end, Value& value )
+ {
+ return read_range( begin, end, value );
+ }
+
+ void json_spirit::read_or_throw( std::string::const_iterator& begin, std::string::const_iterator end, Value& value )
+ {
+ begin = read_range_or_throw( begin, end, value );
+ }
+#endif
+
+#if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ bool json_spirit::read( const std::wstring& s, wValue& value )
+ {
+ return read_string( s, value );
+ }
+
+ void json_spirit::read_or_throw( const std::wstring& s, wValue& value )
+ {
+ read_string_or_throw( s, value );
+ }
+
+ bool json_spirit::read( std::wistream& is, wValue& value )
+ {
+ return read_stream( is, value );
+ }
+
+ void json_spirit::read_or_throw( std::wistream& is, wValue& value )
+ {
+ read_stream_or_throw( is, value );
+ }
+
+ bool json_spirit::read( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wValue& value )
+ {
+ return read_range( begin, end, value );
+ }
+
+ void json_spirit::read_or_throw( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wValue& value )
+ {
+ begin = read_range_or_throw( begin, end, value );
+ }
+#endif
+
+#ifdef JSON_SPIRIT_MVALUE_ENABLED
+ bool json_spirit::read( const std::string& s, mValue& value )
+ {
+ return read_string( s, value );
+ }
+
+ void json_spirit::read_or_throw( const std::string& s, mValue& value )
+ {
+ read_string_or_throw( s, value );
+ }
+
+ bool json_spirit::read( std::istream& is, mValue& value )
+ {
+ return read_stream( is, value );
+ }
+
+ void json_spirit::read_or_throw( std::istream& is, mValue& value )
+ {
+ read_stream_or_throw( is, value );
+ }
+
+ bool json_spirit::read( std::string::const_iterator& begin, std::string::const_iterator end, mValue& value )
+ {
+ return read_range( begin, end, value );
+ }
+
+ void json_spirit::read_or_throw( std::string::const_iterator& begin, std::string::const_iterator end, mValue& value )
+ {
+ begin = read_range_or_throw( begin, end, value );
+ }
+#endif
+
+#if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ bool json_spirit::read( const std::wstring& s, wmValue& value )
+ {
+ return read_string( s, value );
+ }
+
+ void json_spirit::read_or_throw( const std::wstring& s, wmValue& value )
+ {
+ read_string_or_throw( s, value );
+ }
+
+ bool json_spirit::read( std::wistream& is, wmValue& value )
+ {
+ return read_stream( is, value );
+ }
+
+ void json_spirit::read_or_throw( std::wistream& is, wmValue& value )
+ {
+ read_stream_or_throw( is, value );
+ }
+
+ bool json_spirit::read( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wmValue& value )
+ {
+ return read_range( begin, end, value );
+ }
+
+ void json_spirit::read_or_throw( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wmValue& value )
+ {
+ begin = read_range_or_throw( begin, end, value );
+ }
+#endif
diff --git a/src/json_spirit/json_spirit_reader.h b/src/json_spirit/json_spirit_reader.h
new file mode 100644
index 000000000..e8caa8f99
--- /dev/null
+++ b/src/json_spirit/json_spirit_reader.h
@@ -0,0 +1,62 @@
+#ifndef JSON_SPIRIT_READER
+#define JSON_SPIRIT_READER
+
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include "json_spirit_value.h"
+#include "json_spirit_error_position.h"
+#include <iostream>
+
+namespace json_spirit
+{
+ // functions to reads a JSON values
+
+#ifdef JSON_SPIRIT_VALUE_ENABLED
+ bool read( const std::string& s, Value& value );
+ bool read( std::istream& is, Value& value );
+ bool read( std::string::const_iterator& begin, std::string::const_iterator end, Value& value );
+
+ void read_or_throw( const std::string& s, Value& value );
+ void read_or_throw( std::istream& is, Value& value );
+ void read_or_throw( std::string::const_iterator& begin, std::string::const_iterator end, Value& value );
+#endif
+
+#if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ bool read( const std::wstring& s, wValue& value );
+ bool read( std::wistream& is, wValue& value );
+ bool read( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wValue& value );
+
+ void read_or_throw( const std::wstring& s, wValue& value );
+ void read_or_throw( std::wistream& is, wValue& value );
+ void read_or_throw( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wValue& value );
+#endif
+
+#ifdef JSON_SPIRIT_MVALUE_ENABLED
+ bool read( const std::string& s, mValue& value );
+ bool read( std::istream& is, mValue& value );
+ bool read( std::string::const_iterator& begin, std::string::const_iterator end, mValue& value );
+
+ void read_or_throw( const std::string& s, mValue& value );
+ void read_or_throw( std::istream& is, mValue& value );
+ void read_or_throw( std::string::const_iterator& begin, std::string::const_iterator end, mValue& value );
+#endif
+
+#if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ bool read( const std::wstring& s, wmValue& value );
+ bool read( std::wistream& is, wmValue& value );
+ bool read( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wmValue& value );
+
+ void read_or_throw( const std::wstring& s, wmValue& value );
+ void read_or_throw( std::wistream& is, wmValue& value );
+ void read_or_throw( std::wstring::const_iterator& begin, std::wstring::const_iterator end, wmValue& value );
+#endif
+}
+
+#endif
diff --git a/src/json_spirit/json_spirit_reader_template.h b/src/json_spirit/json_spirit_reader_template.h
new file mode 100644
index 000000000..a5790b9b5
--- /dev/null
+++ b/src/json_spirit/json_spirit_reader_template.h
@@ -0,0 +1,661 @@
+#ifndef JSON_SPIRIT_READER_TEMPLATE
+#define JSON_SPIRIT_READER_TEMPLATE
+
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include "json_spirit_value.h"
+#include "json_spirit_error_position.h"
+
+#include "common/utf8.h"
+
+#define BOOST_SPIRIT_THREADSAFE // uncomment for multithreaded use, requires linking to boost.thread
+
+#include <boost/bind/bind.hpp>
+#include <boost/function.hpp>
+#include <boost/version.hpp>
+#include <boost/spirit/include/classic_core.hpp>
+#include <boost/spirit/include/classic_confix.hpp>
+#include <boost/spirit/include/classic_escape_char.hpp>
+#include <boost/spirit/include/classic_multi_pass.hpp>
+#include <boost/spirit/include/classic_position_iterator.hpp>
+
+#include "include/ceph_assert.h"
+
+namespace json_spirit
+{
+ namespace spirit_namespace = boost::spirit::classic;
+
+ const spirit_namespace::int_parser < boost::int64_t > int64_p = spirit_namespace::int_parser < boost::int64_t >();
+ const spirit_namespace::uint_parser< boost::uint64_t > uint64_p = spirit_namespace::uint_parser< boost::uint64_t >();
+
+ template< class Iter_type >
+ bool is_eq( Iter_type first, Iter_type last, const char* c_str )
+ {
+ for( Iter_type i = first; i != last; ++i, ++c_str )
+ {
+ if( *c_str == 0 ) return false;
+
+ if( *i != *c_str ) return false;
+ }
+
+ return true;
+ }
+
+ template< class Char_type >
+ Char_type hex_to_num( const Char_type c )
+ {
+ if( ( c >= '0' ) && ( c <= '9' ) ) return c - '0';
+ if( ( c >= 'a' ) && ( c <= 'f' ) ) return c - 'a' + 10;
+ if( ( c >= 'A' ) && ( c <= 'F' ) ) return c - 'A' + 10;
+ return 0;
+ }
+
+ template< class Char_type, class Iter_type >
+ Char_type hex_str_to_char( Iter_type& begin )
+ {
+ const Char_type c1( *( ++begin ) );
+ const Char_type c2( *( ++begin ) );
+
+ return ( hex_to_num( c1 ) << 4 ) + hex_to_num( c2 );
+ }
+
+ template< class String_type, class Iter_type >
+ String_type unicode_str_to_utf8( Iter_type& begin );
+
+ template<>
+ std::string unicode_str_to_utf8( std::string::const_iterator & begin )
+ {
+ typedef std::string::value_type Char_type;
+
+ const Char_type c1( *( ++begin ) );
+ const Char_type c2( *( ++begin ) );
+ const Char_type c3( *( ++begin ) );
+ const Char_type c4( *( ++begin ) );
+
+ unsigned long uc = ( hex_to_num( c1 ) << 12 ) +
+ ( hex_to_num( c2 ) << 8 ) +
+ ( hex_to_num( c3 ) << 4 ) +
+ hex_to_num( c4 );
+
+ unsigned char buf[7]; // MAX_UTF8_SZ is 6 (see src/common/utf8.c)
+ int r = encode_utf8(uc, buf);
+ if (r >= 0) {
+ return std::string(reinterpret_cast<char *>(buf), r);
+ }
+ return std::string("_");
+ }
+
+ template< class String_type >
+ void append_esc_char_and_incr_iter( String_type& s,
+ typename String_type::const_iterator& begin,
+ typename String_type::const_iterator end )
+ {
+ typedef typename String_type::value_type Char_type;
+
+ const Char_type c2( *begin );
+
+ switch( c2 )
+ {
+ case 't': s += '\t'; break;
+ case 'b': s += '\b'; break;
+ case 'f': s += '\f'; break;
+ case 'n': s += '\n'; break;
+ case 'r': s += '\r'; break;
+ case '\\': s += '\\'; break;
+ case '/': s += '/'; break;
+ case '"': s += '"'; break;
+ case 'x':
+ {
+ if( end - begin >= 3 ) // expecting "xHH..."
+ {
+ s += hex_str_to_char< Char_type >( begin );
+ }
+ break;
+ }
+ case 'u':
+ {
+ if( end - begin >= 5 ) // expecting "uHHHH..."
+ {
+ s += unicode_str_to_utf8< String_type >( begin );
+ }
+ break;
+ }
+ }
+ }
+
+ template< class String_type >
+ String_type substitute_esc_chars( typename String_type::const_iterator begin,
+ typename String_type::const_iterator end )
+ {
+ typedef typename String_type::const_iterator Iter_type;
+
+ if( end - begin < 2 ) return String_type( begin, end );
+
+ String_type result;
+
+ result.reserve( end - begin );
+
+ const Iter_type end_minus_1( end - 1 );
+
+ Iter_type substr_start = begin;
+ Iter_type i = begin;
+
+ for( ; i < end_minus_1; ++i )
+ {
+ if( *i == '\\' )
+ {
+ result.append( substr_start, i );
+
+ ++i; // skip the '\'
+
+ append_esc_char_and_incr_iter( result, i, end );
+
+ substr_start = i + 1;
+ }
+ }
+
+ result.append( substr_start, end );
+
+ return result;
+ }
+
+ template< class String_type >
+ String_type get_str_( typename String_type::const_iterator begin,
+ typename String_type::const_iterator end )
+ {
+ ceph_assert( end - begin >= 2 );
+
+ typedef typename String_type::const_iterator Iter_type;
+
+ Iter_type str_without_quotes( ++begin );
+ Iter_type end_without_quotes( --end );
+
+ return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes );
+ }
+
+ inline std::string get_str( std::string::const_iterator begin, std::string::const_iterator end )
+ {
+ return get_str_< std::string >( begin, end );
+ }
+
+// Need this guard else it tries to instantiate unicode_str_to_utf8 with a
+// std::wstring, which isn't presently implemented
+#if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ inline std::wstring get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end )
+ {
+ return get_str_< std::wstring >( begin, end );
+ }
+#endif
+
+ template< class String_type, class Iter_type >
+ String_type get_str( Iter_type begin, Iter_type end )
+ {
+ const String_type tmp( begin, end ); // convert multipass iterators to string iterators
+
+ return get_str( tmp.begin(), tmp.end() );
+ }
+
+ using namespace boost::placeholders;
+
+ // this class's methods get called by the spirit parse resulting
+ // in the creation of a JSON object or array
+ //
+ // NB Iter_type could be a std::string iterator, wstring iterator, a position iterator or a multipass iterator
+ //
+ template< class Value_type, class Iter_type >
+ class Semantic_actions
+ {
+ public:
+
+ typedef typename Value_type::Config_type Config_type;
+ typedef typename Config_type::String_type String_type;
+ typedef typename Config_type::Object_type Object_type;
+ typedef typename Config_type::Array_type Array_type;
+ typedef typename String_type::value_type Char_type;
+
+ Semantic_actions( Value_type& value )
+ : value_( value )
+ , current_p_( 0 )
+ {
+ }
+
+ void begin_obj( Char_type c )
+ {
+ ceph_assert( c == '{' );
+
+ begin_compound< Object_type >();
+ }
+
+ void end_obj( Char_type c )
+ {
+ ceph_assert( c == '}' );
+
+ end_compound();
+ }
+
+ void begin_array( Char_type c )
+ {
+ ceph_assert( c == '[' );
+
+ begin_compound< Array_type >();
+ }
+
+ void end_array( Char_type c )
+ {
+ ceph_assert( c == ']' );
+
+ end_compound();
+ }
+
+ void new_name( Iter_type begin, Iter_type end )
+ {
+ ceph_assert( current_p_->type() == obj_type );
+
+ name_ = get_str< String_type >( begin, end );
+ }
+
+ void new_str( Iter_type begin, Iter_type end )
+ {
+ add_to_current( get_str< String_type >( begin, end ) );
+ }
+
+ void new_true( Iter_type begin, Iter_type end )
+ {
+ ceph_assert( is_eq( begin, end, "true" ) );
+
+ add_to_current( true );
+ }
+
+ void new_false( Iter_type begin, Iter_type end )
+ {
+ ceph_assert( is_eq( begin, end, "false" ) );
+
+ add_to_current( false );
+ }
+
+ void new_null( Iter_type begin, Iter_type end )
+ {
+ ceph_assert( is_eq( begin, end, "null" ) );
+
+ add_to_current( Value_type() );
+ }
+
+ void new_int( boost::int64_t i )
+ {
+ add_to_current( i );
+ }
+
+ void new_uint64( boost::uint64_t ui )
+ {
+ add_to_current( ui );
+ }
+
+ void new_real( double d )
+ {
+ add_to_current( d );
+ }
+
+ private:
+
+ Semantic_actions& operator=( const Semantic_actions& );
+ // to prevent "assignment operator could not be generated" warning
+
+ Value_type* add_first( const Value_type& value )
+ {
+ ceph_assert( current_p_ == 0 );
+
+ value_ = value;
+ current_p_ = &value_;
+ return current_p_;
+ }
+
+ template< class Array_or_obj >
+ void begin_compound()
+ {
+ if( current_p_ == 0 )
+ {
+ add_first( Array_or_obj() );
+ }
+ else
+ {
+ stack_.push_back( current_p_ );
+
+ Array_or_obj new_array_or_obj; // avoid copy by building new array or object in place
+
+ current_p_ = add_to_current( new_array_or_obj );
+ }
+ }
+
+ void end_compound()
+ {
+ if( current_p_ != &value_ )
+ {
+ current_p_ = stack_.back();
+
+ stack_.pop_back();
+ }
+ }
+
+ Value_type* add_to_current( const Value_type& value )
+ {
+ if( current_p_ == 0 )
+ {
+ return add_first( value );
+ }
+ else if( current_p_->type() == array_type )
+ {
+ current_p_->get_array().push_back( value );
+
+ return &current_p_->get_array().back();
+ }
+
+ ceph_assert( current_p_->type() == obj_type );
+
+ return &Config_type::add( current_p_->get_obj(), name_, value );
+ }
+
+ Value_type& value_; // this is the object or array that is being created
+ Value_type* current_p_; // the child object or array that is currently being constructed
+
+ std::vector< Value_type* > stack_; // previous child objects and arrays
+
+ String_type name_; // of current name/value pair
+ };
+
+ template< typename Iter_type >
+ void throw_error( spirit_namespace::position_iterator< Iter_type > i, const std::string& reason )
+ {
+ throw Error_position( i.get_position().line, i.get_position().column, reason );
+ }
+
+ template< typename Iter_type >
+ void throw_error( Iter_type i, const std::string& reason )
+ {
+ throw reason;
+ }
+
+ // the spirit grammar
+ //
+ template< class Value_type, class Iter_type >
+ class Json_grammer : public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
+ {
+ public:
+
+ typedef Semantic_actions< Value_type, Iter_type > Semantic_actions_t;
+
+ Json_grammer( Semantic_actions_t& semantic_actions )
+ : actions_( semantic_actions )
+ {
+ }
+
+ static void throw_not_value( Iter_type begin, Iter_type end )
+ {
+ throw_error( begin, "not a value" );
+ }
+
+ static void throw_not_array( Iter_type begin, Iter_type end )
+ {
+ throw_error( begin, "not an array" );
+ }
+
+ static void throw_not_object( Iter_type begin, Iter_type end )
+ {
+ throw_error( begin, "not an object" );
+ }
+
+ static void throw_not_pair( Iter_type begin, Iter_type end )
+ {
+ throw_error( begin, "not a pair" );
+ }
+
+ static void throw_not_colon( Iter_type begin, Iter_type end )
+ {
+ throw_error( begin, "no colon in pair" );
+ }
+
+ static void throw_not_string( Iter_type begin, Iter_type end )
+ {
+ throw_error( begin, "not a string" );
+ }
+
+ template< typename ScannerT >
+ class definition
+ {
+ public:
+
+ definition( const Json_grammer& self )
+ {
+ using namespace spirit_namespace;
+
+ typedef typename Value_type::String_type::value_type Char_type;
+
+ // first we convert the semantic action class methods to functors with the
+ // parameter signature expected by spirit
+
+ typedef boost::function< void( Char_type ) > Char_action;
+ typedef boost::function< void( Iter_type, Iter_type ) > Str_action;
+ typedef boost::function< void( double ) > Real_action;
+ typedef boost::function< void( boost::int64_t ) > Int_action;
+ typedef boost::function< void( boost::uint64_t ) > Uint64_action;
+
+ Char_action begin_obj ( boost::bind( &Semantic_actions_t::begin_obj, &self.actions_, _1 ) );
+ Char_action end_obj ( boost::bind( &Semantic_actions_t::end_obj, &self.actions_, _1 ) );
+ Char_action begin_array( boost::bind( &Semantic_actions_t::begin_array, &self.actions_, _1 ) );
+ Char_action end_array ( boost::bind( &Semantic_actions_t::end_array, &self.actions_, _1 ) );
+ Str_action new_name ( boost::bind( &Semantic_actions_t::new_name, &self.actions_, _1, _2 ) );
+ Str_action new_str ( boost::bind( &Semantic_actions_t::new_str, &self.actions_, _1, _2 ) );
+ Str_action new_true ( boost::bind( &Semantic_actions_t::new_true, &self.actions_, _1, _2 ) );
+ Str_action new_false ( boost::bind( &Semantic_actions_t::new_false, &self.actions_, _1, _2 ) );
+ Str_action new_null ( boost::bind( &Semantic_actions_t::new_null, &self.actions_, _1, _2 ) );
+ Real_action new_real ( boost::bind( &Semantic_actions_t::new_real, &self.actions_, _1 ) );
+ Int_action new_int ( boost::bind( &Semantic_actions_t::new_int, &self.actions_, _1 ) );
+ Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &self.actions_, _1 ) );
+
+ // actual grammar
+
+ json_
+ = value_ | eps_p[ &throw_not_value ]
+ ;
+
+ value_
+ = string_[ new_str ]
+ | number_
+ | object_
+ | array_
+ | str_p( "true" ) [ new_true ]
+ | str_p( "false" )[ new_false ]
+ | str_p( "null" ) [ new_null ]
+ ;
+
+ object_
+ = ch_p('{')[ begin_obj ]
+ >> !members_
+ >> ( ch_p('}')[ end_obj ] | eps_p[ &throw_not_object ] )
+ ;
+
+ members_
+ = pair_ >> *( ',' >> pair_ | ch_p(',') )
+ ;
+
+ pair_
+ = string_[ new_name ]
+ >> ( ':' | eps_p[ &throw_not_colon ] )
+ >> ( value_ | eps_p[ &throw_not_value ] )
+ ;
+
+ array_
+ = ch_p('[')[ begin_array ]
+ >> !elements_
+ >> ( ch_p(']')[ end_array ] | eps_p[ &throw_not_array ] )
+ ;
+
+ elements_
+ = value_ >> *( ',' >> value_ | ch_p(',') )
+ ;
+
+ string_
+ = lexeme_d // this causes white space inside a string to be retained
+ [
+ confix_p
+ (
+ '"',
+ *lex_escape_ch_p,
+ '"'
+ )
+ ]
+ ;
+
+ number_
+ = strict_real_p[ new_real ]
+ | int64_p [ new_int ]
+ | uint64_p [ new_uint64 ]
+ ;
+ }
+
+ spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, number_;
+
+ const spirit_namespace::rule< ScannerT >& start() const { return json_; }
+ };
+
+ private:
+
+ Json_grammer& operator=( const Json_grammer& ); // to prevent "assignment operator could not be generated" warning
+
+ Semantic_actions_t& actions_;
+ };
+
+ template< class Iter_type, class Value_type >
+ void add_posn_iter_and_read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
+ {
+ typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
+
+ const Posn_iter_t posn_begin( begin, end );
+ const Posn_iter_t posn_end( end, end );
+
+ read_range_or_throw( posn_begin, posn_end, value );
+ }
+
+ template< class Istream_type >
+ struct Multi_pass_iters
+ {
+ typedef typename Istream_type::char_type Char_type;
+ typedef std::istream_iterator< Char_type, Char_type > istream_iter;
+ typedef spirit_namespace::multi_pass< istream_iter > Mp_iter;
+
+ Multi_pass_iters( Istream_type& is )
+ {
+ is.unsetf( std::ios::skipws );
+
+ begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) );
+ end_ = spirit_namespace::make_multi_pass( istream_iter() );
+ }
+
+ Mp_iter begin_;
+ Mp_iter end_;
+ };
+
+ // reads a JSON Value from a pair of input iterators throwing an exception on invalid input, e.g.
+ //
+ // string::const_iterator start = str.begin();
+ // const string::const_iterator next = read_range_or_throw( str.begin(), str.end(), value );
+ //
+ // The iterator 'next' will point to the character past the
+ // last one read.
+ //
+ template< class Iter_type, class Value_type >
+ Iter_type read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
+ {
+ Semantic_actions< Value_type, Iter_type > semantic_actions( value );
+
+ const spirit_namespace::parse_info< Iter_type > info =
+ spirit_namespace::parse( begin, end,
+ Json_grammer< Value_type, Iter_type >( semantic_actions ),
+ spirit_namespace::space_p );
+
+ if( !info.hit )
+ {
+ ceph_assert( false ); // in theory exception should already have been thrown
+ throw_error( info.stop, "error" );
+ }
+
+ return info.stop;
+ }
+
+ // reads a JSON Value from a pair of input iterators, e.g.
+ //
+ // string::const_iterator start = str.begin();
+ // const bool success = read_string( start, str.end(), value );
+ //
+ // The iterator 'start' will point to the character past the
+ // last one read.
+ //
+ template< class Iter_type, class Value_type >
+ bool read_range( Iter_type& begin, Iter_type end, Value_type& value )
+ {
+ try
+ {
+ begin = read_range_or_throw( begin, end, value );
+
+ return true;
+ }
+ catch( ... )
+ {
+ return false;
+ }
+ }
+
+ // reads a JSON Value from a string, e.g.
+ //
+ // const bool success = read_string( str, value );
+ //
+ template< class String_type, class Value_type >
+ bool read_string( const String_type& s, Value_type& value )
+ {
+ typename String_type::const_iterator begin = s.begin();
+
+ return read_range( begin, s.end(), value );
+ }
+
+ // reads a JSON Value from a string throwing an exception on invalid input, e.g.
+ //
+ // read_string_or_throw( is, value );
+ //
+ template< class String_type, class Value_type >
+ void read_string_or_throw( const String_type& s, Value_type& value )
+ {
+ add_posn_iter_and_read_range_or_throw( s.begin(), s.end(), value );
+ }
+
+ // reads a JSON Value from a stream, e.g.
+ //
+ // const bool success = read_stream( is, value );
+ //
+ template< class Istream_type, class Value_type >
+ bool read_stream( Istream_type& is, Value_type& value )
+ {
+ Multi_pass_iters< Istream_type > mp_iters( is );
+
+ return read_range( mp_iters.begin_, mp_iters.end_, value );
+ }
+
+ // reads a JSON Value from a stream throwing an exception on invalid input, e.g.
+ //
+ // read_stream_or_throw( is, value );
+ //
+ template< class Istream_type, class Value_type >
+ void read_stream_or_throw( Istream_type& is, Value_type& value )
+ {
+ const Multi_pass_iters< Istream_type > mp_iters( is );
+
+ add_posn_iter_and_read_range_or_throw( mp_iters.begin_, mp_iters.end_, value );
+ }
+}
+
+#endif
diff --git a/src/json_spirit/json_spirit_stream_reader.h b/src/json_spirit/json_spirit_stream_reader.h
new file mode 100644
index 000000000..916725c15
--- /dev/null
+++ b/src/json_spirit/json_spirit_stream_reader.h
@@ -0,0 +1,70 @@
+#ifndef JSON_SPIRIT_READ_STREAM
+#define JSON_SPIRIT_READ_STREAM
+
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include "json_spirit_reader_template.h"
+
+namespace json_spirit
+{
+ // these classes allows you to read multiple top level contiguous values from a stream,
+ // the normal stream read functions have a bug that prevent multiple top level values
+ // from being read unless they are separated by spaces
+
+ template< class Istream_type, class Value_type >
+ class Stream_reader
+ {
+ public:
+
+ Stream_reader( Istream_type& is )
+ : iters_( is )
+ {
+ }
+
+ bool read_next( Value_type& value )
+ {
+ return read_range( iters_.begin_, iters_.end_, value );
+ }
+
+ private:
+
+ typedef Multi_pass_iters< Istream_type > Mp_iters;
+
+ Mp_iters iters_;
+ };
+
+ template< class Istream_type, class Value_type >
+ class Stream_reader_thrower
+ {
+ public:
+
+ Stream_reader_thrower( Istream_type& is )
+ : iters_( is )
+ , posn_begin_( iters_.begin_, iters_.end_ )
+ , posn_end_( iters_.end_, iters_.end_ )
+ {
+ }
+
+ void read_next( Value_type& value )
+ {
+ posn_begin_ = read_range_or_throw( posn_begin_, posn_end_, value );
+ }
+
+ private:
+
+ typedef Multi_pass_iters< Istream_type > Mp_iters;
+ typedef spirit_namespace::position_iterator< typename Mp_iters::Mp_iter > Posn_iter_t;
+
+ Mp_iters iters_;
+ Posn_iter_t posn_begin_, posn_end_;
+ };
+}
+
+#endif
diff --git a/src/json_spirit/json_spirit_utils.h b/src/json_spirit/json_spirit_utils.h
new file mode 100644
index 000000000..c4923093a
--- /dev/null
+++ b/src/json_spirit/json_spirit_utils.h
@@ -0,0 +1,63 @@
+#ifndef JSON_SPIRIT_UTILS
+#define JSON_SPIRIT_UTILS
+
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include "json_spirit_value.h"
+#include <map>
+
+namespace json_spirit
+{
+ template< class Obj_t, class Map_t >
+ void obj_to_map( const Obj_t& obj, Map_t& mp_obj )
+ {
+ mp_obj.clear();
+
+ for( typename Obj_t::const_iterator i = obj.begin(); i != obj.end(); ++i )
+ {
+ mp_obj[ i->name_ ] = i->value_;
+ }
+ }
+
+ template< class Obj_t, class Map_t >
+ void map_to_obj( const Map_t& mp_obj, Obj_t& obj )
+ {
+ obj.clear();
+
+ for( typename Map_t::const_iterator i = mp_obj.begin(); i != mp_obj.end(); ++i )
+ {
+ obj.push_back( typename Obj_t::value_type( i->first, i->second ) );
+ }
+ }
+
+#ifdef JSON_SPIRIT_VALUE_ENABLED
+ typedef std::map< std::string, Value > Mapped_obj;
+#endif
+
+#if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ typedef std::map< std::wstring, wValue > wMapped_obj;
+#endif
+
+ template< class Object_type, class String_type >
+ const typename Object_type::value_type::Value_type& find_value( const Object_type& obj, const String_type& name )
+ {
+ for( typename Object_type::const_iterator i = obj.begin(); i != obj.end(); ++i )
+ {
+ if( i->name_ == name )
+ {
+ return i->value_;
+ }
+ }
+
+ return Object_type::value_type::Value_type::null;
+ }
+}
+
+#endif
diff --git a/src/json_spirit/json_spirit_value.cpp b/src/json_spirit/json_spirit_value.cpp
new file mode 100644
index 000000000..dd5b50e5d
--- /dev/null
+++ b/src/json_spirit/json_spirit_value.cpp
@@ -0,0 +1,8 @@
+/* Copyright (c) 2007 John W Wilkinson
+
+ This source code can be used for any purpose as long as
+ this comment is retained. */
+
+// json spirit version 2.00
+
+#include "json_spirit_value.h"
diff --git a/src/json_spirit/json_spirit_value.h b/src/json_spirit/json_spirit_value.h
new file mode 100644
index 000000000..a4126369f
--- /dev/null
+++ b/src/json_spirit/json_spirit_value.h
@@ -0,0 +1,584 @@
+#ifndef JSON_SPIRIT_VALUE
+#define JSON_SPIRIT_VALUE
+
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include <vector>
+#include <map>
+#include <string>
+#include <sstream>
+#include <stdexcept>
+#include <boost/config.hpp>
+#include <boost/cstdint.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/variant.hpp>
+
+// comment out the value types you don't need to reduce build times and intermediate file sizes
+#define JSON_SPIRIT_VALUE_ENABLED
+//#define JSON_SPIRIT_WVALUE_ENABLED
+#define JSON_SPIRIT_MVALUE_ENABLED
+//#define JSON_SPIRIT_WMVALUE_ENABLED
+
+namespace json_spirit
+{
+ enum Value_type{ obj_type, array_type, str_type, bool_type, int_type, real_type, null_type };
+
+ struct Null{};
+
+ template< class Config > // Config determines whether the value uses std::string or std::wstring and
+ // whether JSON Objects are represented as vectors or maps
+ class Value_impl
+ {
+ public:
+
+ typedef Config Config_type;
+ typedef typename Config::String_type String_type;
+ typedef typename Config::Object_type Object;
+ typedef typename Config::Array_type Array;
+ typedef typename String_type::const_pointer Const_str_ptr; // eg const char*
+
+ Value_impl(); // creates null value
+ Value_impl( Const_str_ptr value );
+ Value_impl( const String_type& value );
+ Value_impl( const Object& value );
+ Value_impl( const Array& value );
+ Value_impl( bool value );
+ Value_impl( int value );
+ Value_impl( boost::int64_t value );
+ Value_impl( boost::uint64_t value );
+ Value_impl( double value );
+
+ template< class Iter >
+ Value_impl( Iter first, Iter last ); // constructor from containers, e.g. std::vector or std::list
+
+ template< BOOST_VARIANT_ENUM_PARAMS( typename T ) >
+ Value_impl( const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& variant ); // constructor for compatible variant types
+
+ Value_impl( const Value_impl& other );
+
+ bool operator==( const Value_impl& lhs ) const;
+
+ Value_impl& operator=( const Value_impl& lhs );
+
+ Value_type type() const;
+
+ bool is_uint64() const;
+ bool is_null() const;
+
+ const String_type& get_str() const;
+ const Object& get_obj() const;
+ const Array& get_array() const;
+ bool get_bool() const;
+ int get_int() const;
+ boost::int64_t get_int64() const;
+ boost::uint64_t get_uint64() const;
+ double get_real() const;
+
+ Object& get_obj();
+ Array& get_array();
+
+ template< typename T > T get_value() const; // example usage: int i = value.get_value< int >();
+ // or double d = value.get_value< double >();
+
+ static const Value_impl null;
+
+ private:
+
+ void check_type( const Value_type vtype ) const;
+
+ typedef boost::variant< boost::recursive_wrapper< Object >, boost::recursive_wrapper< Array >,
+ String_type, bool, boost::int64_t, double, Null, boost::uint64_t > Variant;
+
+ Variant v_;
+
+ class Variant_converter_visitor : public boost::static_visitor< Variant >
+ {
+ public:
+
+ template< typename T, typename A, template< typename, typename > class Cont >
+ Variant operator()( const Cont< T, A >& cont ) const
+ {
+ return Array( cont.begin(), cont.end() );
+ }
+
+ Variant operator()( int i ) const
+ {
+ return static_cast< boost::int64_t >( i );
+ }
+
+ template<class T>
+ Variant operator()( const T& t ) const
+ {
+ return t;
+ }
+ };
+ };
+
+ // vector objects
+
+ template< class Config >
+ struct Pair_impl
+ {
+ typedef typename Config::String_type String_type;
+ typedef typename Config::Value_type Value_type;
+
+ Pair_impl()
+ {
+ }
+
+ Pair_impl( const String_type& name, const Value_type& value );
+
+ bool operator==( const Pair_impl& lhs ) const;
+
+ String_type name_;
+ Value_type value_;
+ };
+
+#if defined( JSON_SPIRIT_VALUE_ENABLED ) || defined( JSON_SPIRIT_WVALUE_ENABLED )
+ template< class String >
+ struct Config_vector
+ {
+ typedef String String_type;
+ typedef Value_impl< Config_vector > Value_type;
+ typedef Pair_impl < Config_vector > Pair_type;
+ typedef std::vector< Value_type > Array_type;
+ typedef std::vector< Pair_type > Object_type;
+
+ static Value_type& add( Object_type& obj, const String_type& name, const Value_type& value )
+ {
+ obj.push_back( Pair_type( name , value ) );
+
+ return obj.back().value_;
+ }
+
+ static String_type get_name( const Pair_type& pair )
+ {
+ return pair.name_;
+ }
+
+ static Value_type get_value( const Pair_type& pair )
+ {
+ return pair.value_;
+ }
+ };
+#endif
+
+ // typedefs for ASCII
+
+#ifdef JSON_SPIRIT_VALUE_ENABLED
+ typedef Config_vector< std::string > Config;
+
+ typedef Config::Value_type Value;
+ typedef Config::Pair_type Pair;
+ typedef Config::Object_type Object;
+ typedef Config::Array_type Array;
+#endif
+
+ // typedefs for Unicode
+
+#if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ typedef Config_vector< std::wstring > wConfig;
+
+ typedef wConfig::Value_type wValue;
+ typedef wConfig::Pair_type wPair;
+ typedef wConfig::Object_type wObject;
+ typedef wConfig::Array_type wArray;
+#endif
+
+ // map objects
+
+#if defined( JSON_SPIRIT_MVALUE_ENABLED ) || defined( JSON_SPIRIT_WMVALUE_ENABLED )
+ template< class String >
+ struct Config_map
+ {
+ typedef String String_type;
+ typedef Value_impl< Config_map > Value_type;
+ typedef std::vector< Value_type > Array_type;
+ typedef std::map< String_type, Value_type > Object_type;
+ typedef std::pair< String_type, Value_type > Pair_type;
+
+ static Value_type& add( Object_type& obj, const String_type& name, const Value_type& value )
+ {
+ return obj[ name ] = value;
+ }
+
+ static String_type get_name( const Pair_type& pair )
+ {
+ return pair.first;
+ }
+
+ static Value_type get_value( const Pair_type& pair )
+ {
+ return pair.second;
+ }
+ };
+#endif
+
+ // typedefs for ASCII
+
+#ifdef JSON_SPIRIT_MVALUE_ENABLED
+ typedef Config_map< std::string > mConfig;
+
+ typedef mConfig::Value_type mValue;
+ typedef mConfig::Object_type mObject;
+ typedef mConfig::Array_type mArray;
+#endif
+
+ // typedefs for Unicode
+
+#if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ typedef Config_map< std::wstring > wmConfig;
+
+ typedef wmConfig::Value_type wmValue;
+ typedef wmConfig::Object_type wmObject;
+ typedef wmConfig::Array_type wmArray;
+#endif
+
+ ///////////////////////////////////////////////////////////////////////////////////////////////
+ //
+ // implementation
+
+ inline bool operator==( const Null&, const Null& )
+ {
+ return true;
+ }
+
+ template< class Config >
+ const Value_impl< Config > Value_impl< Config >::null;
+
+ template< class Config >
+ Value_impl< Config >::Value_impl()
+ : v_( Null() )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >::Value_impl( const Const_str_ptr value )
+ : v_( String_type( value ) )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >::Value_impl( const String_type& value )
+ : v_( value )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >::Value_impl( const Object& value )
+ : v_( value )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >::Value_impl( const Array& value )
+ : v_( value )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >::Value_impl( bool value )
+ : v_( value )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >::Value_impl( int value )
+ : v_( static_cast< boost::int64_t >( value ) )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >::Value_impl( boost::int64_t value )
+ : v_( value )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >::Value_impl( boost::uint64_t value )
+ : v_( value )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >::Value_impl( double value )
+ : v_( value )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >::Value_impl( const Value_impl< Config >& other )
+ : v_( other.v_ )
+ {
+ }
+
+ template< class Config >
+ template< class Iter >
+ Value_impl< Config >::Value_impl( Iter first, Iter last )
+ : v_( Array( first, last ) )
+ {
+ }
+
+ template< class Config >
+ template< BOOST_VARIANT_ENUM_PARAMS( typename T ) >
+ Value_impl< Config >::Value_impl( const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& variant )
+ : v_( boost::apply_visitor( Variant_converter_visitor(), variant) )
+ {
+ }
+
+ template< class Config >
+ Value_impl< Config >& Value_impl< Config >::operator=( const Value_impl& lhs )
+ {
+ Value_impl tmp( lhs );
+
+ std::swap( v_, tmp.v_ );
+
+ return *this;
+ }
+
+ template< class Config >
+ bool Value_impl< Config >::operator==( const Value_impl& lhs ) const
+ {
+ if( this == &lhs ) return true;
+
+ if( type() != lhs.type() ) return false;
+
+ return v_ == lhs.v_;
+ }
+
+ template< class Config >
+ Value_type Value_impl< Config >::type() const
+ {
+ if( is_uint64() )
+ {
+ return int_type;
+ }
+
+ return static_cast< Value_type >( v_.which() );
+ }
+
+ template< class Config >
+ bool Value_impl< Config >::is_uint64() const
+ {
+ return v_.which() == null_type + 1;
+ }
+
+ template< class Config >
+ bool Value_impl< Config >::is_null() const
+ {
+ return type() == null_type;
+ }
+
+ template< class Config >
+ void Value_impl< Config >::check_type( const Value_type vtype ) const
+ {
+ if( type() != vtype )
+ {
+ std::ostringstream os;
+
+ os << "value type is " << type() << " not " << vtype;
+
+ throw std::runtime_error( os.str() );
+ }
+ }
+
+ template< class Config >
+ const typename Config::String_type& Value_impl< Config >::get_str() const
+ {
+ check_type( str_type );
+
+ return *boost::get< String_type >( &v_ );
+ }
+
+ template< class Config >
+ const typename Value_impl< Config >::Object& Value_impl< Config >::get_obj() const
+ {
+ check_type( obj_type );
+
+ return *boost::get< Object >( &v_ );
+ }
+
+ template< class Config >
+ const typename Value_impl< Config >::Array& Value_impl< Config >::get_array() const
+ {
+ check_type( array_type );
+
+ return *boost::get< Array >( &v_ );
+ }
+
+ template< class Config >
+ bool Value_impl< Config >::get_bool() const
+ {
+ check_type( bool_type );
+
+ return boost::get< bool >( v_ );
+ }
+
+ template< class Config >
+ int Value_impl< Config >::get_int() const
+ {
+ check_type( int_type );
+
+ return static_cast< int >( get_int64() );
+ }
+
+ template< class Config >
+ boost::int64_t Value_impl< Config >::get_int64() const
+ {
+ check_type( int_type );
+
+ if( is_uint64() )
+ {
+ return static_cast< boost::int64_t >( get_uint64() );
+ }
+
+ return boost::get< boost::int64_t >( v_ );
+ }
+
+ template< class Config >
+ boost::uint64_t Value_impl< Config >::get_uint64() const
+ {
+ check_type( int_type );
+
+ if( !is_uint64() )
+ {
+ return static_cast< boost::uint64_t >( get_int64() );
+ }
+
+ return boost::get< boost::uint64_t >( v_ );
+ }
+
+ template< class Config >
+ double Value_impl< Config >::get_real() const
+ {
+ if( type() == int_type )
+ {
+ return is_uint64() ? static_cast< double >( get_uint64() )
+ : static_cast< double >( get_int64() );
+ }
+
+ check_type( real_type );
+
+ return boost::get< double >( v_ );
+ }
+
+ template< class Config >
+ typename Value_impl< Config >::Object& Value_impl< Config >::get_obj()
+ {
+ check_type( obj_type );
+
+ return *boost::get< Object >( &v_ );
+ }
+
+ template< class Config >
+ typename Value_impl< Config >::Array& Value_impl< Config >::get_array()
+ {
+ check_type( array_type );
+
+ return *boost::get< Array >( &v_ );
+ }
+
+ template< class Config >
+ Pair_impl< Config >::Pair_impl( const String_type& name, const Value_type& value )
+ : name_( name )
+ , value_( value )
+ {
+ }
+
+ template< class Config >
+ bool Pair_impl< Config >::operator==( const Pair_impl< Config >& lhs ) const
+ {
+ if( this == &lhs ) return true;
+
+ return ( name_ == lhs.name_ ) && ( value_ == lhs.value_ );
+ }
+
+ // converts a C string, ie. 8 bit char array, to a string object
+ //
+ template < class String_type >
+ String_type to_str( const char* c_str )
+ {
+ String_type result;
+
+ for( const char* p = c_str; *p != 0; ++p )
+ {
+ result += *p;
+ }
+
+ return result;
+ }
+
+ //
+
+ namespace internal_
+ {
+ template< typename T >
+ struct Type_to_type
+ {
+ };
+
+ template< class Value >
+ int get_value( const Value& value, Type_to_type< int > )
+ {
+ return value.get_int();
+ }
+
+ template< class Value >
+ boost::int64_t get_value( const Value& value, Type_to_type< boost::int64_t > )
+ {
+ return value.get_int64();
+ }
+
+ template< class Value >
+ boost::uint64_t get_value( const Value& value, Type_to_type< boost::uint64_t > )
+ {
+ return value.get_uint64();
+ }
+
+ template< class Value >
+ double get_value( const Value& value, Type_to_type< double > )
+ {
+ return value.get_real();
+ }
+
+ template< class Value >
+ typename Value::String_type get_value( const Value& value, Type_to_type< typename Value::String_type > )
+ {
+ return value.get_str();
+ }
+
+ template< class Value >
+ typename Value::Array get_value( const Value& value, Type_to_type< typename Value::Array > )
+ {
+ return value.get_array();
+ }
+
+ template< class Value >
+ typename Value::Object get_value( const Value& value, Type_to_type< typename Value::Object > )
+ {
+ return value.get_obj();
+ }
+
+ template< class Value >
+ bool get_value( const Value& value, Type_to_type< bool > )
+ {
+ return value.get_bool();
+ }
+ }
+
+ template< class Config >
+ template< typename T >
+ T Value_impl< Config >::get_value() const
+ {
+ return internal_::get_value( *this, internal_::Type_to_type< T >() );
+ }
+}
+
+#endif
diff --git a/src/json_spirit/json_spirit_writer.cpp b/src/json_spirit/json_spirit_writer.cpp
new file mode 100644
index 000000000..da0095486
--- /dev/null
+++ b/src/json_spirit/json_spirit_writer.cpp
@@ -0,0 +1,96 @@
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#include "json_spirit_writer.h"
+#include "json_spirit_writer_template.h"
+
+using namespace json_spirit;
+
+#ifdef JSON_SPIRIT_VALUE_ENABLED
+ void json_spirit::write( const Value& value, std::ostream& os, unsigned int options )
+ {
+ write_stream( value, os, options );
+ }
+ std::string json_spirit::write( const Value& value, unsigned int options )
+ {
+ return write_string( value, options );
+ }
+
+ void json_spirit::write_formatted( const Value& value, std::ostream& os )
+ {
+ write_stream( value, os, pretty_print );
+ }
+
+ std::string json_spirit::write_formatted( const Value& value )
+ {
+ return write_string( value, pretty_print );
+ }
+#endif
+
+#ifdef JSON_SPIRIT_MVALUE_ENABLED
+ void json_spirit::write( const mValue& value, std::ostream& os, unsigned int options )
+ {
+ write_stream( value, os, options );
+ }
+
+ std::string json_spirit::write( const mValue& value, unsigned int options )
+ {
+ return write_string( value, options );
+ }
+
+ void json_spirit::write_formatted( const mValue& value, std::ostream& os )
+ {
+ write_stream( value, os, pretty_print );
+ }
+
+ std::string json_spirit::write_formatted( const mValue& value )
+ {
+ return write_string( value, pretty_print );
+ }
+#endif
+
+#if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ void json_spirit::write( const wValue& value, std::wostream& os, unsigned int options )
+ {
+ write_stream( value, os, options );
+ }
+
+ std::wstring json_spirit::write( const wValue& value, unsigned int options )
+ {
+ return write_string( value, options );
+ }
+
+ void json_spirit::write_formatted( const wValue& value, std::wostream& os )
+ {
+ write_stream( value, os, pretty_print );
+ }
+
+ std::wstring json_spirit::write_formatted( const wValue& value )
+ {
+ return write_string( value, pretty_print );
+ }
+#endif
+
+#if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ void json_spirit::write_formatted( const wmValue& value, std::wostream& os )
+ {
+ write_stream( value, os, pretty_print );
+ }
+
+ std::wstring json_spirit::write_formatted( const wmValue& value )
+ {
+ return write_string( value, pretty_print );
+ }
+
+ void json_spirit::write( const wmValue& value, std::wostream& os, unsigned int options )
+ {
+ write_stream( value, os, options );
+ }
+
+ std::wstring json_spirit::write( const wmValue& value, unsigned int options )
+ {
+ return write_string( value, options );
+ }
+#endif
diff --git a/src/json_spirit/json_spirit_writer.h b/src/json_spirit/json_spirit_writer.h
new file mode 100644
index 000000000..b79b068db
--- /dev/null
+++ b/src/json_spirit/json_spirit_writer.h
@@ -0,0 +1,63 @@
+#ifndef JSON_SPIRIT_WRITER
+#define JSON_SPIRIT_WRITER
+
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include "json_spirit_value.h"
+#include "json_spirit_writer_options.h"
+#include <iostream>
+
+namespace json_spirit
+{
+ // these functions to convert JSON Values to text
+
+#ifdef JSON_SPIRIT_VALUE_ENABLED
+ void write( const Value& value, std::ostream& os, unsigned int options = 0 );
+ std::string write( const Value& value, unsigned int options = 0 );
+#endif
+
+#ifdef JSON_SPIRIT_MVALUE_ENABLED
+ void write( const mValue& value, std::ostream& os, unsigned int options = 0 );
+ std::string write( const mValue& value, unsigned int options = 0 );
+#endif
+
+#if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ void write( const wValue& value, std::wostream& os, unsigned int options = 0 );
+ std::wstring write( const wValue& value, unsigned int options = 0 );
+#endif
+
+#if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ void write( const wmValue& value, std::wostream& os, unsigned int options = 0 );
+ std::wstring write( const wmValue& value, unsigned int options = 0 );
+#endif
+
+ // these "formatted" versions of the "write" functions are the equivalent of the above functions
+ // with option "pretty_print"
+
+#ifdef JSON_SPIRIT_VALUE_ENABLED
+ void write_formatted( const Value& value, std::ostream& os );
+ std::string write_formatted( const Value& value );
+#endif
+#ifdef JSON_SPIRIT_MVALUE_ENABLED
+ void write_formatted( const mValue& value, std::ostream& os );
+ std::string write_formatted( const mValue& value );
+#endif
+
+#if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ void write_formatted( const wValue& value, std::wostream& os );
+ std::wstring write_formatted( const wValue& value );
+#endif
+#if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
+ void write_formatted( const wmValue& value, std::wostream& os );
+ std::wstring write_formatted( const wmValue& value );
+#endif
+}
+
+#endif
diff --git a/src/json_spirit/json_spirit_writer_options.h b/src/json_spirit/json_spirit_writer_options.h
new file mode 100644
index 000000000..1b79421c9
--- /dev/null
+++ b/src/json_spirit/json_spirit_writer_options.h
@@ -0,0 +1,30 @@
+#ifndef JSON_SPIRIT_WRITER_OPTIONS
+#define JSON_SPIRIT_WRITER_OPTIONS
+
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+namespace json_spirit
+{
+ enum Output_options{ pretty_print = 0x01, // Add whitespace to format the output nicely.
+
+ raw_utf8 = 0x02, // This prevents non-printable characters from being escapted using "\uNNNN" notation.
+ // Note, this is an extension to the JSON standard. It disables the escaping of
+ // non-printable characters allowing UTF-8 sequences held in 8 bit char strings
+ // to pass through unaltered.
+
+ remove_trailing_zeros = 0x04,
+ // outputs e.g. "1.200000000000000" as "1.2"
+ single_line_arrays = 0x08,
+ // pretty printing except that arrays printed on single lines unless they contain
+ // composite elements, i.e. objects or arrays
+ };
+}
+
+#endif
diff --git a/src/json_spirit/json_spirit_writer_template.h b/src/json_spirit/json_spirit_writer_template.h
new file mode 100644
index 000000000..2e34268a2
--- /dev/null
+++ b/src/json_spirit/json_spirit_writer_template.h
@@ -0,0 +1,384 @@
+#ifndef JSON_SPIRIT_WRITER_TEMPLATE
+#define JSON_SPIRIT_WRITER_TEMPLATE
+
+// Copyright John W. Wilkinson 2007 - 2011
+// Distributed under the MIT License, see accompanying file LICENSE.txt
+
+// json spirit version 4.05
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include "json_spirit_value.h"
+#include "json_spirit_writer_options.h"
+
+#include <sstream>
+#include <iomanip>
+#include <boost/io/ios_state.hpp>
+
+#include "include/ceph_assert.h"
+
+namespace json_spirit
+{
+ inline char to_hex_char( unsigned int c )
+ {
+ ceph_assert( c <= 0xF );
+
+ const char ch = static_cast< char >( c );
+
+ if( ch < 10 ) return '0' + ch;
+
+ return 'A' - 10 + ch;
+ }
+
+ template< class String_type >
+ String_type non_printable_to_string( unsigned int c )
+ {
+ String_type result( 6, '\\' );
+
+ result[1] = 'u';
+
+ result[ 5 ] = to_hex_char( c & 0x000F ); c >>= 4;
+ result[ 4 ] = to_hex_char( c & 0x000F ); c >>= 4;
+ result[ 3 ] = to_hex_char( c & 0x000F ); c >>= 4;
+ result[ 2 ] = to_hex_char( c & 0x000F );
+
+ return result;
+ }
+
+ template< typename Char_type, class String_type >
+ bool add_esc_char( Char_type c, String_type& s )
+ {
+ switch( c )
+ {
+ case '"': s += to_str< String_type >( "\\\"" ); return true;
+ case '\\': s += to_str< String_type >( "\\\\" ); return true;
+ case '\b': s += to_str< String_type >( "\\b" ); return true;
+ case '\f': s += to_str< String_type >( "\\f" ); return true;
+ case '\n': s += to_str< String_type >( "\\n" ); return true;
+ case '\r': s += to_str< String_type >( "\\r" ); return true;
+ case '\t': s += to_str< String_type >( "\\t" ); return true;
+ }
+
+ return false;
+ }
+
+ template< class String_type >
+ String_type add_esc_chars( const String_type& s, bool raw_utf8 )
+ {
+ typedef typename String_type::const_iterator Iter_type;
+ typedef typename String_type::value_type Char_type;
+
+ String_type result;
+
+ const Iter_type end( s.end() );
+
+ for( Iter_type i = s.begin(); i != end; ++i )
+ {
+ const Char_type c( *i );
+
+ if( add_esc_char( c, result ) ) continue;
+
+ if( raw_utf8 )
+ {
+ result += c;
+ }
+ else
+ {
+ const wint_t unsigned_c( ( c >= 0 ) ? c : 256 + c );
+
+ if( iswprint( unsigned_c ) )
+ {
+ result += c;
+ }
+ else
+ {
+ result += non_printable_to_string< String_type >( unsigned_c );
+ }
+ }
+ }
+
+ return result;
+ }
+
+ template< class Ostream >
+ void append_double( Ostream& os, const double d, const int precision )
+ {
+ os << std::showpoint << std::setprecision( precision ) << d;
+ }
+
+ template< class String_type >
+ void erase_and_extract_exponent( String_type& str, String_type& exp )
+ {
+ const typename String_type::size_type exp_start= str.find( 'e' );
+
+ if( exp_start != String_type::npos )
+ {
+ exp = str.substr( exp_start );
+ str.erase( exp_start );
+ }
+ }
+
+ template< class String_type >
+ typename String_type::size_type find_first_non_zero( const String_type& str )
+ {
+ typename String_type::size_type result = str.size() - 1;
+
+ for( ; result != 0; --result )
+ {
+ if( str[ result ] != '0' )
+ {
+ break;
+ }
+ }
+
+ return result;
+ }
+
+ template< class String_type >
+ void remove_trailing( String_type& str )
+ {
+ String_type exp;
+
+ erase_and_extract_exponent( str, exp );
+
+ const typename String_type::size_type first_non_zero = find_first_non_zero( str );
+
+ if( first_non_zero != 0 )
+ {
+ const int offset = str[first_non_zero] == '.' ? 2 : 1; // note zero digits following a decimal point is non standard
+ str.erase( first_non_zero + offset );
+ }
+
+ str += exp;
+ }
+
+ // this class generates the JSON text,
+ // it keeps track of the indentation level etc.
+ //
+ template< class Value_type, class Ostream_type >
+ class Generator
+ {
+ typedef typename Value_type::Config_type Config_type;
+ typedef typename Config_type::String_type String_type;
+ typedef typename Config_type::Object_type Object_type;
+ typedef typename Config_type::Array_type Array_type;
+ typedef typename String_type::value_type Char_type;
+ typedef typename Object_type::value_type Obj_member_type;
+
+ public:
+
+ Generator( const Value_type& value, Ostream_type& os, unsigned int options )
+ : os_( os )
+ , indentation_level_( 0 )
+ , pretty_( ( options & pretty_print ) != 0 || ( options & single_line_arrays ) != 0 )
+ , raw_utf8_( ( options & raw_utf8 ) != 0 )
+ , remove_trailing_zeros_( ( options & remove_trailing_zeros ) != 0 )
+ , single_line_arrays_( ( options & single_line_arrays ) != 0 )
+ , ios_saver_( os )
+ {
+ output( value );
+ }
+
+ private:
+
+ void output( const Value_type& value )
+ {
+ switch( value.type() )
+ {
+ case obj_type: output( value.get_obj() ); break;
+ case array_type: output( value.get_array() ); break;
+ case str_type: output( value.get_str() ); break;
+ case bool_type: output( value.get_bool() ); break;
+ case real_type: output( value.get_real() ); break;
+ case int_type: output_int( value ); break;
+ case null_type: os_ << "null"; break;
+ default: ceph_assert( false );
+ }
+ }
+
+ void output( const Object_type& obj )
+ {
+ output_array_or_obj( obj, '{', '}' );
+ }
+
+ void output( const Obj_member_type& member )
+ {
+ output( Config_type::get_name( member ) ); space();
+ os_ << ':'; space();
+ output( Config_type::get_value( member ) );
+ }
+
+ void output_int( const Value_type& value )
+ {
+ if( value.is_uint64() )
+ {
+ os_ << value.get_uint64();
+ }
+ else
+ {
+ os_ << value.get_int64();
+ }
+ }
+
+ void output( const String_type& s )
+ {
+ os_ << '"' << add_esc_chars( s, raw_utf8_ ) << '"';
+ }
+
+ void output( bool b )
+ {
+ os_ << to_str< String_type >( b ? "true" : "false" );
+ }
+
+ void output( double d )
+ {
+ if( remove_trailing_zeros_ )
+ {
+ std::basic_ostringstream< Char_type > os;
+
+ append_double( os, d, 16 ); // note precision is 16 so that we get some trailing space that we can remove,
+ // otherwise, 0.1234 gets converted to "0.12399999..."
+
+ String_type str = os.str();
+
+ remove_trailing( str );
+
+ os_ << str;
+ }
+ else
+ {
+ append_double( os_, d, 17 );
+ }
+ }
+
+ static bool contains_composite_elements( const Array_type& arr )
+ {
+ for( typename Array_type::const_iterator i = arr.begin(); i != arr.end(); ++i )
+ {
+ const Value_type& val = *i;
+
+ if( val.type() == obj_type ||
+ val.type() == array_type )
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ template< class Iter >
+ void output_composite_item( Iter i, Iter last )
+ {
+ output( *i );
+
+ if( ++i != last )
+ {
+ os_ << ',';
+ }
+ }
+
+ void output( const Array_type& arr )
+ {
+ if( single_line_arrays_ && !contains_composite_elements( arr ) )
+ {
+ os_ << '['; space();
+
+ for( typename Array_type::const_iterator i = arr.begin(); i != arr.end(); ++i )
+ {
+ output_composite_item( i, arr.end() );
+
+ space();
+ }
+
+ os_ << ']';
+ }
+ else
+ {
+ output_array_or_obj( arr, '[', ']' );
+ }
+ }
+
+ template< class T >
+ void output_array_or_obj( const T& t, Char_type start_char, Char_type end_char )
+ {
+ os_ << start_char; new_line();
+
+ ++indentation_level_;
+
+ for( typename T::const_iterator i = t.begin(); i != t.end(); ++i )
+ {
+ indent();
+
+ output_composite_item( i, t.end() );
+
+ new_line();
+ }
+
+ --indentation_level_;
+
+ indent(); os_ << end_char;
+ }
+
+ void indent()
+ {
+ if( !pretty_ ) return;
+
+ for( int i = 0; i < indentation_level_; ++i )
+ {
+ os_ << " ";
+ }
+ }
+
+ void space()
+ {
+ if( pretty_ ) os_ << ' ';
+ }
+
+ void new_line()
+ {
+ if( pretty_ ) os_ << '\n';
+ }
+
+ Generator& operator=( const Generator& ); // to prevent "assignment operator could not be generated" warning
+
+ Ostream_type& os_;
+ int indentation_level_;
+ bool pretty_;
+ bool raw_utf8_;
+ bool remove_trailing_zeros_;
+ bool single_line_arrays_;
+ boost::io::basic_ios_all_saver< Char_type > ios_saver_; // so that ostream state is reset after control is returned to the caller
+ };
+
+ // writes JSON Value to a stream, e.g.
+ //
+ // write_stream( value, os, pretty_print );
+ //
+ template< class Value_type, class Ostream_type >
+ void write_stream( const Value_type& value, Ostream_type& os, unsigned int options = 0 )
+ {
+ os << std::dec;
+ Generator< Value_type, Ostream_type >( value, os, options );
+ }
+
+ // writes JSON Value to a stream, e.g.
+ //
+ // const string json_str = write( value, pretty_print );
+ //
+ template< class Value_type >
+ typename Value_type::String_type write_string( const Value_type& value, unsigned int options = 0 )
+ {
+ typedef typename Value_type::String_type::value_type Char_type;
+
+ std::basic_ostringstream< Char_type > os;
+
+ write_stream( value, os, options );
+
+ return os.str();
+ }
+}
+
+#endif