Files
ArduinoJson/include/ArduinoJson/JsonObject.hpp

107 lines
3.7 KiB
C++
Raw Normal View History

2014-10-23 23:39:22 +02:00
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
2014-10-16 00:11:23 +02:00
#pragma once
2014-11-05 08:53:41 +01:00
#include "Internals/JsonPrintable.hpp"
#include "Internals/List.hpp"
2014-10-31 12:02:15 +01:00
#include "Internals/ReferenceType.hpp"
#include "JsonPair.hpp"
2014-10-30 21:51:59 +01:00
2014-11-06 16:29:29 +01:00
// Returns the size (in bytes) of an object with n elements.
// Can be very handy to determine the size of a StaticJsonBuffer.
2014-10-30 21:51:59 +01:00
#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \
(sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type))
2014-10-16 00:11:23 +02:00
2014-10-23 19:54:00 +02:00
namespace ArduinoJson {
2014-10-30 14:03:33 +01:00
2014-11-06 16:29:29 +01:00
// Forward declarations
2014-10-31 12:27:33 +01:00
class JsonArray;
class JsonBuffer;
2014-11-06 16:29:29 +01:00
// A dictionary of JsonVariant indexed by string (char*)
//
// The constructor is private, instances must be created via
// JsonBuffer::createObject() or JsonBuffer::parseObject().
// A JsonObject can be serialized to a JSON string via JsonObject::printTo().
// It can also be deserialized from a JSON string via JsonBuffer::parseObject().
2014-11-05 08:53:41 +01:00
class JsonObject : public Internals::JsonPrintable<JsonObject>,
public Internals::ReferenceType,
public Internals::List<JsonPair> {
2014-11-06 16:29:29 +01:00
// JsonBuffer is a friend because it needs to call the private constructor.
2014-10-30 10:49:02 +01:00
friend class JsonBuffer;
public:
2014-10-29 14:24:34 +01:00
typedef const char *key_type;
2014-10-25 15:55:58 +02:00
typedef JsonPair value_type;
2014-11-06 16:29:29 +01:00
// Gets the JsonVariant associated with the specified key.
// Returns a reference or JsonVariant::invalid() if not found.
2014-11-04 09:51:25 +01:00
JsonVariant &at(key_type key);
2014-11-06 16:29:29 +01:00
// Gets the JsonVariant associated with the specified key.
// Returns a constant reference or JsonVariant::invalid() if not found.
2014-11-04 09:51:25 +01:00
const JsonVariant &at(key_type key) const;
2014-11-06 16:29:29 +01:00
// Gets or create the JsonVariant associated with the specified key.
// Returns a reference or JsonVariant::invalid() if allocation failed.
2014-11-04 09:51:25 +01:00
JsonVariant &operator[](key_type key);
2014-11-06 16:29:29 +01:00
// Gets the JsonVariant associated with the specified key.
// Returns a constant reference or JsonVariant::invalid() if not found.
2014-11-04 09:51:25 +01:00
const JsonVariant &operator[](key_type key) const { return at(key); }
2014-10-30 14:03:33 +01:00
2014-11-06 16:29:29 +01:00
// Adds an uninitialized JsonVariant associated with the specified key.
// Return a reference or JsonVariant::invalid() if allocation fails.
JsonVariant &add(key_type key) { return (*this)[key]; }
2014-10-27 22:50:50 +01:00
2014-11-06 16:29:29 +01:00
// Adds the specified key with the specified value.
2014-10-29 14:24:34 +01:00
template <typename T>
void add(key_type key, T value) {
2014-10-30 10:49:02 +01:00
add(key).set(value);
2014-10-29 21:18:27 +01:00
}
2014-11-06 16:29:29 +01:00
// Adds the specified key with a reference to the specified JsonArray.
2014-10-30 10:49:02 +01:00
void add(key_type key, JsonArray &array) { add(key).set(array); }
2014-11-06 16:29:29 +01:00
// Adds the specified key with a reference to the specified JsonObject.
2014-10-30 10:49:02 +01:00
void add(key_type key, JsonObject &object) { add(key).set(object); }
2014-10-29 21:18:27 +01:00
2014-11-06 16:29:29 +01:00
// Creates and adds a JsonArray.
// This is a shortcut for JsonBuffer::createArray() and JsonObject::add().
2014-10-29 14:24:34 +01:00
JsonArray &createNestedArray(key_type key);
2014-11-06 16:29:29 +01:00
// Creates and adds a JsonObject.
// This is a shortcut for JsonBuffer::createObject() and JsonObject::add().
2014-10-29 14:24:34 +01:00
JsonObject &createNestedObject(key_type key);
2014-10-23 19:54:00 +02:00
2014-11-06 16:29:29 +01:00
// Removes the specified key and the associated value.
void remove(key_type key);
// Returns a reference an invalid JsonObject.
// This object is meant to replace a NULL pointer.
// This is used when memory allocation or JSON parsing fail.
2014-10-29 14:24:34 +01:00
static JsonObject &invalid() { return _invalid; }
2014-10-27 22:50:50 +01:00
2014-11-03 12:32:47 +01:00
template <typename T>
void writeTo(T &writer) const;
2014-10-27 22:50:50 +01:00
private:
2014-11-06 16:29:29 +01:00
// Create an empty JsonArray attached to the specified JsonBuffer.
2014-11-06 14:08:53 +01:00
explicit JsonObject(JsonBuffer *buffer) : List(buffer) {}
2014-10-30 10:49:02 +01:00
2014-11-06 16:29:29 +01:00
// Returns the list node that matches the specified key.
node_type *getNodeAt(key_type key) const;
2014-11-06 16:29:29 +01:00
// Returns the list node that matches the specified key, creating it if
// needed.
node_type *getOrCreateNodeAt(key_type key);
2014-10-29 14:24:34 +01:00
2014-11-06 16:29:29 +01:00
// The instance returned by JsonObject::invalid()
2014-10-29 14:24:34 +01:00
static JsonObject _invalid;
2014-10-23 19:54:00 +02:00
};
2014-10-23 23:45:36 +02:00
}