Browse Source

Do some TLC on to_json...

Sam Jaffe 6 năm trước cách đây
mục cha
commit
b503a60dc7

+ 34 - 19
include/logger/detail/to_json.h

@@ -7,27 +7,42 @@
 
 #pragma once
 
-#if defined(LOGGING_USE_JSON)
+#include <iostream>
+
 #if defined(LOGGING_JSON_SJAFFE)
-#include "json/json.hpp"
-namespace logging {
-  typedef json::value json;
-}
-#elif defined(LOGGING_JSON_VALUE)
-#include "json/json_fwd.h"
-namespace logging {
-  typedef Json::Value json;
-}
+#  include "json/json.hpp"
+#endif
+#if defined(LOGGING_JSON_VALUE)
+#  include <memory>
+#  include "json/json.h"
 #endif
+
 namespace logging { namespace detail {
+  template <typename T> std::string to_string(T const & obj);
+
+// TODO: Establish bindings
+#if defined(LOGGING_JSON_SJAFFE)
+  template <typename T, typename S>
+  void to_stream(json::binder::visitor<T, S> const & visitor,
+                 std::ostream & out) {
+    json::parser::write(visitor, out);
+  }
+  
+  void to_stream(json::value const & obj, std::ostream & os) {
+    json::parser::write(obj, os);
+  }
+#endif
+
+#if defined(LOGGING_JSON_VALUE)
+  void to_stream(Json::Value const & obj, std::ostream & os) {
+    Json::StreamWriterBuilder build;
+    std::unique_ptr<Json::StreamWriter> ptr(build.newStreamWriter());
+    return ptr->write(obj, &os);
+  }
+#endif
+  
   template <typename T>
-  json to_json(T const & obj);
+  std::string to_json(T const & obj, bool compact = false) {
+    return to_string(obj);
+  }
 } }
-#else
-namespace logging {
-  typedef void json;
-}
-namespace logging { namespace detail {
-  template <typename T> void to_json(T const &) {}
-} }
-#endif

+ 1 - 9
include/logger/wrapper_object.h

@@ -23,12 +23,10 @@ namespace logging { namespace detail {
       return os << obj.to_string_(obj.ptr_);
     }
     template <typename> static std::string to_string_impl(void * ptr);
-    template <typename> static json to_json_impl(void * ptr);
 
   private:
     void * ptr_;
     std::string (*to_string_)(void*);
-    json (*to_json_)(void*);
   };
   
   
@@ -37,16 +35,10 @@ namespace logging { namespace detail {
     return to_string(*static_cast<T*>(ptr));
   }
   
-  template <typename T>
-  json object::to_json_impl(void * ptr) {
-    return to_json(*static_cast<T*>(ptr));
-  }
-  
   template <typename T>
   object::object(T & object)
   : ptr_(&object),
-  to_string_(&object::to_string_impl<T>),
-  to_json_(&object::to_json_impl<T>) {
+  to_string_(&object::to_string_impl<T>) {
     
   }
 } }

+ 1 - 1
logger.xcodeproj/xcuserdata/samjaffe.xcuserdatad/xcschemes/xcschememanagement.plist

@@ -7,7 +7,7 @@
 		<key>logger_test.xcscheme_^#shared#^_</key>
 		<dict>
 			<key>orderHint</key>
-			<integer>46</integer>
+			<integer>44</integer>
 		</dict>
 		<key>logging.xcscheme</key>
 		<dict>