Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jsonl_iterator.h
1#ifndef SOURCEMETA_CORE_JSONL_ITERATOR_H_
2#define SOURCEMETA_CORE_JSONL_ITERATOR_H_
3
4#ifndef SOURCEMETA_CORE_JSONL_EXPORT
5#include <sourcemeta/core/jsonl_export.h>
6#endif
7
8#include <sourcemeta/core/json_value.h>
9
10#include <cstddef> // std::ptrdiff_t
11#include <cstdint> // std::uint64_t
12#include <istream> // std::basic_istream
13#include <iterator> // std::forward_iterator_tag
14#include <memory> // std::unique_ptr
15
16namespace sourcemeta::core {
17
20enum class JSONLFraming : std::uint8_t {
26};
27
31class SOURCEMETA_CORE_JSONL_EXPORT ConstJSONLIterator {
32public:
35 ConstJSONLIterator(std::basic_istream<JSON::Char, JSON::CharTraits> *stream,
38 using iterator_category = std::forward_iterator_tag;
39 using difference_type = std::ptrdiff_t;
40 using value_type = JSON;
41 using pointer = const value_type *;
42 using reference = const value_type &;
43
44 auto operator*() const -> reference;
45 auto operator->() const -> pointer;
46 auto operator++() -> ConstJSONLIterator &;
47
48 SOURCEMETA_CORE_JSONL_EXPORT friend auto
49 operator==(const ConstJSONLIterator &left, const ConstJSONLIterator &right)
50 -> bool;
51
52private:
53 std::uint64_t line_{0};
54 std::uint64_t column_{0};
55 JSONLFraming framing_{JSONLFraming::LineFeed};
56 bool at_sequence_start_{true};
57 auto parse_next() -> JSON;
58 auto parse_next_line() -> JSON;
59 auto parse_next_record() -> JSON;
60 std::basic_istream<JSON::Char, JSON::CharTraits> *data_{};
61
62// Exporting symbols that depends on the standard C++ library is considered
63// safe.
64// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
65#if defined(_MSC_VER)
66#pragma warning(push)
67#pragma warning(disable : 4251)
68#endif
69 // Use PIMPL idiom to hide internal details, mainly
70 // templated members, which are tricky to DLL-export.
71 struct Internal;
72 std::unique_ptr<Internal> internal_{};
73#if defined(_MSC_VER)
74#pragma warning(pop)
75#endif
76};
77
78} // namespace sourcemeta::core
79
80#endif
Definition json_value.h:39
ConstJSONLIterator(std::basic_istream< JSON::Char, JSON::CharTraits > *stream, JSONLFraming framing=JSONLFraming::LineFeed)
JSONLFraming
Definition jsonl_iterator.h:20
@ LineFeed
Every value is terminated by a line feed, as in JSON Lines and NDJSON.
Definition jsonl_iterator.h:22
@ RecordSeparator
Definition jsonl_iterator.h:25