JsonDom.hh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * https://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef avro_json_JsonDom_hh__
  19. #define avro_json_JsonDom_hh__
  20. #include <cstdint>
  21. #include <iostream>
  22. #include <map>
  23. #include <memory>
  24. #include <string>
  25. #include <vector>
  26. #include "Config.hh"
  27. #include "boost/any.hpp"
  28. namespace avro {
  29. class AVRO_DECL InputStream;
  30. namespace json {
  31. class Entity;
  32. typedef bool Bool;
  33. typedef int64_t Long;
  34. typedef double Double;
  35. typedef std::string String;
  36. typedef std::vector<Entity> Array;
  37. typedef std::map<std::string, Entity> Object;
  38. class AVRO_DECL JsonParser;
  39. class JsonNullFormatter;
  40. template<typename F = JsonNullFormatter>
  41. class AVRO_DECL JsonGenerator;
  42. enum class EntityType {
  43. Null,
  44. Bool,
  45. Long,
  46. Double,
  47. String,
  48. Arr,
  49. Obj
  50. };
  51. const char *typeToString(EntityType t);
  52. inline std::ostream &operator<<(std::ostream &os, EntityType et) {
  53. return os << typeToString(et);
  54. }
  55. class AVRO_DECL Entity {
  56. EntityType type_;
  57. boost::any value_;
  58. size_t line_; // can't be const else noncopyable...
  59. void ensureType(EntityType) const;
  60. public:
  61. explicit Entity(size_t line = 0) : type_(EntityType::Null), line_(line) {}
  62. // Not explicit because do want implicit conversion
  63. // NOLINTNEXTLINE(google-explicit-constructor)
  64. explicit Entity(Bool v, size_t line = 0) : type_(EntityType::Bool), value_(v), line_(line) {}
  65. // Not explicit because do want implicit conversion
  66. // NOLINTNEXTLINE(google-explicit-constructor)
  67. explicit Entity(Long v, size_t line = 0) : type_(EntityType::Long), value_(v), line_(line) {}
  68. // Not explicit because do want implicit conversion
  69. // NOLINTNEXTLINE(google-explicit-constructor)
  70. explicit Entity(Double v, size_t line = 0) : type_(EntityType::Double), value_(v), line_(line) {}
  71. // Not explicit because do want implicit conversion
  72. // NOLINTNEXTLINE(google-explicit-constructor)
  73. explicit Entity(const std::shared_ptr<String> &v, size_t line = 0) : type_(EntityType::String), value_(v), line_(line) {}
  74. // Not explicit because do want implicit conversion
  75. // NOLINTNEXTLINE(google-explicit-constructor)
  76. explicit Entity(const std::shared_ptr<Array> &v, size_t line = 0) : type_(EntityType::Arr), value_(v), line_(line) {}
  77. // Not explicit because do want implicit conversion
  78. // NOLINTNEXTLINE(google-explicit-constructor)
  79. explicit Entity(const std::shared_ptr<Object> &v, size_t line = 0) : type_(EntityType::Obj), value_(v), line_(line) {}
  80. EntityType type() const { return type_; }
  81. size_t line() const { return line_; }
  82. Bool boolValue() const {
  83. ensureType(EntityType::Bool);
  84. return boost::any_cast<Bool>(value_);
  85. }
  86. Long longValue() const {
  87. ensureType(EntityType::Long);
  88. return boost::any_cast<Long>(value_);
  89. }
  90. Double doubleValue() const {
  91. ensureType(EntityType::Double);
  92. return boost::any_cast<Double>(value_);
  93. }
  94. String stringValue() const;
  95. String bytesValue() const;
  96. const Array &arrayValue() const {
  97. ensureType(EntityType::Arr);
  98. return **boost::any_cast<std::shared_ptr<Array>>(&value_);
  99. }
  100. const Object &objectValue() const {
  101. ensureType(EntityType::Obj);
  102. return **boost::any_cast<std::shared_ptr<Object>>(&value_);
  103. }
  104. std::string toString() const;
  105. };
  106. template<typename T>
  107. struct type_traits {
  108. };
  109. template<>
  110. struct type_traits<bool> {
  111. static EntityType type() { return EntityType::Bool; }
  112. static const char *name() { return "bool"; }
  113. };
  114. template<>
  115. struct type_traits<int64_t> {
  116. static EntityType type() { return EntityType::Long; }
  117. static const char *name() { return "long"; }
  118. };
  119. template<>
  120. struct type_traits<double> {
  121. static EntityType type() { return EntityType::Double; }
  122. static const char *name() { return "double"; }
  123. };
  124. template<>
  125. struct type_traits<std::string> {
  126. static EntityType type() { return EntityType::String; }
  127. static const char *name() { return "string"; }
  128. };
  129. template<>
  130. struct type_traits<std::vector<Entity>> {
  131. static EntityType type() { return EntityType::Arr; }
  132. static const char *name() { return "array"; }
  133. };
  134. template<>
  135. struct type_traits<std::map<std::string, Entity>> {
  136. static EntityType type() { return EntityType::Obj; }
  137. static const char *name() { return "object"; }
  138. };
  139. AVRO_DECL Entity readEntity(JsonParser &p);
  140. AVRO_DECL Entity loadEntity(InputStream &in);
  141. AVRO_DECL Entity loadEntity(const char *text);
  142. AVRO_DECL Entity loadEntity(const uint8_t *text, size_t len);
  143. void writeEntity(JsonGenerator<JsonNullFormatter> &g, const Entity &n);
  144. } // namespace json
  145. } // namespace avro
  146. #endif