ascii-chat 0.0.0
Real-time terminal-based video chat with ASCII art conversion
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 80 of file json.h.

Member Enumeration Documentation

◆ Type

Constructor & Destructor Documentation

◆ JsonValue() [1/10]

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

Definition at line 84 of file json.h.

84: type_(Type::Null) {}

◆ JsonValue() [2/10]

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

Definition at line 85 of file json.h.

85: type_(Type::Null) {}

◆ JsonValue() [3/10]

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

Definition at line 86 of file json.h.

86: type_(Type::Bool), bool_val_(b) {}

◆ JsonValue() [4/10]

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

Definition at line 87 of file json.h.

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

◆ JsonValue() [5/10]

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

Definition at line 88 of file json.h.

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

◆ JsonValue() [6/10]

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

Definition at line 89 of file json.h.

89: type_(Type::UInt), uint_val_(u) {}

◆ JsonValue() [7/10]

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

Definition at line 90 of file json.h.

90: type_(Type::Double), double_val_(d) {}

◆ JsonValue() [8/10]

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

Definition at line 91 of file json.h.

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

◆ JsonValue() [9/10]

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

Definition at line 92 of file json.h.

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

◆ JsonValue() [10/10]

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

Definition at line 93 of file json.h.

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

Member Function Documentation

◆ fromArray()

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

Definition at line 96 of file json.h.

96 {
97 JsonValue v;
98 v.type_ = Type::Array;
99 v.str_val_ = serialized;
100 return v;
101 }

References Array.

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

◆ fromObject()

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

Definition at line 103 of file json.h.

103 {
104 JsonValue v;
105 v.type_ = Type::Object;
106 v.str_val_ = serialized;
107 return v;
108 }

References Object.

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

◆ toString()

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

Definition at line 110 of file json.h.

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

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: