C/C++ API Reference
Loading...
Searching...
No Matches
pw_json

Oveview

Simple, efficient C++ JSON serialization. Main docs: https://pigweed.dev/pw_json.

Submodules

 Pw_json_builder_api
 

Classes

struct  pw::json_impl::InvalidJsonType< T >
 
struct  pw::json_impl::LiteralChars
 

Macros

#define PW_JSON_COMMON_INTERFACE_IMPL(name)
 

Enumerations

enum class  Uninitialized
 

Functions

 pw::NestedJsonArray::NestedJsonArray (const NestedJsonArray &)=delete
 
NestedJsonArraypw::NestedJsonArray::operator= (const NestedJsonArray &)=delete
 
constexpr pw::NestedJsonArray::NestedJsonArray (NestedJsonArray &&)=default
 
constexpr NestedJsonArraypw::NestedJsonArray::operator= (NestedJsonArray &&)=default
 
template<typename T >
constexpr NestedJsonArraypw::NestedJsonArray::Append (const T &value)
 Appends to the nested array.
 
constexpr NestedJsonArray pw::NestedJsonArray::AppendNestedArray ()
 Appends a new nested array to this nested array.
 
constexpr NestedJsonObject pw::NestedJsonArray::AppendNestedObject ()
 Appends a new nested object to this nested array.
 
 pw::NestedJsonObject::NestedJsonObject (const NestedJsonObject &)=delete
 
NestedJsonObjectpw::NestedJsonObject::operator= (const NestedJsonObject &)=delete
 
constexpr pw::NestedJsonObject::NestedJsonObject (NestedJsonObject &&)=default
 
constexpr NestedJsonObjectpw::NestedJsonObject::operator= (NestedJsonObject &&)=default
 
template<typename T >
constexpr NestedJsonObjectpw::NestedJsonObject::Add (std::string_view key, const T &value)
 Adds a key-value pair to the nested object.
 
constexpr NestedJsonArray pw::NestedJsonObject::AddNestedArray (std::string_view key)
 Adds a nested array to the nested object.
 
constexpr NestedJsonObject pw::NestedJsonObject::AddNestedObject (std::string_view key)
 Adds a nested object to the nested object.
 
constexpr pw::JsonValue::JsonValue (const JsonValue &)=delete
 
constexpr JsonValuepw::JsonValue::operator= (const JsonValue &)=delete
 
constexpr bool pw::JsonValue::IsValue () const
 
constexpr bool pw::JsonValue::IsArray () const
 
constexpr bool pw::JsonValue::IsObject () const
 
constexpr pw::JsonValue::operator std::string_view () const
 
constexpr const char * pw::JsonValue::data () const
 
constexpr size_t pw::JsonValue::size () const
 
constexpr size_t pw::JsonValue::max_size () const
 
constexpr bool pw::JsonValue::ok () const
 
constexpr Status pw::JsonValue::status () const
 
constexpr Status pw::JsonValue::last_status () const
 
constexpr void pw::JsonValue::clear ()
 
constexpr void pw::JsonValue::clear_status ()
 
template<typename T >
constexpr Status pw::JsonValue::Set (const T &value)
 
constexpr pw::JsonArray::JsonArray (const JsonArray &)=delete
 
constexpr JsonArraypw::JsonArray::operator= (const JsonArray &)=delete
 
constexpr bool pw::JsonArray::IsValue () const
 
constexpr bool pw::JsonArray::IsArray () const
 
constexpr bool pw::JsonArray::IsObject () const
 
constexpr pw::JsonArray::operator std::string_view () const
 
constexpr const char * pw::JsonArray::data () const
 
constexpr size_t pw::JsonArray::size () const
 
constexpr size_t pw::JsonArray::max_size () const
 
constexpr bool pw::JsonArray::ok () const
 
constexpr Status pw::JsonArray::status () const
 
constexpr Status pw::JsonArray::last_status () const
 
constexpr void pw::JsonArray::clear ()
 
constexpr void pw::JsonArray::clear_status ()
 
template<typename T >
constexpr JsonArraypw::JsonArray::Append (const T &value)
 
constexpr NestedJsonArray pw::JsonArray::AppendNestedArray ()
 Appends a nested array to this array.
 
constexpr NestedJsonObject pw::JsonArray::AppendNestedObject ()
 Appends a nested object to this array.
 
