Add a Flatbuffers containing object, and more variants

We now have DetachedBuffers which represent flatbuffers, the new
Flatbuffer object, and Table objects.  It should be easy to merge and
copy these various objects now.  (And print).

Change-Id: Ie8fcff1e97f0ae5a7ef8a3becb1be467010460dc
diff --git a/aos/json_to_flatbuffer.h b/aos/json_to_flatbuffer.h
index 78d0703..183e6b2 100644
--- a/aos/json_to_flatbuffer.h
+++ b/aos/json_to_flatbuffer.h
@@ -5,20 +5,52 @@
 #include <string>
 
 #include "absl/strings/string_view.h"
+#include "aos/flatbuffers.h"
 #include "flatbuffers/flatbuffers.h"
 
 namespace aos {
 
 // Parses the flatbuffer into the vector, or returns an empty vector.
-::std::vector<uint8_t> JsonToFlatbuffer(
+flatbuffers::DetachedBuffer JsonToFlatbuffer(
     const absl::string_view data, const flatbuffers::TypeTable *typetable);
 
 // Converts a flatbuffer into a Json string.
-//
 // multi_line controls if the Json is written out on multiple lines or one.
-::std::string FlatbufferToJson(const uint8_t *buffer,
-                               const flatbuffers::TypeTable *typetable,
-                               bool multi_line = false);
+// The methods below are generally more useful than BufferFlatbufferToJson and
+// TableFlatbufferToJson.
+::std::string BufferFlatbufferToJson(const uint8_t *buffer,
+                                     const flatbuffers::TypeTable *typetable,
+                                     bool multi_line = false);
+
+::std::string TableFlatbufferToJson(const flatbuffers::Table *t,
+                                    const ::flatbuffers::TypeTable *typetable,
+                                    bool multi_line);
+
+// Converts a DetachedBuffer holding a flatbuffer to JSON.
+inline ::std::string FlatbufferToJson(const flatbuffers::DetachedBuffer &buffer,
+                                      const flatbuffers::TypeTable *typetable,
+                                      bool multi_line = false) {
+  return BufferFlatbufferToJson(buffer.data(), typetable, multi_line);
+}
+
+// Converts a Flatbuffer<T> holding a flatbuffer to JSON.
+template <typename T>
+inline ::std::string FlatbufferToJson(const Flatbuffer<T> &flatbuffer,
+                                      bool multi_line = false) {
+  return BufferFlatbufferToJson(
+      flatbuffer.data(), Flatbuffer<T>::MiniReflectTypeTable(), multi_line);
+}
+
+// Converts a flatbuffer::Table to JSON.
+template <typename T>
+typename std::enable_if<
+    std::is_base_of<flatbuffers::Table, T>::value,
+    std::string>::type inline FlatbufferToJson(const T *flatbuffer,
+                                               bool multi_line = false) {
+  return TableFlatbufferToJson(
+      reinterpret_cast<const flatbuffers::Table *>(flatbuffer),
+      Flatbuffer<T>::MiniReflectTypeTable(), multi_line);
+}
 
 }  // namespace aos