to_json.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // to_json.h
  3. // logger
  4. //
  5. // Created by Sam Jaffe on 4/4/19.
  6. //
  7. #pragma once
  8. #include <iostream>
  9. #if defined(LOGGING_JSON_SJAFFE)
  10. # include "json/json.hpp"
  11. #endif
  12. #if defined(LOGGING_JSON_VALUE)
  13. # include <memory>
  14. # include "json/json.h"
  15. #endif
  16. namespace logging { namespace detail {
  17. template <typename T> std::string to_string(T const & obj);
  18. // TODO: Establish bindings
  19. #if defined(LOGGING_JSON_SJAFFE)
  20. template <typename T, typename S>
  21. void to_stream(json::binder::visitor<T, S> const & visitor,
  22. std::ostream & out) {
  23. json::parser::write(visitor, out);
  24. }
  25. void to_stream(json::value const & obj, std::ostream & os) {
  26. json::parser::write(obj, os);
  27. }
  28. #endif
  29. #if defined(LOGGING_JSON_VALUE)
  30. void to_stream(Json::Value const & obj, std::ostream & os) {
  31. Json::StreamWriterBuilder build;
  32. std::unique_ptr<Json::StreamWriter> ptr(build.newStreamWriter());
  33. return ptr->write(obj, &os);
  34. }
  35. #endif
  36. template <typename T>
  37. std::string to_json(T const & obj, bool compact = false) {
  38. return "\"" + to_string(obj) + "\"";
  39. }
  40. } }