template<typename Iterable >
constexpr JsonArraypw::JsonArray::Extend (const Iterable &iterable)
 
template<typename T , size_t kSize>
constexpr JsonArraypw::JsonArray::Extend (const T(&iterable)[kSize])
 
constexpr pw::JsonObject::JsonObject (const JsonObject &)=delete
 
constexpr JsonObjectpw::JsonObject::operator= (const JsonObject &)=delete
 
constexpr bool pw::JsonObject::IsValue () const
 
constexpr bool pw::JsonObject::IsArray () const
 
constexpr bool pw::JsonObject::IsObject () const
 
constexpr pw::JsonObject::operator std::string_view () const
 
constexpr const char * pw::JsonObject::data () const
 
constexpr size_t pw::JsonObject::size () const
 
constexpr size_t pw::JsonObject::max_size () const
 
constexpr bool pw::JsonObject::ok () const
 
constexpr Status pw::JsonObject::status () const
 
constexpr Status pw::JsonObject::last_status () const
 
constexpr void pw::JsonObject::clear ()
 
constexpr void pw::JsonObject::clear_status ()
 
template<typename T >
constexpr JsonObjectpw::JsonObject::Add (std::string_view key, const T &value)
 
template<typename T >
constexpr JsonObjectpw::JsonObject::Add (std::nullptr_t, const T &value)=delete
 
constexpr NestedJsonArray pw::JsonObject::AddNestedArray (std::string_view key)
 
constexpr NestedJsonObject pw::JsonObject::AddNestedObject (std::string_view key)
 
static constexpr size_t pw::JsonBuilder::MinBufferSize ()
 JsonBuilder requires at least 5 characters in its buffer.
 
constexpr pw::JsonBuilder::JsonBuilder (span< char > buffer)
 Initializes to the value null. buffer.size() must be at least 5.
 
constexpr pw::JsonBuilder::JsonBuilder (char *buffer, size_t buffer_size)
 Initializes to the value null. buffer_size must be at least 5.
 
constexpr bool pw::JsonBuilder::IsValue () const
 True if the top-level JSON entity is a simple value (not array or object).
 
constexpr bool pw::JsonBuilder::IsArray () const
 True if the top-level JSON entity is an array.
 
constexpr bool pw::JsonBuilder::IsObject () const
 True if the top-level JSON entity is an object.
 
constexpr pw::JsonBuilder::operator std::string_view () const
 JsonBuilder converts to std::string_view.
 
constexpr const char * pw::JsonBuilder::data () const
 Pointer to the serialized JSON, which is always a null-terminated string.
 
constexpr size_t pw::JsonBuilder::size () const
 The current size of the JSON string, excluding the null terminator.
 
constexpr size_t pw::JsonBuilder::max_size () const
 The maximum size of the JSON string, excluding the null terminator.
 
constexpr bool pw::JsonBuilder::ok () const
 True if.
 
constexpr Status pw::JsonBuilder::status () const
 
constexpr Status pw::JsonBuilder::last_status () const
 
constexpr void pw::JsonBuilder::clear ()
 Sets the JSON null and clears the status.
 
constexpr void pw::JsonBuilder::clear_status ()
 Resets status() and last_status().
 
template<typename T >
constexpr Status pw::JsonBuilder::SetValue (const T &value)
 Clears the JSON and sets it to a single JSON value (see JsonValue::Set).
 
constexpr JsonValuepw::JsonBuilder::StartValue ()
 
constexpr JsonArraypw::JsonBuilder::StartArray ()
 
constexpr JsonObjectpw::JsonBuilder::StartObject ()
 
constexpr pw::JsonBuilder::JsonBuilder (char *buffer, size_t buffer_size, Uninitialized)
 
constexpr void pw::JsonBuilder::MakeNull ()
 
constexpr void pw::JsonBuilder::set_json_size (size_t json_size)
 
constexpr void pw::JsonBuilder::set_statuses (Status status, Status last_status)
 
template<typename T >
static constexpr JsonBuffer pw::JsonBuffer< kMaxSize >::Value (const T &initial_value)
 
constexpr pw::JsonBuffer< kMaxSize >::JsonBuffer (const JsonBuffer &other)
 
template<size_t kOtherSize>
constexpr pw::JsonBuffer< kMaxSize >::JsonBuffer (const JsonBuffer< kOtherSize > &other)
 
