Просмотр исходного кода

Test that integers can be stored in a JsonObject

Benoit Blanchon 11 лет назад
Родитель
Сommit
71fd2de675
7 измененных файлов с 63 добавлено и 18 удалено
  1. 1 1
      srcs/JsonNode.h
  2. 1 1
      srcs/JsonObject.cpp
  3. 28 0
      srcs/JsonValue.cpp
  4. 16 16
      srcs/JsonValue.h
  5. 1 0
      srcs/srcs.vcxproj
  6. 3 0
      srcs/srcs.vcxproj.filters
  7. 13 0
      tests/JsonObjectTests.cpp

+ 1 - 1
srcs/JsonNode.h

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

+ 1 - 1
srcs/JsonObject.cpp

@@ -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 - 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
 {
-//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>
     <ClCompile Include="JsonBuffer.cpp" />
     <ClCompile Include="JsonObject.cpp" />
+    <ClCompile Include="JsonValue.cpp" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">

+ 3 - 0
srcs/srcs.vcxproj.filters

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

+ 13 - 0
tests/JsonObjectTests.cpp

@@ -26,4 +26,17 @@ 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"]);
 }