1#ifndef SOURCEMETA_CORE_JSON_H_
2#define SOURCEMETA_CORE_JSON_H_
4#ifndef SOURCEMETA_CORE_JSON_EXPORT
5#include <sourcemeta/core/json_export.h>
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>
15#include <sourcemeta/core/preprocessor.h>
21#include <initializer_list>
38namespace sourcemeta::core {
64SOURCEMETA_CORE_JSON_EXPORT
65auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream)
90SOURCEMETA_CORE_JSON_EXPORT
92 const std::basic_string_view<JSON::Char, JSON::CharTraits> input) ->
JSON;
108SOURCEMETA_CORE_JSON_EXPORT
110 const std::basic_string_view<JSON::Char, JSON::CharTraits> input)
111 -> std::optional<JSON>;
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;
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;
170SOURCEMETA_CORE_JSON_EXPORT
182SOURCEMETA_CORE_JSON_EXPORT
183auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
195SOURCEMETA_CORE_JSON_EXPORT
197 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
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,
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,
236SOURCEMETA_CORE_JSON_EXPORT
256SOURCEMETA_CORE_JSON_EXPORT
258 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream)
277SOURCEMETA_CORE_JSON_EXPORT
279 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
280 const std::size_t spaces = 2) -> void;
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> &;
319SOURCEMETA_CORE_JSON_EXPORT
320auto operator<<(std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
322 -> std::basic_ostream<JSON::Char, JSON::CharTraits> &;
335SOURCEMETA_FORCEINLINE
constexpr auto
338 for (
const auto type : types) {
339 result.set(
static_cast<std::size_t
>(type));
347template <>
struct std::formatter<sourcemeta::core::JSON> {
348 constexpr auto parse(std::format_parse_context &context)
349 ->
decltype(context.begin()) {
350 return context.begin();
353 auto format(
const sourcemeta::core::JSON &value,
354 std::format_context &context)
const ->
decltype(context.out()) {
355 std::ostringstream stream;
357 return std::format_to(context.out(),
"{}", stream.str());
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();
368 std::format_context &context)
const ->
decltype(context.out()) {
369 std::ostringstream stream;
371 return std::format_to(context.out(),
"{}", stream.str());
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
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