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_property_set.h>
12#include <sourcemeta/core/json_value.h>
13// NOLINTEND(misc-include-cleaner)
14
15#include <sourcemeta/core/preprocessor.h>
16
17#include <cstdint> // std::uint64_t
18#include <filesystem> // std::filesystem
19#include <format> // std::formatter, std::format_context, std::format_parse_context, std::format_to
20#include <fstream> // std::basic_ifstream
21#include <initializer_list> // std::initializer_list
22#include <istream> // std::basic_istream
23#include <optional> // std::optional
24#include <ostream> // std::basic_ostream
25#include <sstream> // std::ostringstream
26#include <string> // std::basic_string
27
37
38namespace sourcemeta::core {
39
64SOURCEMETA_CORE_JSON_EXPORT
65auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream)
66 -> JSON;
67
90SOURCEMETA_CORE_JSON_EXPORT
92 const std::basic_string_view<JSON::Char, JSON::CharTraits> input) -> JSON;
93
108SOURCEMETA_CORE_JSON_EXPORT
110 const std::basic_string_view<JSON::Char, JSON::CharTraits> input)
111 -> std::optional<JSON>;
112
130SOURCEMETA_CORE_JSON_EXPORT
131auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
132 std::uint64_t &line, std::uint64_t &column) -> JSON;
133
149SOURCEMETA_CORE_JSON_EXPORT
151 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
152 std::uint64_t &line, std::uint64_t &column) -> JSON;
153
170SOURCEMETA_CORE_JSON_EXPORT
171auto read_json(const std::filesystem::path &path) -> JSON;
172
182SOURCEMETA_CORE_JSON_EXPORT
183auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
184 JSON &output, const JSON::ParseCallback &callback) -> void;
185
195SOURCEMETA_CORE_JSON_EXPORT
197 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
198 JSON &output, const JSON::ParseCallback &callback) -> void;
199
208SOURCEMETA_CORE_JSON_EXPORT
209auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
210 std::uint64_t &line, std::uint64_t &column, JSON &output,
211 const JSON::ParseCallback &callback) -> void;
212
221SOURCEMETA_CORE_JSON_EXPORT
223 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
224 std::uint64_t &line, std::uint64_t &column, JSON &output,
225 const JSON::ParseCallback &callback) -> void;
226
236SOURCEMETA_CORE_JSON_EXPORT
237auto read_json(const std::filesystem::path &path, JSON &output,
238 const JSON::ParseCallback &callback) -> void;
239
256SOURCEMETA_CORE_JSON_EXPORT
257auto stringify(const JSON &document,
258 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream)
259 -> void;
260
277SOURCEMETA_CORE_JSON_EXPORT
278auto prettify(const JSON &document,
279 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
280 const std::size_t spaces = 2) -> void;
281
299SOURCEMETA_CORE_JSON_EXPORT
300auto operator<<(std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
301 const JSON &document)
302 -> std::basic_ostream<JSON::Char, JSON::CharTraits> &;
303
319SOURCEMETA_CORE_JSON_EXPORT
320auto operator<<(std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
321 const JSON::Type type)
322 -> std::basic_ostream<JSON::Char, JSON::CharTraits> &;
323
335SOURCEMETA_FORCEINLINE constexpr auto
336make_set(std::initializer_list<JSON::Type> types) -> JSON::TypeSet {
337 JSON::TypeSet result;
338 for (const auto type : types) {
339 result.set(static_cast<std::size_t>(type));
340 }
341 return result;
342}
343
344} // namespace sourcemeta::core
345
347template <> struct std::formatter<sourcemeta::core::JSON> {
348 constexpr auto parse(std::format_parse_context &context)
349 -> decltype(context.begin()) {
350 return context.begin();
351 }
352
353 auto format(const sourcemeta::core::JSON &value,
354 std::format_context &context) const -> decltype(context.out()) {
355 std::ostringstream stream;
356 stream << value;
357 return std::format_to(context.out(), "{}", stream.str());
358 }
359};
360
361template <> struct std::formatter<sourcemeta::core::JSON::Type> {
362 constexpr auto parse(std::format_parse_context &context)
363 -> decltype(context.begin()) {
364 return context.begin();
365 }
366
367 auto format(const sourcemeta::core::JSON::Type value,
368 std::format_context &context) const -> decltype(context.out()) {
369 std::ostringstream stream;
370 stream << value;
371 return std::format_to(context.out(), "{}", stream.str());
372 }
373};
375
376#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 constexpr auto make_set(std::initializer_list< JSON::Type > types) -> JSON::TypeSet
Definition json.h:336
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