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_value.h>
14#include <sourcemeta/core/preprocessor.h>
20#include <initializer_list>
37namespace sourcemeta::core {
63SOURCEMETA_CORE_JSON_EXPORT
64auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream)
89SOURCEMETA_CORE_JSON_EXPORT
91 const std::basic_string_view<JSON::Char, JSON::CharTraits> input) ->
JSON;
107SOURCEMETA_CORE_JSON_EXPORT
109 const std::basic_string_view<JSON::Char, JSON::CharTraits> input)
110 -> std::optional<JSON>;
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;
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;
169SOURCEMETA_CORE_JSON_EXPORT
181SOURCEMETA_CORE_JSON_EXPORT
182auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
194SOURCEMETA_CORE_JSON_EXPORT
196 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
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,
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,
235SOURCEMETA_CORE_JSON_EXPORT
255SOURCEMETA_CORE_JSON_EXPORT
257 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream)
276SOURCEMETA_CORE_JSON_EXPORT
278 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
279 const std::size_t spaces = 2) -> void;
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> &;
318SOURCEMETA_CORE_JSON_EXPORT
319auto operator<<(std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
321 -> std::basic_ostream<JSON::Char, JSON::CharTraits> &;
334SOURCEMETA_FORCEINLINE
inline auto
337 for (
const auto type : types) {
338 result.set(
static_cast<std::size_t
>(type));
346template <>
struct std::formatter<sourcemeta::core::JSON> {
347 constexpr auto parse(std::format_parse_context &context)
348 ->
decltype(context.begin()) {
349 return context.begin();
352 auto format(
const sourcemeta::core::JSON &value,
353 std::format_context &context)
const ->
decltype(context.out()) {
354 std::ostringstream stream;
356 return std::format_to(context.out(),
"{}", stream.str());
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();
367 std::format_context &context)
const ->
decltype(context.out()) {
368 std::ostringstream stream;
370 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 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