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>
39namespace sourcemeta::core {
48 std::numeric_limits<std::uint32_t>::max()};
79SOURCEMETA_CORE_JSON_EXPORT
80auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
111SOURCEMETA_CORE_JSON_EXPORT
113 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
130SOURCEMETA_CORE_JSON_EXPORT
132 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
134 -> std::optional<JSON>;
153SOURCEMETA_CORE_JSON_EXPORT
154auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
155 std::uint64_t &line, std::uint64_t &column) ->
JSON;
172SOURCEMETA_CORE_JSON_EXPORT
174 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
175 std::uint64_t &line, std::uint64_t &column) ->
JSON;
193SOURCEMETA_CORE_JSON_EXPORT
205SOURCEMETA_CORE_JSON_EXPORT
206auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
218SOURCEMETA_CORE_JSON_EXPORT
220 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
231SOURCEMETA_CORE_JSON_EXPORT
232auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
233 std::uint64_t &line, std::uint64_t &column,
JSON &output,
244SOURCEMETA_CORE_JSON_EXPORT
246 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
247 std::uint64_t &line, std::uint64_t &column,
JSON &output,
259SOURCEMETA_CORE_JSON_EXPORT
279SOURCEMETA_CORE_JSON_EXPORT
281 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream)
300SOURCEMETA_CORE_JSON_EXPORT
302 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
303 const std::size_t spaces = 2) -> void;
322SOURCEMETA_CORE_JSON_EXPORT
323auto operator<<(std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
324 const JSON &document)
325 -> std::basic_ostream<JSON::Char, JSON::CharTraits> &;
342SOURCEMETA_CORE_JSON_EXPORT
343auto operator<<(std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
345 -> std::basic_ostream<JSON::Char, JSON::CharTraits> &;
358SOURCEMETA_FORCEINLINE
constexpr auto
361 for (
const auto type : types) {
362 result.set(
static_cast<std::size_t
>(type));
370template <>
struct std::formatter<sourcemeta::core::JSON> {
371 constexpr auto parse(std::format_parse_context &context)
372 ->
decltype(context.begin()) {
373 return context.begin();
376 auto format(
const sourcemeta::core::JSON &value,
377 std::format_context &context)
const ->
decltype(context.out()) {
378 std::ostringstream stream;
380 return std::format_to(context.out(),
"{}", stream.str());
384template <>
struct std::formatter<sourcemeta::core::JSON::Type> {
385 constexpr auto parse(std::format_parse_context &context)
386 ->
decltype(context.begin()) {
387 return context.begin();
391 std::format_context &context)
const ->
decltype(context.out()) {
392 std::ostringstream stream;
394 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, std::uint32_t maximum_input_bytes=JSON_MAXIMUM_INPUT_BYTES) -> JSON
SOURCEMETA_FORCEINLINE constexpr auto make_set(std::initializer_list< JSON::Type > types) -> JSON::TypeSet
Definition json.h:359
SOURCEMETA_CORE_JSON_EXPORT auto try_parse_json(const std::basic_string_view< JSON::Char, JSON::CharTraits > input, std::uint32_t maximum_input_bytes=JSON_MAXIMUM_INPUT_BYTES) -> std::optional< JSON >
constexpr std::uint32_t JSON_MAXIMUM_INPUT_BYTES
Definition json.h:47
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