PageRenderTime 36ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/json_spirit_v3.00/json_demo/json_demo.cpp

http://github.com/RJ/playdar
C++ | 197 lines | 127 code | 48 blank | 22 comment | 13 complexity | 7d7015f6826f630435a6d044ae8e4fda MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* Copyright (c) 2007-2009 John W Wilkinson
  2. This source code can be used for any purpose as long as
  3. this comment is retained. */
  4. // json spirit version 3.00
  5. #include "json_spirit.h"
  6. #include <cassert>
  7. #include <algorithm>
  8. #include <fstream>
  9. #include <boost/bind.hpp>
  10. using namespace std;
  11. using namespace boost;
  12. using namespace json_spirit;
  13. void add_pair( Object& obj, const string& name, const Value& value )
  14. {
  15. obj.push_back( Pair( name, value ) );
  16. }
  17. // adds a new child object to an existing object,
  18. // returns the new child object
  19. //
  20. Object& add_child_obj( Object& parent, const string& name )
  21. {
  22. parent.push_back( Pair( name, Object() ) );
  23. return parent.back().value_.get_obj();
  24. }
  25. // adds a new child object to an existing array,
  26. // returns the new child object
  27. //
  28. Object& add_child_obj( Array& parent )
  29. {
  30. parent.push_back( Object() );
  31. return parent.back().get_obj();
  32. }
  33. Array& add_array( Object& obj, const string& name )
  34. {
  35. obj.push_back( Pair( name, Array() ) );
  36. return obj.back().value_.get_array();
  37. }
  38. bool same_name( const Pair& pair, const string& name )
  39. {
  40. return pair.name_ == name;
  41. }
  42. const Value& find_value( const Object& obj, const string& name )
  43. {
  44. Object::const_iterator i = find_if( obj.begin(), obj.end(), bind( same_name, _1, ref( name ) ) );
  45. if( i == obj.end() ) return Value::null;
  46. return i->value_;
  47. }
  48. const Array& find_array( const Object& obj, const string& name )
  49. {
  50. return find_value( obj, name ).get_array();
  51. }
  52. int find_int( const Object& obj, const string& name )
  53. {
  54. return find_value( obj, name ).get_int();
  55. }
  56. string find_str( const Object& obj, const string& name )
  57. {
  58. return find_value( obj, name ).get_str();
  59. }
  60. struct Address
  61. {
  62. bool operator==( const Address& lhs ) const
  63. {
  64. return ( house_number_ == lhs.house_number_ ) &&
  65. ( road_ == lhs.road_ ) &&
  66. ( town_ == lhs.town_ ) &&
  67. ( county_ == lhs.county_ ) &&
  68. ( country_ == lhs.country_ );
  69. }
  70. int house_number_;
  71. string road_;
  72. string town_;
  73. string county_;
  74. string country_;
  75. };
  76. void write_address( Array& a, const Address& addr )
  77. {
  78. Object& addr_obj( add_child_obj( a ) );
  79. add_pair( addr_obj, "house_number", addr.house_number_ );
  80. add_pair( addr_obj, "road", addr.road_ );
  81. add_pair( addr_obj, "town", addr.town_ );
  82. add_pair( addr_obj, "county", addr.county_ );
  83. add_pair( addr_obj, "country", addr.country_ );
  84. // an alternative method to do the above would be as below,
  85. // but the push_back causes a copy of the object to made
  86. // which could be expensive for complex objects
  87. //
  88. //Object addr_obj;
  89. //add_pair( addr_obj, "house_number", addr.house_number_ );
  90. //add_pair( addr_obj, "road", addr.road_ );
  91. //add_pair( addr_obj, "town", addr.town_ );
  92. //add_pair( addr_obj, "county", addr.county_ );
  93. //add_pair( addr_obj, "country", addr.country_ );
  94. //a.push_back( addr_obj );
  95. }
  96. Address read_address( const Object& obj )
  97. {
  98. Address addr;
  99. addr.house_number_ = find_int( obj, "house_number" );
  100. addr.road_ = find_str( obj, "road" );
  101. addr.town_ = find_str( obj, "town" );
  102. addr.county_ = find_str( obj, "county" );
  103. addr.country_ = find_str( obj, "country" );
  104. return addr;
  105. }
  106. void write_addrs( const char* file_name, const Address addrs[] )
  107. {
  108. Object root_obj;
  109. Array& addr_array( add_array( root_obj, "addresses" ) );
  110. for( int i = 0; i < 5; ++i )
  111. {
  112. write_address( addr_array, addrs[i] );
  113. }
  114. ofstream os( file_name );
  115. write_formatted( root_obj, os );
  116. os.close();
  117. }
  118. vector< Address > read_addrs( const char* file_name )
  119. {
  120. ifstream is( file_name );
  121. Value value;
  122. read( is, value );
  123. Object root_obj( value.get_obj() ); // a 'value' read from a stream could be an JSON array or object
  124. const Array& addr_array( find_array( root_obj, "addresses" ) );
  125. vector< Address > addrs;
  126. for( unsigned int i = 0; i < addr_array.size(); ++i )
  127. {
  128. addrs.push_back( read_address( addr_array[i].get_obj() ) );
  129. }
  130. return addrs;
  131. }
  132. int main()
  133. {
  134. const Address addrs[5] = { { 42, "East Street", "Newtown", "Essex", "England" },
  135. { 1, "West Street", "Hull", "Yorkshire", "England" },
  136. { 12, "South Road", "Aberystwyth", "Dyfed", "Wales" },
  137. { 45, "North Road", "Paignton", "Devon", "England" },
  138. { 78, "Upper Street", "Ware", "Hertforshire", "England" } };
  139. const char* file_name( "demo.txt" );
  140. write_addrs( file_name, addrs );
  141. vector< Address > new_addrs( read_addrs( file_name ) );
  142. assert( new_addrs.size() == 5 );
  143. for( int i = 0; i < 5; ++i )
  144. {
  145. assert( new_addrs[i] == addrs[i] );
  146. }
  147. return 0;
  148. }