json_test.cpp 1.2 KB

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