JsonParserGeneratorRK
Public Member Functions | Protected Attributes
JsonModifier Class Reference

Class for modifying a JSON object in place, without needing to make a copy of it. More...

#include <JsonParserGeneratorRK.h>

Inheritance diagram for JsonModifier:
JsonWriter JsonBuffer

Public Member Functions

 JsonModifier (JsonParser &jp)
 
template<class T >
void insertOrUpdateKeyValue (const JsonParserGeneratorRK::jsmntok_t *container, const char *key, T value)
 Inserts or updates a key/value pair into an object. More...
 
template<class T >
void appendArrayValue (const JsonParserGeneratorRK::jsmntok_t *arrayToken, T value)
 Appends a value to an array. More...
 
bool removeKeyValue (const JsonParserGeneratorRK::jsmntok_t *container, const char *key)
 Removes a key and value from an object. More...
 
bool removeArrayIndex (const JsonParserGeneratorRK::jsmntok_t *container, size_t index)
 Removes an entry from an array. More...
 
bool startModify (const JsonParserGeneratorRK::jsmntok_t *token)
 Low level function to modify a token in place. More...
 
bool startAppend (const JsonParserGeneratorRK::jsmntok_t *arrayOrObjectToken)
 Low level function to append to an object or array. More...
 
void finish ()
 Finish modifying the object. More...
 
JsonParserGeneratorRK::jsmntok_t tokenWithQuotes (const JsonParserGeneratorRK::jsmntok_t *tok) const
 Return a copy of tok, but moving so start and end include the double quotes for strings. More...
 
int findLeftComma (const JsonParserGeneratorRK::jsmntok_t *tok) const
 Find the offset of the comma to the left of the token, or -1 if there isn't one. More...
 
int findRightComma (const JsonParserGeneratorRK::jsmntok_t *tok) const
 Find the offset of the comma to the left of the token, or -1 if there isn't one. More...
 
- Public Member Functions inherited from JsonWriter
 JsonWriter ()
 Construct a JsonWriter with a dynamically allocated buffer. More...
 
virtual ~JsonWriter ()
 Destroy the object. If the buffer was dynamically allocated it will be freed. More...
 
 JsonWriter (char *buffer, size_t bufferLen)
 Construct a JsonWriter to write to a static buffer. More...
 
void init ()
 Reset the writer, clearing all data. More...
 
bool startObject ()
 Start a new JSON object. Make sure you finish it with finishObjectOrArray()
 
bool startArray ()
 Start a new JSON array. Make sure you finish it with finishObjectOrArray()
 
void finishObjectOrArray ()
 Finsh an object or array started with startObject() or startArray()
 
void insertValue (bool value)
 Inserts a boolean value ("true" or "false"). More...
 
void insertValue (int value)
 Inserts an integer value. More...
 
void insertValue (unsigned int value)
 Inserts an unsigned integer value. More...
 
void insertValue (long value)
 Inserts a long integer value. More...
 
void insertValue (unsigned long value)
 Inserts an unsigned long integer value. More...
 
void insertValue (float value)
 Inserts a floating point value. More...
 
void insertValue (double value)
 Inserts a floating point double value. More...
 
void insertValue (const char *value)
 Inserts a quoted string value. This escapes special characters and encodes utf-8. More...
 
void insertValue (const String &value)
 Inserts a quoted string value. More...
 
void insertKeyObject (const char *key)
 Inserts a new key and empty object. You must close the object using finishObjectOrArray()! More...
 
void insertKeyArray (const char *key)
 Inserts a new key and empty array. You must close the object using finishObjectOrArray()! More...
 
template<class T >
void insertKeyValue (const char *key, T value)
 Inserts a key/value pair into an object. More...
 
template<class T >
void insertArrayValue (T value)
 Inserts a value into an array. More...
 
bool isTruncated () const
 
void setFloatPlaces (int floatPlaces)
 Sets the number of digits for formatting float and double values. More...
 
