mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Reduced generator size. Fixed Visual Studio warnings
This commit is contained in:
		| @@ -15,13 +15,7 @@ class String : public std::string { | ||||
|  public: | ||||
|   String(const char *cstr = "") : std::string(cstr) {} | ||||
|   String(const String &str) : std::string(str) {} | ||||
|   explicit String(char c); | ||||
|   explicit String(unsigned char); | ||||
|   explicit String(int); | ||||
|   explicit String(unsigned int); | ||||
|   explicit String(long); | ||||
|   explicit String(unsigned long); | ||||
|   explicit String(float, unsigned char decimalPlaces = 2); | ||||
|   explicit String(double, unsigned char decimalPlaces = 2); | ||||
| }; | ||||
|  | ||||
|   | ||||
| @@ -23,6 +23,8 @@ class DynamicStringBuilder : public Print { | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   DynamicStringBuilder &operator=(const DynamicStringBuilder &); | ||||
|  | ||||
|   String &_str; | ||||
| }; | ||||
| } | ||||
|   | ||||
| @@ -8,6 +8,7 @@ | ||||
|  | ||||
| #include "../Arduino/Print.hpp" | ||||
| #include "Encoding.hpp" | ||||
| #include "ForceInline.hpp" | ||||
|  | ||||
| namespace ArduinoJson { | ||||
| namespace Internals { | ||||
| @@ -69,7 +70,7 @@ class JsonWriter { | ||||
|  | ||||
|  protected: | ||||
|   void write(char c) { _length += _sink.write(c); } | ||||
|   void write(const char *s) { _length += _sink.print(s); } | ||||
|   FORCE_INLINE void write(const char *s) { _length += _sink.print(s); } | ||||
|  | ||||
|   Print &_sink; | ||||
|   size_t _length; | ||||
|   | ||||
| @@ -16,13 +16,6 @@ class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> { | ||||
|  | ||||
|   using JsonSubscriptBase::operator=; | ||||
|  | ||||
|   FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& other) { | ||||
|     // to prevent Visual Studio warning C4512: assignment operator could not be | ||||
|     // generated | ||||
|     _array.set(_index, other._array.get(other._index)); | ||||
|     return *this; | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE bool success() const { return _index < _array.size(); } | ||||
|  | ||||
|   FORCE_INLINE operator JsonVariant() const { return _array.get(_index); } | ||||
|   | ||||
| @@ -19,13 +19,6 @@ class JsonObjectSubscript | ||||
|  | ||||
|   using JsonSubscriptBase<JsonObjectSubscript<TKey> >::operator=; | ||||
|  | ||||
|   FORCE_INLINE JsonObjectSubscript<TKey>& operator=( | ||||
|       const JsonObjectSubscript<TKey>& other) { | ||||
|     // to prevent Visual Studio warning C4512: assignment operator could not be | ||||
|     // generated | ||||
|     return set(other.get()); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE bool success() const { return _object.containsKey(_key); } | ||||
|  | ||||
|   FORCE_INLINE operator JsonVariant() const { return _object.get(_key); } | ||||
|   | ||||
| @@ -101,7 +101,7 @@ const char *JsonVariant::as<const char *>() const; | ||||
|  | ||||
| template <> | ||||
| inline bool JsonVariant::as<bool>() const { | ||||
|   return as<long>(); | ||||
|   return as<long>() != 0; | ||||
| } | ||||
|  | ||||
| template <> | ||||
|   | ||||
| @@ -16,12 +16,6 @@ String::String(double value, unsigned char digits) { | ||||
|   *this = tmp; | ||||
| } | ||||
|  | ||||
| String::String(int value) { | ||||
|   char tmp[32]; | ||||
|   sprintf(tmp, "%d", value); | ||||
|   *this = tmp; | ||||
| } | ||||
|  | ||||
| String::String(long value) { | ||||
|   char tmp[32]; | ||||
|   sprintf(tmp, "%ld", value); | ||||
|   | ||||
| @@ -99,19 +99,25 @@ bool JsonVariant::is<double>() const { | ||||
| } | ||||
|  | ||||
| void JsonVariant::writeTo(JsonWriter &writer) const { | ||||
|   if (_type == JSON_ARRAY) _content.asArray->writeTo(writer); | ||||
|   if (_type == JSON_ARRAY) | ||||
|     _content.asArray->writeTo(writer); | ||||
|  | ||||
|   if (_type == JSON_OBJECT) _content.asObject->writeTo(writer); | ||||
|   else if (_type == JSON_OBJECT) | ||||
|     _content.asObject->writeTo(writer); | ||||
|  | ||||
|   if (_type == JSON_STRING) writer.writeString(_content.asString); | ||||
|   else if (_type == JSON_STRING) | ||||
|     writer.writeString(_content.asString); | ||||
|  | ||||
|   if (_type == JSON_UNPARSED) writer.writeRaw(_content.asString); | ||||
|   else if (_type == JSON_UNPARSED) | ||||
|     writer.writeRaw(_content.asString); | ||||
|  | ||||
|   if (_type == JSON_LONG) writer.writeLong(_content.asLong); | ||||
|   else if (_type == JSON_LONG) | ||||
|     writer.writeLong(_content.asLong); | ||||
|  | ||||
|   if (_type == JSON_BOOLEAN) writer.writeBoolean(_content.asLong); | ||||
|   else if (_type == JSON_BOOLEAN) | ||||
|     writer.writeBoolean(_content.asLong != 0); | ||||
|  | ||||
|   if (_type >= JSON_DOUBLE_0_DECIMALS) { | ||||
|   else if (_type >= JSON_DOUBLE_0_DECIMALS) { | ||||
|     uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS); | ||||
|     writer.writeDouble(_content.asDouble, decimals); | ||||
|   } | ||||
|   | ||||
| @@ -13,21 +13,26 @@ TEST(JsonParser_Nested_Tests, ArrayNestedInObject) { | ||||
|  | ||||
|   JsonObject &object = jsonBuffer.parseObject(jsonString); | ||||
|   JsonArray &array1 = object["ab"]; | ||||
|   JsonArray &array2 = object["cd"]; | ||||
|   const JsonArray &array2 = object["cd"]; | ||||
|   JsonArray &array3 = object["ef"]; | ||||
|  | ||||
|   ASSERT_TRUE(object.success()); | ||||
|  | ||||
|   ASSERT_TRUE(array1.success()); | ||||
|   ASSERT_TRUE(array2.success()); | ||||
|   ASSERT_FALSE(array3.success()); | ||||
|  | ||||
|   ASSERT_EQ(2, array1.size()); | ||||
|   ASSERT_EQ(2, array2.size()); | ||||
|   ASSERT_EQ(0, array3.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>()); | ||||
|  | ||||
|   EXPECT_EQ(0, array3[0].as<int>()); | ||||
| } | ||||
|  | ||||
| TEST(JsonParser_Nested_Tests, ObjectNestedInArray) { | ||||
| @@ -37,18 +42,22 @@ TEST(JsonParser_Nested_Tests, ObjectNestedInArray) { | ||||
|  | ||||
|   JsonArray &array = jsonBuffer.parseArray(jsonString); | ||||
|   JsonObject &object1 = array[0]; | ||||
|   JsonObject &object2 = array[1]; | ||||
|   const JsonObject &object2 = array[1]; | ||||
|   JsonObject &object3 = array[2]; | ||||
|  | ||||
|   ASSERT_TRUE(array.success()); | ||||
|  | ||||
|   ASSERT_TRUE(object1.success()); | ||||
|   ASSERT_TRUE(object2.success()); | ||||
|   ASSERT_FALSE(object3.success()); | ||||
|  | ||||
|   ASSERT_EQ(2, object1.size()); | ||||
|   ASSERT_EQ(2, object2.size()); | ||||
|   ASSERT_EQ(0, object3.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>()); | ||||
|   EXPECT_EQ(0, object3["e"].as<int>()); | ||||
| } | ||||
|   | ||||
| @@ -145,6 +145,11 @@ TEST(JsonVariant_As_Tests, RandomStringAsLong) { | ||||
|   ASSERT_EQ(0L, variant.as<long>()); | ||||
| } | ||||
|  | ||||
| TEST(JsonVariant_As_Tests, RandomStringAsString) { | ||||
|   JsonVariant variant = "hello"; | ||||
|   ASSERT_EQ(String("hello"), variant.as<String>()); | ||||
| } | ||||
|  | ||||
| TEST(JsonVariant_As_Tests, TrueStringAsBool) { | ||||
|   JsonVariant variant = "true"; | ||||
|   ASSERT_TRUE(variant.as<bool>()); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user