Procházet zdrojové kódy

Test that integers can be stored in a JsonObject

Benoit Blanchon před 11 roky
rodič
revize
71fd2de675

+ 1 - 1
srcs/JsonNode.h

@@ -24,7 +24,7 @@ struct JsonNode
 
 
     union
     union
     {
     {
-     //   int asInteger;
+        int asInteger;
 
 
         struct 
         struct 
         {
         {

+ 1 - 1
srcs/JsonObject.cpp

@@ -48,7 +48,7 @@ size_t JsonObject::size()
 JsonValue JsonObject::operator[](char const* key)
 JsonValue JsonObject::operator[](char const* key)
 {
 {
     JsonNode* node = getOrCreateNodeAt(key);
     JsonNode* node = getOrCreateNodeAt(key);
-    return JsonValue(/*node*/);
+    return JsonValue(node);
 }
 }
 
 
 JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
 JsonNode* JsonObject::getOrCreateNodeAt(char const* key)

+ 28 - 0
srcs/JsonValue.cpp

@@ -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;
+}

+ 16 - 16
srcs/JsonValue.h

@@ -6,21 +6,21 @@
 
 
 class JsonValue
 class JsonValue
 {
 {
-//public:
-//
-//    JsonValue(JsonBuffer& buffer, JsonNode& node)
-//        : _buffer(buffer), _node(node)
-//    {
-//    }
-//
-//    void operator=(const JsonObject& object);
-//    void operator=(int);
-//    
-//    operator JsonObject();
-//    operator int();
-//
-//private:
-//    JsonBuffer& _buffer;
-//    JsonNode& _node;
+public:
+
+    JsonValue(JsonNode* node)
+        : _node(node)
+    {
+    }
+
+   // void operator=(const JsonObject& object);
+    void operator=(int);
+    
+   // operator JsonObject();
+    operator int();
+
+private:
+    //JsonBuffer& _buffer;
+    JsonNode* _node;
 };
 };
 
 

+ 1 - 0
srcs/srcs.vcxproj

@@ -74,6 +74,7 @@
   <ItemGroup>
   <ItemGroup>
     <ClCompile Include="JsonBuffer.cpp" />
     <ClCompile Include="JsonBuffer.cpp" />
     <ClCompile Include="JsonObject.cpp" />
     <ClCompile Include="JsonObject.cpp" />
+    <ClCompile Include="JsonValue.cpp" />
   </ItemGroup>
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   <ImportGroup Label="ExtensionTargets">

+ 3 - 0
srcs/srcs.vcxproj.filters

@@ -38,5 +38,8 @@
     <ClCompile Include="JsonBuffer.cpp">
     <ClCompile Include="JsonBuffer.cpp">
       <Filter>Source Files</Filter>
       <Filter>Source Files</Filter>
     </ClCompile>
     </ClCompile>
+    <ClCompile Include="JsonValue.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   </ItemGroup>
 </Project>
 </Project>

+ 13 - 0
tests/JsonObjectTests.cpp

@@ -26,4 +26,17 @@ TEST(JsonObjectTests, WhenTheSameValueIsAddedTwice_ThenSizeIsOnlyIncreasedByOne)
 
 
     object["hello"];
     object["hello"];
     EXPECT_EQ(1, object.size());
     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"]);
 }
 }