void insertCheckSeparator ()
 Check to see if a separator needs to be inserted. Used internally. More...
 
bool startObjectOrArray (char startChar, char endChar)
 Used internally to start an object or array. More...
 
void insertChar (char ch)
 Used internally to insert a character. More...
 
void insertString (const char *s, bool quoted=false)
 Used internally to insert a string, quoted or not. More...
 
void insertsprintf (const char *fmt,...)
 Used internally to insert using snprintf formatting. More...
 
void insertvsprintf (const char *fmt, va_list ap)
 Used internally to insert using snprintf formatting with a va_list. More...
 
void setIsFirst (bool isFirst=true)
 Used internally to set the current isFirst flag in the context.
 
- Public Member Functions inherited from JsonBuffer
 JsonBuffer ()
 Construct a JsonBuffer object with no external buffer specified.
 
virtual ~JsonBuffer ()
 Destructor. Destroying the object does not delete any underlying buffer!
 
 JsonBuffer (char *buffer, size_t bufferLen)
 Construct a JsonBuffer with an external buffer of a given size. More...
 
void setBuffer (char *buffer, size_t bufferLen)
 Sets the buffers to the specified buffer and length. More...
 
bool allocate (size_t len)
 Allocate the buffer using malloc/realloc. More...
 
bool addString (const char *data)
 Add a c-string to the end of the buffer. More...
 
bool addData (const char *data, size_t dataLen)
 Add a string to the end of the buffer. More...
 
char * getBuffer () const
 Gets a pointer to the internal buffer. More...
 
size_t getOffset () const
 Gets the current offset for writing.
 
void setOffset (size_t offset)
 swets the current offset for writing
 
size_t getBufferLen () const
 Gets the current length of the buffer. More...
 
void clear ()
 Clears the current buffer for writing. More...
 

Protected Attributes

JsonParserjp
 The JsonParser object passed to the constructor.
 
int start = -1
 Start offset in the buffer. Set to -1 when startModify() or startAppend() is not in progress.
 
int origAfter = 0
 Number of bytes after the insertion position, saved at saveLoc when start is in progress.
 
int saveLoc = 0
 Location where data is temporarily saved until finish() is called.
 
- Protected Attributes inherited from JsonWriter
size_t contextIndex
 Index into the context for the current level of nesting.
 
JsonWriterContext context [MAX_NESTED_CONTEXT]
 Structure for managing nested objects.
 
bool truncated
 true if data was added that didn't fit and was truncated
 
int floatPlaces
 default number of places to display for floating point numbers (default is -1, the default for sprintf)
 
- Protected Attributes inherited from JsonBuffer
char * buffer
 The buffer to to read from or write to. This is not null-terminated.
 
size_t bufferLen
 The length of the buffer in bytes,.
 
size_t offset
 The read or write offset.
 
bool staticBuffers
 True if the buffers were passed in and should not freed or reallocated.
 

Additional Inherited Members

- Static Public Attributes inherited from JsonWriter
static const size_t MAX_NESTED_CONTEXT = 9
 

Detailed Description

Class for modifying a JSON object in place, without needing to make a copy of it.

Make sure the underlying JsonParser is big enough to hold the modified object. If you use JsonParserStatic<> make sure you have enough bytes and tokens.

The most commonly used method is insertOrUpdateKeyValue(). This inserts or updates a key in an array. Another is appendArrayValue() which appends a value to an array. Both methods are templated so you can use them with any valid type supported by insertValue() in JsonWriter: bool, int, float, double, const char *.

This class is a subclass of JsonWriter, so you can also use the low-level functions and JsonWriter methods to do unusual object manipulations.

You can also use removeKeyValue() and removeArrayIndex() to remove keys or array entries.

Member Function Documentation

◆ appendArrayValue()

template<class T >
void JsonModifier::appendArrayValue ( const JsonParserGeneratorRK::jsmntok_t arrayToken,
value 
)
inline

