mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-14 11:06:35 +02:00
Added JsonValue::as<T>()
This commit is contained in:
@ -25,7 +25,7 @@ namespace ArduinoJson
|
||||
void operator=(int);
|
||||
void operator=(const JsonValue& value) { duplicate(value); }
|
||||
void operator=(const Internals::JsonNodeWrapper& object) { duplicate(object); }
|
||||
|
||||
|
||||
operator bool() const;
|
||||
operator const char*() const;
|
||||
operator double() const;
|
||||
@ -35,5 +35,11 @@ namespace ArduinoJson
|
||||
operator JsonObject() const;
|
||||
|
||||
void set(double value, int decimals);
|
||||
|
||||
template<typename T>
|
||||
T as()
|
||||
{
|
||||
return static_cast<T>(*this);
|
||||
}
|
||||
};
|
||||
}
|
@ -41,7 +41,7 @@ private:
|
||||
template<typename T>
|
||||
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();
|
||||
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(12, static_cast<int>(*it));
|
||||
EXPECT_EQ(12, (*it).as<int>()); // TODO: use ->
|
||||
++it;
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(34, static_cast<int>(*it));
|
||||
EXPECT_EQ(34, (*it).as<int>()); // TODO: use ->
|
||||
++it;
|
||||
EXPECT_EQ(array.end(), it);
|
||||
}
|
@ -17,11 +17,11 @@ TEST(JsonObject_Iterator_Test, SimpleTest)
|
||||
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_STREQ("ab", it.key());
|
||||
EXPECT_EQ(12, static_cast<int>(it.value()));
|
||||
EXPECT_EQ(12, it.value().as<int>()); // TODO: use ->
|
||||
++it;
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_STREQ("cd", it.key());
|
||||
EXPECT_EQ(34, static_cast<int>(it.value()));
|
||||
EXPECT_EQ(34, it.value().as<int>()); // TODO: use ->
|
||||
++it;
|
||||
EXPECT_EQ(object.end(), it);
|
||||
}
|
@ -44,12 +44,12 @@ protected:
|
||||
template<typename T>
|
||||
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)
|
||||
{
|
||||
EXPECT_STREQ(expected, static_cast<const char*>(_array[index]));
|
||||
EXPECT_STREQ(expected, _array[index].as<const char*>());
|
||||
}
|
||||
|
||||
StaticJsonBuffer<42> _jsonBuffer;
|
||||
|
@ -20,11 +20,11 @@ TEST(JsonParser_Nested_Tests, ArrayNestedInObject)
|
||||
ASSERT_EQ(2, array1.size());
|
||||
ASSERT_EQ(2, array2.size());
|
||||
|
||||
EXPECT_EQ(1, static_cast<int>(array1[0]));
|
||||
EXPECT_EQ(2, static_cast<int>(array1[1]));
|
||||
EXPECT_EQ(1, array1[0].as<int>());
|
||||
EXPECT_EQ(2, array1[1].as<int>());
|
||||
|
||||
EXPECT_EQ(3, static_cast<int>(array2[0]));
|
||||
EXPECT_EQ(4, static_cast<int>(array2[1]));
|
||||
EXPECT_EQ(3, array2[0].as<int>());
|
||||
EXPECT_EQ(4, array2[1].as<int>());
|
||||
}
|
||||
|
||||
TEST(JsonParser_Nested_Tests, ObjectNestedInArray)
|
||||
@ -44,8 +44,8 @@ TEST(JsonParser_Nested_Tests, ObjectNestedInArray)
|
||||
ASSERT_EQ(2, object1.size());
|
||||
ASSERT_EQ(2, object2.size());
|
||||
|
||||
EXPECT_EQ(1, static_cast<int>(object1["a"]));
|
||||
EXPECT_EQ(2, static_cast<int>(object1["b"]));
|
||||
EXPECT_EQ(3, static_cast<int>(object2["c"]));
|
||||
EXPECT_EQ(4, static_cast<int>(object2["d"]));
|
||||
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)
|
||||
{
|
||||
EXPECT_STREQ(expected, static_cast<const char*>(_object[key]));
|
||||
EXPECT_STREQ(expected, _object[key].as<const char*>());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void keyMustHaveValue(const char* key, T expected)
|
||||
{
|
||||
EXPECT_EQ(expected, static_cast<T>(_object[key]));
|
||||
EXPECT_EQ(expected, _object[key].as<T>());
|
||||
}
|
||||
|
||||
private:
|
||||
|
Reference in New Issue
Block a user