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, std::uint32_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 <limits> // std::numeric_limits
24#include <optional> // std::optional
25#include <ostream> // std::basic_ostream
26#include <sstream> // std::ostringstream
27#include <string> // std::basic_string
28
38
39namespace sourcemeta::core {
40
47inline constexpr std::uint32_t JSON_MAXIMUM_INPUT_BYTES{
48 std::numeric_limits<std::uint32_t>::max()};
49
79SOURCEMETA_CORE_JSON_EXPORT
80auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
81 std::uint32_t maximum_input_bytes = JSON_MAXIMUM_INPUT_BYTES)
82 -> JSON;
83
111SOURCEMETA_CORE_JSON_EXPORT
113 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
114 std::uint32_t maximum_input_bytes = JSON_MAXIMUM_INPUT_BYTES) -> JSON;
115
130SOURCEMETA_CORE_JSON_EXPORT
132 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
133 std::uint32_t maximum_input_bytes = JSON_MAXIMUM_INPUT_BYTES)
134 -> std::optional<JSON>;
135
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;
156
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;
176
193SOURCEMETA_CORE_JSON_EXPORT
194auto read_json(const std::filesystem::path &path) -> JSON;
195
205SOURCEMETA_CORE_JSON_EXPORT
206auto parse_json(std::basic_istream<JSON::Char, JSON::CharTraits> &stream,
207 JSON &output, const JSON::ParseCallback &callback) -> void;
208
218SOURCEMETA_CORE_JSON_EXPORT
220 const std::basic_string_view<JSON::Char, JSON::CharTraits> input,
221 JSON &output, const JSON::ParseCallback &callback) -> void;
222
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,
234 const JSON::ParseCallback &callback) -> void;
235
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,
248 const JSON::ParseCallback &callback) -> void;
249
259SOURCEMETA_CORE_JSON_EXPORT
260auto read_json(const std::filesystem::path &path, JSON &output,
261 const JSON::ParseCallback &callback) -> void;
262
279SOURCEMETA_CORE_JSON_EXPORT
280auto stringify(const JSON &document,
281 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream)
282 -> void;
283
300SOURCEMETA_CORE_JSON_EXPORT
301auto prettify(const JSON &document,
302 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
303 const std::size_t spaces = 2) -> void;
304
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> &;
326
342SOURCEMETA_CORE_JSON_EXPORT
343auto operator<<(std::basic_ostream<JSON::Char, JSON::CharTraits> &stream,
344 const JSON::Type type)
345 -> std::basic_ostream<JSON::Char, JSON::CharTraits> &;
346
358SOURCEMETA_FORCEINLINE constexpr auto
359make_set(std::initializer_list<JSON::Type> types) -> JSON::TypeSet {
360 JSON::TypeSet result;
361 for (const auto type : types) {
362 result.set(static_cast<std::size_t>(type));
363 }
364 return result;
365}
366
367} // namespace sourcemeta::core
368
370template <> struct std::formatter<sourcemeta::core::JSON> {
371 constexpr auto parse(std::format_parse_context &context)
372 -> decltype(context.begin()) {
373 return context.begin();
374 }
375
376 auto format(const sourcemeta::core::JSON &value,
377 std::format_context &context) const -> decltype(context.out()) {
378 std::ostringstream stream;
379 stream << value;
380 return std::format_to(context.out(), "{}", stream.str());
381 }
382};
383
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();
388 }
389
390 auto format(const sourcemeta::core::JSON::Type value,
391 std::format_context &context) const -> decltype(context.out()) {
392 std::ostringstream stream;
393 stream << value;
394 return std::format_to(context.out(), "{}", stream.str());
395 }
396};
398
399#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, 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