Pigweed
 
Loading...
Searching...
No Matches
pw::JsonBuilder Class Reference

#include <builder.h>

Inheritance diagram for pw::JsonBuilder:
pw::JsonValue pw::JsonArray pw::JsonObject pw::JsonBuffer< kMaxSize >

Public Member Functions

constexpr JsonBuilder (span< char > buffer)
 Initializes to the value null. buffer.size() must be at least 5.
 
constexpr JsonBuilder (char *buffer, size_t buffer_size)
 Initializes to the value null. buffer_size must be at least 5.
 
constexpr bool IsValue () const
 True if the top-level JSON entity is a simple value (not array or object).
 
constexpr bool IsArray () const
 True if the top-level JSON entity is an array.
 
constexpr bool IsObject () const
 True if the top-level JSON entity is an object.
 
constexpr operator std::string_view () const
 JsonBuilder converts to std::string_view.
 
constexpr const char * data () const
 Pointer to the serialized JSON, which is always a null-terminated string.
 
constexpr size_t size () const
 The current size of the JSON string, excluding the null terminator.
 
constexpr size_t max_size () const
 The maximum size of the JSON string, excluding the null terminator.
 
constexpr bool ok () const
 True if.
 
constexpr Status status () const
 
constexpr Status last_status () const
 
constexpr void clear ()
 Sets the JSON null and clears the status.
 
constexpr void clear_status ()
 Resets status() and last_status().
 
template<typename T >
constexpr Status SetValue (const T &value)
 Clears the JSON and sets it to a single JSON value (see JsonValue::Set).
 
constexpr JsonValueStartValue ()
 
constexpr JsonArrayStartArray ()
 
constexpr JsonObjectStartObject ()
 

Static Public Member Functions

static constexpr size_t MinBufferSize ()
 JsonBuilder requires at least 5 characters in its buffer.
 

Protected Types

enum class  Uninitialized
 

Protected Member Functions

constexpr JsonBuilder (char *buffer, size_t buffer_size, Uninitialized)
 
constexpr void MakeNull ()
 
constexpr void set_json_size (size_t json_size)
 
constexpr void set_statuses (Status status, Status last_status)
 

Friends

class JsonValue
 
class JsonArray
 
class JsonObject
 
class NestedJsonArray
 
class NestedJsonObject
 

Detailed Description

JsonBuilder is used to create arbitrary JSON. Contains a JSON value, which may be an object or array. Arrays and objects may contain other values, objects, or arrays.

Member Function Documentation

◆ last_status()

constexpr Status pw::JsonBuilder::last_status ( ) const
inlineconstexpr

Returns the status from the most recent change to the JSON. This is set with each JSON update and may be

embed:rst:inline :c:enumerator:`OK` 

while status() is not.

◆ ok()

constexpr bool pw::JsonBuilder::ok ( ) const
inlineconstexpr

True if.

embed:rst:inline :cpp:func:`status` 

is

embed:rst:inline :c:enumerator:`OK` 

; no errors have occurred.

◆ StartArray()

constexpr JsonArray & pw::JsonBuilder::StartArray ( )
inlineconstexpr

Clears the JSON and sets it to an empty array ([]). Returns a JsonArray reference to this JsonBuilder. For example:

builder.StartArray()
.Append("item1")
.Append(2)
.Extend({"3", "4", "5"});

◆ StartObject()

constexpr JsonObject & pw::JsonBuilder::StartObject ( )
inlineconstexpr

Clears the JSON and sets it to an empty object ({}). Returns a JsonObject reference to this JsonBuilder. For example:

JsonObject& object = builder.StartObject()
.Add("key1", 1)
.Add("key2", "val2");
object.Add("another", "entry");
Definition: builder.h:521
constexpr JsonObject & StartObject()
Definition: builder.h:379
Definition: builder.h:220
constexpr JsonObject & Add(std::string_view key, const T &value)
Definition: builder.h:847

◆ StartValue()

constexpr JsonValue & pw::JsonBuilder::StartValue ( )
inlineconstexpr

Sets the JSON to null and returns a JsonValue reference to this JsonBuilder.

◆ status()

constexpr Status pw::JsonBuilder::status ( ) const
inlineconstexpr

Returns the JsonBuilder's status, which reflects the first error that occurred while updating the JSON. After an update fails, the non-OK status remains until it is reset with clear, clear_status, or SetValue.

Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: All previous updates have succeeded.
* 
*     RESOURCE_EXHAUSTED: An update did not fit in the buffer.
* 
*  

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