mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 12:02:14 +02:00
Test that integers can be stored in a JsonObject
This commit is contained in:
@ -24,7 +24,7 @@ struct JsonNode
|
||||
|
||||
union
|
||||
{
|
||||
// int asInteger;
|
||||
int asInteger;
|
||||
|
||||
struct
|
||||
{
|
||||
|
@ -48,7 +48,7 @@ size_t JsonObject::size()
|
||||
JsonValue JsonObject::operator[](char const* key)
|
||||
{
|
||||
JsonNode* node = getOrCreateNodeAt(key);
|
||||
return JsonValue(/*node*/);
|
||||
return JsonValue(node);
|
||||
}
|
||||
|
||||
JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
|
||||
|
28
srcs/JsonValue.cpp
Normal file
28
srcs/JsonValue.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include "JsonObject.h"
|
||||
#include "JsonNode.h"
|
||||
#include "JsonValue.h"
|
||||
|
||||
//void JsonValue::operator=(JsonObject const& object)
|
||||
//{
|
||||
// _node = object._node;
|
||||
//}
|
||||
|
||||
void JsonValue::operator=(int value)
|
||||
{
|
||||
if (!_node) return;
|
||||
|
||||
_node->type = JSON_INTEGER;
|
||||
_node->content.asInteger = value;
|
||||
}
|
||||
|
||||
//JsonValue::operator JsonObject()
|
||||
//{
|
||||
// return JsonObject(_buffer, _node);
|
||||
//}
|
||||
|
||||
JsonValue::operator int()
|
||||
{
|
||||
if (!_node || _node->type != JSON_INTEGER) return 0;
|
||||
|
||||
return _node->content.asInteger;
|
||||
}
|
@ -6,21 +6,21 @@
|
||||
|
||||
class JsonValue
|
||||
{
|
||||
//public:
|
||||
//
|
||||
// JsonValue(JsonBuffer& buffer, JsonNode& node)
|
||||
// : _buffer(buffer), _node(node)
|
||||
// {
|
||||
// }
|
||||
//
|
||||
public:
|
||||
|
||||
JsonValue(JsonNode* node)
|
||||
: _node(node)
|
||||
{
|
||||
}
|
||||
|
||||
// void operator=(const JsonObject& object);
|
||||
// void operator=(int);
|
||||
//
|
||||
void operator=(int);
|
||||
|
||||
// operator JsonObject();
|
||||
// operator int();
|
||||
//
|
||||
//private:
|
||||
operator int();
|
||||
|
||||
private:
|
||||
//JsonBuffer& _buffer;
|
||||
// JsonNode& _node;
|
||||
JsonNode* _node;
|
||||
};
|
||||
|
||||
|
@ -74,6 +74,7 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="JsonBuffer.cpp" />
|
||||
<ClCompile Include="JsonObject.cpp" />
|
||||
<ClCompile Include="JsonValue.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
@ -38,5 +38,8 @@
|
||||
<ClCompile Include="JsonBuffer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonValue.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -27,3 +27,16 @@ TEST(JsonObjectTests, WhenTheSameValueIsAddedTwice_ThenSizeIsOnlyIncreasedByOne)
|
||||
object["hello"];
|
||||
EXPECT_EQ(1, object.size());
|
||||
}
|
||||
|
||||
TEST(JsonObjectTests, WhenAnIntegerIsStore_TheSameIntegerIsRetreived)
|
||||
{
|
||||
StaticJsonBuffer<42> json;
|
||||
|
||||
JsonObject object = json.createObject();
|
||||
|
||||
object["hello"] = 123;
|
||||
object["world"] = 456;
|
||||
|
||||
EXPECT_EQ(123, (int) object["hello"]);
|
||||
EXPECT_EQ(456, (int) object["world"]);
|
||||
}
|
Reference in New Issue
Block a user