json_common.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // json_binder.cpp
  3. // json
  4. //
  5. // Created by Sam Jaffe on 4/23/16.
  6. //
  7. #include "json_common.h"
  8. namespace json {
  9. namespace helper {
  10. namespace {
  11. std::string get_no_end_error(char expected, char found) {
  12. char str[64] = { '\0' };
  13. snprintf(str, sizeof(str),
  14. "Expected delimeter: ',' or '%c', got '%c' instead",
  15. expected, found);
  16. return str;
  17. }
  18. }
  19. const char get_next_element(char const*& data) {
  20. while (isspace(*data)) ++data;
  21. return *data;
  22. }
  23. void advance_to_boundary(char const endtok, char const*& data) {
  24. char const next = get_next_element(data);
  25. if (next == ',') {
  26. ++data;
  27. } else if (next != endtok) {
  28. throw json::malformed_json_exception(get_no_end_error(endtok, *data));
  29. }
  30. }
  31. std::string parse_string(char const * & data) {
  32. char const* start = data;
  33. while (*++data) {
  34. if (*data == '"' && *(data-1) != '\\') {
  35. return std::string(start+1, data++);
  36. }
  37. }
  38. throw json::malformed_json_exception("Could not locate end of string");
  39. }
  40. }
  41. }