constexpr JsonBufferpw::JsonBuffer< kMaxSize >::operator= (const JsonBuffer &rhs)
 
template<size_t kOtherSize>
constexpr JsonBufferpw::JsonBuffer< kMaxSize >::operator= (const JsonBuffer< kOtherSize > &rhs)
 
static constexpr size_t pw::JsonBuffer< kMaxSize >::max_size ()
 
constexpr StatusWithSize pw::json_impl::WriteString (std::string_view value, char *buffer, size_t remaining)
 
constexpr char pw::json_impl::NibbleToHex (uint8_t nibble)
 
constexpr int pw::json_impl::EscapedStringCopy (char *destination, int copy_limit, std::string_view source)
 
constexpr StatusWithSize pw::json_impl::WriteQuotedString (std::string_view value, char *buffer, size_t buffer_size)
 
constexpr StatusWithSize pw::json_impl::WriteCharPointer (const char *ptr, char *buffer, size_t buffer_size)
 
template<typename T >
constexpr StatusWithSize pw::json_impl::SerializeJson (const T &value, char *buffer, size_t remaining)
 

Variables

constexpr char pw::json_impl::kArray [2] = {'[', ']'}
 
constexpr char pw::json_impl::kObject [2] = {'{', '}'}
 
template<typename T >
constexpr bool pw::json_impl::kIsJson
 
const char(& pw::json_impl::LiteralChars::open_close )[2]
 

Friends

class pw::NestedJsonArray::JsonArray
 
class pw::NestedJsonArray::JsonObject
 
class pw::NestedJsonArray::NestedJsonObject
 
class pw::NestedJsonObject::JsonArray
 
class pw::NestedJsonObject::JsonObject
 
class pw::NestedJsonObject::NestedJsonArray
 
class pw::JsonValue::JsonBuilder
 
class pw::JsonArray::JsonBuilder
 
class pw::JsonObject::JsonBuilder
 
class pw::JsonBuilder::JsonValue
 
class pw::JsonBuilder::JsonArray
 
class pw::JsonBuilder::JsonObject
 
class pw::JsonBuilder::NestedJsonArray
 
class pw::JsonBuilder::NestedJsonObject
 

Function Documentation

◆ Add()

template<typename T >
constexpr JsonObject & pw::JsonObject::Add ( std::string_view  key,
const T &  value 
)
constexpr

Adds a key-value pair to the JSON object. Updates the status.

It is an error to call Add() if the underlying JsonBuilder is no longer an object.

Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The value was appended successfully.
* 
*     RESOURCE_EXHAUSTED: Insufficient buffer space to serialize.
* 
*  

◆ Append()

template<typename T >
constexpr JsonArray & pw::JsonArray::Append ( const T &  value)
constexpr

Adds a value to the JSON array. Updates the status.

It is an error to call Append() if the underlying JsonBuilder is no longer an array.

◆ Extend() [1/2]

template<typename Iterable >
constexpr JsonArray & pw::JsonArray::Extend ( const Iterable &  iterable)
constexpr

Appends all elements from an iterable container. If there is an error, changes are reverted.

◆ Extend() [2/2]

template<typename T , size_t kSize>
constexpr JsonArray & pw::JsonArray::Extend ( const T(&)  iterable[kSize])
constexpr

Appends all elements from an iterable container. If there is an error, changes are reverted.

◆ 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.

◆ Set()

template<typename T >
constexpr Status pw::JsonValue::Set ( const T &  value)
constexpr

Sets the JSON value to a boolean, number, string, or null. Sets and returns the status. If a Set call fails, the value is set to null.

It is an error to call Set() on a JsonValue if StartArray or StartObject was called on the JsonBuilder. Setting the JsonValue to a JSON object or array is also an error.

Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The value serialized successfully.
* 
*     RESOURCE_EXHAUSTED: There is insufficient buffer space to
*     serialize.
* 
*  

◆ 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:523
Definition: builder.h:222
constexpr JsonObject & StartObject()
Definition: builder.h:381
constexpr JsonObject & Add(std::string_view key, const T &value)
Definition: builder.h:849

◆ 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.
* 
*  

Variable Documentation

◆ kIsJson

template<typename T >
constexpr bool pw::json_impl::kIsJson
inlineconstexpr
Initial value:
=
std::is_base_of_v<JsonValue, T> || std::is_base_of_v<JsonArray, T> ||
std::is_base_of_v<JsonObject, T>