json_common.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // json_common.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 4/22/16.
  6. //
  7. #pragma once
  8. #include <cstdlib>
  9. #include <stdexcept>
  10. #include <string>
  11. namespace json {
  12. class malformed_json_exception : public std::domain_error {
  13. using std::domain_error::domain_error;
  14. };
  15. }
  16. namespace json {
  17. namespace helper {
  18. const char get_next_element(char const*& data);
  19. /**
  20. * @throws json::malformed_json_exception
  21. */
  22. void advance_to_boundary(char const endtok, char const*& data);
  23. /**
  24. * @throws json::malformed_json_exception
  25. */
  26. std::string parse_string(char const * & data);
  27. template <typename T>
  28. void parse_string(T& json, char const*& data) {
  29. json = parse_string(data);
  30. }
  31. template <typename T>
  32. void parse_numeric(T& json, char const*& data) {
  33. // TODO: more sophisticated float detection?
  34. char const* start = data;
  35. while (*++data) {
  36. if (*data == '.') {
  37. while (isnumber(*++data));
  38. json = atof(start);
  39. break;
  40. } else if (!isnumber(*data)) {
  41. json = atoi(start);
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. }