pulsatrix 1.1.0
Loading...
Searching...
No Matches
json.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstdint>
8#include <string>
9#include <string_view>
10#include <utility>
11#include <vector>
12
13namespace pulsatrix {
14
22class JsonValue {
23public:
24 enum class Type { Null, Bool, Number, String, Array, Object };
25 using Array = std::vector<JsonValue>;
26 using Object = std::vector<std::pair<std::string, JsonValue>>;
27
29 JsonValue() = default;
31 JsonValue(std::nullptr_t) {}
32 JsonValue(bool b);
35 JsonValue(double d);
36 JsonValue(int64_t i);
37 JsonValue(int i) : JsonValue(static_cast<int64_t>(i)) {}
38 JsonValue(std::string s);
39 JsonValue(const char* s) : JsonValue(std::string(s)) {}
42
45 static JsonValue Float(float f);
46
47 [[nodiscard]] Type type() const { return type_; }
48 [[nodiscard]] bool is_null() const { return type_ == Type::Null; }
49
52 [[nodiscard]] bool as_bool() const;
53 [[nodiscard]] double as_double() const;
54 [[nodiscard]] float as_float() const;
56 [[nodiscard]] int64_t as_int64() const;
57 [[nodiscard]] const std::string& as_string() const;
58 [[nodiscard]] const Array& as_array() const;
59 [[nodiscard]] const Object& as_object() const;
61 [[nodiscard]] const std::string& number_text() const;
63
66 [[nodiscard]] const JsonValue* find(std::string_view key) const;
67
70 void add(std::string key, JsonValue value);
71
74 void push_back(JsonValue value);
75
76private:
77 Type type_ = Type::Null;
78 bool bool_ = false;
79 std::string text_; // a number's text, or a string's contents
80 Array array_;
81 Object object_;
82
83 friend JsonValue ParseJson(std::string_view text);
84 friend class JsonParser;
85};
86
94[[nodiscard]] JsonValue ParseJson(std::string_view text);
95
103[[nodiscard]] std::string WriteJson(const JsonValue& value);
104
112[[nodiscard]] std::string WriteJsonCompact(const JsonValue& value);
113
114} // namespace pulsatrix
One JSON value: null, a boolean, a number, a string, an array or an object.
Definition json.hpp:22
Type type() const
Definition json.hpp:47
const Object & as_object() const
const Array & as_array() const
const JsonValue * find(std::string_view key) const
The member named key of an object, or nullptr if there is none.
static JsonValue Float(float f)
A number written with the shortest text that reads back as the same float.
JsonValue(std::string s)
JsonValue(double d)
A number written with the shortest text that reads back as the same double.
const std::string & as_string() const
void push_back(JsonValue value)
Appends an element to an array.
friend JsonValue ParseJson(std::string_view text)
Parses one JSON document.
const std::string & number_text() const
A number's text exactly as written or parsed, for example "0.1".
int64_t as_int64() const
bool as_bool() const
JsonValue(const char *s)
Definition json.hpp:39
std::vector< JsonValue > Array
Definition json.hpp:25
double as_double() const
std::vector< std::pair< std::string, JsonValue > > Object
Definition json.hpp:26
JsonValue(std::nullptr_t)
A null.
Definition json.hpp:31
void add(std::string key, JsonValue value)
Appends a member to an object. Keys must be unique.
friend class JsonParser
Definition json.hpp:84
float as_float() const
Type
Definition json.hpp:24
JsonValue(int i)
Definition json.hpp:37
bool is_null() const
Definition json.hpp:48
JsonValue()=default
A null.
Definition acquisition_functions.hpp:16
JsonValue ParseJson(std::string_view text)
Parses one JSON document.
std::string WriteJson(const JsonValue &value)
Writes value as JSON, indented by two spaces and ending with a newline.
std::string WriteJsonCompact(const JsonValue &value)
Writes value as JSON on one line, with no whitespace and no trailing newline.