Skip to content

Commit

Permalink
baby changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sweetlikecandy committed Feb 1, 2018
1 parent a1dae10 commit ced674d
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ find_package(GTest REQUIRED)
include_directories(json_parser)

add_executable( json_parser_tests ${test_sources} )

target_link_libraries(json_parser_tests GTest::GTest GTest::Main)

add_executable( example example.cpp )
target_link_libraries(example)

70 changes: 70 additions & 0 deletions example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "JsonObject.h"


int main(){
using namespace json_parser;
JsonObject obj;

// parse
obj.Parse(R"( { "primes":[2,5,7,11,13] } )");

// assign an array
obj["nice_numbers"] = Array("two", 3, 4.4);

// assign a map
obj["next"] = Map(1,"one")
("two",2);


obj["empty_array"] = Array;
obj["empty_map"] = Map;

// we can use an integer as a key as well
obj[23] = "twentry three";

// iterator over the intergers
for( auto p : obj["primes"] ){
std::cout << p.AsInteger() << "\n";
}
// or use as an zero index array
auto const& primes = obj["primes"];
for(size_t i = 0; i!= primes.size(); ++i){
std::cout << primes[0] << "\n";
}

for( auto p : obj["nice_numbers"] ){
switch(p.GetType()){
case Type_Integer:
std::cout << p.AsInteger() << "\n";
break;
case Type_String:
std::cout << p.AsString() << "\n";
break;
case Type_Float:
std::cout << p.AsFloat() << "\n";
break;
default:
break;
}
}

// iteratate over a map
using MI = JsonObject::const_iterator;
for(MI iter( obj["next"].begin() ), end( obj["next"].end());iter!=end;++iter){
std::cout << iter.key() << " => " << iter.value() << "\n";
}


// display pretty
obj.Display();

// print to a single line
std::string s = obj.ToString();

JsonObject other;
// reparse
other.Parse(s);


}

65 changes: 52 additions & 13 deletions json_parser/JsonObject.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "json_parser.h"

#include <list>
#include <iterator>

