From 83f1d2c81b5af42d58b4e52b12a41336d5790ba3 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 19 Feb 2017 21:17:05 +0100 Subject: [PATCH] :sparkles: added start index for from_cbor and from_msgpack (#462) --- src/json.hpp | 35 ++++++++++++++++++++++++----------- src/json.hpp.re2c | 35 ++++++++++++++++++++++++----------- test/src/unit-cbor.cpp | 4 ++++ test/src/unit-msgpack.cpp | 4 ++++ test/src/unit-regression.cpp | 2 +- 5 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 08b95cdb..c849f7f4 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -7917,9 +7917,11 @@ class basic_json vector in MessagePack format.,to_msgpack} @sa http://msgpack.org - @sa @ref from_msgpack(const std::vector&) for the analogous - deserialization + @sa @ref from_msgpack(const std::vector&, const size_t) for the + analogous deserialization @sa @ref to_cbor(const basic_json& for the related CBOR format + + @since version 2.0.9 */ static std::vector to_msgpack(const basic_json& j) { @@ -7935,6 +7937,7 @@ class basic_json serialization format. @param[in] v a byte vector in MessagePack format + @param[in] start_index the index to start reading from @a v (0 by default) @return deserialized JSON value @throw std::invalid_argument if unsupported features from MessagePack were @@ -7948,11 +7951,15 @@ class basic_json @sa http://msgpack.org @sa @ref to_msgpack(const basic_json&) for the analogous serialization - @sa @ref from_cbor(const std::vector&) for the related CBOR format + @sa @ref from_cbor(const std::vector&, const size_t) for the + related CBOR format + + @since version 2.0.9, parameter @a start_index since 2.0.11 */ - static basic_json from_msgpack(const std::vector& v) + static basic_json from_msgpack(const std::vector& v, + const size_t start_index = 0) { - size_t i = 0; + size_t i = start_index; return from_msgpack_internal(v, i); } @@ -7973,9 +7980,11 @@ class basic_json vector in CBOR format.,to_cbor} @sa http://cbor.io - @sa @ref from_cbor(const std::vector&) for the analogous - deserialization + @sa @ref from_cbor(const std::vector&, const size_t) for the + analogous deserialization @sa @ref to_msgpack(const basic_json& for the related MessagePack format + + @since version 2.0.9 */ static std::vector to_cbor(const basic_json& j) { @@ -7991,6 +8000,7 @@ class basic_json (Concise Binary Object Representation) serialization format. @param[in] v a byte vector in CBOR format + @param[in] start_index the index to start reading from @a v (0 by default) @return deserialized JSON value @throw std::invalid_argument if unsupported features from CBOR were used in @@ -8004,12 +8014,15 @@ class basic_json @sa http://cbor.io @sa @ref to_cbor(const basic_json&) for the analogous serialization - @sa @ref from_msgpack(const std::vector&) for the related - MessagePack format + @sa @ref from_msgpack(const std::vector&, const size_t) for the + related MessagePack format + + @since version 2.0.9, parameter @a start_index since 2.0.11 */ - static basic_json from_cbor(const std::vector& v) + static basic_json from_cbor(const std::vector& v, + const size_t start_index = 0) { - size_t i = 0; + size_t i = start_index; return from_cbor_internal(v, i); } diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 050b0b6b..2e591919 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -7917,9 +7917,11 @@ class basic_json vector in MessagePack format.,to_msgpack} @sa http://msgpack.org - @sa @ref from_msgpack(const std::vector&) for the analogous - deserialization + @sa @ref from_msgpack(const std::vector&, const size_t) for the + analogous deserialization @sa @ref to_cbor(const basic_json& for the related CBOR format + + @since version 2.0.9 */ static std::vector to_msgpack(const basic_json& j) { @@ -7935,6 +7937,7 @@ class basic_json serialization format. @param[in] v a byte vector in MessagePack format + @param[in] start_index the index to start reading from @a v (0 by default) @return deserialized JSON value @throw std::invalid_argument if unsupported features from MessagePack were @@ -7948,11 +7951,15 @@ class basic_json @sa http://msgpack.org @sa @ref to_msgpack(const basic_json&) for the analogous serialization - @sa @ref from_cbor(const std::vector&) for the related CBOR format + @sa @ref from_cbor(const std::vector&, const size_t) for the + related CBOR format + + @since version 2.0.9, parameter @a start_index since 2.0.11 */ - static basic_json from_msgpack(const std::vector& v) + static basic_json from_msgpack(const std::vector& v, + const size_t start_index = 0) { - size_t i = 0; + size_t i = start_index; return from_msgpack_internal(v, i); } @@ -7973,9 +7980,11 @@ class basic_json vector in CBOR format.,to_cbor} @sa http://cbor.io - @sa @ref from_cbor(const std::vector&) for the analogous - deserialization + @sa @ref from_cbor(const std::vector&, const size_t) for the + analogous deserialization @sa @ref to_msgpack(const basic_json& for the related MessagePack format + + @since version 2.0.9 */ static std::vector to_cbor(const basic_json& j) { @@ -7991,6 +8000,7 @@ class basic_json (Concise Binary Object Representation) serialization format. @param[in] v a byte vector in CBOR format + @param[in] start_index the index to start reading from @a v (0 by default) @return deserialized JSON value @throw std::invalid_argument if unsupported features from CBOR were used in @@ -8004,12 +8014,15 @@ class basic_json @sa http://cbor.io @sa @ref to_cbor(const basic_json&) for the analogous serialization - @sa @ref from_msgpack(const std::vector&) for the related - MessagePack format + @sa @ref from_msgpack(const std::vector&, const size_t) for the + related MessagePack format + + @since version 2.0.9, parameter @a start_index since 2.0.11 */ - static basic_json from_cbor(const std::vector& v) + static basic_json from_cbor(const std::vector& v, + const size_t start_index = 0) { - size_t i = 0; + size_t i = start_index; return from_cbor_internal(v, i); } diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 539d408b..a8bbe463 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -1184,6 +1184,10 @@ TEST_CASE("single CBOR roundtrip") // compare parsed JSON values CHECK(j1 == j2); + + // check with different start index + packed.insert(packed.begin(), 5, 0xff); + CHECK(j1 == json::from_cbor(packed, 5)); } } diff --git a/test/src/unit-msgpack.cpp b/test/src/unit-msgpack.cpp index cad78ae8..34befb56 100644 --- a/test/src/unit-msgpack.cpp +++ b/test/src/unit-msgpack.cpp @@ -1039,6 +1039,10 @@ TEST_CASE("single MessagePack roundtrip") // compare parsed JSON values CHECK(j1 == j2); + + // check with different start index + packed.insert(packed.begin(), 5, 0xff); + CHECK(j1 == json::from_msgpack(packed, 5)); } } diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 29ca82ec..8c9c8b92 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -385,7 +385,7 @@ TEST_CASE("regression tests") { return '.'; } - + std::string do_grouping() const { return "\03";