omni::structuredlog::JsonBuilder

Defined in omni/structuredlog/JsonTree.h

class JsonBuilder

A class to build JSON trees using JsonNode structs.

These functions all expect an empty node to have been passed in, which speeds up tree creation by avoiding unnecessary clearNode() calls.

Note

These functions do expect that memory allocation may fail. This is used in the unit tests to verify that the node size calculator is correct.

Public Functions

inline JsonBuilder(Allocator *alloc)

Constructor.

Parameters

alloc[in] The allocator that will be used to create new JsonNode objects.

inline bool createObject(JsonNode *node, uint16_t propertyCount)

Create a JSON object node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • propertyCount[in] The number of properties that JSON node has. For example {"a": 0, "b": 2} is an object with 2 properties.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool createObjectArray(JsonNode *node, uint16_t propertyCount, uint16_t len)

Create a JSON node that is an array of objects.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • propertyCount[in] The number of properties that each object element has. This implies that each element will be an object with the same layout. For an array of objects with varying layouts, calcJsonObjectSize() would need to be called for each element.

  • len[in] The length of the object array. When defining an object array in a schema, you should set this length to 1, then specify the object layout in that element.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, bool b)

Set a bool node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • b[in] The boolean value to set on this node.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const bool *data, uint16_t len)

Set a bool array node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • data[in] The boolean array to set on this node. This is copied into the node.

  • len[in] The length of the array data.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, int32_t i)

Set a 32 bit integer node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • i[in] The integer value to set on this node.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const int32_t *data, uint16_t len)

Set a 32 bit integer array node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • data[in] The integer array to set on this node. This is copied into the node.

  • len[in] The length of the array data.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, uint32_t u)

Set an unsigned 32 bit integer node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • u[in] The integer value to set on this node.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const uint32_t *data, uint16_t len)

Set an unsigned 32 bit integer array node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • data[in] The integer array to set on this node. This is copied into the node.

  • len[in] The length of the array data.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, int64_t i)

Set a 64 bit integer node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • i[in] The integer value to set on this node.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const int64_t *data, uint16_t len)

Set a 64 bit integer array node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • data[in] The integer array to set on this node. This is copied into the node.

  • len[in] The length of the array data.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, uint64_t u)

Set an unsigned 64 bit integer node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • u[in] The integer value to set on this node.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const uint64_t *data, uint16_t len)

Set an unsigned 64 bit integer array node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • data[in] The integer array to set on this node. This is copied into the node.

  • len[in] The length of the array data.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, float f)

Set a float node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • f[in] The float value to set on this node.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const float *data, uint16_t len)

Set a float array node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • data[in] The double array to set on this node. This is copied into the node.

  • len[in] The length of the array data.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, double f)

Set a double node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • f[in] The double value to set on this node.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const double *data, uint16_t len)

Set a double array node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • data[in] The double array to set on this node. This is copied into the node.

  • len[in] The length of the array data.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const char *const *data, uint16_t len)

Set a string array node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • data[in] The string array to set on this node. This is copied into the node.

  • len[in] The length of the array data.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const char *str, uint16_t len)

Set a string node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • str[in] The string to copy into this node.

  • len[in] The length of str including the null terminator.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const char *str)

Set a string node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • str[in] The string to copy into this node.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

Returns

false if str was longer than 64KiB.

inline bool setNode(JsonNode *node, const void *blob, uint16_t len)

Set a binary blob node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • blob[in] The data to copy into this node.

  • len[in] The number of bytes of data in blob.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setNode(JsonNode *node, const uint8_t *blob, uint16_t len)

Set a binary blob node.

Parameters
  • node[inout] The node to create. This node must be equal to {} when passed in.

  • blob[in] The data to copy into this node.

  • len[in] The number of bytes of data in blob.

Returns

true if the node was successfully created.

Returns

false if a memory allocation failed.

inline bool setName(JsonNode *node, const char *name, uint16_t nameLen)

Set the name of a JSON node.

Parameters
  • node[inout] The node to modify. The previous name will be cleared out, if any.

  • name[in] The name to copy into the node. This may be nullptr to just free the previous name.

  • nameLen[in] The length of name, including the null terminator. This must be 0 if name is nullptr.

Returns

true if the node’s name was successfully copied.

Returns

false if a memory allocation failed.

inline bool setName(JsonNode *node, const char *name)

Set the name of a JSON node.

Parameters
  • node[inout] The node to modify. The previous name will be cleared out, if any.

  • name[in] The name to copy into the node. This may be nullptr to just free the previous name.

Returns

true if the node’s name was successfully copied.

Returns

false if a memory allocation failed.

inline bool deepCopy(const JsonNode *node, JsonNode *out)

Perform a deep copy of a node.

Parameters
  • node[in] The node to deep copy. This node must be equal to {} when passed in.

  • out[inout] The new node that was created.

Returns

The deep copied node.

Public Static Functions

template<bool failOnNonFatal = false>
static inline bool setFlags(JsonNode *node, JsonNode::Flag flags)

Set the flags on a JsonNode.

Parameters
  • node[in] The node to update the flags on. This node must have already had its value and length set. This may not be nullptr.

  • flags[in] The flags to set on node.

Template Parameters

failOnNonFatal – If this is set to true, the function will return false when a non-fatal flag error occurred. This is intended for testing. The behavior is not changed otherwise by this flag. The only fatal cases are: passing in unknown flags, setting an enum flag on an incorrect type and setting an enum flag on an empty array.

Returns

true if the flags set were valid.

Returns

false if the flags set were invalid for the given node.