Files
ArduinoJson/srcs/Internals/JsonNode.h

186 lines
4.1 KiB
C
Raw Normal View History

#pragma once
class JsonBuffer;
2014-10-05 16:25:49 +02:00
class JsonWriter;
2014-10-09 14:19:43 +02:00
class JsonNodeIterator;
2014-10-05 16:25:49 +02:00
2014-10-09 14:19:43 +02:00
class JsonNode
{
2014-10-09 14:19:43 +02:00
friend class JsonNodeIterator;
2014-10-09 14:31:25 +02:00
enum JsonNodeType
{
JSON_UNDEFINED,
JSON_NULL,
JSON_ARRAY,
JSON_OBJECT,
JSON_KEY_VALUE,
JSON_BOOLEAN,
JSON_STRING,
JSON_LONG,
JSON_PROXY,
2014-10-09 14:31:25 +02:00
JSON_DOUBLE_0_DECIMALS,
JSON_DOUBLE_1_DECIMAL,
JSON_DOUBLE_2_DECIMALS,
// etc.
};
union JsonNodeContent
{
bool asBoolean;
double asDouble;
long asInteger;
const char* asString;
struct
{
const char* key;
JsonNode* value;
} asKey;
struct
{
JsonNode* child;
JsonBuffer* buffer;
} asContainer;
struct
{
JsonNode* target;
} asProxy;
};
2014-10-09 14:19:43 +02:00
public:
2014-10-09 14:17:09 +02:00
JsonNode()
: type(JSON_UNDEFINED), next(0)
{
}
2014-10-09 14:19:43 +02:00
2014-10-09 12:14:10 +02:00
void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer
void setAsArray(JsonBuffer* buffer)
{
type = JSON_ARRAY;
content.asContainer.child = 0;
content.asContainer.buffer = buffer;
}
void setAsBoolean(bool value)
{
type = JSON_BOOLEAN;
content.asBoolean = value;
}
void setAsLong(int value)
{
type = JSON_LONG;
content.asInteger = value;
}
void setAsString(char const* value)
{
type = JSON_STRING;
content.asString = value;
}
void setAsDouble(double value, int decimals)
{
2014-10-09 14:48:55 +02:00
type = static_cast<JsonNodeType>(JSON_DOUBLE_0_DECIMALS + decimals);
2014-10-09 12:14:10 +02:00
content.asDouble = value;
}
void setAsObject(JsonBuffer* buffer)
{
type = JSON_OBJECT;
content.asContainer.child = 0;
content.asContainer.buffer = buffer;
}
void setAsObjectKeyValue(const char* key, JsonNode* value)
{
type = JSON_KEY_VALUE;
content.asKey.key = key;
content.asKey.value = value;
}
bool getAsBoolean()
{
return type == JSON_BOOLEAN ? content.asBoolean : false;
}
double getAsDouble()
{
2014-10-09 14:48:55 +02:00
return type >= JSON_DOUBLE_0_DECIMALS ? content.asDouble : 0;
2014-10-09 12:14:10 +02:00
}
long getAsInteger()
{
return type == JSON_LONG ? content.asInteger : 0;
}
const char* getAsString()
{
return type == JSON_STRING ? content.asString : 0;
}
JsonBuffer* getContainerBuffer()
{
if (type == JSON_PROXY) return content.asProxy.target->getContainerBuffer();
2014-10-09 12:14:10 +02:00
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.buffer : 0;
}
JsonNode* getContainerChild()
{
if (type == JSON_PROXY) return content.asProxy.target->getContainerChild();
2014-10-09 12:14:10 +02:00
return type == JSON_ARRAY || type == JSON_OBJECT ? content.asContainer.child : 0;
}
const char* getAsObjectKey()
{
return type == JSON_KEY_VALUE ? content.asKey.key : 0;
}
JsonNode* getAsObjectValue()
{
return type == JSON_KEY_VALUE ? content.asKey.value : 0;
}
2014-10-09 14:48:55 +02:00
void addChild(JsonNode* childToAdd);
2014-10-09 12:14:10 +02:00
2014-10-09 14:48:55 +02:00
void removeChild(JsonNode* childToRemove);
2014-10-09 12:14:10 +02:00
void duplicate(JsonNode* other)
{
if (!other)
{
type = JSON_UNDEFINED;
}
else if (other->type == JSON_ARRAY || other->type==JSON_OBJECT)
{
other->setAsProxyOfSelf();
setAsProxyOf(other->content.asProxy.target);
}
else
{
*this = *other;
}
}
2014-10-09 12:14:10 +02:00
private:
2014-10-09 14:19:43 +02:00
JsonNode* next;
2014-10-09 14:31:25 +02:00
JsonNodeContent content;
JsonNodeType type;
2014-10-09 14:17:09 +02:00
2014-10-09 12:14:10 +02:00
inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
inline void writeObjectTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
void setAsProxyOfSelf();
void setAsProxyOf(JsonNode* target)
{
type = JSON_PROXY;
content.asProxy.target = target;
}
};