Helper.JSON.hpp 934 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #pragma once
  2. #include "ResDataObject.h"
  3. template <typename T>
  4. static inline T JSONTo (const std::string& in)
  5. {
  6. static_assert (false, "bad type for JSONTo");
  7. }
  8. template <>
  9. static inline float JSONTo (const std::string& in)
  10. {
  11. ResDataObject json;
  12. json.decode (in.c_str ());
  13. auto value = atof (((string) json [0]).c_str ());
  14. return (float) value;
  15. }
  16. template <>
  17. static inline double JSONTo (const std::string& in)
  18. {
  19. ResDataObject json;
  20. json.decode (in.c_str ());
  21. auto value = atof (((string) json [0]).c_str ());
  22. return value;
  23. }
  24. template <>
  25. static inline int JSONTo (const std::string& in)
  26. {
  27. ResDataObject json;
  28. json.decode (in.c_str ());
  29. auto value = atoi (((string) json [0]).c_str ());
  30. return value;
  31. }
  32. static inline std::string ToJSON (int value)
  33. {
  34. return std::to_string (value);
  35. }
  36. static inline std::string ToJSON (float value)
  37. {
  38. return std::to_string (value);
  39. }