forked from bblanchon/ArduinoJson
Merge branch 'merge-parser-and-generator' of github.com:bblanchon/ArduinoJson into merge-parser-and-generator
Conflicts: test/JsonObject_Iterator_Tests.cpp
This commit is contained in:
@ -35,5 +35,11 @@ namespace ArduinoJson
|
|||||||
operator JsonObject() const;
|
operator JsonObject() const;
|
||||||
|
|
||||||
void set(double value, int decimals);
|
void set(double value, int decimals);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T as()
|
||||||
|
{
|
||||||
|
return static_cast<T>(*this);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -41,7 +41,7 @@ private:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
void elementAtIndexMustBe(int index, T expected)
|
void elementAtIndexMustBe(int index, T expected)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(expected, static_cast<T>(array[index]));
|
EXPECT_EQ(expected, array[index].as<T>());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -16,10 +16,10 @@ TEST(JsonArray_Iterator_Test, SimpleTest)
|
|||||||
JsonArrayIterator end = array.end();
|
JsonArrayIterator end = array.end();
|
||||||
|
|
||||||
EXPECT_NE(end, it);
|
EXPECT_NE(end, it);
|
||||||
EXPECT_EQ(12, static_cast<int>(*it));
|
EXPECT_EQ(12, (*it).as<int>()); // TODO: use ->
|
||||||
++it;
|
++it;
|
||||||
EXPECT_NE(end, it);
|
EXPECT_NE(end, it);
|
||||||
EXPECT_EQ(34, static_cast<int>(*it));
|
EXPECT_EQ(34, (*it).as<int>()); // TODO: use ->
|
||||||
++it;
|
++it;
|
||||||
EXPECT_EQ(array.end(), it);
|
EXPECT_EQ(array.end(), it);
|
||||||
}
|
}
|
@ -17,11 +17,10 @@ TEST(JsonObject_Iterator_Test, SimpleTest)
|
|||||||
|
|
||||||
EXPECT_NE(end, it);
|
EXPECT_NE(end, it);
|
||||||
EXPECT_STREQ("ab", it->key());
|
EXPECT_STREQ("ab", it->key());
|
||||||
EXPECT_EQ(12, static_cast<int>(it->value()));
|
EXPECT_EQ(12, it->value().as<int>()); ++it;
|
||||||
++it;
|
|
||||||
EXPECT_NE(end, it);
|
EXPECT_NE(end, it);
|
||||||
EXPECT_STREQ("cd", it->key());
|
EXPECT_STREQ("cd", it->key());
|
||||||
EXPECT_EQ(34, static_cast<int>(it->value()));
|
EXPECT_EQ(34, it->value().as<int>());
|
||||||
++it;
|
++it;
|
||||||
EXPECT_EQ(object.end(), it);
|
EXPECT_EQ(object.end(), it);
|
||||||
}
|
}
|
@ -44,12 +44,12 @@ protected:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
void elementAtIndexMustBe(int index, T expected)
|
void elementAtIndexMustBe(int index, T expected)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(expected, static_cast<T>(_array[index]));
|
EXPECT_EQ(expected, _array[index].as<T>());
|
||||||
}
|
}
|
||||||
|
|
||||||
void elementAtIndexMustBe(int index, const char* expected)
|
void elementAtIndexMustBe(int index, const char* expected)
|
||||||
{
|
{
|
||||||
EXPECT_STREQ(expected, static_cast<const char*>(_array[index]));
|
EXPECT_STREQ(expected, _array[index].as<const char*>());
|
||||||
}
|
}
|
||||||
|
|
||||||
StaticJsonBuffer<42> _jsonBuffer;
|
StaticJsonBuffer<42> _jsonBuffer;
|
||||||
|
51
test/JsonParser_Nested_Tests.cpp
Normal file
51
test/JsonParser_Nested_Tests.cpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
||||||
|
|
||||||
|
using namespace ArduinoJson;
|
||||||
|
|
||||||
|
TEST(JsonParser_Nested_Tests, ArrayNestedInObject)
|
||||||
|
{
|
||||||
|
StaticJsonBuffer<42> jsonBuffer;
|
||||||
|
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
||||||
|
|
||||||
|
JsonObject object = jsonBuffer.parseObject(jsonString);
|
||||||
|
JsonArray array1 = object["ab"];
|
||||||
|
JsonArray array2 = object["cd"];
|
||||||
|
|
||||||
|
ASSERT_TRUE(object.success());
|
||||||
|
|
||||||
|
ASSERT_TRUE(array1.success());
|
||||||
|
ASSERT_TRUE(array2.success());
|
||||||
|
|
||||||
|
ASSERT_EQ(2, array1.size());
|
||||||
|
ASSERT_EQ(2, array2.size());
|
||||||
|
|
||||||
|
EXPECT_EQ(1, array1[0].as<int>());
|
||||||
|
EXPECT_EQ(2, array1[1].as<int>());
|
||||||
|
|
||||||
|
EXPECT_EQ(3, array2[0].as<int>());
|
||||||
|
EXPECT_EQ(4, array2[1].as<int>());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(JsonParser_Nested_Tests, ObjectNestedInArray)
|
||||||
|
{
|
||||||
|
StaticJsonBuffer<42> jsonBuffer;
|
||||||
|
char jsonString[] = " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
||||||
|
|
||||||
|
JsonArray array = jsonBuffer.parseArray(jsonString);
|
||||||
|
JsonObject object1 = array[0];
|
||||||
|
JsonObject object2 = array[1];
|
||||||
|
|
||||||
|
ASSERT_TRUE(array.success());
|
||||||
|
|
||||||
|
ASSERT_TRUE(object1.success());
|
||||||
|
ASSERT_TRUE(object2.success());
|
||||||
|
|
||||||
|
ASSERT_EQ(2, object1.size());
|
||||||
|
ASSERT_EQ(2, object2.size());
|
||||||
|
|
||||||
|
EXPECT_EQ(1, object1["a"].as<int>());
|
||||||
|
EXPECT_EQ(2, object1["b"].as<int>());
|
||||||
|
EXPECT_EQ(3, object2["c"].as<int>());
|
||||||
|
EXPECT_EQ(4, object2["d"].as<int>());
|
||||||
|
}
|
@ -31,13 +31,13 @@ protected:
|
|||||||
|
|
||||||
void keyMustHaveValue(const char* key, const char* expected)
|
void keyMustHaveValue(const char* key, const char* expected)
|
||||||
{
|
{
|
||||||
EXPECT_STREQ(expected, static_cast<const char*>(_object[key]));
|
EXPECT_STREQ(expected, _object[key].as<const char*>());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void keyMustHaveValue(const char* key, T expected)
|
void keyMustHaveValue(const char* key, T expected)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(expected, static_cast<T>(_object[key]));
|
EXPECT_EQ(expected, _object[key].as<T>());
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Reference in New Issue
Block a user