Fixed deserializer that stopped reading at the first 0xFF (closes #1118)

This commit is contained in:
Benoit Blanchon
2019-10-29 14:17:11 +01:00
parent 3a169df0a5
commit ef63757b1a
4 changed files with 80 additions and 5 deletions

View File

@ -0,0 +1,14 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#pragma once
// Reproduces Arduino's Stream class
class Stream // : public Print
{
public:
virtual ~Stream() {}
virtual int read() = 0;
virtual size_t readBytes(char *buffer, size_t length) = 0;
};

View File

@ -2,6 +2,7 @@
// Copyright Benoit Blanchon 2014-2019
// MIT License
#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 1
#include <ArduinoJson/Deserialization/Reader.hpp>
#include <catch.hpp>
@ -164,3 +165,61 @@ TEST_CASE("IteratorReader") {
REQUIRE(buffer[6] == 'g');
}
}
class StreamStub : public Stream {
public:
StreamStub(const char* s) : _stream(s) {}
int read() {
return _stream.get();
}
size_t readBytes(char* buffer, size_t length) {
_stream.read(buffer, static_cast<std::streamsize>(length));
return static_cast<size_t>(_stream.gcount());
}
private:
std::istringstream _stream;
};
TEST_CASE("Reader<Stream>") {
SECTION("read()") {
StreamStub src("\x01\xFF");
Reader<StreamStub> reader(src);
REQUIRE(reader.read() == 0x01);
REQUIRE(reader.read() == 0xFF);
REQUIRE(reader.read() == -1);
}
SECTION("readBytes() all at once") {
StreamStub src("ABC");
Reader<StreamStub> reader(src);
char buffer[8] = "abcd";
REQUIRE(reader.readBytes(buffer, 4) == 3);
REQUIRE(buffer[0] == 'A');
REQUIRE(buffer[1] == 'B');
REQUIRE(buffer[2] == 'C');
REQUIRE(buffer[3] == 'd');
}
SECTION("readBytes() in two parts") {
StreamStub src("ABCDEF");
Reader<StreamStub> reader(src);
char buffer[12] = "abcdefg";
REQUIRE(reader.readBytes(buffer, 4) == 4);
REQUIRE(reader.readBytes(buffer + 4, 4) == 2);
REQUIRE(buffer[0] == 'A');
REQUIRE(buffer[1] == 'B');
REQUIRE(buffer[2] == 'C');
REQUIRE(buffer[3] == 'D');
REQUIRE(buffer[4] == 'E');
REQUIRE(buffer[5] == 'F');
REQUIRE(buffer[6] == 'g');
}
}