Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
json.h
1#ifndef SOURCEMETA_CORE_JSON_H_
2#define SOURCEMETA_CORE_JSON_H_
3
4#ifndef SOURCEMETA_CORE_JSON_EXPORT
5#include <sourcemeta/core/json_export.h>
6#endif
7
8// NOLINTBEGIN(misc-include-cleaner)
9#include <sourcemeta/core/json_auto.h>
10#include <sourcemeta/core/json_error.h>
11#include <sourcemeta/core/json_value.h>
12// NOLINTEND(misc-include-cleaner)
13
14#include <sourcemeta/core/preprocessor.h>
15
16#include <cstdint> // std::uint64_t
17#include <filesystem> // std::filesystem
18#include <format> // std::formatter, std::format_context, std::format_parse_context, std::format_to
19#include <fstream> // std::basic_ifstream
20#include <initializer_list> // std::initializer_list
21#include <istream> // std::basic_istream
22#include <optional> // std::optional
23#include <ostream> // std::basic_ostream
24#include <sstream> // std::ostringstream
25#include <string> // std::basic_string
26
36
37namespace sourcemeta::core {
38
63SOURCEMETA_CORE_JSON_EXPORT
64auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream)
65 -> JSON;
66
89SOURCEMETA_CORE_JSON_EXPORT
91 const std::basic_string_view<JSON::Char, JSON::CharTraits> input) -> JSON;
92
107SOURCEMETA_CORE_JSON_EXPORT
109 const std::basic_string_view<JSON::Char, JSON::CharTraits> input)
110 -> std::optional<JSON>;
111
129SOURCEMETA_CORE_JSON_EXPORT
130auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
131 std::uint64_t &line, std::uint64_t &column) -> JSON;
132
148SOURCEMETA_CORE_JSON_EXPORT
150 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
151 std::uint64_t &line, std::uint64_t &column) -> JSON;
152
169SOURCEMETA_CORE_JSON_EXPORT
170auto read_json(const std::filesystem::path &path) -> JSON;
171
181SOURCEMETA_CORE_JSON_EXPORT
182auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
183 JSON &output, const JSON::ParseCallback &callback) -> void;
184
194SOURCEMETA_CORE_JSON_EXPORT
196 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
197 JSON &output, const JSON::ParseCallback &callback) -> void;
198
207SOURCEMETA_CORE_JSON_EXPORT
208auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
209 std::uint64_t &line, std::uint64_t &column, JSON &output,
210 const JSON::ParseCallback &callback) -> void;
211
220SOURCEMETA_CORE_JSON_EXPORT
222 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
223 std::uint64_t &line, std::uint64_t &column, JSON &output,
224 const JSON::ParseCallback &callback) -> void;
225
235SOURCEMETA_CORE_JSON_EXPORT
236auto read_json(const std::filesystem::path &path, JSON &output,
237 const JSON::ParseCallback &callback) -> void;
238
255SOURCEMETA_CORE_JSON_EXPORT
256auto stringify(const JSON &document,
257 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream)
258 -> void;
259
276SOURCEMETA_CORE_JSON_EXPORT
277auto prettify(const JSON &document,
278 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
279 const std::size_t spaces = 2) -> void;
280
298SOURCEMETA_CORE_JSON_EXPORT
299auto operator<<(std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
300 const JSON &document)
301 -> std::basic_ostream<JSON::Char, JSON::CharTraits> &;
302
318SOURCEMETA_CORE_JSON_EXPORT
319auto operator<<(std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
320 const JSON::Type type)
321 -> std::basic_ostream<JSON::Char, JSON::CharTraits> &;
322
334SOURCEMETA_FORCEINLINE inline auto
335make_set(std::initializer_list<JSON::Type> types) -> JSON::TypeSet {
336 JSON::TypeSet result;
337 for (const auto type : types) {
338 result.set(static_cast<std::size_t>(type));
339 }
340 return result;
341}
342
343} // namespace sourcemeta::core
344
346template <> struct std::formatter<sourcemeta::core::JSON> {
347 constexpr auto parse(std::format_parse_context &context)
348 -> decltype(context.begin()) {
349 return context.begin();
350 }
351
352 auto format(const sourcemeta::core::JSON &value,
353 std::format_context &context) const -> decltype(context.out()) {
354 std::ostringstream stream;
355 stream << value;
356 return std::format_to(context.out(), "{}", stream.str());
357 }
358};
359
360template <> struct std::formatter<sourcemeta::core::JSON::Type> {
361 constexpr auto parse(std::format_parse_context &context)
362 -> decltype(context.begin()) {
363 return context.begin();
364 }
365
366 auto format(const sourcemeta::core::JSON::Type value,
367 std::format_context &context) const -> decltype(context.out()) {
368 std::ostringstream stream;
369 stream << value;
370 return std::format_to(context.out(), "{}", stream.str());
371 }
372};
374
375#endif
SOURCEMETA_CORE_DIFF_EXPORT auto stringify(const Diff &document, std::ostream &stream, const Diff::Format format, const Diff::FormatOptions &options={}) -> void
std::function< void( const ParsePhase phase, const Type type, const std::uint64_t line, const std::uint64_t column, const ParseContext context, const std::size_t index, const String &property)> ParseCallback
Definition json_value.h:103
std::bitset< 8 > TypeSet
A set of types.
Definition json_value.h:89
Type
The different types of a JSON instance.
Definition json_value.h:69
Definition json_value.h:39
SOURCEMETA_CORE_JSON_EXPORT auto parse_json(std::basic_istream< JSON::Char, JSON::CharTraits > &stream) -> JSON
SOURCEMETA_CORE_JSON_EXPORT auto try_parse_json(const std::basic_string_view< JSON::Char, JSON::CharTraits > input) -> std::optional< JSON >
SOURCEMETA_FORCEINLINE auto make_set(std::initializer_list< JSON::Type > types) -> JSON::TypeSet
Definition json.h:335
SOURCEMETA_CORE_JSON_EXPORT auto prettify(const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream, const std::size_t spaces=2) -> void
SOURCEMETA_CORE_JSON_EXPORT auto read_json(const std::filesystem::path &path) -> JSON