Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. /** This software is distributed under BSD 3-clause license (see LICENSE file).
  2. *
  3. * Authors: Sergey Lisitsyn, Viktor Gal
  4. */
  5.  
  6. #include <memory>
  7.  
  8. #include <shogun/io/serialization/JsonSerializer.h>
  9. #include <shogun/lib/SGVector.h>
  10. #include <shogun/lib/SGMatrix.h>
  11.  
  12. #include <rapidjson/writer.h>
  13.  
  14. using namespace shogun;
  15.  
  16. struct COutputStreamAdapter
  17. {
  18. typedef char Ch;
  19. void Put(Ch c)
  20. {
  21. m_stream->write(&c, 1);
  22. }
  23.  
  24. void Flush()
  25. {
  26. m_stream->flush();
  27. }
  28.  
  29. COutputStream* m_stream;
  30. };
  31.  
  32. static void object_writer(rapidjson::Writer<COutputStreamAdapter>& writer, Some<CSGObject> object);
  33.  
  34. template<typename RapidJsonWriter>
  35. class JSONWriterVisitor : public AnyVisitor
  36. {
  37. public:
  38. JSONWriterVisitor(RapidJsonWriter& jw):
  39. AnyVisitor(), m_json_writer(jw) {}
  40.  
  41. virtual void on(bool* v)
  42. {
  43. SG_SDEBUG("writing bool with value %d\n", *v)
  44. m_json_writer.Bool(*v);
  45. }
  46. virtual void on(int32_t* v)
  47. {
  48. SG_SDEBUG("writing int32_t with value %d\n", *v)
  49. m_json_writer.Int(*v);
  50. }
  51. virtual void on(int64_t* v)
  52. {
  53. SG_SDEBUG("writing int64_t with value %d\n", *v)
  54. m_json_writer.Int64(*v);
  55. }
  56. virtual void on(float* v)
  57. {
  58. SG_SDEBUG("writing float with value %f\n", *v)
  59. m_json_writer.Double(*v);
  60. }
  61. virtual void on(double* v)
  62. {
  63. SG_SDEBUG("writing double with value %f\n", *v)
  64. m_json_writer.Double(*v);
  65. }
  66. virtual void on(CSGObject** v)
  67. {
  68. if (*v)
  69. {
  70. SG_SDEBUG("writing SGObject with of type\n")
  71. object_writer(m_json_writer, wrap<CSGObject>(*v));
  72. }
  73. }
  74. virtual void on(SGVector<int>* v)
  75. {
  76. SG_SDEBUG("writing SGVector<int>\n")
  77. m_json_writer.StartArray();
  78. for (const auto& i: *v)
  79. m_json_writer.Int(i);
  80. m_json_writer.EndArray();
  81. }
  82. virtual void on(SGVector<float>* v)
  83. {
  84. SG_SDEBUG("writing SGVector<float>\n")
  85. }
  86. virtual void on(SGVector<double>* v)
  87. {
  88. SG_SDEBUG("writing SGVector<double>\n")
  89.  
  90. }
  91. virtual void on(SGMatrix<int>* v)
  92. {
  93. SG_SDEBUG("writing SGMatrix<int>\n")
  94.  
  95. }
  96. virtual void on(SGMatrix<float>* v)
  97. {
  98. SG_SDEBUG("writing SGMatrix<float>\n")
  99.  
  100. }
  101. virtual void on(SGMatrix<double>* v)
  102. {
  103. SG_SDEBUG("writing SGMatrix<double>\n")
  104.  
  105. }
  106.  
  107. private:
  108. RapidJsonWriter& m_json_writer;
  109. };
  110.  
  111. static void object_writer(rapidjson::Writer<COutputStreamAdapter>& writer, Some<CSGObject> object)
  112. {
  113. auto writer_visitor = std::make_unique<JSONWriterVisitor<rapidjson::Writer<COutputStreamAdapter>>>(writer);
  114. writer.Key("name");
  115. writer.String(object->get_name());
  116. writer.Key("generic");
  117. writer.Int(object->get_generic());
  118. auto param_names = object->parameter_names();
  119. writer.Key("parameters");
  120. writer.StartObject();
  121. for (auto param_name: param_names)
  122. {
  123. writer.Key(param_name.c_str());
  124. BaseTag tag(param_name);
  125. auto param = object->get_parameter(tag);
  126. param.get_value().visit(writer_visitor.get());
  127. }
  128. writer.EndObject();
  129. }
  130.  
  131. CJsonSerializer::CJsonSerializer() : CSerializer()
  132. {
  133. }
  134.  
  135. CJsonSerializer::~CJsonSerializer()
  136. {
  137. }
  138.  
  139. void CJsonSerializer::write(Some<CSGObject> object)
  140. {
  141. COutputStreamAdapter adapter{ .m_stream = stream().get() };
  142. rapidjson::Writer<COutputStreamAdapter> writer(adapter);
  143. object_writer(writer, object);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement