input_adapters.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #pragma once
  9. #include <array> // array
  10. #include <cstddef> // size_t
  11. #include <cstring> // strlen
  12. #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
  13. #include <memory> // shared_ptr, make_shared, addressof
  14. #include <numeric> // accumulate
  15. #include <string> // string, char_traits
  16. #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
  17. #include <utility> // pair, declval
  18. #ifndef JSON_NO_IO
  19. #include <cstdio> // FILE *
  20. #include <istream> // istream
  21. #endif // JSON_NO_IO
  22. #include <nlohmann/detail/iterators/iterator_traits.hpp>
  23. #include <nlohmann/detail/macro_scope.hpp>
  24. NLOHMANN_JSON_NAMESPACE_BEGIN
  25. namespace detail
  26. {
  27. /// the supported input formats
  28. enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
  29. ////////////////////
  30. // input adapters //
  31. ////////////////////
  32. #ifndef JSON_NO_IO
  33. /*!
  34. Input adapter for stdio file access. This adapter read only 1 byte and do not use any
  35. buffer. This adapter is a very low level adapter.
  36. */
  37. class file_input_adapter
  38. {
  39. public:
  40. using char_type = char;
  41. JSON_HEDLEY_NON_NULL(2)
  42. explicit file_input_adapter(std::FILE* f) noexcept
  43. : m_file(f)
  44. {
  45. JSON_ASSERT(m_file != nullptr);
  46. }
  47. // make class move-only
  48. file_input_adapter(const file_input_adapter&) = delete;
  49. file_input_adapter(file_input_adapter&&) noexcept = default;
  50. file_input_adapter& operator=(const file_input_adapter&) = delete;
  51. file_input_adapter& operator=(file_input_adapter&&) = delete;
  52. ~file_input_adapter() = default;
  53. std::char_traits<char>::int_type get_character() noexcept
  54. {
  55. return std::fgetc(m_file);
  56. }
  57. private:
  58. /// the file pointer to read from
  59. std::FILE* m_file;
  60. };
  61. /*!
  62. Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
  63. beginning of input. Does not support changing the underlying std::streambuf
  64. in mid-input. Maintains underlying std::istream and std::streambuf to support
  65. subsequent use of standard std::istream operations to process any input
  66. characters following those used in parsing the JSON input. Clears the
  67. std::istream flags; any input errors (e.g., EOF) will be detected by the first
  68. subsequent call for input from the std::istream.
  69. */
  70. class input_stream_adapter
  71. {
  72. public:
  73. using char_type = char;
  74. ~input_stream_adapter()
  75. {
  76. // clear stream flags; we use underlying streambuf I/O, do not
  77. // maintain ifstream flags, except eof
  78. if (is != nullptr)
  79. {
  80. is->clear(is->rdstate() & std::ios::eofbit);
  81. }
  82. }
  83. explicit input_stream_adapter(std::istream& i)
  84. : is(&i), sb(i.rdbuf())
  85. {}
  86. // delete because of pointer members
  87. input_stream_adapter(const input_stream_adapter&) = delete;
  88. input_stream_adapter& operator=(input_stream_adapter&) = delete;
  89. input_stream_adapter& operator=(input_stream_adapter&&) = delete;
  90. input_stream_adapter(input_stream_adapter&& rhs) noexcept
  91. : is(rhs.is), sb(rhs.sb)
  92. {
  93. rhs.is = nullptr;
  94. rhs.sb = nullptr;
  95. }
  96. // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
  97. // ensure that std::char_traits<char>::eof() and the character 0xFF do not
  98. // end up as the same value, e.g. 0xFFFFFFFF.
  99. std::char_traits<char>::int_type get_character()
  100. {
  101. auto res = sb->sbumpc();
  102. // set eof manually, as we don't use the istream interface.
  103. if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
  104. {
  105. is->clear(is->rdstate() | std::ios::eofbit);
  106. }
  107. return res;
  108. }
  109. private:
  110. /// the associated input stream
  111. std::istream* is = nullptr;
  112. std::streambuf* sb = nullptr;
  113. };
  114. #endif // JSON_NO_IO
  115. // General-purpose iterator-based adapter. It might not be as fast as
  116. // theoretically possible for some containers, but it is extremely versatile.
  117. template<typename IteratorType>
  118. class iterator_input_adapter
  119. {
  120. public:
  121. using char_type = typename std::iterator_traits<IteratorType>::value_type;
  122. iterator_input_adapter(IteratorType first, IteratorType last)
  123. : current(std::move(first)), end(std::move(last))
  124. {}
  125. typename std::char_traits<char_type>::int_type get_character()
  126. {
  127. if (JSON_HEDLEY_LIKELY(current != end))
  128. {
  129. auto result = std::char_traits<char_type>::to_int_type(*current);
  130. std::advance(current, 1);
  131. return result;
  132. }
  133. return std::char_traits<char_type>::eof();
  134. }
  135. private:
  136. IteratorType current;
  137. IteratorType end;
  138. template<typename BaseInputAdapter, size_t T>
  139. friend struct wide_string_input_helper;
  140. bool empty() const
  141. {
  142. return current == end;
  143. }
  144. };
  145. template<typename BaseInputAdapter, size_t T>
  146. struct wide_string_input_helper;
  147. template<typename BaseInputAdapter>
  148. struct wide_string_input_helper<BaseInputAdapter, 4>
  149. {
  150. // UTF-32
  151. static void fill_buffer(BaseInputAdapter& input,
  152. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  153. size_t& utf8_bytes_index,
  154. size_t& utf8_bytes_filled)
  155. {
  156. utf8_bytes_index = 0;
  157. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  158. {
  159. utf8_bytes[0] = std::char_traits<char>::eof();
  160. utf8_bytes_filled = 1;
  161. }
  162. else
  163. {
  164. // get the current character
  165. const auto wc = input.get_character();
  166. // UTF-32 to UTF-8 encoding
  167. if (wc < 0x80)
  168. {
  169. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  170. utf8_bytes_filled = 1;
  171. }
  172. else if (wc <= 0x7FF)
  173. {
  174. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
  175. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  176. utf8_bytes_filled = 2;
  177. }
  178. else if (wc <= 0xFFFF)
  179. {
  180. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
  181. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  182. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  183. utf8_bytes_filled = 3;
  184. }
  185. else if (wc <= 0x10FFFF)
  186. {
  187. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
  188. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
  189. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  190. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  191. utf8_bytes_filled = 4;
  192. }
  193. else
  194. {
  195. // unknown character
  196. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  197. utf8_bytes_filled = 1;
  198. }
  199. }
  200. }
  201. };
  202. template<typename BaseInputAdapter>
  203. struct wide_string_input_helper<BaseInputAdapter, 2>
  204. {
  205. // UTF-16
  206. static void fill_buffer(BaseInputAdapter& input,
  207. std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
  208. size_t& utf8_bytes_index,
  209. size_t& utf8_bytes_filled)
  210. {
  211. utf8_bytes_index = 0;
  212. if (JSON_HEDLEY_UNLIKELY(input.empty()))
  213. {
  214. utf8_bytes[0] = std::char_traits<char>::eof();
  215. utf8_bytes_filled = 1;
  216. }
  217. else
  218. {
  219. // get the current character
  220. const auto wc = input.get_character();
  221. // UTF-16 to UTF-8 encoding
  222. if (wc < 0x80)
  223. {
  224. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  225. utf8_bytes_filled = 1;
  226. }
  227. else if (wc <= 0x7FF)
  228. {
  229. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
  230. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  231. utf8_bytes_filled = 2;
  232. }
  233. else if (0xD800 > wc || wc >= 0xE000)
  234. {
  235. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
  236. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
  237. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
  238. utf8_bytes_filled = 3;
  239. }
  240. else
  241. {
  242. if (JSON_HEDLEY_UNLIKELY(!input.empty()))
  243. {
  244. const auto wc2 = static_cast<unsigned int>(input.get_character());
  245. const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
  246. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
  247. utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
  248. utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
  249. utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
  250. utf8_bytes_filled = 4;
  251. }
  252. else
  253. {
  254. utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
  255. utf8_bytes_filled = 1;
  256. }
  257. }
  258. }
  259. }
  260. };
  261. // Wraps another input apdater to convert wide character types into individual bytes.
  262. template<typename BaseInputAdapter, typename WideCharType>
  263. class wide_string_input_adapter
  264. {
  265. public:
  266. using char_type = char;
  267. wide_string_input_adapter(BaseInputAdapter base)
  268. : base_adapter(base) {}
  269. typename std::char_traits<char>::int_type get_character() noexcept
  270. {
  271. // check if buffer needs to be filled
  272. if (utf8_bytes_index == utf8_bytes_filled)
  273. {
  274. fill_buffer<sizeof(WideCharType)>();
  275. JSON_ASSERT(utf8_bytes_filled > 0);
  276. JSON_ASSERT(utf8_bytes_index == 0);
  277. }
  278. // use buffer
  279. JSON_ASSERT(utf8_bytes_filled > 0);
  280. JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
  281. return utf8_bytes[utf8_bytes_index++];
  282. }
  283. private:
  284. BaseInputAdapter base_adapter;
  285. template<size_t T>
  286. void fill_buffer()
  287. {
  288. wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
  289. }
  290. /// a buffer for UTF-8 bytes
  291. std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
  292. /// index to the utf8_codes array for the next valid byte
  293. std::size_t utf8_bytes_index = 0;
  294. /// number of valid bytes in the utf8_codes array
  295. std::size_t utf8_bytes_filled = 0;
  296. };
  297. template<typename IteratorType, typename Enable = void>
  298. struct iterator_input_adapter_factory
  299. {
  300. using iterator_type = IteratorType;
  301. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  302. using adapter_type = iterator_input_adapter<iterator_type>;
  303. static adapter_type create(IteratorType first, IteratorType last)
  304. {
  305. return adapter_type(std::move(first), std::move(last));
  306. }
  307. };
  308. template<typename T>
  309. struct is_iterator_of_multibyte
  310. {
  311. using value_type = typename std::iterator_traits<T>::value_type;
  312. enum
  313. {
  314. value = sizeof(value_type) > 1
  315. };
  316. };
  317. template<typename IteratorType>
  318. struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
  319. {
  320. using iterator_type = IteratorType;
  321. using char_type = typename std::iterator_traits<iterator_type>::value_type;
  322. using base_adapter_type = iterator_input_adapter<iterator_type>;
  323. using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
  324. static adapter_type create(IteratorType first, IteratorType last)
  325. {
  326. return adapter_type(base_adapter_type(std::move(first), std::move(last)));
  327. }
  328. };
  329. // General purpose iterator-based input
  330. template<typename IteratorType>
  331. typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
  332. {
  333. using factory_type = iterator_input_adapter_factory<IteratorType>;
  334. return factory_type::create(first, last);
  335. }
  336. // Convenience shorthand from container to iterator
  337. // Enables ADL on begin(container) and end(container)
  338. // Encloses the using declarations in namespace for not to leak them to outside scope
  339. namespace container_input_adapter_factory_impl
  340. {
  341. using std::begin;
  342. using std::end;
  343. template<typename ContainerType, typename Enable = void>
  344. struct container_input_adapter_factory {};
  345. template<typename ContainerType>
  346. struct container_input_adapter_factory< ContainerType,
  347. void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
  348. {
  349. using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
  350. static adapter_type create(const ContainerType& container)
  351. {
  352. return input_adapter(begin(container), end(container));
  353. }
  354. };
  355. } // namespace container_input_adapter_factory_impl
  356. template<typename ContainerType>
  357. typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
  358. {
  359. return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
  360. }
  361. #ifndef JSON_NO_IO
  362. // Special cases with fast paths
  363. inline file_input_adapter input_adapter(std::FILE* file)
  364. {
  365. return file_input_adapter(file);
  366. }
  367. inline input_stream_adapter input_adapter(std::istream& stream)
  368. {
  369. return input_stream_adapter(stream);
  370. }
  371. inline input_stream_adapter input_adapter(std::istream&& stream)
  372. {
  373. return input_stream_adapter(stream);
  374. }
  375. #endif // JSON_NO_IO
  376. using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
  377. // Null-delimited strings, and the like.
  378. template < typename CharT,
  379. typename std::enable_if <
  380. std::is_pointer<CharT>::value&&
  381. !std::is_array<CharT>::value&&
  382. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  383. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  384. int >::type = 0 >
  385. contiguous_bytes_input_adapter input_adapter(CharT b)
  386. {
  387. auto length = std::strlen(reinterpret_cast<const char*>(b));
  388. const auto* ptr = reinterpret_cast<const char*>(b);
  389. return input_adapter(ptr, ptr + length);
  390. }
  391. template<typename T, std::size_t N>
  392. auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + N)) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
  393. {
  394. return input_adapter(array, array + N);
  395. }
  396. // This class only handles inputs of input_buffer_adapter type.
  397. // It's required so that expressions like {ptr, len} can be implicitly cast
  398. // to the correct adapter.
  399. class span_input_adapter
  400. {
  401. public:
  402. template < typename CharT,
  403. typename std::enable_if <
  404. std::is_pointer<CharT>::value&&
  405. std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
  406. sizeof(typename std::remove_pointer<CharT>::type) == 1,
  407. int >::type = 0 >
  408. span_input_adapter(CharT b, std::size_t l)
  409. : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
  410. template<class IteratorType,
  411. typename std::enable_if<
  412. std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
  413. int>::type = 0>
  414. span_input_adapter(IteratorType first, IteratorType last)
  415. : ia(input_adapter(first, last)) {}
  416. contiguous_bytes_input_adapter&& get()
  417. {
  418. return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
  419. }
  420. private:
  421. contiguous_bytes_input_adapter ia;
  422. };
  423. } // namespace detail
  424. NLOHMANN_JSON_NAMESPACE_END