json_test.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // json_test.cpp
  3. // json
  4. //
  5. // Created by Sam Jaffe on 4/22/16.
  6. //
  7. #include "json_binder.hpp"
  8. struct test_t {
  9. int a, b;
  10. };
  11. struct test_2_t {
  12. test_t t;
  13. double d;
  14. };
  15. int main(int argc, char const** argv) {
  16. auto bind1 = json::binder::object_binder<test_t>{}
  17. ("a", &test_t::a)
  18. ("b", &test_t::b);
  19. json::binder::tuple_binder<test_t>{}
  20. (&test_t::a)
  21. (&test_t::b);
  22. auto bind2 = json::binder::object_binder<test_2_t>{}
  23. ("t", &test_2_t::t, bind1)("d", &test_2_t::d);
  24. {
  25. test_t out;
  26. std::string data = "{\"a\":1,\"b\":2}";
  27. json::parser::parse(json::binder::bind(out, bind1), data);
  28. std::cout << out.a << ',' << out.b << std::endl;
  29. }
  30. {
  31. test_2_t out;
  32. std::string data = "{\"t\":{\"a\":1,\"b\":2},\"d\":1.5}";
  33. std::cout << data << std::endl;
  34. json::parser::parse(json::binder::bind(out, bind2), data);
  35. std::cout << '{' << out.t.a << ',' << out.t.b << "}," << out.d << std::endl;
  36. std::string sout;
  37. json::parser::write(json::binder::bind(out, bind2), sout);
  38. std::cout << sout << std::endl;
  39. }
  40. }