ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
ascii_query::json::JsonValue Class Reference

JSON value wrapper (can hold any JSON type). More...

#include <src/tooling/query/json.h>

Public Types

enum class  Type {
  Null , Bool , Int , UInt ,
  Double , String , Array , Object
}

Public Member Functions

 JsonValue ()
 JsonValue (std::nullptr_t)
 JsonValue (bool b)
 JsonValue (int i)
 JsonValue (int64_t i)
 JsonValue (uint64_t u)
 JsonValue (double d)
 JsonValue (const char *s)
 JsonValue (const std::string &s)
 JsonValue (std::string &&s)
std::string toString () const

Static Public Member Functions

static JsonValue fromArray (const std::string &serialized)
static JsonValue fromObject (const std::string &serialized)

Detailed Description

JSON value wrapper (can hold any JSON type).

Definition at line 83 of file src/tooling/query/json.h.

Member Enumeration Documentation

◆ Type

Enumerator
Null 
Bool 
Int 
UInt 
Double 
String 
Array 
Object 

Definition at line 85 of file src/tooling/query/json.h.

85{ Null, Bool, Int, UInt, Double, String, Array, Object };

Constructor & Destructor Documentation

◆ JsonValue() [1/10]

ascii_query::json::JsonValue::JsonValue ( )
inline

Definition at line 87 of file src/tooling/query/json.h.

Referenced by fromArray(), and fromObject().

◆ JsonValue() [2/10]

ascii_query::json::JsonValue::JsonValue ( std::nullptr_t )
inline

Definition at line 88 of file src/tooling/query/json.h.

88: type_(Type::Null) {}

◆ JsonValue() [3/10]

ascii_query::json::JsonValue::JsonValue ( bool b)
inline

Definition at line 89 of file src/tooling/query/json.h.

◆ JsonValue() [4/10]

ascii_query::json::JsonValue::JsonValue ( int i)
inline

Definition at line 90 of file src/tooling/query/json.h.

◆ JsonValue() [5/10]

ascii_query::json::JsonValue::JsonValue ( int64_t i)
inline

Definition at line 91 of file src/tooling/query/json.h.

91: type_(Type::Int), int_val_(i) {}

◆ JsonValue() [6/10]

ascii_query::json::JsonValue::JsonValue ( uint64_t u)
inline

Definition at line 92 of file src/tooling/query/json.h.

◆ JsonValue() [7/10]

ascii_query::json::JsonValue::JsonValue ( double d)
inline

Definition at line 93 of file src/tooling/query/json.h.

◆ JsonValue() [8/10]

ascii_query::json::JsonValue::JsonValue ( const char * s)
inline

Definition at line 94 of file src/tooling/query/json.h.

◆ JsonValue() [9/10]

ascii_query::json::JsonValue::JsonValue ( const std::string & s)
inline

Definition at line 95 of file src/tooling/query/json.h.

95: type_(Type::String), str_val_(s) {}

◆ JsonValue() [10/10]

ascii_query::json::JsonValue::JsonValue ( std::string && s)
inline

Definition at line 96 of file src/tooling/query/json.h.

96: type_(Type::String), str_val_(std::move(s)) {}

Member Function Documentation

◆ fromArray()

JsonValue ascii_query::json::JsonValue::fromArray ( const std::string & serialized)
inlinestatic

Definition at line 99 of file src/tooling/query/json.h.

99 {
100 JsonValue v;
101 v.type_ = Type::Array;
102 v.str_val_ = serialized;
103 return v;
104 }

References Array, and JsonValue().

Referenced by ascii_query::json::JsonArray::add(), and ascii_query::json::JsonObject::set().

◆ fromObject()

JsonValue ascii_query::json::JsonValue::fromObject ( const std::string & serialized)
inlinestatic

Definition at line 106 of file src/tooling/query/json.h.

106 {
107 JsonValue v;
108 v.type_ = Type::Object;
109 v.str_val_ = serialized;
110 return v;
111 }

References JsonValue(), and Object.

Referenced by ascii_query::json::JsonArray::add(), and ascii_query::json::JsonObject::set().

◆ toString()

std::string ascii_query::json::JsonValue::toString ( ) const
inlinenodiscard

Definition at line 113 of file src/tooling/query/json.h.

113 {
114 switch (type_) {
115 case Type::Null:
116 return "null";
117 case Type::Bool:
118 return bool_val_ ? "true" : "false";
119 case Type::Int:
120 return std::to_string(int_val_);
121 case Type::UInt:
122 return std::to_string(uint_val_);
123 case Type::Double: {
124 std::ostringstream oss;
125 oss << double_val_;
126 return oss.str();
127 }
128 case Type::String:
129 return "\"" + escape(str_val_) + "\"";
130 case Type::Array:
131 case Type::Object:
132 return str_val_; // Already serialized
133 }
134 return "null";
135 }
std::string escape(const std::string &str)
Escape a string for JSON output.

References Array, Bool, Double, ascii_query::json::escape(), Int, Null, Object, String, and UInt.


The documentation for this class was generated from the following file: