Exemplo n.º 1
0
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  DynamicJsonBuffer jsonBuffer;
  memstream json(data, size);
  JsonVariant variant = jsonBuffer.parse(json);
  if (variant.success()) {
    variant.as<std::string>();  // <- serialize to JSON
  }
  return 0;
}
String FirebaseArduino::push(const String& path, const JsonVariant& value) {
  String buf;
  value.printTo(buf);
  auto push = FirebasePush(host_, auth_, path.c_str(), buf.c_str(), http_.get());
  error_ = push.error();
  return push.name().c_str();
}
Exemplo n.º 3
0
void jsonResponse(AsyncWebServerRequest *request, int res, JsonVariant json)
{
  // touch
  g_lastAccessTime = millis();

  AsyncResponseStream *response = request->beginResponseStream(F(CONTENT_TYPE_JSON));
  response->addHeader(F(CORS_HEADER), "*");
  json.printTo(*response);
  request->send(response);
}
static void GenStat(Stat& stat, const JsonVariant& v) {
    if (v.is<const JsonArray&>()) {
        const JsonArray& a = v.asArray();
        size_t size = 0;
        for (JsonArray::const_iterator itr = a.begin(); itr != a.end(); ++itr)
            GenStat(stat, *itr);
        stat.arrayCount++;
        stat.elementCount += size;
    }
    else if (v.is<const JsonObject&>()) {
        const JsonObject& o = v.asObject();
        for (JsonObject::const_iterator itr = o.begin(); itr != o.end(); ++itr) {
            GenStat(stat, itr->value);
            stat.stringLength += strlen(itr->key);
        }
        stat.objectCount++;
        stat.memberCount += o.size();
        stat.stringCount += o.size();
    }
    else if (v.is<const char*>()) {
        if (v.asString()) {
            stat.stringCount++;
            stat.stringLength += strlen(v.asString());
        }
        else
            stat.nullCount++; // JSON null value is treat as string null pointer
    }
    else if (v.is<long>() || v.is<double>())
        stat.numberCount++;
    else if (v.is<bool>()) {
        if ((bool)v)
            stat.trueCount++;
        else
            stat.falseCount++;
    }
}
Exemplo n.º 5
0
void JsonParser::parseNumberTo(JsonVariant &destination) {
  char *endOfLong;
  long longValue = strtol(_ptr, &endOfLong, 10);
  char stopChar = *endOfLong;

  // Could it be a floating point value?
  bool couldBeFloat = stopChar == '.' || stopChar == 'e' || stopChar == 'E';

  if (couldBeFloat) {
    // Yes => parse it as a double
    double doubleValue = strtod(_ptr, &_ptr);
    // Count the decimal digits
    uint8_t decimals = static_cast<uint8_t>(_ptr - endOfLong - 1);
    // Set the variant as a double
    destination.set(doubleValue, decimals);
  } else {
    // No => set the variant as a long
    _ptr = endOfLong;
    destination = longValue;
  }
}
TEST(JsonVariant_Success_Tests, ReturnsFalse_WhenInvalidObject) {
    JsonVariant variant = JsonObject::invalid();
    EXPECT_FALSE(variant.success());
}
TEST(JsonVariant_Success_Tests, ReturnsFalse_WhenInvalidArray) {
    JsonVariant variant = JsonArray::invalid();
    EXPECT_FALSE(variant.success());
}
TEST(JsonVariant_Success_Tests, ReturnsTrue_WhenEmptyObject) {
    DynamicJsonBuffer jsonBuffer;

    JsonVariant variant = jsonBuffer.createObject();
    EXPECT_TRUE(variant.success());
}
TEST(JsonVariant_Success_Tests, ReturnsTrue_WhenInteger) {
    JsonVariant variant = 0;
    EXPECT_TRUE(variant.success());
}
TEST(JsonVariant_Success_Tests, ReturnsFalse_WhenUndefined) {
    JsonVariant variant;
    EXPECT_FALSE(variant.success());
}
Exemplo n.º 11
0
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!

#include <ArduinoJson.h>
#include <catch.hpp>

using namespace Catch::Matchers;

TEST_CASE("JsonBuffer::parse()") {
  DynamicJsonBuffer jb;

  SECTION("EmptyObject") {
    JsonVariant variant = jb.parse("{}");
    REQUIRE(variant.success());
    REQUIRE(variant.is<JsonObject>());
  }

  SECTION("EmptyArray") {
    JsonVariant variant = jb.parse("[]");
    REQUIRE(variant.success());
    REQUIRE(variant.is<JsonArray>());
  }

  SECTION("Integer") {
    JsonVariant variant = jb.parse("-42");
    REQUIRE(variant.success());
    REQUIRE(variant.is<int>());
    REQUIRE_FALSE(variant.is<bool>());
Exemplo n.º 12
0
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!

#include <ArduinoJson.h>
#include <catch.hpp>

TEST_CASE("JsonVariant::success()") {
  SECTION("ReturnsFalse_WhenUndefined") {
    JsonVariant variant;
    REQUIRE(false == variant.success());
  }

  SECTION("ReturnsTrue_WhenInteger") {
    JsonVariant variant = 0;
    REQUIRE(true == variant.success());
  }

  SECTION("ReturnsTrue_WhenEmptyArray") {
    DynamicJsonBuffer jsonBuffer;

    JsonVariant variant = jsonBuffer.createArray();
    REQUIRE(true == variant.success());
  }

  SECTION("ReturnsTrue_WhenEmptyObject") {
    DynamicJsonBuffer jsonBuffer;
 void outputMustBe(const char *expected) {
   char buffer[256] = "";
   size_t n = variant.printTo(buffer, sizeof(buffer));
   EXPECT_STREQ(expected, buffer);
   EXPECT_EQ(strlen(expected), n);
 }
Exemplo n.º 14
0
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License

#include <ArduinoJson.h>
#include <catch.hpp>

TEST_CASE("JsonVariant::operator[]") {
  DynamicJsonBuffer _jsonBuffer;

  SECTION("Array") {
    JsonArray &array = _jsonBuffer.createArray();
    array.add("element at index 0");
    array.add("element at index 1");

    JsonVariant var = array;

    REQUIRE(2 == var.size());
    REQUIRE(std::string("element at index 0") == var[0]);
    REQUIRE(std::string("element at index 1") == var[1]);
    REQUIRE(std::string("element at index 0") ==
            var[static_cast<unsigned char>(0)]);  // issue #381
    REQUIRE_FALSE(var[666].success());
    REQUIRE_FALSE(var[3].success());
    REQUIRE_FALSE(var["0"].success());
  }

  SECTION("Object") {
    JsonObject &object = _jsonBuffer.createObject();
    object["a"] = "element at key \"a\"";
    object["b"] = "element at key \"b\"";
Exemplo n.º 15
0
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License

#include <ArduinoJson.h>
#include <catch.hpp>

TEST_CASE("JsonVariant from JsonArray") {
  SECTION("JsonArray is null") {
    JsonArray arr;
    JsonVariant v = arr;
    REQUIRE(v.isNull() == true);
  }

  SECTION("JsonArray is not null") {
    DynamicJsonDocument doc;
    JsonArray arr = doc.to<JsonArray>();
    arr.add(12);
    arr.add(34);

    JsonVariant v = arr;

    REQUIRE(v.is<JsonArray>() == true);
    REQUIRE(v.size() == 2);
    REQUIRE(v[0] == 12);
    REQUIRE(v[1] == 34);
  }
}

TEST_CASE("JsonVariant from JsonObject") {
  SECTION("JsonObject is null") {
void FirebaseArduino::set(const String& path, const JsonVariant& value) {
  String buf;
  value.printTo(buf);
  auto set = FirebaseSet(host_, auth_, path.c_str(), buf.c_str(), http_.get());
  error_ = set.error();
}
Exemplo n.º 17
0
#include <ArduinoJson.h>
#include <catch.hpp>

#if defined(__clang__)
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning(disable : 4996)
#endif

TEST_CASE("Deprecated functions") {
  DynamicJsonBuffer jsonBuffer;

  SECTION("JsonVariant::asArray()") {
    JsonVariant variant = jsonBuffer.createArray();
    REQUIRE(variant.asArray().success());
  }

  SECTION("JsonVariant::asObject()") {
    JsonVariant variant = jsonBuffer.createObject();
    REQUIRE(variant.asObject().success());
  }

  SECTION("JsonVariant::asString()") {
    JsonVariant variant = "hello";
    REQUIRE(std::string("hello") == variant.asString());
  }

  SECTION("JsonArray::removeAt()") {
    JsonArray& arr = jsonBuffer.createArray();