Appends a value to an array.

Uses templates so you can pass any type object that's supported by insertValue() overloads, for example: bool, int, float, double, const char *.

To modify the outermost array, use jp.getOuterArray() for the arrayToken. You can also modify arrays in an object using getValueTokenByKey().

Note: This method call jp.parse() so any jsmntok_t may be changed by this method. If you've fetched one, such as by using getValueTokenByKey() be sure to fetch it again to be safe.

◆ findLeftComma()

int JsonModifier::findLeftComma ( const JsonParserGeneratorRK::jsmntok_t tok) const

Find the offset of the comma to the left of the token, or -1 if there isn't one.

Used internally, you probably won't need to use this.

◆ findRightComma()

int JsonModifier::findRightComma ( const JsonParserGeneratorRK::jsmntok_t tok) const

Find the offset of the comma to the left of the token, or -1 if there isn't one.

Used internally, you probably won't need to use this.

◆ finish()

void JsonModifier::finish ( )

Finish modifying the object.

Finish must be called after startModify or startAppend otherwise the object will be corrupted.

Note: This method call jp.parse() so any jsmntok_t may be changed by this method. If you've fetched one, such as by using getValueTokenByKey() be sure to fetch it again to be safe.

The high level function like insertOrUpdateKeyValue, appendArrayValue, removeKeyValue, and removeArrayIndex internally call finish so you should not call it again with those methods.

◆ insertOrUpdateKeyValue()

template<class T >
void JsonModifier::insertOrUpdateKeyValue ( const JsonParserGeneratorRK::jsmntok_t container,
const char *  key,
value 
)
inline

Inserts or updates a key/value pair into an object.

Uses templates so you can pass any type object that's supported by insertValue() overloads, for example: bool, int, float, double, const char *.

To modify the outermost object, use jp.getOuterObject() for the container.

Note: This method call jp.parse() so any jsmntok_t may be changed by this method. If you've fetched one, such as by using getValueTokenByKey() be sure to fetch it again to be safe.

◆ removeArrayIndex()

bool JsonModifier::removeArrayIndex ( const JsonParserGeneratorRK::jsmntok_t container,
size_t  index 
)

Removes an entry from an array.

Note: This method call jp.parse() so any jsmntok_t may be changed by this method. If you've fetched one, such as by using getValueTokenByKey() be sure to fetch it again to be safe.

◆ removeKeyValue()

bool JsonModifier::removeKeyValue ( const JsonParserGeneratorRK::jsmntok_t container,
const char *  key 
)

Removes a key and value from an object.

Note: This method call jp.parse() so any jsmntok_t may be changed by this method. If you've fetched one, such as by using getValueTokenByKey() be sure to fetch it again to be safe.

◆ startAppend()

bool JsonModifier::startAppend ( const JsonParserGeneratorRK::jsmntok_t arrayOrObjectToken)

Low level function to append to an object or array.

Parameters
arrayOrObjectTokenthe jsmntok_t to append to. This must be an object or array token.

You must call finish() after modification is done to restore the object to a valid state.

◆ startModify()

bool JsonModifier::startModify ( const JsonParserGeneratorRK::jsmntok_t token)

Low level function to modify a token in place.

Parameters
tokenthe jsmntok_t to modify

You must call finish() after modification is done to restore the object to a valid state!

Note: insertOrUpdateKeyValue() does not use this. Instead it removes then appends the new value. The reason is that startModify does not work if you change the type of the data to or from a string. This is tricky to deal with correctly, so it's easier to just remove and add the item again.

◆ tokenWithQuotes()

JsonParserGeneratorRK::jsmntok_t JsonModifier::tokenWithQuotes ( const JsonParserGeneratorRK::jsmntok_t tok) const

Return a copy of tok, but moving so start and end include the double quotes for strings.

Used internally, you probably won't need to use this.


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