namespace json_parser{

namespace Detail{
Expand Down Expand Up @@ -56,6 +59,7 @@ enum VisitorCtrl{
VisitorCtrl_Nop,
};


struct JsonObject{
using array_type = std::vector<JsonObject>;
using map_type = std::map<JsonObject, JsonObject>;
Expand All @@ -73,12 +77,12 @@ struct JsonObject{
using Tag_Array = std::integral_constant<Type, Type_Array>;
using Tag_Map = std::integral_constant<Type, Type_Map>;

enum IteratorType{
IteratorType_Array,
IteratorType_Map,
};
template<bool is_constant>
struct basic_iterator{
enum IteratorType{
IteratorType_Array,
IteratorType_Map,
};
struct basic_iterator : std::iterator<std::forward_iterator_tag, JsonObject>{
using IterTag_Array = std::integral_constant<IteratorType, IteratorType_Array>;
using IterTag_Map = std::integral_constant<IteratorType, IteratorType_Map>;
using array_iter_type = std::conditional_t<is_constant,
Expand All @@ -98,6 +102,19 @@ struct JsonObject{
JsonObject const&,
JsonObject&>;

friend struct basic_iterator<true>;

template<class Iter>
basic_iterator(Iter const& that)
:iter_type_(that.iter_type_)
{
if( iter_type_ == IteratorType_Map ){
map_iter_ = that.map_iter_;
} else {
array_iter_ = that.array_iter_;
}
}

basic_iterator(IterTag_Array, array_iter_type iter){
iter_type_ = IteratorType_Array;
array_iter_ = iter;
Expand Down Expand Up @@ -143,11 +160,7 @@ struct JsonObject{
}
}
ref_type operator*(){
if( iter_type_ == IteratorType_Map ){
return *const_cast<ptr_type>(&map_iter_->first); // return key
} else {
return *array_iter_;
}
return key();
}
ptr_type map_value__(){
if( iter_type_ == IteratorType_Map ){
Expand All @@ -156,6 +169,16 @@ struct JsonObject{
return &*array_iter_;
}
}
ref_type value(){
return *map_value__();
}
ref_type key(){
if( iter_type_ == IteratorType_Map ){
return *const_cast<ptr_type>(&map_iter_->first); // return key
} else {
return *array_iter_;
}
}
private:
IteratorType iter_type_;
union{
Expand Down Expand Up @@ -209,13 +232,15 @@ struct JsonObject{
new (&as_map_) map_type{std::forward<MapTypeParam>(val)};
}

#if 0
template<class Value>
void AssignImpl( Detail::precedence_device<0>, Value&& val)
{
std::cout << "val = " << boost::typeindex::type_id_with_cvr<Value>() << "\n";
std::cout << "val = " << boost::typeindex::type_id_with_cvr< std::remove_cv_t<std::decay_t<Value> > >() << "\n";
throw std::domain_error("not a known type");
}
#endif
template<class Value>
std::enable_if_t< std::is_same<std::decay_t<Value>, Tag_Array >::value >
AssignImpl( Detail::precedence_device<3>, Value&& val){
Expand Down Expand Up @@ -253,14 +278,24 @@ struct JsonObject{
DoAssign( Tag_Integer{}, std::forward<Value>(val) );
}
template<class Value>
std::enable_if_t< std::is_same<typename std::decay_t<Value>::IAmAnEmptyArray, int>::value >
AssignImpl( Detail::precedence_device<13>, Value&& val){
DoAssign( Tag_Array{} );
}
template<class Value>
std::enable_if_t< std::is_same<typename std::decay_t<Value>::IAmAnEmptyMap, int>::value >
AssignImpl( Detail::precedence_device<14>, Value&& val){
DoAssign( Tag_Map{} );
}
template<class Value>
std::enable_if_t< std::is_same<std::decay_t<Value>, bool>::value >
AssignImpl( Detail::precedence_device<10>, Value&& val){
AssignImpl( Detail::precedence_device<15>, Value&& val){
DoAssign( Tag_Bool{}, std::forward<Value>(val));
}
template<class Value>
std::enable_if_t< ! std::is_same<JsonObject, std::remove_cv_t<std::decay_t<Value> > >::value >
Assign(Value&& val){
AssignImpl( Detail::precedence_device<10>{}, std::forward<Value>(val) );
AssignImpl( Detail::precedence_device<15>{}, std::forward<Value>(val) );
}
template<class Value>
std::enable_if_t< std::is_same<JsonObject, std::remove_cv_t<std::decay_t<Value> > >::value >
Expand Down Expand Up @@ -1328,6 +1363,8 @@ void JsonObject::Display(std::ostream& ostr, unsigned indent)const{
this->Accept(v);
v.Optmize();
v.Render(ctx);
ostr << "\n";
ostr.flush();
}
std::string JsonObject::ToString()const{
std::stringstream sstr;
Expand All @@ -1347,6 +1384,7 @@ namespace Detail{
int aux[] = {0, ( obj.push_back_unchecked( args), 0 )... };
return obj;
}
using IAmAnEmptyArray = int;
};
struct MapType{
struct Impl{
Expand Down Expand Up @@ -1382,6 +1420,7 @@ namespace Detail{
JsonObject obj(JsonObject::Tag_Map{});
return obj;
}
using IAmAnEmptyMap = int;
};
} // Defailt

Expand Down Expand Up @@ -1425,7 +1464,7 @@ namespace Detail{
add_any_( JsonObject{value});
}
void make_null(){
add_any_( JsonObject{JsonObject::Tag_Nil{}});
//add_any_( JsonObject{JsonObject::Tag_Nil{}});
}
void make_true(){
add_any_( JsonObject{true} );
Expand Down

0 comments on commit ced674d

Please sign in to comment.