Skip to content

Commit

Permalink
Refactored SAX classes
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Sep 14, 2016
1 parent dca5cc9 commit 41380ba
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 193 deletions.
8 changes: 6 additions & 2 deletions include/tao/json.hh
Expand Up @@ -5,18 +5,22 @@
#define TAOCPP_JSON_INCLUDE_JSON_HH

#include "json/value.hh"

#include "json/comparison_operators.hh"
#include "json/object_operators.hh"

#include "json/sax/to_stream.hh"
#include "json/sax/to_pretty_stream.hh"
#include "json/sax/to_value.hh"
#include "json/stream.hh"
#include "json/to_string.hh"

#include "json/sax/traverse_value.hh"
#include "json/sax/from_string.hh"
#include "json/sax/parse_file.hh"
#include "json/from_string.hh"
#include "json/parse_file.hh"

#include "json/self_contained.hh"

#include "json/patch.hh"

#endif
72 changes: 20 additions & 52 deletions include/tao/json/from_string.hh
Expand Up @@ -4,99 +4,67 @@
#ifndef TAOCPP_JSON_INCLUDE_FROM_STRING_HH
#define TAOCPP_JSON_INCLUDE_FROM_STRING_HH

#include "external/pegtl/parse.hh"
#include <cstddef>
#include <utility>
#include <string>

#include "internal/value_builder.hh"

#include "internal/grammar.hh"
#include "internal/action.hh"
#include "internal/control.hh"
#include "sax/from_string.hh"
#include "sax/to_value.hh"

namespace tao
{
namespace json
{
template< typename Handler >
inline void from_string( const char * data, const std::size_t size, Handler & handler, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
{
tao_json_pegtl::input input( line, column, data, data + size, source ? source : __PRETTY_FUNCTION__ );
tao_json_pegtl::parse_input< internal::grammar, internal::action, internal::control >( input, handler );
}

template< typename Handler >
inline void from_string( const std::string & data, Handler & handler, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
{
json::from_string( data.data(), data.size(), handler, source, line, column );
}

template< template< typename ... > class Traits >
inline basic_value< Traits > from_string( const char * data, const std::size_t size, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
{
internal::value_builder< Traits > handler;
json::from_string( data, size, handler, source, line, column );
return std::move( handler.value_ );
sax::to_basic_value< Traits > handler;
sax::from_string( data, size, handler, source, line, column );
return std::move( handler.result() );
}

template< template< typename ... > class Traits >
inline void from_string( basic_value< Traits > & output, const char * data, const std::size_t size, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
inline basic_value< Traits > from_string( const char * data, const std::size_t size, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
{
output = from_string< Traits >( data, size, source, line, column );
return from_string< Traits >( data, size, source.c_str(), line, column );
}

inline value from_string( const char * data, const std::size_t size, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
{
return from_string< traits >( data, size, source, line, column );
}

template< template< typename ... > class Traits >
inline void from_string( basic_value< Traits > & output, const char * data, const std::size_t size, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
{
json::from_string( output, data, size, source.c_str(), line, column );
}

template< template< typename ... > class Traits >
inline basic_value< Traits > from_string( const char * data, const std::size_t size, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
{
return from_string< Traits >( data, size, source.c_str(), line, column );
}

inline value from_string( const char * data, const std::size_t size, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
{
return json::from_string( data, size, source.c_str(), line, column );
return from_string< traits >( data, size, source.c_str(), line, column );
}

template< template< typename ... > class Traits >
inline void from_string( basic_value< Traits > & output, const std::string & data, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
inline basic_value< Traits > from_string( const std::string & data, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
{
json::from_string( output, data.data(), data.size(), source, line, column );
return from_string< Traits >( data.data(), data.size(), source, line, column );
}

template< template< typename ... > class Traits >
inline basic_value< Traits > from_string( const std::string & data, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
inline basic_value< Traits > from_string( const std::string & data, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
{
return from_string< Traits >( data.data(), data.size(), source, line, column );
return from_string< Traits >( data.data(), data.size(), source.c_str(), line, column );
}

inline value from_string( const std::string & data, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
{
return json::from_string( data.data(), data.size(), source, line, column );
}

template< template< typename ... > class Traits >
inline void from_string( basic_value< Traits > & output, const std::string & data, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
{
json::from_string( output, data.data(), data.size(), source.c_str(), line, column );
}

template< template< typename ... > class Traits >
inline basic_value< Traits > from_string( const std::string & data, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
inline value from_string( const std::string & data, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
{
return from_string< Traits >( data.data(), data.size(), source.c_str(), line, column );
return json::from_string( data.data(), data.size(), source.c_str(), line, column );
}

inline value from_string( const std::string & data, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
template< template< typename ... > class Traits, typename ... Ts >
inline void from_string( basic_value< Traits > & output, Ts && ... ts )
{
return json::from_string( data.data(), data.size(), source.c_str(), line, column );
output = from_string< Traits >( std::forward< Ts >( ts ) ... );
}

inline namespace literals
Expand Down
20 changes: 10 additions & 10 deletions include/tao/json/internal/to_stream.hh
Expand Up @@ -5,12 +5,12 @@
#define TAOCPP_JSON_INCLUDE_INTERNAL_TO_STREAM_HH

#include <ostream>
#include <cstddef>

#include "../value.hh"
#include "../traverse.hh"

#include "value_writer.hh"
#include "pretty_writer.hh"
#include "../sax/to_stream.hh"
#include "../sax/to_pretty_stream.hh"
#include "../sax/traverse_value.hh"

namespace tao
{
Expand All @@ -19,17 +19,17 @@ namespace tao
namespace internal
{
template< template< typename ... > class Traits >
void to_stream( std::ostream & o, const basic_value< Traits > & v )
void to_stream( std::ostream & os, const basic_value< Traits > & v )
{
internal::value_writer writer( o );
json::traverse( v, writer );
sax::to_stream handler( os );
sax::traverse_value( v, handler );
}

template< template< typename ... > class Traits >
void to_stream( std::ostream & o, const basic_value< Traits > & v, const unsigned indent )
void to_stream( std::ostream & os, const basic_value< Traits > & v, const std::size_t indent )
{
internal::pretty_writer writer( o, indent );
json::traverse( v, writer );
sax::to_pretty_stream handler( os, indent );
sax::traverse_value( v, handler );
}

} // internal
Expand Down
22 changes: 7 additions & 15 deletions include/tao/json/parse_file.hh
Expand Up @@ -4,30 +4,22 @@
#ifndef TAOCPP_JSON_INCLUDE_PARSE_FILE_HH
#define TAOCPP_JSON_INCLUDE_PARSE_FILE_HH

#include "external/pegtl/file_parser.hh"
#include <string>
#include <utility>

#include "internal/value_builder.hh"

#include "internal/grammar.hh"
#include "internal/action.hh"
#include "internal/control.hh"
#include "sax/parse_file.hh"
#include "sax/to_value.hh"

namespace tao
{
namespace json
{
template< typename Handler >
void parse_file( const std::string & filename, Handler & handler )
{
tao_json_pegtl::file_parser( filename ).parse< internal::grammar, internal::action, internal::control >( handler );
}

template< template< typename ... > class Traits >
basic_value< Traits > parse_file( const std::string & filename )
{
internal::value_builder< Traits > handler;
json::parse_file( filename, handler );
return std::move( handler.value_ );
sax::to_basic_value< Traits > handler;
sax::parse_file( filename, handler );
return std::move( handler.result() );
}

template< template< typename ... > class Traits >
Expand Down
50 changes: 50 additions & 0 deletions include/tao/json/sax/from_string.hh
@@ -0,0 +1,50 @@
// Copyright (c) 2016 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/json/

#ifndef TAOCPP_JSON_INCLUDE_SAX_FROM_STRING_HH
#define TAOCPP_JSON_INCLUDE_SAX_FROM_STRING_HH

#include "../external/pegtl/parse.hh"

#include "../internal/grammar.hh"
#include "../internal/action.hh"
#include "../internal/control.hh"

namespace tao
{
namespace json
{
namespace sax
{
template< typename Handler >
inline void from_string( const char * data, const std::size_t size, Handler & handler, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
{
tao_json_pegtl::input input( line, column, data, data + size, source ? source : __PRETTY_FUNCTION__ );
tao_json_pegtl::parse_input< internal::grammar, internal::action, internal::control >( input, handler );
}

template< typename Handler >
inline void from_string( const char * data, const std::size_t size, Handler & handler, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
{
sax::from_string( data, size, handler, source.c_str(), line, column );
}

template< typename Handler >
inline void from_string( const std::string & data, Handler & handler, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
{
sax::from_string( data.data(), data.size(), handler, source, line, column );
}

template< typename Handler >
inline void from_string( const std::string & data, Handler & handler, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
{
sax::from_string( data, handler, source.c_str(), line, column );
}

} // sax

} // json

} // tao

#endif
31 changes: 31 additions & 0 deletions include/tao/json/sax/parse_file.hh
@@ -0,0 +1,31 @@
// Copyright (c) 2016 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/json/

#ifndef TAOCPP_JSON_INCLUDE_SAX_PARSE_FILE_HH
#define TAOCPP_JSON_INCLUDE_SAX_PARSE_FILE_HH

#include "../external/pegtl/file_parser.hh"

#include "../internal/grammar.hh"
#include "../internal/action.hh"
#include "../internal/control.hh"

namespace tao
{
namespace json
{
namespace sax
{
template< typename Handler >
void parse_file( const std::string & filename, Handler & handler )
{
tao_json_pegtl::file_parser( filename ).parse< internal::grammar, internal::action, internal::control >( handler );
}

} // sax

} // json

} // tao

#endif

0 comments on commit 41380ba

Please sign in to comment.