From efa55eb5b3d3a4942789bdf397c3a6d6226475d4 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 7 Sep 2020 12:57:22 +0200 Subject: Revert "remove external libraries from main branch" This reverts commit 0558244645611f314f47e0fa427f7323ce253eaf. --- include/lib/ArduinoJson.h | 19 + include/lib/ArduinoJson/Configuration.hpp | 154 + include/lib/ArduinoJson/Data/Encoding.hpp | 37 + .../lib/ArduinoJson/Data/JsonBufferAllocated.hpp | 22 + include/lib/ArduinoJson/Data/JsonFloat.hpp | 18 + include/lib/ArduinoJson/Data/JsonInteger.hpp | 23 + include/lib/ArduinoJson/Data/JsonVariantAs.hpp | 42 + .../lib/ArduinoJson/Data/JsonVariantContent.hpp | 27 + .../lib/ArduinoJson/Data/JsonVariantDefault.hpp | 23 + include/lib/ArduinoJson/Data/JsonVariantType.hpp | 27 + include/lib/ArduinoJson/Data/List.hpp | 94 + include/lib/ArduinoJson/Data/ListConstIterator.hpp | 50 + include/lib/ArduinoJson/Data/ListIterator.hpp | 60 + include/lib/ArduinoJson/Data/ListNode.hpp | 24 + include/lib/ArduinoJson/Data/NonCopyable.hpp | 23 + include/lib/ArduinoJson/Data/ReferenceType.hpp | 24 + include/lib/ArduinoJson/Data/ValueSaver.hpp | 52 + .../lib/ArduinoJson/Deserialization/Comments.hpp | 61 + .../lib/ArduinoJson/Deserialization/JsonParser.hpp | 102 + .../ArduinoJson/Deserialization/JsonParserImpl.hpp | 189 + .../ArduinoJson/Deserialization/StringWriter.hpp | 41 + include/lib/ArduinoJson/DynamicJsonBuffer.hpp | 170 + include/lib/ArduinoJson/JsonArray.hpp | 227 + include/lib/ArduinoJson/JsonArrayImpl.hpp | 26 + include/lib/ArduinoJson/JsonArraySubscript.hpp | 122 + include/lib/ArduinoJson/JsonBuffer.hpp | 78 + include/lib/ArduinoJson/JsonBufferBase.hpp | 127 + include/lib/ArduinoJson/JsonBufferImpl.hpp | 17 + include/lib/ArduinoJson/JsonObject.hpp | 328 + include/lib/ArduinoJson/JsonObjectImpl.hpp | 28 + include/lib/ArduinoJson/JsonObjectSubscript.hpp | 110 + include/lib/ArduinoJson/JsonPair.hpp | 16 + include/lib/ArduinoJson/JsonVariant.hpp | 355 + include/lib/ArduinoJson/JsonVariantBase.hpp | 24 + include/lib/ArduinoJson/JsonVariantCasts.hpp | 59 + include/lib/ArduinoJson/JsonVariantComparisons.hpp | 139 + include/lib/ArduinoJson/JsonVariantImpl.hpp | 126 + include/lib/ArduinoJson/JsonVariantOr.hpp | 52 + include/lib/ArduinoJson/JsonVariantSubscripts.hpp | 86 + include/lib/ArduinoJson/Polyfills/attributes.hpp | 29 + include/lib/ArduinoJson/Polyfills/ctype.hpp | 18 + include/lib/ArduinoJson/Polyfills/isFloat.hpp | 38 + include/lib/ArduinoJson/Polyfills/isInteger.hpp | 19 + include/lib/ArduinoJson/Polyfills/math.hpp | 19 + include/lib/ArduinoJson/Polyfills/parseFloat.hpp | 90 + include/lib/ArduinoJson/Polyfills/parseInteger.hpp | 41 + include/lib/ArduinoJson/RawJson.hpp | 46 + .../lib/ArduinoJson/Serialization/DummyPrint.hpp | 22 + .../Serialization/DynamicStringBuilder.hpp | 35 + .../lib/ArduinoJson/Serialization/FloatParts.hpp | 89 + .../ArduinoJson/Serialization/IndentedPrint.hpp | 68 + .../ArduinoJson/Serialization/JsonPrintable.hpp | 117 + .../ArduinoJson/Serialization/JsonSerializer.hpp | 32 + .../Serialization/JsonSerializerImpl.hpp | 103 + .../lib/ArduinoJson/Serialization/JsonWriter.hpp | 155 + .../lib/ArduinoJson/Serialization/Prettyfier.hpp | 133 + .../Serialization/StaticStringBuilder.hpp | 36 + .../Serialization/StreamPrintAdapter.hpp | 39 + include/lib/ArduinoJson/StaticJsonBuffer.hpp | 126 + .../lib/ArduinoJson/StringTraits/ArduinoStream.hpp | 61 + .../lib/ArduinoJson/StringTraits/CharPointer.hpp | 64 + .../lib/ArduinoJson/StringTraits/FlashString.hpp | 61 + include/lib/ArduinoJson/StringTraits/StdStream.hpp | 60 + include/lib/ArduinoJson/StringTraits/StdString.hpp | 77 + .../lib/ArduinoJson/StringTraits/StringTraits.hpp | 36 + include/lib/ArduinoJson/TypeTraits/EnableIf.hpp | 19 + include/lib/ArduinoJson/TypeTraits/FloatTraits.hpp | 171 + include/lib/ArduinoJson/TypeTraits/IsArray.hpp | 24 + include/lib/ArduinoJson/TypeTraits/IsBaseOf.hpp | 27 + include/lib/ArduinoJson/TypeTraits/IsChar.hpp | 23 + include/lib/ArduinoJson/TypeTraits/IsConst.hpp | 21 + .../lib/ArduinoJson/TypeTraits/IsFloatingPoint.hpp | 18 + include/lib/ArduinoJson/TypeTraits/IsIntegral.hpp | 26 + include/lib/ArduinoJson/TypeTraits/IsSame.hpp | 21 + .../ArduinoJson/TypeTraits/IsSignedIntegral.hpp | 28 + .../ArduinoJson/TypeTraits/IsUnsignedIntegral.hpp | 28 + include/lib/ArduinoJson/TypeTraits/IsVariant.hpp | 17 + include/lib/ArduinoJson/TypeTraits/RemoveConst.hpp | 20 + .../lib/ArduinoJson/TypeTraits/RemoveReference.hpp | 20 + include/lib/ArduinoJson/version.hpp | 10 + include/lib/binn.h | 923 +++ include/lib/capnp-c/capn-list.inc | 162 + include/lib/capnp-c/capnp_c.h | 446 ++ include/lib/capnp-c/capnp_priv.h | 62 + include/lib/modernjson/adl_serializer.hpp | 49 + .../modernjson/detail/conversions/from_json.hpp | 370 + .../lib/modernjson/detail/conversions/to_chars.hpp | 1094 +++ .../lib/modernjson/detail/conversions/to_json.hpp | 342 + include/lib/modernjson/detail/exceptions.hpp | 349 + .../lib/modernjson/detail/input/binary_reader.hpp | 1984 +++++ .../lib/modernjson/detail/input/input_adapters.hpp | 398 + include/lib/modernjson/detail/input/json_sax.hpp | 701 ++ include/lib/modernjson/detail/input/lexer.hpp | 1506 ++++ include/lib/modernjson/detail/input/parser.hpp | 504 ++ include/lib/modernjson/detail/input/position_t.hpp | 27 + .../detail/iterators/internal_iterator.hpp | 25 + .../lib/modernjson/detail/iterators/iter_impl.hpp | 614 ++ .../detail/iterators/iteration_proxy.hpp | 125 + .../detail/iterators/json_reverse_iterator.hpp | 119 + .../detail/iterators/primitive_iterator.hpp | 120 + include/lib/modernjson/detail/json_pointer.hpp | 696 ++ include/lib/modernjson/detail/json_ref.hpp | 69 + include/lib/modernjson/detail/macro_scope.hpp | 135 + include/lib/modernjson/detail/macro_unscope.hpp | 22 + include/lib/modernjson/detail/meta/cpp_future.hpp | 63 + include/lib/modernjson/detail/meta/detected.hpp | 56 + include/lib/modernjson/detail/meta/is_sax.hpp | 141 + include/lib/modernjson/detail/meta/type_traits.hpp | 347 + include/lib/modernjson/detail/meta/void_t.hpp | 13 + .../lib/modernjson/detail/output/binary_writer.hpp | 1339 ++++ .../modernjson/detail/output/output_adapters.hpp | 119 + .../lib/modernjson/detail/output/serializer.hpp | 741 ++ include/lib/modernjson/detail/value_t.hpp | 76 + include/lib/modernjson/json.h | 7992 ++++++++++++++++++++ include/lib/modernjson/json_fwd.h | 64 + include/lib/mpack/mpack.h | 7172 ++++++++++++++++++ include/lib/mpmalloc.h | 17 + include/lib/nanopb/pb.h | 594 ++ include/lib/nanopb/pb_common.h | 42 + include/lib/nanopb/pb_decode.h | 175 + include/lib/nanopb/pb_encode.h | 170 + include/lib/ubjson/ubj.h | 231 + include/lib/ubjson/ubj_internal.h | 163 + include/lib/xdr.h | 66 + include/lib/xdr16.h | 68 + src/app/prototest/Makefile.inc | 96 + src/app/prototest/main.cc | 79 + src/lib/binn.cc | 3372 +++++++++ src/lib/capnp-c/capn-malloc.cc | 424 ++ src/lib/capnp-c/capn-stream.cc | 217 + src/lib/capnp-c/capn.cc | 1117 +++ src/lib/mpack/mpack.cc | 6440 ++++++++++++++++ src/lib/mpmalloc.cc | 42 + src/lib/nanopb/pb_common.cc | 97 + src/lib/nanopb/pb_decode.cc | 1528 ++++ src/lib/nanopb/pb_encode.cc | 893 +++ src/lib/ubjson/ubjr.c | 525 ++ src/lib/ubjson/ubjrw.c | 169 + src/lib/ubjson/ubjw.c | 626 ++ src/lib/xdr.cc | 188 + src/lib/xdr16.cc | 216 + 141 files changed, 51809 insertions(+) create mode 100644 include/lib/ArduinoJson.h create mode 100644 include/lib/ArduinoJson/Configuration.hpp create mode 100644 include/lib/ArduinoJson/Data/Encoding.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonBufferAllocated.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonFloat.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonInteger.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonVariantAs.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonVariantContent.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonVariantDefault.hpp create mode 100644 include/lib/ArduinoJson/Data/JsonVariantType.hpp create mode 100644 include/lib/ArduinoJson/Data/List.hpp create mode 100644 include/lib/ArduinoJson/Data/ListConstIterator.hpp create mode 100644 include/lib/ArduinoJson/Data/ListIterator.hpp create mode 100644 include/lib/ArduinoJson/Data/ListNode.hpp create mode 100644 include/lib/ArduinoJson/Data/NonCopyable.hpp create mode 100644 include/lib/ArduinoJson/Data/ReferenceType.hpp create mode 100644 include/lib/ArduinoJson/Data/ValueSaver.hpp create mode 100644 include/lib/ArduinoJson/Deserialization/Comments.hpp create mode 100644 include/lib/ArduinoJson/Deserialization/JsonParser.hpp create mode 100644 include/lib/ArduinoJson/Deserialization/JsonParserImpl.hpp create mode 100644 include/lib/ArduinoJson/Deserialization/StringWriter.hpp create mode 100644 include/lib/ArduinoJson/DynamicJsonBuffer.hpp create mode 100644 include/lib/ArduinoJson/JsonArray.hpp create mode 100644 include/lib/ArduinoJson/JsonArrayImpl.hpp create mode 100644 include/lib/ArduinoJson/JsonArraySubscript.hpp create mode 100644 include/lib/ArduinoJson/JsonBuffer.hpp create mode 100644 include/lib/ArduinoJson/JsonBufferBase.hpp create mode 100644 include/lib/ArduinoJson/JsonBufferImpl.hpp create mode 100644 include/lib/ArduinoJson/JsonObject.hpp create mode 100644 include/lib/ArduinoJson/JsonObjectImpl.hpp create mode 100644 include/lib/ArduinoJson/JsonObjectSubscript.hpp create mode 100644 include/lib/ArduinoJson/JsonPair.hpp create mode 100644 include/lib/ArduinoJson/JsonVariant.hpp create mode 100644 include/lib/ArduinoJson/JsonVariantBase.hpp create mode 100644 include/lib/ArduinoJson/JsonVariantCasts.hpp create mode 100644 include/lib/ArduinoJson/JsonVariantComparisons.hpp create mode 100644 include/lib/ArduinoJson/JsonVariantImpl.hpp create mode 100644 include/lib/ArduinoJson/JsonVariantOr.hpp create mode 100644 include/lib/ArduinoJson/JsonVariantSubscripts.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/attributes.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/ctype.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/isFloat.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/isInteger.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/math.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/parseFloat.hpp create mode 100644 include/lib/ArduinoJson/Polyfills/parseInteger.hpp create mode 100644 include/lib/ArduinoJson/RawJson.hpp create mode 100644 include/lib/ArduinoJson/Serialization/DummyPrint.hpp create mode 100644 include/lib/ArduinoJson/Serialization/DynamicStringBuilder.hpp create mode 100644 include/lib/ArduinoJson/Serialization/FloatParts.hpp create mode 100644 include/lib/ArduinoJson/Serialization/IndentedPrint.hpp create mode 100644 include/lib/ArduinoJson/Serialization/JsonPrintable.hpp create mode 100644 include/lib/ArduinoJson/Serialization/JsonSerializer.hpp create mode 100644 include/lib/ArduinoJson/Serialization/JsonSerializerImpl.hpp create mode 100644 include/lib/ArduinoJson/Serialization/JsonWriter.hpp create mode 100644 include/lib/ArduinoJson/Serialization/Prettyfier.hpp create mode 100644 include/lib/ArduinoJson/Serialization/StaticStringBuilder.hpp create mode 100644 include/lib/ArduinoJson/Serialization/StreamPrintAdapter.hpp create mode 100644 include/lib/ArduinoJson/StaticJsonBuffer.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/ArduinoStream.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/CharPointer.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/FlashString.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/StdStream.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/StdString.hpp create mode 100644 include/lib/ArduinoJson/StringTraits/StringTraits.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/EnableIf.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/FloatTraits.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/IsArray.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/IsBaseOf.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/IsChar.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/IsConst.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/IsFloatingPoint.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/IsIntegral.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/IsSame.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/IsSignedIntegral.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/IsUnsignedIntegral.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/IsVariant.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/RemoveConst.hpp create mode 100644 include/lib/ArduinoJson/TypeTraits/RemoveReference.hpp create mode 100644 include/lib/ArduinoJson/version.hpp create mode 100644 include/lib/binn.h create mode 100644 include/lib/capnp-c/capn-list.inc create mode 100644 include/lib/capnp-c/capnp_c.h create mode 100644 include/lib/capnp-c/capnp_priv.h create mode 100644 include/lib/modernjson/adl_serializer.hpp create mode 100644 include/lib/modernjson/detail/conversions/from_json.hpp create mode 100644 include/lib/modernjson/detail/conversions/to_chars.hpp create mode 100644 include/lib/modernjson/detail/conversions/to_json.hpp create mode 100644 include/lib/modernjson/detail/exceptions.hpp create mode 100644 include/lib/modernjson/detail/input/binary_reader.hpp create mode 100644 include/lib/modernjson/detail/input/input_adapters.hpp create mode 100644 include/lib/modernjson/detail/input/json_sax.hpp create mode 100644 include/lib/modernjson/detail/input/lexer.hpp create mode 100644 include/lib/modernjson/detail/input/parser.hpp create mode 100644 include/lib/modernjson/detail/input/position_t.hpp create mode 100644 include/lib/modernjson/detail/iterators/internal_iterator.hpp create mode 100644 include/lib/modernjson/detail/iterators/iter_impl.hpp create mode 100644 include/lib/modernjson/detail/iterators/iteration_proxy.hpp create mode 100644 include/lib/modernjson/detail/iterators/json_reverse_iterator.hpp create mode 100644 include/lib/modernjson/detail/iterators/primitive_iterator.hpp create mode 100644 include/lib/modernjson/detail/json_pointer.hpp create mode 100644 include/lib/modernjson/detail/json_ref.hpp create mode 100644 include/lib/modernjson/detail/macro_scope.hpp create mode 100644 include/lib/modernjson/detail/macro_unscope.hpp create mode 100644 include/lib/modernjson/detail/meta/cpp_future.hpp create mode 100644 include/lib/modernjson/detail/meta/detected.hpp create mode 100644 include/lib/modernjson/detail/meta/is_sax.hpp create mode 100644 include/lib/modernjson/detail/meta/type_traits.hpp create mode 100644 include/lib/modernjson/detail/meta/void_t.hpp create mode 100644 include/lib/modernjson/detail/output/binary_writer.hpp create mode 100644 include/lib/modernjson/detail/output/output_adapters.hpp create mode 100644 include/lib/modernjson/detail/output/serializer.hpp create mode 100644 include/lib/modernjson/detail/value_t.hpp create mode 100644 include/lib/modernjson/json.h create mode 100644 include/lib/modernjson/json_fwd.h create mode 100644 include/lib/mpack/mpack.h create mode 100644 include/lib/mpmalloc.h create mode 100644 include/lib/nanopb/pb.h create mode 100644 include/lib/nanopb/pb_common.h create mode 100644 include/lib/nanopb/pb_decode.h create mode 100644 include/lib/nanopb/pb_encode.h create mode 100644 include/lib/ubjson/ubj.h create mode 100644 include/lib/ubjson/ubj_internal.h create mode 100644 include/lib/xdr.h create mode 100644 include/lib/xdr16.h create mode 100644 src/app/prototest/Makefile.inc create mode 100644 src/app/prototest/main.cc create mode 100644 src/lib/binn.cc create mode 100644 src/lib/capnp-c/capn-malloc.cc create mode 100644 src/lib/capnp-c/capn-stream.cc create mode 100644 src/lib/capnp-c/capn.cc create mode 100644 src/lib/mpack/mpack.cc create mode 100644 src/lib/mpmalloc.cc create mode 100644 src/lib/nanopb/pb_common.cc create mode 100644 src/lib/nanopb/pb_decode.cc create mode 100644 src/lib/nanopb/pb_encode.cc create mode 100644 src/lib/ubjson/ubjr.c create mode 100644 src/lib/ubjson/ubjrw.c create mode 100644 src/lib/ubjson/ubjw.c create mode 100644 src/lib/xdr.cc create mode 100644 src/lib/xdr16.cc diff --git a/include/lib/ArduinoJson.h b/include/lib/ArduinoJson.h new file mode 100644 index 0000000..c493c06 --- /dev/null +++ b/include/lib/ArduinoJson.h @@ -0,0 +1,19 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "ArduinoJson/version.hpp" + +#include "ArduinoJson/DynamicJsonBuffer.hpp" +#include "ArduinoJson/JsonArray.hpp" +#include "ArduinoJson/JsonObject.hpp" +#include "ArduinoJson/StaticJsonBuffer.hpp" + +#include "ArduinoJson/Deserialization/JsonParserImpl.hpp" +#include "ArduinoJson/JsonArrayImpl.hpp" +#include "ArduinoJson/JsonBufferImpl.hpp" +#include "ArduinoJson/JsonObjectImpl.hpp" +#include "ArduinoJson/JsonVariantImpl.hpp" +#include "ArduinoJson/Serialization/JsonSerializerImpl.hpp" diff --git a/include/lib/ArduinoJson/Configuration.hpp b/include/lib/ArduinoJson/Configuration.hpp new file mode 100644 index 0000000..0e1a4de --- /dev/null +++ b/include/lib/ArduinoJson/Configuration.hpp @@ -0,0 +1,154 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +// Small or big machine? +#ifndef ARDUINOJSON_EMBEDDED_MODE +#if defined(ARDUINO) || defined(__IAR_SYSTEMS_ICC__) || defined(__XC) || \ + defined(__ARMCC_VERSION) || defined(MULTIPASS_ARCH_msp430fr5969lp) || \ + defined(MULTIPASS_ARCH_msp430fr5994lp) || \ + defined(MULTIPASS_ARCH_arduino_nano) || defined(MULTIPASS_ARCH_blinkenrocket) || \ + defined(MULTIPASS_ARCH_esp8266) +#define ARDUINOJSON_EMBEDDED_MODE 1 +#else +#define ARDUINOJSON_EMBEDDED_MODE 0 +#endif +#endif + +#if ARDUINOJSON_EMBEDDED_MODE + +// Store floats by default to reduce the memory usage (issue #134) +#ifndef ARDUINOJSON_USE_DOUBLE +#define ARDUINOJSON_USE_DOUBLE 0 +#endif + +// Store longs by default, because they usually match the size of a float. +#ifndef ARDUINOJSON_USE_LONG_LONG +#define ARDUINOJSON_USE_LONG_LONG 0 +#endif +#ifndef ARDUINOJSON_USE_INT64 +#define ARDUINOJSON_USE_INT64 0 +#endif + +// Embedded systems usually don't have std::string +#ifndef ARDUINOJSON_ENABLE_STD_STRING +#define ARDUINOJSON_ENABLE_STD_STRING 0 +#endif + +// Embedded systems usually don't have std::stream +#ifndef ARDUINOJSON_ENABLE_STD_STREAM +#define ARDUINOJSON_ENABLE_STD_STREAM 0 +#endif + +// Limit nesting as the stack is likely to be small +#ifndef ARDUINOJSON_DEFAULT_NESTING_LIMIT +#define ARDUINOJSON_DEFAULT_NESTING_LIMIT 10 +#endif + +#else // ARDUINOJSON_EMBEDDED_MODE + +// On a computer we have plenty of memory so we can use doubles +#ifndef ARDUINOJSON_USE_DOUBLE +#define ARDUINOJSON_USE_DOUBLE 1 +#endif + +// Use long long when available +#ifndef ARDUINOJSON_USE_LONG_LONG +#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800) +#define ARDUINOJSON_USE_LONG_LONG 1 +#else +#define ARDUINOJSON_USE_LONG_LONG 0 +#endif +#endif + +// Use _int64 on old versions of Visual Studio +#ifndef ARDUINOJSON_USE_INT64 +#if defined(_MSC_VER) && _MSC_VER <= 1700 +#define ARDUINOJSON_USE_INT64 1 +#else +#define ARDUINOJSON_USE_INT64 0 +#endif +#endif + +// On a computer, we can use std::string +#ifndef ARDUINOJSON_ENABLE_STD_STRING +#define ARDUINOJSON_ENABLE_STD_STRING 1 +#endif + +// On a computer, we can assume std::stream +#ifndef ARDUINOJSON_ENABLE_STD_STREAM +#define ARDUINOJSON_ENABLE_STD_STREAM 1 +#endif + +// On a computer, the stack is large so we can increase nesting limit +#ifndef ARDUINOJSON_DEFAULT_NESTING_LIMIT +#define ARDUINOJSON_DEFAULT_NESTING_LIMIT 50 +#endif + +#endif // ARDUINOJSON_EMBEDDED_MODE + +#ifdef ARDUINO + +// Enable support for Arduino String +#ifndef ARDUINOJSON_ENABLE_ARDUINO_STRING +#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1 +#endif + +// Enable support for Arduino Stream +#ifndef ARDUINOJSON_ENABLE_ARDUINO_STREAM +#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 1 +#endif + +#else // ARDUINO + +// Disable support for Arduino String +#ifndef ARDUINOJSON_ENABLE_ARDUINO_STRING +#define ARDUINOJSON_ENABLE_ARDUINO_STRING 0 +#endif + +// Disable support for Arduino Stream +#ifndef ARDUINOJSON_ENABLE_ARDUINO_STREAM +#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 0 +#endif + +#endif // ARDUINO + +#ifndef ARDUINOJSON_ENABLE_PROGMEM +#ifdef PROGMEM +#define ARDUINOJSON_ENABLE_PROGMEM 1 +#else +#define ARDUINOJSON_ENABLE_PROGMEM 0 +#endif +#endif + +#ifndef ARDUINOJSON_ENABLE_ALIGNMENT +#ifdef ARDUINO_ARCH_AVR +// alignment isn't needed for 8-bit AVR +#define ARDUINOJSON_ENABLE_ALIGNMENT 0 +#else +// but most processors need pointers to be align on word size +#define ARDUINOJSON_ENABLE_ALIGNMENT 1 +#endif +#endif + +// Enable deprecated functions by default +#ifndef ARDUINOJSON_ENABLE_DEPRECATED +#define ARDUINOJSON_ENABLE_DEPRECATED 1 +#endif + +// Control the exponentiation threshold for big numbers +// CAUTION: cannot be more that 1e9 !!!! +#ifndef ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD +#define ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD 1e7 +#endif + +// Control the exponentiation threshold for small numbers +#ifndef ARDUINOJSON_NEGATIVE_EXPONENTIATION_THRESHOLD +#define ARDUINOJSON_NEGATIVE_EXPONENTIATION_THRESHOLD 1e-5 +#endif + +#if ARDUINOJSON_USE_LONG_LONG && ARDUINOJSON_USE_INT64 +#error ARDUINOJSON_USE_LONG_LONG and ARDUINOJSON_USE_INT64 cannot be set together +#endif diff --git a/include/lib/ArduinoJson/Data/Encoding.hpp b/include/lib/ArduinoJson/Data/Encoding.hpp new file mode 100644 index 0000000..a0efa2c --- /dev/null +++ b/include/lib/ArduinoJson/Data/Encoding.hpp @@ -0,0 +1,37 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +class Encoding { + public: + // Optimized for code size on a 8-bit AVR + static char escapeChar(char c) { + const char *p = escapeTable(false); + while (p[0] && p[1] != c) { + p += 2; + } + return p[0]; + } + + // Optimized for code size on a 8-bit AVR + static char unescapeChar(char c) { + const char *p = escapeTable(true); + for (;;) { + if (p[0] == '\0') return c; + if (p[0] == c) return p[1]; + p += 2; + } + } + + private: + static const char *escapeTable(bool excludeIdenticals) { + return &"\"\"\\\\b\bf\fn\nr\rt\t"[excludeIdenticals ? 4 : 0]; + } +}; +} +} diff --git a/include/lib/ArduinoJson/Data/JsonBufferAllocated.hpp b/include/lib/ArduinoJson/Data/JsonBufferAllocated.hpp new file mode 100644 index 0000000..443aae4 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonBufferAllocated.hpp @@ -0,0 +1,22 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../JsonBuffer.hpp" + +namespace ArduinoJson { +namespace Internals { + +class JsonBufferAllocated { + public: + void *operator new(size_t n, JsonBuffer *jsonBuffer) throw() { + if (!jsonBuffer) return NULL; + return jsonBuffer->alloc(n); + } + + void operator delete(void *, JsonBuffer *)throw(); +}; +} +} diff --git a/include/lib/ArduinoJson/Data/JsonFloat.hpp b/include/lib/ArduinoJson/Data/JsonFloat.hpp new file mode 100644 index 0000000..0ed4214 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonFloat.hpp @@ -0,0 +1,18 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../Configuration.hpp" + +namespace ArduinoJson { +namespace Internals { + +#if ARDUINOJSON_USE_DOUBLE +typedef double JsonFloat; +#else +typedef float JsonFloat; +#endif +} +} diff --git a/include/lib/ArduinoJson/Data/JsonInteger.hpp b/include/lib/ArduinoJson/Data/JsonInteger.hpp new file mode 100644 index 0000000..c8ddd00 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonInteger.hpp @@ -0,0 +1,23 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../Configuration.hpp" + +namespace ArduinoJson { +namespace Internals { + +#if ARDUINOJSON_USE_LONG_LONG +typedef long long JsonInteger; +typedef unsigned long long JsonUInt; +#elif ARDUINOJSON_USE_INT64 +typedef __int64 JsonInteger; +typedef unsigned _int64 JsonUInt; +#else +typedef long JsonInteger; +typedef unsigned long JsonUInt; +#endif +} +} diff --git a/include/lib/ArduinoJson/Data/JsonVariantAs.hpp b/include/lib/ArduinoJson/Data/JsonVariantAs.hpp new file mode 100644 index 0000000..8f202c5 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonVariantAs.hpp @@ -0,0 +1,42 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A metafunction that returns the type of the value returned by +// JsonVariant::as() +template +struct JsonVariantAs { + typedef T type; +}; + +template <> +struct JsonVariantAs { + typedef const char* type; +}; + +template <> +struct JsonVariantAs { + typedef JsonArray& type; +}; + +template <> +struct JsonVariantAs { + typedef const JsonArray& type; +}; + +template <> +struct JsonVariantAs { + typedef JsonObject& type; +}; + +template <> +struct JsonVariantAs { + typedef const JsonObject& type; +}; +} +} diff --git a/include/lib/ArduinoJson/Data/JsonVariantContent.hpp b/include/lib/ArduinoJson/Data/JsonVariantContent.hpp new file mode 100644 index 0000000..c525a60 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonVariantContent.hpp @@ -0,0 +1,27 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "JsonFloat.hpp" +#include "JsonInteger.hpp" + +namespace ArduinoJson { + +// Forward declarations +class JsonArray; +class JsonObject; + +namespace Internals { +// A union that defines the actual content of a JsonVariant. +// The enum JsonVariantType determines which member is in use. +union JsonVariantContent { + JsonFloat asFloat; // used for double and float + JsonUInt asInteger; // used for bool, char, short, int and longs + const char* asString; // asString can be null + JsonArray* asArray; // asArray cannot be null + JsonObject* asObject; // asObject cannot be null +}; +} +} diff --git a/include/lib/ArduinoJson/Data/JsonVariantDefault.hpp b/include/lib/ArduinoJson/Data/JsonVariantDefault.hpp new file mode 100644 index 0000000..57ecc83 --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonVariantDefault.hpp @@ -0,0 +1,23 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +template +struct JsonVariantDefault { + static T get() { + return T(); + } +}; + +template +struct JsonVariantDefault : JsonVariantDefault {}; + +template +struct JsonVariantDefault : JsonVariantDefault {}; +} +} diff --git a/include/lib/ArduinoJson/Data/JsonVariantType.hpp b/include/lib/ArduinoJson/Data/JsonVariantType.hpp new file mode 100644 index 0000000..21f890e --- /dev/null +++ b/include/lib/ArduinoJson/Data/JsonVariantType.hpp @@ -0,0 +1,27 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +class JsonArray; +class JsonObject; + +namespace Internals { + +// Enumerated type to know the current type of a JsonVariant. +// The value determines which member of JsonVariantContent is used. +enum JsonVariantType { + JSON_UNDEFINED, // JsonVariant has not been initialized + JSON_UNPARSED, // JsonVariant contains an unparsed string + JSON_STRING, // JsonVariant stores a const char* + JSON_BOOLEAN, // JsonVariant stores a bool + JSON_POSITIVE_INTEGER, // JsonVariant stores an JsonUInt + JSON_NEGATIVE_INTEGER, // JsonVariant stores an JsonUInt that must be negated + JSON_ARRAY, // JsonVariant stores a pointer to a JsonArray + JSON_OBJECT, // JsonVariant stores a pointer to a JsonObject + JSON_FLOAT // JsonVariant stores a JsonFloat +}; +} +} diff --git a/include/lib/ArduinoJson/Data/List.hpp b/include/lib/ArduinoJson/Data/List.hpp new file mode 100644 index 0000000..506308c --- /dev/null +++ b/include/lib/ArduinoJson/Data/List.hpp @@ -0,0 +1,94 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../JsonBuffer.hpp" +#include "ListConstIterator.hpp" +#include "ListIterator.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A singly linked list of T. +// The linked list is composed of ListNode. +// It is derived by JsonArray and JsonObject +template +class List { + public: + typedef T value_type; + typedef ListNode node_type; + typedef ListIterator iterator; + typedef ListConstIterator const_iterator; + + // Creates an empty List attached to a JsonBuffer. + // The JsonBuffer allows to allocate new nodes. + // When buffer is NULL, the List is not able to grow and success() returns + // false. This is used to identify bad memory allocations and parsing + // failures. + explicit List(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} + + // Returns true if the object is valid + // Would return false in the following situation: + // - the memory allocation failed (StaticJsonBuffer was too small) + // - the JSON parsing failed + bool success() const { + return _buffer != NULL; + } + + // Returns the numbers of elements in the list. + // For a JsonObject, it would return the number of key-value pairs + size_t size() const { + size_t nodeCount = 0; + for (node_type *node = _firstNode; node; node = node->next) nodeCount++; + return nodeCount; + } + + iterator add() { + node_type *newNode = new (_buffer) node_type(); + + if (_firstNode) { + node_type *lastNode = _firstNode; + while (lastNode->next) lastNode = lastNode->next; + lastNode->next = newNode; + } else { + _firstNode = newNode; + } + + return iterator(newNode); + } + + iterator begin() { + return iterator(_firstNode); + } + iterator end() { + return iterator(NULL); + } + + const_iterator begin() const { + return const_iterator(_firstNode); + } + const_iterator end() const { + return const_iterator(NULL); + } + + void remove(iterator it) { + node_type *nodeToRemove = it._node; + if (!nodeToRemove) return; + if (nodeToRemove == _firstNode) { + _firstNode = nodeToRemove->next; + } else { + for (node_type *node = _firstNode; node; node = node->next) + if (node->next == nodeToRemove) node->next = nodeToRemove->next; + } + } + + protected: + JsonBuffer *_buffer; + + private: + node_type *_firstNode; +}; +} +} diff --git a/include/lib/ArduinoJson/Data/ListConstIterator.hpp b/include/lib/ArduinoJson/Data/ListConstIterator.hpp new file mode 100644 index 0000000..a6af685 --- /dev/null +++ b/include/lib/ArduinoJson/Data/ListConstIterator.hpp @@ -0,0 +1,50 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "ListNode.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A read-only forward itertor for List +template +class ListConstIterator { + public: + explicit ListConstIterator(const ListNode *node = NULL) : _node(node) {} + + const T &operator*() const { + return _node->content; + } + const T *operator->() { + return &_node->content; + } + + bool operator==(const ListConstIterator &other) const { + return _node == other._node; + } + + bool operator!=(const ListConstIterator &other) const { + return _node != other._node; + } + + ListConstIterator &operator++() { + if (_node) _node = _node->next; + return *this; + } + + ListConstIterator &operator+=(size_t distance) { + while (_node && distance) { + _node = _node->next; + --distance; + } + return *this; + } + + private: + const ListNode *_node; +}; +} +} diff --git a/include/lib/ArduinoJson/Data/ListIterator.hpp b/include/lib/ArduinoJson/Data/ListIterator.hpp new file mode 100644 index 0000000..01fa287 --- /dev/null +++ b/include/lib/ArduinoJson/Data/ListIterator.hpp @@ -0,0 +1,60 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "ListConstIterator.hpp" +#include "ListNode.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +class List; + +// A read-write forward iterator for List +template +class ListIterator { + friend class List; + + public: + explicit ListIterator(ListNode *node = NULL) : _node(node) {} + + T &operator*() const { + return _node->content; + } + T *operator->() { + return &_node->content; + } + + bool operator==(const ListIterator &other) const { + return _node == other._node; + } + + bool operator!=(const ListIterator &other) const { + return _node != other._node; + } + + ListIterator &operator++() { + if (_node) _node = _node->next; + return *this; + } + + ListIterator &operator+=(size_t distance) { + while (_node && distance) { + _node = _node->next; + --distance; + } + return *this; + } + + operator ListConstIterator() const { + return ListConstIterator(_node); + } + + private: + ListNode *_node; +}; +} +} diff --git a/include/lib/ArduinoJson/Data/ListNode.hpp b/include/lib/ArduinoJson/Data/ListNode.hpp new file mode 100644 index 0000000..c090712 --- /dev/null +++ b/include/lib/ArduinoJson/Data/ListNode.hpp @@ -0,0 +1,24 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include // for NULL + +#include "JsonBufferAllocated.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A node for a singly-linked list. +// Used by List and its iterators. +template +struct ListNode : public Internals::JsonBufferAllocated { + ListNode() throw() : next(NULL) {} + + ListNode *next; + T content; +}; +} +} diff --git a/include/lib/ArduinoJson/Data/NonCopyable.hpp b/include/lib/ArduinoJson/Data/NonCopyable.hpp new file mode 100644 index 0000000..73f3d8e --- /dev/null +++ b/include/lib/ArduinoJson/Data/NonCopyable.hpp @@ -0,0 +1,23 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A type that cannot be copied +class NonCopyable { + protected: + NonCopyable() {} + + private: + // copy constructor is private + NonCopyable(const NonCopyable&); + + // copy operator is private + NonCopyable& operator=(const NonCopyable&); +}; +} +} diff --git a/include/lib/ArduinoJson/Data/ReferenceType.hpp b/include/lib/ArduinoJson/Data/ReferenceType.hpp new file mode 100644 index 0000000..1e49117 --- /dev/null +++ b/include/lib/ArduinoJson/Data/ReferenceType.hpp @@ -0,0 +1,24 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A type that is meant to be used by reference only (JsonArray and JsonObject) +class ReferenceType { + public: + bool operator==(const ReferenceType& other) const { + // two JsonArray are equal if they are the same instance + // (we don't compare the content) + return this == &other; + } + + bool operator!=(const ReferenceType& other) const { + return this != &other; + } +}; +} +} diff --git a/include/lib/ArduinoJson/Data/ValueSaver.hpp b/include/lib/ArduinoJson/Data/ValueSaver.hpp new file mode 100644 index 0000000..9750f1a --- /dev/null +++ b/include/lib/ArduinoJson/Data/ValueSaver.hpp @@ -0,0 +1,52 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../JsonBuffer.hpp" +#include "../JsonVariant.hpp" +#include "../StringTraits/StringTraits.hpp" +#include "../TypeTraits/EnableIf.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +struct ValueSaver { + template + static bool save(JsonBuffer*, Destination& destination, Source source) { + destination = source; + return true; + } +}; + +template +struct ValueSaver< + Source, typename EnableIf::should_duplicate>::type> { + template + static bool save(JsonBuffer* buffer, Destination& dest, Source source) { + if (!StringTraits::is_null(source)) { + typename StringTraits::duplicate_t dup = + StringTraits::duplicate(source, buffer); + if (!dup) return false; + dest = dup; + } else { + dest = reinterpret_cast(0); + } + return true; + } +}; + +// const char*, const signed char*, const unsigned char* +template +struct ValueSaver< + Char*, typename EnableIf::should_duplicate>::type> { + template + static bool save(JsonBuffer*, Destination& dest, Char* source) { + dest = reinterpret_cast(source); + return true; + } +}; +} +} diff --git a/include/lib/ArduinoJson/Deserialization/Comments.hpp b/include/lib/ArduinoJson/Deserialization/Comments.hpp new file mode 100644 index 0000000..c2c48eb --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/Comments.hpp @@ -0,0 +1,61 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { +template +void skipSpacesAndComments(TInput& input) { + for (;;) { + switch (input.current()) { + // spaces + case ' ': + case '\t': + case '\r': + case '\n': + input.move(); + continue; + + // comments + case '/': + switch (input.next()) { + // C-style block comment + case '*': + input.move(); // skip '/' + // no need to skip '*' + for (;;) { + input.move(); + if (input.current() == '\0') return; + if (input.current() == '*' && input.next() == '/') { + input.move(); // skip '*' + input.move(); // skip '/' + break; + } + } + break; + + // C++-style line comment + case '/': + // not need to skip "//" + for (;;) { + input.move(); + if (input.current() == '\0') return; + if (input.current() == '\n') break; + } + break; + + // not a comment, just a '/' + default: + return; + } + break; + + default: + return; + } + } +} +} +} diff --git a/include/lib/ArduinoJson/Deserialization/JsonParser.hpp b/include/lib/ArduinoJson/Deserialization/JsonParser.hpp new file mode 100644 index 0000000..4cbaf45 --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/JsonParser.hpp @@ -0,0 +1,102 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../JsonBuffer.hpp" +#include "../JsonVariant.hpp" +#include "../TypeTraits/IsConst.hpp" +#include "StringWriter.hpp" + +namespace ArduinoJson { +namespace Internals { + +// Parse JSON string to create JsonArrays and JsonObjects +// This internal class is not indended to be used directly. +// Instead, use JsonBuffer.parseArray() or .parseObject() +template +class JsonParser { + public: + JsonParser(JsonBuffer *buffer, TReader reader, TWriter writer, + uint8_t nestingLimit) + : _buffer(buffer), + _reader(reader), + _writer(writer), + _nestingLimit(nestingLimit) {} + + JsonArray &parseArray(); + JsonObject &parseObject(); + + JsonVariant parseVariant() { + JsonVariant result; + parseAnythingTo(&result); + return result; + } + + private: + JsonParser &operator=(const JsonParser &); // non-copiable + + static bool eat(TReader &, char charToSkip); + FORCE_INLINE bool eat(char charToSkip) { + return eat(_reader, charToSkip); + } + + const char *parseString(); + bool parseAnythingTo(JsonVariant *destination); + + inline bool parseArrayTo(JsonVariant *destination); + inline bool parseObjectTo(JsonVariant *destination); + inline bool parseStringTo(JsonVariant *destination); + + static inline bool isBetween(char c, char min, char max) { + return min <= c && c <= max; + } + + static inline bool canBeInNonQuotedString(char c) { + return isBetween(c, '0', '9') || isBetween(c, '_', 'z') || + isBetween(c, 'A', 'Z') || c == '+' || c == '-' || c == '.'; + } + + static inline bool isQuote(char c) { + return c == '\'' || c == '\"'; + } + + JsonBuffer *_buffer; + TReader _reader; + TWriter _writer; + uint8_t _nestingLimit; +}; + +template +struct JsonParserBuilder { + typedef typename StringTraits::Reader InputReader; + typedef JsonParser TParser; + + static TParser makeParser(TJsonBuffer *buffer, TString &json, + uint8_t nestingLimit) { + return TParser(buffer, InputReader(json), *buffer, nestingLimit); + } +}; + +template +struct JsonParserBuilder::value>::type> { + typedef typename StringTraits::Reader TReader; + typedef StringWriter TWriter; + typedef JsonParser TParser; + + static TParser makeParser(TJsonBuffer *buffer, TChar *json, + uint8_t nestingLimit) { + return TParser(buffer, TReader(json), TWriter(json), nestingLimit); + } +}; + +template +inline typename JsonParserBuilder::TParser makeParser( + TJsonBuffer *buffer, TString &json, uint8_t nestingLimit) { + return JsonParserBuilder::makeParser(buffer, json, + nestingLimit); +} +} // namespace Internals +} // namespace ArduinoJson diff --git a/include/lib/ArduinoJson/Deserialization/JsonParserImpl.hpp b/include/lib/ArduinoJson/Deserialization/JsonParserImpl.hpp new file mode 100644 index 0000000..5042673 --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/JsonParserImpl.hpp @@ -0,0 +1,189 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Comments.hpp" +#include "JsonParser.hpp" + +template +inline bool ArduinoJson::Internals::JsonParser::eat( + TReader &reader, char charToSkip) { + skipSpacesAndComments(reader); + if (reader.current() != charToSkip) return false; + reader.move(); + return true; +} + +template +inline bool +ArduinoJson::Internals::JsonParser::parseAnythingTo( + JsonVariant *destination) { + skipSpacesAndComments(_reader); + + switch (_reader.current()) { + case '[': + return parseArrayTo(destination); + + case '{': + return parseObjectTo(destination); + + default: + return parseStringTo(destination); + } +} + +template +inline ArduinoJson::JsonArray & +ArduinoJson::Internals::JsonParser::parseArray() { + if (_nestingLimit == 0) return JsonArray::invalid(); + _nestingLimit--; + + // Create an empty array + JsonArray &array = _buffer->createArray(); + + // Check opening braket + if (!eat('[')) goto ERROR_MISSING_BRACKET; + if (eat(']')) goto SUCCESS_EMPTY_ARRAY; + + // Read each value + for (;;) { + // 1 - Parse value + JsonVariant value; + if (!parseAnythingTo(&value)) goto ERROR_INVALID_VALUE; + if (!array.add(value)) goto ERROR_NO_MEMORY; + + // 2 - More values? + if (eat(']')) goto SUCCES_NON_EMPTY_ARRAY; + if (!eat(',')) goto ERROR_MISSING_COMMA; + } + +SUCCESS_EMPTY_ARRAY: +SUCCES_NON_EMPTY_ARRAY: + _nestingLimit++; + return array; + +ERROR_INVALID_VALUE: +ERROR_MISSING_BRACKET: +ERROR_MISSING_COMMA: +ERROR_NO_MEMORY: + return JsonArray::invalid(); +} + +template +inline bool ArduinoJson::Internals::JsonParser::parseArrayTo( + JsonVariant *destination) { + JsonArray &array = parseArray(); + if (!array.success()) return false; + + *destination = array; + return true; +} + +template +inline ArduinoJson::JsonObject & +ArduinoJson::Internals::JsonParser::parseObject() { + if (_nestingLimit == 0) return JsonObject::invalid(); + _nestingLimit--; + + // Create an empty object + JsonObject &object = _buffer->createObject(); + + // Check opening brace + if (!eat('{')) goto ERROR_MISSING_BRACE; + if (eat('}')) goto SUCCESS_EMPTY_OBJECT; + + // Read each key value pair + for (;;) { + // 1 - Parse key + const char *key = parseString(); + if (!key) goto ERROR_INVALID_KEY; + if (!eat(':')) goto ERROR_MISSING_COLON; + + // 2 - Parse value + JsonVariant value; + if (!parseAnythingTo(&value)) goto ERROR_INVALID_VALUE; + if (!object.set(key, value)) goto ERROR_NO_MEMORY; + + // 3 - More keys/values? + if (eat('}')) goto SUCCESS_NON_EMPTY_OBJECT; + if (!eat(',')) goto ERROR_MISSING_COMMA; + } + +SUCCESS_EMPTY_OBJECT: +SUCCESS_NON_EMPTY_OBJECT: + _nestingLimit++; + return object; + +ERROR_INVALID_KEY: +ERROR_INVALID_VALUE: +ERROR_MISSING_BRACE: +ERROR_MISSING_COLON: +ERROR_MISSING_COMMA: +ERROR_NO_MEMORY: + return JsonObject::invalid(); +} + +template +inline bool ArduinoJson::Internals::JsonParser::parseObjectTo( + JsonVariant *destination) { + JsonObject &object = parseObject(); + if (!object.success()) return false; + + *destination = object; + return true; +} + +template +inline const char * +ArduinoJson::Internals::JsonParser::parseString() { + typename RemoveReference::type::String str = _writer.startString(); + + skipSpacesAndComments(_reader); + char c = _reader.current(); + + if (isQuote(c)) { // quotes + _reader.move(); + char stopChar = c; + for (;;) { + c = _reader.current(); + if (c == '\0') break; + _reader.move(); + + if (c == stopChar) break; + + if (c == '\\') { + // replace char + c = Encoding::unescapeChar(_reader.current()); + if (c == '\0') break; + _reader.move(); + } + + str.append(c); + } + } else { // no quotes + for (;;) { + if (!canBeInNonQuotedString(c)) break; + _reader.move(); + str.append(c); + c = _reader.current(); + } + } + + return str.c_str(); +} + +template +inline bool ArduinoJson::Internals::JsonParser::parseStringTo( + JsonVariant *destination) { + bool hasQuotes = isQuote(_reader.current()); + const char *value = parseString(); + if (value == NULL) return false; + if (hasQuotes) { + *destination = value; + } else { + *destination = RawJson(value); + } + return true; +} diff --git a/include/lib/ArduinoJson/Deserialization/StringWriter.hpp b/include/lib/ArduinoJson/Deserialization/StringWriter.hpp new file mode 100644 index 0000000..fd5507e --- /dev/null +++ b/include/lib/ArduinoJson/Deserialization/StringWriter.hpp @@ -0,0 +1,41 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +template +class StringWriter { + public: + class String { + public: + String(TChar** ptr) : _writePtr(ptr), _startPtr(*ptr) {} + + void append(char c) { + *(*_writePtr)++ = TChar(c); + } + + const char* c_str() const { + *(*_writePtr)++ = 0; + return reinterpret_cast(_startPtr); + } + + private: + TChar** _writePtr; + TChar* _startPtr; + }; + + StringWriter(TChar* buffer) : _ptr(buffer) {} + + String startString() { + return String(&_ptr); + } + + private: + TChar* _ptr; +}; +} +} diff --git a/include/lib/ArduinoJson/DynamicJsonBuffer.hpp b/include/lib/ArduinoJson/DynamicJsonBuffer.hpp new file mode 100644 index 0000000..bdbd5dd --- /dev/null +++ b/include/lib/ArduinoJson/DynamicJsonBuffer.hpp @@ -0,0 +1,170 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "JsonBufferBase.hpp" + +#include + +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wnon-virtual-dtor" +#elif defined(__GNUC__) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) +#pragma GCC diagnostic push +#endif +#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" +#endif + +namespace ArduinoJson { +namespace Internals { +class DefaultAllocator { + public: + void* allocate(size_t size) { + return malloc(size); + } + void deallocate(void* pointer) { + free(pointer); + } +}; + +template +class DynamicJsonBufferBase + : public JsonBufferBase > { + struct Block; + struct EmptyBlock { + Block* next; + size_t capacity; + size_t size; + }; + struct Block : EmptyBlock { + uint8_t data[1]; + }; + + public: + enum { EmptyBlockSize = sizeof(EmptyBlock) }; + + DynamicJsonBufferBase(size_t initialSize = 256) + : _head(NULL), _nextBlockCapacity(initialSize) {} + + ~DynamicJsonBufferBase() { + clear(); + } + + // Gets the number of bytes occupied in the buffer + size_t size() const { + size_t total = 0; + for (const Block* b = _head; b; b = b->next) total += b->size; + return total; + } + + // Allocates the specified amount of bytes in the buffer + virtual void* alloc(size_t bytes) { + alignNextAlloc(); + return canAllocInHead(bytes) ? allocInHead(bytes) : allocInNewBlock(bytes); + } + + // Resets the buffer. + // USE WITH CAUTION: this invalidates all previously allocated data + void clear() { + Block* currentBlock = _head; + while (currentBlock != NULL) { + _nextBlockCapacity = currentBlock->capacity; + Block* nextBlock = currentBlock->next; + _allocator.deallocate(currentBlock); + currentBlock = nextBlock; + } + _head = 0; + } + + class String { + public: + String(DynamicJsonBufferBase* parent) + : _parent(parent), _start(NULL), _length(0) {} + + void append(char c) { + if (_parent->canAllocInHead(1)) { + char* end = static_cast(_parent->allocInHead(1)); + *end = c; + if (_length == 0) _start = end; + } else { + char* newStart = + static_cast(_parent->allocInNewBlock(_length + 1)); + if (_start && newStart) memcpy(newStart, _start, _length); + if (newStart) newStart[_length] = c; + _start = newStart; + } + _length++; + } + + const char* c_str() { + append(0); + return _start; + } + + private: + DynamicJsonBufferBase* _parent; + char* _start; + size_t _length; + }; + + String startString() { + return String(this); + } + + private: + void alignNextAlloc() { + if (_head) _head->size = this->round_size_up(_head->size); + } + + bool canAllocInHead(size_t bytes) const { + return _head != NULL && _head->size + bytes <= _head->capacity; + } + + void* allocInHead(size_t bytes) { + void* p = _head->data + _head->size; + _head->size += bytes; + return p; + } + + void* allocInNewBlock(size_t bytes) { + size_t capacity = _nextBlockCapacity; + if (bytes > capacity) capacity = bytes; + if (!addNewBlock(capacity)) return NULL; + _nextBlockCapacity *= 2; + return allocInHead(bytes); + } + + bool addNewBlock(size_t capacity) { + size_t bytes = EmptyBlockSize + capacity; + Block* block = static_cast(_allocator.allocate(bytes)); + if (block == NULL) return false; + block->capacity = capacity; + block->size = 0; + block->next = _head; + _head = block; + return true; + } + + TAllocator _allocator; + Block* _head; + size_t _nextBlockCapacity; +}; +} + +#if defined(__clang__) +#pragma clang diagnostic pop +#elif defined(__GNUC__) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) +#pragma GCC diagnostic pop +#endif +#endif + +// Implements a JsonBuffer with dynamic memory allocation. +// You are strongly encouraged to consider using StaticJsonBuffer which is much +// more suitable for embedded systems. +typedef Internals::DynamicJsonBufferBase + DynamicJsonBuffer; +} diff --git a/include/lib/ArduinoJson/JsonArray.hpp b/include/lib/ArduinoJson/JsonArray.hpp new file mode 100644 index 0000000..4431e7a --- /dev/null +++ b/include/lib/ArduinoJson/JsonArray.hpp @@ -0,0 +1,227 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Data/JsonBufferAllocated.hpp" +#include "Data/List.hpp" +#include "Data/ReferenceType.hpp" +#include "Data/ValueSaver.hpp" +#include "JsonVariant.hpp" +#include "Serialization/JsonPrintable.hpp" +#include "StringTraits/StringTraits.hpp" +#include "TypeTraits/EnableIf.hpp" +#include "TypeTraits/IsArray.hpp" +#include "TypeTraits/IsFloatingPoint.hpp" +#include "TypeTraits/IsSame.hpp" + +// Returns the size (in bytes) of an array with n elements. +// Can be very handy to determine the size of a StaticJsonBuffer. +#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \ + (sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(JsonArray::node_type)) + +namespace ArduinoJson { + +// Forward declarations +class JsonObject; +class JsonBuffer; +namespace Internals { +class JsonArraySubscript; +} + +// An array of JsonVariant. +// +// The constructor is private, instances must be created via +// JsonBuffer::createArray() or JsonBuffer::parseArray(). +// A JsonArray can be serialized to a JSON string via JsonArray::printTo(). +// It can also be deserialized from a JSON string via JsonBuffer::parseArray(). +class JsonArray : public Internals::JsonPrintable, + public Internals::ReferenceType, + public Internals::NonCopyable, + public Internals::List, + public Internals::JsonBufferAllocated { + public: + // Create an empty JsonArray attached to the specified JsonBuffer. + // You should not call this constructor directly. + // Instead, use JsonBuffer::createArray() or JsonBuffer::parseArray(). + explicit JsonArray(JsonBuffer *buffer) throw() + : Internals::List(buffer) {} + + // Gets the value at the specified index + const Internals::JsonArraySubscript operator[](size_t index) const; + + // Gets or sets the value at specified index + Internals::JsonArraySubscript operator[](size_t index); + + // Adds the specified value at the end of the array. + // + // bool add(TValue); + // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, + // std::string, String, JsonArray, JsonObject + template + bool add(const T &value) { + return add_impl(value); + } + // + // bool add(TValue); + // TValue = char*, const char*, const FlashStringHelper* + template + bool add(T *value) { + return add_impl(value); + } + // + // bool add(TValue value, uint8_t decimals); + // TValue = float, double + template + DEPRECATED("Second argument is not supported anymore") + bool add(T value, uint8_t) { + return add_impl(JsonVariant(value)); + } + + // Sets the value at specified index. + // + // bool add(size_t index, const TValue&); + // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, + // std::string, String, JsonArray, JsonObject + template + bool set(size_t index, const T &value) { + return set_impl(index, value); + } + // + // bool add(size_t index, TValue); + // TValue = char*, const char*, const FlashStringHelper* + template + bool set(size_t index, T *value) { + return set_impl(index, value); + } + // + // bool set(size_t index, TValue value, uint8_t decimals); + // TValue = float, double + template + typename Internals::EnableIf::value, bool>::type + set(size_t index, T value, uint8_t decimals) { + return set_impl(index, JsonVariant(value, decimals)); + } + + // Gets the value at the specified index. + template + typename Internals::JsonVariantAs::type get(size_t index) const { + const_iterator it = begin() += index; + return it != end() ? it->as() : Internals::JsonVariantDefault::get(); + } + + // Check the type of the value at specified index. + template + bool is(size_t index) const { + const_iterator it = begin() += index; + return it != end() ? it->is() : false; + } + + // Creates a JsonArray and adds a reference at the end of the array. + // It's a shortcut for JsonBuffer::createArray() and JsonArray::add() + JsonArray &createNestedArray(); + + // Creates a JsonObject and adds a reference at the end of the array. + // It's a shortcut for JsonBuffer::createObject() and JsonArray::add() + JsonObject &createNestedObject(); + + // Removes element at specified index. + void remove(size_t index) { + remove(begin() += index); + } + using Internals::List::remove; + + // Returns a reference an invalid JsonArray. + // This object is meant to replace a NULL pointer. + // This is used when memory allocation or JSON parsing fail. + static JsonArray &invalid() { + static JsonArray instance(NULL); + return instance; + } + + // Imports a 1D array + template + bool copyFrom(T (&array)[TN]) { + return copyFrom(array, TN); + } + + // Imports a 1D array + template + bool copyFrom(T *array, size_t len) { + bool ok = true; + for (size_t i = 0; i < len; i++) { + ok &= add(array[i]); + } + return ok; + } + + // Imports a 2D array + template + bool copyFrom(T (&array)[N1][N2]) { + bool ok = true; + for (size_t i = 0; i < N1; i++) { + JsonArray &nestedArray = createNestedArray(); + for (size_t j = 0; j < N2; j++) { + ok &= nestedArray.add(array[i][j]); + } + } + return ok; + } + + // Exports a 1D array + template + size_t copyTo(T (&array)[TN]) const { + return copyTo(array, TN); + } + + // Exports a 1D array + template + size_t copyTo(T *array, size_t len) const { + size_t i = 0; + for (const_iterator it = begin(); it != end() && i < len; ++it) + array[i++] = *it; + return i; + } + + // Exports a 2D array + template + void copyTo(T (&array)[N1][N2]) const { + size_t i = 0; + for (const_iterator it = begin(); it != end() && i < N1; ++it) { + it->as().copyTo(array[i++]); + } + } + +#if ARDUINOJSON_ENABLE_DEPRECATED + DEPRECATED("use remove() instead") + FORCE_INLINE void removeAt(size_t index) { + return remove(index); + } +#endif + + private: + template + bool set_impl(size_t index, TValueRef value) { + iterator it = begin() += index; + if (it == end()) return false; + return Internals::ValueSaver::save(_buffer, *it, value); + } + + template + bool add_impl(TValueRef value) { + iterator it = Internals::List::add(); + if (it == end()) return false; + return Internals::ValueSaver::save(_buffer, *it, value); + } +}; + +namespace Internals { +template <> +struct JsonVariantDefault { + static JsonArray &get() { + return JsonArray::invalid(); + } +}; +} +} diff --git a/include/lib/ArduinoJson/JsonArrayImpl.hpp b/include/lib/ArduinoJson/JsonArrayImpl.hpp new file mode 100644 index 0000000..924b7ea --- /dev/null +++ b/include/lib/ArduinoJson/JsonArrayImpl.hpp @@ -0,0 +1,26 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "JsonArray.hpp" +#include "JsonArraySubscript.hpp" +#include "JsonObject.hpp" + +namespace ArduinoJson { + +inline JsonArray &JsonArray::createNestedArray() { + if (!_buffer) return JsonArray::invalid(); + JsonArray &array = _buffer->createArray(); + add(array); + return array; +} + +inline JsonObject &JsonArray::createNestedObject() { + if (!_buffer) return JsonObject::invalid(); + JsonObject &object = _buffer->createObject(); + add(object); + return object; +} +} diff --git a/include/lib/ArduinoJson/JsonArraySubscript.hpp b/include/lib/ArduinoJson/JsonArraySubscript.hpp new file mode 100644 index 0000000..afb4dc1 --- /dev/null +++ b/include/lib/ArduinoJson/JsonArraySubscript.hpp @@ -0,0 +1,122 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Configuration.hpp" +#include "JsonVariantBase.hpp" + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4522) +#endif + +namespace ArduinoJson { +namespace Internals { +class JsonArraySubscript : public JsonVariantBase { + public: + FORCE_INLINE JsonArraySubscript(JsonArray& array, size_t index) + : _array(array), _index(index) {} + + FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& src) { + _array.set(_index, src); + return *this; + } + + // Replaces the value + // + // operator=(const TValue&) + // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, + // std::string, String, JsonArray, JsonObject + template + FORCE_INLINE JsonArraySubscript& operator=(const T& src) { + _array.set(_index, src); + return *this; + } + // + // operator=(TValue) + // TValue = char*, const char*, const FlashStringHelper* + template + FORCE_INLINE JsonArraySubscript& operator=(T* src) { + _array.set(_index, src); + return *this; + } + + FORCE_INLINE bool success() const { + return _index < _array.size(); + } + + template + FORCE_INLINE typename JsonVariantAs::type as() const { + return _array.get(_index); + } + + template + FORCE_INLINE bool is() const { + return _array.is(_index); + } + + // Replaces the value + // + // bool set(const TValue&) + // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, + // std::string, String, JsonArray, JsonObject + template + FORCE_INLINE bool set(const TValue& value) { + return _array.set(_index, value); + } + // + // bool set(TValue) + // TValue = char*, const char*, const FlashStringHelper* + template + FORCE_INLINE bool set(TValue* value) { + return _array.set(_index, value); + } + // + // bool set(TValue, uint8_t decimals); + // TValue = float, double + template + DEPRECATED("Second argument is not supported anymore") + FORCE_INLINE bool set(const TValue& value, uint8_t) { + return _array.set(_index, value); + } + + private: + JsonArray& _array; + const size_t _index; +}; + +template +inline JsonArraySubscript JsonVariantSubscripts::operator[]( + size_t index) { + return impl()->template as()[index]; +} + +template +inline const JsonArraySubscript JsonVariantSubscripts::operator[]( + size_t index) const { + return impl()->template as()[index]; +} + +#if ARDUINOJSON_ENABLE_STD_STREAM +inline std::ostream& operator<<(std::ostream& os, + const JsonArraySubscript& source) { + return source.printTo(os); +} +#endif +} + +inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) { + return Internals::JsonArraySubscript(*this, index); +} + +inline const Internals::JsonArraySubscript JsonArray::operator[]( + size_t index) const { + return Internals::JsonArraySubscript(*const_cast(this), index); +} +} + +#ifdef _MSC_VER +#pragma warning(pop) +#endif diff --git a/include/lib/ArduinoJson/JsonBuffer.hpp b/include/lib/ArduinoJson/JsonBuffer.hpp new file mode 100644 index 0000000..26101e0 --- /dev/null +++ b/include/lib/ArduinoJson/JsonBuffer.hpp @@ -0,0 +1,78 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include // for size_t +#include // for uint8_t +#include + +#include "Data/NonCopyable.hpp" +#include "JsonVariant.hpp" +#include "TypeTraits/EnableIf.hpp" +#include "TypeTraits/IsArray.hpp" + +namespace ArduinoJson { +class JsonArray; +class JsonObject; + +// Entry point for using the library. +// +// Handle the memory management (done in derived classes) and calls the parser. +// This abstract class is implemented by StaticJsonBuffer which implements a +// fixed memory allocation. +class JsonBuffer : Internals::NonCopyable { + public: + // Allocates an empty JsonArray. + // + // Returns a reference to the new JsonArray or JsonArray::invalid() if the + // allocation fails. + JsonArray &createArray(); + + // Allocates an empty JsonObject. + // + // Returns a reference to the new JsonObject or JsonObject::invalid() if the + // allocation fails. + JsonObject &createObject(); + + // Duplicates a string + // + // const char* strdup(TValue); + // TValue = const std::string&, const String&, + template + DEPRECATED("char* are duplicated, you don't need strdup() anymore") + typename Internals::EnableIf::value, + const char *>::type strdup(const TString &src) { + return Internals::StringTraits::duplicate(src, this); + } + // + // const char* strdup(TValue); + // TValue = char*, const char*, const FlashStringHelper* + template + DEPRECATED("char* are duplicated, you don't need strdup() anymore") + const char *strdup(TString *src) { + return Internals::StringTraits::duplicate(src, this); + } + + // Allocates n bytes in the JsonBuffer. + // Return a pointer to the allocated memory or NULL if allocation fails. + virtual void *alloc(size_t size) = 0; + + protected: + // CAUTION: NO VIRTUAL DESTRUCTOR! + // If we add a virtual constructor the Arduino compiler will add malloc() + // and free() to the binary, adding 706 useless bytes. + ~JsonBuffer() {} + + // Preserve aligment if necessary + static FORCE_INLINE size_t round_size_up(size_t bytes) { +#if ARDUINOJSON_ENABLE_ALIGNMENT + const size_t x = sizeof(void *) - 1; + return (bytes + x) & ~x; +#else + return bytes; +#endif + } +}; +} diff --git a/include/lib/ArduinoJson/JsonBufferBase.hpp b/include/lib/ArduinoJson/JsonBufferBase.hpp new file mode 100644 index 0000000..1e771bf --- /dev/null +++ b/include/lib/ArduinoJson/JsonBufferBase.hpp @@ -0,0 +1,127 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Deserialization/JsonParser.hpp" + +namespace ArduinoJson { +namespace Internals { +template +class JsonBufferBase : public JsonBuffer { + public: + // Allocates and populate a JsonArray from a JSON string. + // + // The First argument is a pointer to the JSON string, the memory must be + // writable + // because the parser will insert null-terminators and replace escaped chars. + // + // The second argument set the nesting limit + // + // Returns a reference to the new JsonObject or JsonObject::invalid() if the + // allocation fails. + // With this overload, the JsonBuffer will make a copy of the string + // + // JsonArray& parseArray(TString); + // TString = const std::string&, const String& + template + typename Internals::EnableIf::value, + JsonArray &>::type + parseArray(const TString &json, + uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { + return Internals::makeParser(that(), json, nestingLimit).parseArray(); + } + // + // JsonArray& parseArray(TString); + // TString = const char*, const char[N], const FlashStringHelper* + template + JsonArray &parseArray( + TString *json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { + return Internals::makeParser(that(), json, nestingLimit).parseArray(); + } + // + // JsonArray& parseArray(TString); + // TString = std::istream&, Stream& + template + JsonArray &parseArray( + TString &json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { + return Internals::makeParser(that(), json, nestingLimit).parseArray(); + } + + // Allocates and populate a JsonObject from a JSON string. + // + // The First argument is a pointer to the JSON string, the memory must be + // writable + // because the parser will insert null-terminators and replace escaped chars. + // + // The second argument set the nesting limit + // + // Returns a reference to the new JsonObject or JsonObject::invalid() if the + // allocation fails. + // + // JsonObject& parseObject(TString); + // TString = const std::string&, const String& + template + typename Internals::EnableIf::value, + JsonObject &>::type + parseObject(const TString &json, + uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { + return Internals::makeParser(that(), json, nestingLimit).parseObject(); + } + // + // JsonObject& parseObject(TString); + // TString = const char*, const char[N], const FlashStringHelper* + template + JsonObject &parseObject( + TString *json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { + return Internals::makeParser(that(), json, nestingLimit).parseObject(); + } + // + // JsonObject& parseObject(TString); + // TString = std::istream&, Stream& + template + JsonObject &parseObject( + TString &json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { + return Internals::makeParser(that(), json, nestingLimit).parseObject(); + } + + // Generalized version of parseArray() and parseObject(), also works for + // integral types. + // + // JsonVariant parse(TString); + // TString = const std::string&, const String& + template + typename Internals::EnableIf::value, + JsonVariant>::type + parse(const TString &json, + uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { + return Internals::makeParser(that(), json, nestingLimit).parseVariant(); + } + // + // JsonVariant parse(TString); + // TString = const char*, const char[N], const FlashStringHelper* + template + JsonVariant parse(TString *json, + uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { + return Internals::makeParser(that(), json, nestingLimit).parseVariant(); + } + // + // JsonVariant parse(TString); + // TString = std::istream&, Stream& + template + JsonVariant parse(TString &json, + uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { + return Internals::makeParser(that(), json, nestingLimit).parseVariant(); + } + + protected: + ~JsonBufferBase() {} + + private: + TDerived *that() { + return static_cast(this); + } +}; +} +} diff --git a/include/lib/ArduinoJson/JsonBufferImpl.hpp b/include/lib/ArduinoJson/JsonBufferImpl.hpp new file mode 100644 index 0000000..cdea374 --- /dev/null +++ b/include/lib/ArduinoJson/JsonBufferImpl.hpp @@ -0,0 +1,17 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Deserialization/JsonParser.hpp" + +inline ArduinoJson::JsonArray &ArduinoJson::JsonBuffer::createArray() { + JsonArray *ptr = new (this) JsonArray(this); + return ptr ? *ptr : JsonArray::invalid(); +} + +inline ArduinoJson::JsonObject &ArduinoJson::JsonBuffer::createObject() { + JsonObject *ptr = new (this) JsonObject(this); + return ptr ? *ptr : JsonObject::invalid(); +} diff --git a/include/lib/ArduinoJson/JsonObject.hpp b/include/lib/ArduinoJson/JsonObject.hpp new file mode 100644 index 0000000..caf698a --- /dev/null +++ b/include/lib/ArduinoJson/JsonObject.hpp @@ -0,0 +1,328 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Data/JsonBufferAllocated.hpp" +#include "Data/List.hpp" +#include "Data/ReferenceType.hpp" +#include "Data/ValueSaver.hpp" +#include "JsonPair.hpp" +#include "Serialization/JsonPrintable.hpp" +#include "StringTraits/StringTraits.hpp" +#include "TypeTraits/EnableIf.hpp" +#include "TypeTraits/IsArray.hpp" +#include "TypeTraits/IsFloatingPoint.hpp" +#include "TypeTraits/IsSame.hpp" + +// Returns the size (in bytes) of an object with n elements. +// Can be very handy to determine the size of a StaticJsonBuffer. +#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \ + (sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type)) + +namespace ArduinoJson { + +// Forward declarations +class JsonArray; +class JsonBuffer; +namespace Internals { +template +class JsonObjectSubscript; +} + +// A dictionary of JsonVariant indexed by string (char*) +// +// The constructor is private, instances must be created via +// JsonBuffer::createObject() or JsonBuffer::parseObject(). +// A JsonObject can be serialized to a JSON string via JsonObject::printTo(). +// It can also be deserialized from a JSON string via JsonBuffer::parseObject(). +class JsonObject : public Internals::JsonPrintable, + public Internals::ReferenceType, + public Internals::NonCopyable, + public Internals::List, + public Internals::JsonBufferAllocated { + public: + // Create an empty JsonArray attached to the specified JsonBuffer. + // You should not use this constructor directly. + // Instead, use JsonBuffer::createObject() or JsonBuffer.parseObject(). + explicit JsonObject(JsonBuffer* buffer) throw() + : Internals::List(buffer) {} + + // Gets or sets the value associated with the specified key. + // + // JsonObjectSubscript operator[](TKey) + // TKey = const std::string&, const String& + template + Internals::JsonObjectSubscript operator[]( + const TString& key) { + return Internals::JsonObjectSubscript(*this, key); + } + // + // JsonObjectSubscript operator[](TKey) + // TKey = char*, const char*, char[], const char[N], const FlashStringHelper* + template + Internals::JsonObjectSubscript operator[](TString* key) { + return Internals::JsonObjectSubscript(*this, key); + } + + // Gets the value associated with the specified key. + // + // const JsonObjectSubscript operator[](TKey) const; + // TKey = const std::string&, const String& + template + const Internals::JsonObjectSubscript operator[]( + const TString& key) const { + return Internals::JsonObjectSubscript( + *const_cast(this), key); + } + // + // const JsonObjectSubscript operator[](TKey) const; + // TKey = const char*, const char[N], const FlashStringHelper* + template + const Internals::JsonObjectSubscript operator[]( + TString* key) const { + return Internals::JsonObjectSubscript( + *const_cast(this), key); + } + + // Sets the specified key with the specified value. + // + // bool set(TKey, TValue); + // TKey = const std::string&, const String& + // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, + // std::string, String, JsonArray, JsonObject + template + bool set(const TString& key, const TValue& value) { + return set_impl(key, value); + } + // + // bool set(TKey, TValue); + // TKey = const std::string&, const String& + // TValue = char*, const char*, const FlashStringHelper* + template + bool set(const TString& key, TValue* value) { + return set_impl(key, value); + } + // + // bool set(TKey, const TValue&); + // TKey = char*, const char*, const FlashStringHelper* + // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, + // std::string, String, JsonArray, JsonObject + template + bool set(TString* key, const TValue& value) { + return set_impl(key, value); + } + // + // bool set(TKey, TValue); + // TKey = char*, const char*, const FlashStringHelper* + // TValue = char*, const char*, const FlashStringHelper* + template + bool set(TString* key, TValue* value) { + return set_impl(key, value); + } + // + // bool set(TKey, TValue, uint8_t decimals); + // TKey = const std::string&, const String& + // TValue = float, double + template + DEPRECATED("Second argument is not supported anymore") + typename Internals::EnableIf::value, + bool>::type + set(const TString& key, TValue value, uint8_t) { + return set_impl(key, + JsonVariant(value)); + } + // + // bool set(TKey, TValue, uint8_t decimals); + // TKey = char*, const char*, const FlashStringHelper* + // TValue = float, double + template + DEPRECATED("Second argument is not supported anymore") + typename Internals::EnableIf::value, + bool>::type + set(TString* key, TValue value, uint8_t) { + return set_impl(key, JsonVariant(value)); + } + + // Gets the value associated with the specified key. + // + // TValue get(TKey) const; + // TKey = const std::string&, const String& + // TValue = bool, char, long, int, short, float, double, + // std::string, String, JsonArray, JsonObject + template + typename Internals::JsonVariantAs::type get( + const TString& key) const { + return get_impl(key); + } + // + // TValue get(TKey) const; + // TKey = char*, const char*, const FlashStringHelper* + // TValue = bool, char, long, int, short, float, double, + // std::string, String, JsonArray, JsonObject + template + typename Internals::JsonVariantAs::type get(TString* key) const { + return get_impl(key); + } + + // Checks the type of the value associated with the specified key. + // + // + // bool is(TKey) const; + // TKey = const std::string&, const String& + // TValue = bool, char, long, int, short, float, double, + // std::string, String, JsonArray, JsonObject + template + bool is(const TString& key) const { + return is_impl(key); + } + // + // bool is(TKey) const; + // TKey = char*, const char*, const FlashStringHelper* + // TValue = bool, char, long, int, short, float, double, + // std::string, String, JsonArray, JsonObject + template + bool is(TString* key) const { + return is_impl(key); + } + + // Creates and adds a JsonArray. + // + // JsonArray& createNestedArray(TKey); + // TKey = const std::string&, const String& + template + JsonArray& createNestedArray(const TString& key) { + return createNestedArray_impl(key); + } + // JsonArray& createNestedArray(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + JsonArray& createNestedArray(TString* key) { + return createNestedArray_impl(key); + } + + // Creates and adds a JsonObject. + // + // JsonObject& createNestedObject(TKey); + // TKey = const std::string&, const String& + template + JsonObject& createNestedObject(const TString& key) { + return createNestedObject_impl(key); + } + // + // JsonObject& createNestedObject(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + JsonObject& createNestedObject(TString* key) { + return createNestedObject_impl(key); + } + + // Tells weither the specified key is present and associated with a value. + // + // bool containsKey(TKey); + // TKey = const std::string&, const String& + template + bool containsKey(const TString& key) const { + return findKey(key) != end(); + } + // + // bool containsKey(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + bool containsKey(TString* key) const { + return findKey(key) != end(); + } + + // Removes the specified key and the associated value. + // + // void remove(TKey); + // TKey = const std::string&, const String& + template + void remove(const TString& key) { + remove(findKey(key)); + } + // + // void remove(TKey); + // TKey = char*, const char*, char[], const char[], const FlashStringHelper* + template + void remove(TString* key) { + remove(findKey(key)); + } + // + // void remove(iterator) + using Internals::List::remove; + + // Returns a reference an invalid JsonObject. + // This object is meant to replace a NULL pointer. + // This is used when memory allocation or JSON parsing fail. + static JsonObject& invalid() { + static JsonObject instance(NULL); + return instance; + } + + private: + // Returns the list node that matches the specified key. + template + iterator findKey(TStringRef key) { + iterator it; + for (it = begin(); it != end(); ++it) { + if (Internals::StringTraits::equals(key, it->key)) break; + } + return it; + } + template + const_iterator findKey(TStringRef key) const { + return const_cast(this)->findKey(key); + } + + template + typename Internals::JsonVariantAs::type get_impl( + TStringRef key) const { + const_iterator it = findKey(key); + return it != end() ? it->value.as() + : Internals::JsonVariantDefault::get(); + } + + template + bool set_impl(TStringRef key, TValueRef value) { + // ignore null key + if (Internals::StringTraits::is_null(key)) return false; + + // search a matching key + iterator it = findKey(key); + if (it == end()) { + // add the key + it = Internals::List::add(); + if (it == end()) return false; + bool key_ok = + Internals::ValueSaver::save(_buffer, it->key, key); + if (!key_ok) return false; + } + + // save the value + return Internals::ValueSaver::save(_buffer, it->value, value); + } + + template + bool is_impl(TStringRef key) const { + const_iterator it = findKey(key); + return it != end() ? it->value.is() : false; + } + + template + JsonArray& createNestedArray_impl(TStringRef key); + + template + JsonObject& createNestedObject_impl(TStringRef key); +}; + +namespace Internals { +template <> +struct JsonVariantDefault { + static JsonObject& get() { + return JsonObject::invalid(); + } +}; +} // namespace Internals +} // namespace ArduinoJson diff --git a/include/lib/ArduinoJson/JsonObjectImpl.hpp b/include/lib/ArduinoJson/JsonObjectImpl.hpp new file mode 100644 index 0000000..e7689b5 --- /dev/null +++ b/include/lib/ArduinoJson/JsonObjectImpl.hpp @@ -0,0 +1,28 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "JsonArray.hpp" +#include "JsonObject.hpp" +#include "JsonObjectSubscript.hpp" + +namespace ArduinoJson { + +template +inline JsonArray &JsonObject::createNestedArray_impl(TStringRef key) { + if (!_buffer) return JsonArray::invalid(); + JsonArray &array = _buffer->createArray(); + set(key, array); + return array; +} + +template +inline JsonObject &JsonObject::createNestedObject_impl(TStringRef key) { + if (!_buffer) return JsonObject::invalid(); + JsonObject &object = _buffer->createObject(); + set(key, object); + return object; +} +} diff --git a/include/lib/ArduinoJson/JsonObjectSubscript.hpp b/include/lib/ArduinoJson/JsonObjectSubscript.hpp new file mode 100644 index 0000000..6ac4763 --- /dev/null +++ b/include/lib/ArduinoJson/JsonObjectSubscript.hpp @@ -0,0 +1,110 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Configuration.hpp" +#include "JsonVariantBase.hpp" +#include "TypeTraits/EnableIf.hpp" + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4522) +#endif + +namespace ArduinoJson { +namespace Internals { + +template +class JsonObjectSubscript + : public JsonVariantBase > { + typedef JsonObjectSubscript this_type; + + public: + FORCE_INLINE JsonObjectSubscript(JsonObject& object, TStringRef key) + : _object(object), _key(key) {} + + FORCE_INLINE this_type& operator=(const this_type& src) { + _object.set(_key, src); + return *this; + } + + // Set the specified value + // + // operator=(const TValue&); + // TValue = bool, char, long, int, short, float, double, + // std::string, String, JsonArray, JsonObject + template + FORCE_INLINE typename EnableIf::value, this_type&>::type + operator=(const TValue& src) { + _object.set(_key, src); + return *this; + } + // + // operator=(TValue); + // TValue = char*, const char*, const FlashStringHelper* + template + FORCE_INLINE this_type& operator=(TValue* src) { + _object.set(_key, src); + return *this; + } + + FORCE_INLINE bool success() const { + return _object.containsKey(_key); + } + + template + FORCE_INLINE typename JsonVariantAs::type as() const { + return _object.get(_key); + } + + template + FORCE_INLINE bool is() const { + return _object.is(_key); + } + + // Sets the specified value. + // + // bool set(const TValue&); + // TValue = bool, char, long, int, short, float, double, RawJson, JsonVariant, + // std::string, String, JsonArray, JsonObject + template + FORCE_INLINE typename EnableIf::value, bool>::type set( + const TValue& value) { + return _object.set(_key, value); + } + // + // bool set(TValue); + // TValue = char*, const char, const FlashStringHelper* + template + FORCE_INLINE bool set(const TValue* value) { + return _object.set(_key, value); + } + // + // bool set(TValue, uint8_t decimals); + // TValue = float, double + template + DEPRECATED("Second argument is not supported anymore") + FORCE_INLINE bool set(const TValue& value, uint8_t) { + return _object.set(_key, value); + } + + private: + JsonObject& _object; + TStringRef _key; +}; + +#if ARDUINOJSON_ENABLE_STD_STREAM +template +inline std::ostream& operator<<(std::ostream& os, + const JsonObjectSubscript& source) { + return source.printTo(os); +} +#endif +} +} + +#ifdef _MSC_VER +#pragma warning(pop) +#endif diff --git a/include/lib/ArduinoJson/JsonPair.hpp b/include/lib/ArduinoJson/JsonPair.hpp new file mode 100644 index 0000000..4172430 --- /dev/null +++ b/include/lib/ArduinoJson/JsonPair.hpp @@ -0,0 +1,16 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "JsonVariant.hpp" + +namespace ArduinoJson { + +// A key value pair for JsonObject. +struct JsonPair { + const char* key; + JsonVariant value; +}; +} diff --git a/include/lib/ArduinoJson/JsonVariant.hpp b/include/lib/ArduinoJson/JsonVariant.hpp new file mode 100644 index 0000000..8326cbe --- /dev/null +++ b/include/lib/ArduinoJson/JsonVariant.hpp @@ -0,0 +1,355 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include +#include // for uint8_t + +#include "Data/JsonVariantContent.hpp" +#include "Data/JsonVariantDefault.hpp" +#include "Data/JsonVariantType.hpp" +#include "JsonVariantBase.hpp" +#include "RawJson.hpp" +#include "Serialization/JsonPrintable.hpp" +#include "TypeTraits/EnableIf.hpp" +#include "TypeTraits/IsChar.hpp" +#include "TypeTraits/IsFloatingPoint.hpp" +#include "TypeTraits/IsIntegral.hpp" +#include "TypeTraits/IsSame.hpp" +#include "TypeTraits/IsSignedIntegral.hpp" +#include "TypeTraits/IsUnsignedIntegral.hpp" +#include "TypeTraits/RemoveConst.hpp" +#include "TypeTraits/RemoveReference.hpp" + +namespace ArduinoJson { + +// Forward declarations. +class JsonArray; +class JsonObject; + +// A variant that can be a any value serializable to a JSON value. +// +// It can be set to: +// - a boolean +// - a char, short, int or a long (signed or unsigned) +// - a string (const char*) +// - a reference to a JsonArray or JsonObject +class JsonVariant : public Internals::JsonVariantBase { + template + friend class Internals::JsonSerializer; + + public: + // Creates an uninitialized JsonVariant + JsonVariant() : _type(Internals::JSON_UNDEFINED) {} + + // Create a JsonVariant containing a boolean value. + // It will be serialized as "true" or "false" in JSON. + JsonVariant(bool value) { + using namespace Internals; + _type = JSON_BOOLEAN; + _content.asInteger = static_cast(value); + } + + // Create a JsonVariant containing a floating point value. + // JsonVariant(double value); + // JsonVariant(float value); + template + JsonVariant(T value, typename Internals::EnableIf< + Internals::IsFloatingPoint::value>::type * = 0) { + using namespace Internals; + _type = JSON_FLOAT; + _content.asFloat = static_cast(value); + } + template + DEPRECATED("Second argument is not supported anymore") + JsonVariant(T value, uint8_t, + typename Internals::EnableIf< + Internals::IsFloatingPoint::value>::type * = 0) { + using namespace Internals; + _type = JSON_FLOAT; + _content.asFloat = static_cast(value); + } + + // Create a JsonVariant containing an integer value. + // JsonVariant(char) + // JsonVariant(signed short) + // JsonVariant(signed int) + // JsonVariant(signed long) + // JsonVariant(signed char) + template + JsonVariant( + T value, + typename Internals::EnableIf::value || + Internals::IsSame::value>::type * = + 0) { + using namespace Internals; + if (value >= 0) { + _type = JSON_POSITIVE_INTEGER; + _content.asInteger = static_cast(value); + } else { + _type = JSON_NEGATIVE_INTEGER; + _content.asInteger = static_cast(-value); + } + } + // JsonVariant(unsigned short) + // JsonVariant(unsigned int) + // JsonVariant(unsigned long) + template + JsonVariant(T value, + typename Internals::EnableIf< + Internals::IsUnsignedIntegral::value>::type * = 0) { + using namespace Internals; + _type = JSON_POSITIVE_INTEGER; + _content.asInteger = static_cast(value); + } + + // Create a JsonVariant containing a string. + // JsonVariant(const char*); + // JsonVariant(const signed char*); + // JsonVariant(const unsigned char*); + template + JsonVariant( + const TChar *value, + typename Internals::EnableIf::value>::type * = + 0) { + _type = Internals::JSON_STRING; + _content.asString = reinterpret_cast(value); + } + + // Create a JsonVariant containing an unparsed string + JsonVariant(Internals::RawJsonString value) { + _type = Internals::JSON_UNPARSED; + _content.asString = value; + } + + // Create a JsonVariant containing a reference to an array. + // CAUTION: we are lying about constness, because the array can be modified if + // the variant is converted back to a JsonArray& + JsonVariant(const JsonArray &array); + + // Create a JsonVariant containing a reference to an object. + // CAUTION: we are lying about constness, because the object can be modified + // if the variant is converted back to a JsonObject& + JsonVariant(const JsonObject &object); + + // Get the variant as the specified type. + // + // char as() const; + // signed char as() const; + // signed short as() const; + // signed int as() const; + // signed long as() const; + // unsigned char as() const; + // unsigned short as() const; + // unsigned int as() const; + // unsigned long as() const; + template + const typename Internals::EnableIf::value, T>::type + as() const { + return variantAsInteger(); + } + // bool as() const + template + const typename Internals::EnableIf::value, T>::type + as() const { + return variantAsInteger() != 0; + } + // + // double as() const; + // float as() const; + template + const typename Internals::EnableIf::value, + T>::type + as() const { + return variantAsFloat(); + } + // + // const char* as() const; + // const char* as() const; + template + typename Internals::EnableIf::value || + Internals::IsSame::value, + const char *>::type + as() const { + return variantAsString(); + } + // + // std::string as() const; + // String as() const; + template + typename Internals::EnableIf::has_append, T>::type + as() const { + const char *cstr = variantAsString(); + if (cstr) return T(cstr); + T s; + printTo(s); + return s; + } + // + // JsonArray& as const; + // JsonArray& as const; + template + typename Internals::EnableIf< + Internals::IsSame::type, + JsonArray>::value, + JsonArray &>::type + as() const { + return variantAsArray(); + } + // + // const JsonArray& as const; + template + typename Internals::EnableIf< + Internals::IsSame::type, + const JsonArray>::value, + const JsonArray &>::type + as() const { + return variantAsArray(); + } + // + // JsonObject& as const; + // JsonObject& as const; + template + typename Internals::EnableIf< + Internals::IsSame::type, + JsonObject>::value, + JsonObject &>::type + as() const { + return variantAsObject(); + } + // + // JsonObject& as const; + // JsonObject& as const; + template + typename Internals::EnableIf< + Internals::IsSame::type, + const JsonObject>::value, + const JsonObject &>::type + as() const { + return variantAsObject(); + } + // + // JsonVariant as const; + template + typename Internals::EnableIf::value, + T>::type + as() const { + return *this; + } + + // Tells weither the variant has the specified type. + // Returns true if the variant has type type T, false otherwise. + // + // bool is() const; + // bool is() const; + // bool is() const; + // bool is() const; + // bool is() const; + // bool is() const; + // bool is() const; + // bool is() const; + // bool is() const; + template + typename Internals::EnableIf::value, bool>::type is() + const { + return variantIsInteger(); + } + // + // bool is() const; + // bool is() const; + template + typename Internals::EnableIf::value, bool>::type + is() const { + return variantIsFloat(); + } + // + // bool is() const + template + typename Internals::EnableIf::value, bool>::type + is() const { + return variantIsBoolean(); + } + // + // bool is() const; + // bool is() const; + template + typename Internals::EnableIf::value || + Internals::IsSame::value, + bool>::type + is() const { + return variantIsString(); + } + // + // bool is const; + // bool is const; + // bool is const; + template + typename Internals::EnableIf< + Internals::IsSame::type>::type, + JsonArray>::value, + bool>::type + is() const { + return variantIsArray(); + } + // + // bool is const; + // bool is const; + // bool is const; + template + typename Internals::EnableIf< + Internals::IsSame::type>::type, + JsonObject>::value, + bool>::type + is() const { + return variantIsObject(); + } + + // Returns true if the variant has a value + bool success() const { + return _type != Internals::JSON_UNDEFINED; + } + + private: + JsonArray &variantAsArray() const; + JsonObject &variantAsObject() const; + const char *variantAsString() const; + template + T variantAsFloat() const; + template + T variantAsInteger() const; + bool variantIsBoolean() const; + bool variantIsFloat() const; + bool variantIsInteger() const; + bool variantIsArray() const { + return _type == Internals::JSON_ARRAY; + } + bool variantIsObject() const { + return _type == Internals::JSON_OBJECT; + } + bool variantIsString() const { + return _type == Internals::JSON_STRING || + (_type == Internals::JSON_UNPARSED && _content.asString && + !strcmp("null", _content.asString)); + } + + // The current type of the variant + Internals::JsonVariantType _type; + + // The various alternatives for the value of the variant. + Internals::JsonVariantContent _content; +}; + +DEPRECATED("Decimal places are ignored, use the float value instead") +inline JsonVariant float_with_n_digits(float value, uint8_t) { + return JsonVariant(value); +} + +DEPRECATED("Decimal places are ignored, use the double value instead") +inline JsonVariant double_with_n_digits(double value, uint8_t) { + return JsonVariant(value); +} +} diff --git a/include/lib/ArduinoJson/JsonVariantBase.hpp b/include/lib/ArduinoJson/JsonVariantBase.hpp new file mode 100644 index 0000000..44acf2e --- /dev/null +++ b/include/lib/ArduinoJson/JsonVariantBase.hpp @@ -0,0 +1,24 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "JsonVariantCasts.hpp" +#include "JsonVariantComparisons.hpp" +#include "JsonVariantOr.hpp" +#include "JsonVariantSubscripts.hpp" +#include "Serialization/JsonPrintable.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +class JsonVariantBase : public JsonPrintable, + public JsonVariantCasts, + public JsonVariantComparisons, + public JsonVariantOr, + public JsonVariantSubscripts, + public JsonVariantTag {}; +} +} diff --git a/include/lib/ArduinoJson/JsonVariantCasts.hpp b/include/lib/ArduinoJson/JsonVariantCasts.hpp new file mode 100644 index 0000000..68f5bd7 --- /dev/null +++ b/include/lib/ArduinoJson/JsonVariantCasts.hpp @@ -0,0 +1,59 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Data/JsonVariantAs.hpp" +#include "Polyfills/attributes.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +class JsonVariantCasts { + public: +#if ARDUINOJSON_ENABLE_DEPRECATED + DEPRECATED("use as() instead") + FORCE_INLINE JsonArray &asArray() const { + return impl()->template as(); + } + + DEPRECATED("use as() instead") + FORCE_INLINE JsonObject &asObject() const { + return impl()->template as(); + } + + DEPRECATED("use as() instead") + FORCE_INLINE const char *asString() const { + return impl()->template as(); + } +#endif + + // Gets the variant as an array. + // Returns a reference to the JsonArray or JsonArray::invalid() if the + // variant + // is not an array. + FORCE_INLINE operator JsonArray &() const { + return impl()->template as(); + } + + // Gets the variant as an object. + // Returns a reference to the JsonObject or JsonObject::invalid() if the + // variant is not an object. + FORCE_INLINE operator JsonObject &() const { + return impl()->template as(); + } + + template + FORCE_INLINE operator T() const { + return impl()->template as(); + } + + private: + const TImpl *impl() const { + return static_cast(this); + } +}; +} +} diff --git a/include/lib/ArduinoJson/JsonVariantComparisons.hpp b/include/lib/ArduinoJson/JsonVariantComparisons.hpp new file mode 100644 index 0000000..47f9d63 --- /dev/null +++ b/include/lib/ArduinoJson/JsonVariantComparisons.hpp @@ -0,0 +1,139 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "StringTraits/StringTraits.hpp" +#include "TypeTraits/EnableIf.hpp" +#include "TypeTraits/IsVariant.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +class JsonVariantComparisons { + public: + template + friend bool operator==(const JsonVariantComparisons &variant, + TComparand comparand) { + return variant.equals(comparand); + } + + template + friend typename EnableIf::value, bool>::type + operator==(TComparand comparand, const JsonVariantComparisons &variant) { + return variant.equals(comparand); + } + + template + friend bool operator!=(const JsonVariantComparisons &variant, + TComparand comparand) { + return !variant.equals(comparand); + } + + template + friend typename EnableIf::value, bool>::type + operator!=(TComparand comparand, const JsonVariantComparisons &variant) { + return !variant.equals(comparand); + } + + template + friend bool operator<=(const JsonVariantComparisons &left, TComparand right) { + return left.as() <= right; + } + + template + friend bool operator<=(TComparand comparand, + const JsonVariantComparisons &variant) { + return comparand <= variant.as(); + } + + template + friend bool operator>=(const JsonVariantComparisons &variant, + TComparand comparand) { + return variant.as() >= comparand; + } + + template + friend bool operator>=(TComparand comparand, + const JsonVariantComparisons &variant) { + return comparand >= variant.as(); + } + + template + friend bool operator<(const JsonVariantComparisons &varian, + TComparand comparand) { + return varian.as() < comparand; + } + + template + friend bool operator<(TComparand comparand, + const JsonVariantComparisons &variant) { + return comparand < variant.as(); + } + + template + friend bool operator>(const JsonVariantComparisons &variant, + TComparand comparand) { + return variant.as() > comparand; + } + + template + friend bool operator>(TComparand comparand, + const JsonVariantComparisons &variant) { + return comparand > variant.as(); + } + + private: + const TImpl *impl() const { + return static_cast(this); + } + + template + const typename JsonVariantAs::type as() const { + return impl()->template as(); + } + + template + bool is() const { + return impl()->template is(); + } + + template + typename EnableIf::has_equals, bool>::type equals( + const TString &comparand) const { + const char *value = as(); + return StringTraits::equals(comparand, value); + } + + template + typename EnableIf::value && + !StringTraits::has_equals, + bool>::type + equals(const TComparand &comparand) const { + return as() == comparand; + } + + template + bool equals(const JsonVariantComparisons &right) const { + using namespace Internals; + if (is() && right.template is()) + return as() == right.template as(); + if (is() && right.template is()) + return as() == right.template as(); + if (is() && right.template is()) + return as() == right.template as(); + if (is() && right.template is()) + return as() == right.template as(); + if (is() && right.template is()) + return as() == right.template as(); + if (is() && right.template is()) + return StringTraits::equals(as(), + right.template as()); + + return false; + } +}; +} // namespace Internals +} // namespace ArduinoJson diff --git a/include/lib/ArduinoJson/JsonVariantImpl.hpp b/include/lib/ArduinoJson/JsonVariantImpl.hpp new file mode 100644 index 0000000..31f96ce --- /dev/null +++ b/include/lib/ArduinoJson/JsonVariantImpl.hpp @@ -0,0 +1,126 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Configuration.hpp" +#include "JsonArray.hpp" +#include "JsonObject.hpp" +#include "JsonVariant.hpp" +#include "Polyfills/isFloat.hpp" +#include "Polyfills/isInteger.hpp" +#include "Polyfills/parseFloat.hpp" +#include "Polyfills/parseInteger.hpp" + +#include // for strcmp + +namespace ArduinoJson { + +inline JsonVariant::JsonVariant(const JsonArray &array) { + if (array.success()) { + _type = Internals::JSON_ARRAY; + _content.asArray = const_cast(&array); + } else { + _type = Internals::JSON_UNDEFINED; + } +} + +inline JsonVariant::JsonVariant(const JsonObject &object) { + if (object.success()) { + _type = Internals::JSON_OBJECT; + _content.asObject = const_cast(&object); + } else { + _type = Internals::JSON_UNDEFINED; + } +} + +inline JsonArray &JsonVariant::variantAsArray() const { + if (_type == Internals::JSON_ARRAY) return *_content.asArray; + return JsonArray::invalid(); +} + +inline JsonObject &JsonVariant::variantAsObject() const { + if (_type == Internals::JSON_OBJECT) return *_content.asObject; + return JsonObject::invalid(); +} + +template +inline T JsonVariant::variantAsInteger() const { + using namespace Internals; + switch (_type) { + case JSON_UNDEFINED: + return 0; + case JSON_POSITIVE_INTEGER: + case JSON_BOOLEAN: + return T(_content.asInteger); + case JSON_NEGATIVE_INTEGER: + return T(~_content.asInteger + 1); + case JSON_STRING: + case JSON_UNPARSED: + return parseInteger(_content.asString); + default: + return T(_content.asFloat); + } +} + +inline const char *JsonVariant::variantAsString() const { + using namespace Internals; + if (_type == JSON_UNPARSED && _content.asString && + !strcmp("null", _content.asString)) + return NULL; + if (_type == JSON_STRING || _type == JSON_UNPARSED) return _content.asString; + return NULL; +} + +template +inline T JsonVariant::variantAsFloat() const { + using namespace Internals; + switch (_type) { + case JSON_UNDEFINED: + return 0; + case JSON_POSITIVE_INTEGER: + case JSON_BOOLEAN: + return static_cast(_content.asInteger); + case JSON_NEGATIVE_INTEGER: + return -static_cast(_content.asInteger); + case JSON_STRING: + case JSON_UNPARSED: + return parseFloat(_content.asString); + default: + return static_cast(_content.asFloat); + } +} + +inline bool JsonVariant::variantIsBoolean() const { + using namespace Internals; + if (_type == JSON_BOOLEAN) return true; + + if (_type != JSON_UNPARSED || _content.asString == NULL) return false; + + return !strcmp(_content.asString, "true") || + !strcmp(_content.asString, "false"); +} + +inline bool JsonVariant::variantIsInteger() const { + using namespace Internals; + + return _type == JSON_POSITIVE_INTEGER || _type == JSON_NEGATIVE_INTEGER || + (_type == JSON_UNPARSED && isInteger(_content.asString)); +} + +inline bool JsonVariant::variantIsFloat() const { + using namespace Internals; + + return _type == JSON_FLOAT || _type == JSON_POSITIVE_INTEGER || + _type == JSON_NEGATIVE_INTEGER || + (_type == JSON_UNPARSED && isFloat(_content.asString)); +} + +#if ARDUINOJSON_ENABLE_STD_STREAM +inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) { + return source.printTo(os); +} +#endif + +} // namespace ArduinoJson diff --git a/include/lib/ArduinoJson/JsonVariantOr.hpp b/include/lib/ArduinoJson/JsonVariantOr.hpp new file mode 100644 index 0000000..d8022fc --- /dev/null +++ b/include/lib/ArduinoJson/JsonVariantOr.hpp @@ -0,0 +1,52 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Data/JsonVariantAs.hpp" +#include "Polyfills/attributes.hpp" +#include "TypeTraits/EnableIf.hpp" +#include "TypeTraits/IsIntegral.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +class JsonVariantOr { + public: + // Returns the default value if the JsonVariant is undefined of incompatible + template + typename EnableIf::value, T>::type operator|( + const T &defaultValue) const { + if (impl()->template is()) + return impl()->template as(); + else + return defaultValue; + } + + // Returns the default value if the JsonVariant is undefined of incompatible + // Special case for string: null is treated as undefined + const char *operator|(const char *defaultValue) const { + const char *value = impl()->template as(); + return value ? value : defaultValue; + } + + // Returns the default value if the JsonVariant is undefined of incompatible + // Special case for integers: we also accept double + template + typename EnableIf::value, Integer>::type operator|( + const Integer &defaultValue) const { + if (impl()->template is()) + return impl()->template as(); + else + return defaultValue; + } + + private: + const TImpl *impl() const { + return static_cast(this); + } +}; +} // namespace Internals +} // namespace ArduinoJson diff --git a/include/lib/ArduinoJson/JsonVariantSubscripts.hpp b/include/lib/ArduinoJson/JsonVariantSubscripts.hpp new file mode 100644 index 0000000..279ee01 --- /dev/null +++ b/include/lib/ArduinoJson/JsonVariantSubscripts.hpp @@ -0,0 +1,86 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "Data/JsonVariantAs.hpp" +#include "Polyfills/attributes.hpp" +#include "StringTraits/StringTraits.hpp" +#include "TypeTraits/EnableIf.hpp" + +namespace ArduinoJson { +namespace Internals { + +// Forward declarations. +class JsonArraySubscript; +template +class JsonObjectSubscript; + +template +class JsonVariantSubscripts { + public: + // Mimics an array or an object. + // Returns the size of the array or object if the variant has that type. + // Returns 0 if the variant is neither an array nor an object + size_t size() const { + return impl()->template as().size() + + impl()->template as().size(); + } + + // Mimics an array. + // Returns the element at specified index if the variant is an array. + // Returns JsonVariant::invalid() if the variant is not an array. + FORCE_INLINE const JsonArraySubscript operator[](size_t index) const; + FORCE_INLINE JsonArraySubscript operator[](size_t index); + + // Mimics an object. + // Returns the value associated with the specified key if the variant is + // an object. + // Return JsonVariant::invalid() if the variant is not an object. + // + // const JsonObjectSubscript operator[](TKey) const; + // TKey = const std::string&, const String& + template + FORCE_INLINE + typename EnableIf::has_equals, + const JsonObjectSubscript >::type + operator[](const TString &key) const { + return impl()->template as()[key]; + } + // + // const JsonObjectSubscript operator[](TKey) const; + // TKey = const std::string&, const String& + template + FORCE_INLINE typename EnableIf::has_equals, + JsonObjectSubscript >::type + operator[](const TString &key) { + return impl()->template as()[key]; + } + // + // JsonObjectSubscript operator[](TKey); + // TKey = const char*, const char[N], const FlashStringHelper* + template + FORCE_INLINE typename EnableIf::has_equals, + JsonObjectSubscript >::type + operator[](const TString *key) { + return impl()->template as()[key]; + } + // + // JsonObjectSubscript operator[](TKey); + // TKey = const char*, const char[N], const FlashStringHelper* + template + FORCE_INLINE + typename EnableIf::has_equals, + const JsonObjectSubscript >::type + operator[](const TString *key) const { + return impl()->template as()[key]; + } + + private: + const TImpl *impl() const { + return static_cast(this); + } +}; +} +} diff --git a/include/lib/ArduinoJson/Polyfills/attributes.hpp b/include/lib/ArduinoJson/Polyfills/attributes.hpp new file mode 100644 index 0000000..b49091d --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/attributes.hpp @@ -0,0 +1,29 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#ifdef _MSC_VER // Visual Studio + +#define FORCE_INLINE // __forceinline causes C4714 when returning std::string +#define NO_INLINE __declspec(noinline) +#define DEPRECATED(msg) __declspec(deprecated(msg)) + +#elif defined(__GNUC__) // GCC or Clang + +#define FORCE_INLINE __attribute__((always_inline)) +#define NO_INLINE __attribute__((noinline)) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) +#define DEPRECATED(msg) __attribute__((deprecated(msg))) +#else +#define DEPRECATED(msg) __attribute__((deprecated)) +#endif + +#else // Other compilers + +#define FORCE_INLINE +#define NO_INLINE +#define DEPRECATED(msg) + +#endif diff --git a/include/lib/ArduinoJson/Polyfills/ctype.hpp b/include/lib/ArduinoJson/Polyfills/ctype.hpp new file mode 100644 index 0000000..2d52703 --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/ctype.hpp @@ -0,0 +1,18 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +inline bool isdigit(char c) { + return '0' <= c && c <= '9'; +} + +inline bool issign(char c) { + return '-' == c || c == '+'; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/isFloat.hpp b/include/lib/ArduinoJson/Polyfills/isFloat.hpp new file mode 100644 index 0000000..973b89f --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/isFloat.hpp @@ -0,0 +1,38 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include // for strcmp +#include "./ctype.hpp" + +namespace ArduinoJson { +namespace Internals { + +inline bool isFloat(const char* s) { + if (!s) return false; + + if (!strcmp(s, "NaN")) return true; + if (issign(*s)) s++; + if (!strcmp(s, "Infinity")) return true; + if (*s == '\0') return false; + + while (isdigit(*s)) s++; + + if (*s == '.') { + s++; + while (isdigit(*s)) s++; + } + + if (*s == 'e' || *s == 'E') { + s++; + if (issign(*s)) s++; + if (!isdigit(*s)) return false; + while (isdigit(*s)) s++; + } + + return *s == '\0'; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/isInteger.hpp b/include/lib/ArduinoJson/Polyfills/isInteger.hpp new file mode 100644 index 0000000..8049079 --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/isInteger.hpp @@ -0,0 +1,19 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "./ctype.hpp" + +namespace ArduinoJson { +namespace Internals { + +inline bool isInteger(const char* s) { + if (!s || !*s) return false; + if (issign(*s)) s++; + while (isdigit(*s)) s++; + return *s == '\0'; +} +} // namespace Internals +} // namespace ArduinoJson diff --git a/include/lib/ArduinoJson/Polyfills/math.hpp b/include/lib/ArduinoJson/Polyfills/math.hpp new file mode 100644 index 0000000..48773ed --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/math.hpp @@ -0,0 +1,19 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { +template +bool isNaN(T x) { + return x != x; +} + +template +bool isInfinity(T x) { + return x != 0.0 && x * 2 == x; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/parseFloat.hpp b/include/lib/ArduinoJson/Polyfills/parseFloat.hpp new file mode 100644 index 0000000..49b0f6f --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/parseFloat.hpp @@ -0,0 +1,90 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../TypeTraits/FloatTraits.hpp" +#include "./ctype.hpp" +#include "./math.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +inline T parseFloat(const char* s) { + typedef FloatTraits traits; + typedef typename traits::mantissa_type mantissa_t; + typedef typename traits::exponent_type exponent_t; + + if (!s) return 0; // NULL + + bool negative_result = false; + switch (*s) { + case '-': + negative_result = true; + s++; + break; + case '+': + s++; + break; + } + + if (*s == 't') return 1; // true + if (*s == 'n' || *s == 'N') return traits::nan(); + if (*s == 'i' || *s == 'I') + return negative_result ? -traits::inf() : traits::inf(); + + mantissa_t mantissa = 0; + exponent_t exponent_offset = 0; + + while (isdigit(*s)) { + if (mantissa < traits::mantissa_max / 10) + mantissa = mantissa * 10 + (*s - '0'); + else + exponent_offset++; + s++; + } + + if (*s == '.') { + s++; + while (isdigit(*s)) { + if (mantissa < traits::mantissa_max / 10) { + mantissa = mantissa * 10 + (*s - '0'); + exponent_offset--; + } + s++; + } + } + + int exponent = 0; + if (*s == 'e' || *s == 'E') { + s++; + bool negative_exponent = false; + if (*s == '-') { + negative_exponent = true; + s++; + } else if (*s == '+') { + s++; + } + + while (isdigit(*s)) { + exponent = exponent * 10 + (*s - '0'); + if (exponent + exponent_offset > traits::exponent_max) { + if (negative_exponent) + return negative_result ? -0.0f : 0.0f; + else + return negative_result ? -traits::inf() : traits::inf(); + } + s++; + } + if (negative_exponent) exponent = -exponent; + } + exponent += exponent_offset; + + T result = traits::make_float(static_cast(mantissa), exponent); + + return negative_result ? -result : result; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/parseInteger.hpp b/include/lib/ArduinoJson/Polyfills/parseInteger.hpp new file mode 100644 index 0000000..e8f1974 --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/parseInteger.hpp @@ -0,0 +1,41 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include + +#include "../Configuration.hpp" +#include "./ctype.hpp" + +namespace ArduinoJson { +namespace Internals { +template +T parseInteger(const char *s) { + if (!s) return 0; // NULL + + if (*s == 't') return 1; // "true" + + T result = 0; + bool negative_result = false; + + switch (*s) { + case '-': + negative_result = true; + s++; + break; + case '+': + s++; + break; + } + + while (isdigit(*s)) { + result = T(result * 10 + T(*s - '0')); + s++; + } + + return negative_result ? T(~result + 1) : result; +} +} +} diff --git a/include/lib/ArduinoJson/RawJson.hpp b/include/lib/ArduinoJson/RawJson.hpp new file mode 100644 index 0000000..4beb980 --- /dev/null +++ b/include/lib/ArduinoJson/RawJson.hpp @@ -0,0 +1,46 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { + +namespace Internals { +// A special type of data that can be used to insert pregenerated JSON portions. +template +class RawJsonString { + public: + explicit RawJsonString(T str) : _str(str) {} + operator T() const { + return _str; + } + + private: + T _str; +}; + +template +struct StringTraits, void> { + static bool is_null(RawJsonString source) { + return StringTraits::is_null(static_cast(source)); + } + + typedef RawJsonString duplicate_t; + + template + static duplicate_t duplicate(RawJsonString source, Buffer* buffer) { + return duplicate_t(StringTraits::duplicate(source, buffer)); + } + + static const bool has_append = false; + static const bool has_equals = false; + static const bool should_duplicate = StringTraits::should_duplicate; +}; +} + +template +inline Internals::RawJsonString RawJson(T str) { + return Internals::RawJsonString(str); +} +} diff --git a/include/lib/ArduinoJson/Serialization/DummyPrint.hpp b/include/lib/ArduinoJson/Serialization/DummyPrint.hpp new file mode 100644 index 0000000..9fdf2d6 --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/DummyPrint.hpp @@ -0,0 +1,22 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A dummy Print implementation used in JsonPrintable::measureLength() +class DummyPrint { + public: + size_t print(char) { + return 1; + } + + size_t print(const char* s) { + return strlen(s); + } +}; +} +} diff --git a/include/lib/ArduinoJson/Serialization/DynamicStringBuilder.hpp b/include/lib/ArduinoJson/Serialization/DynamicStringBuilder.hpp new file mode 100644 index 0000000..41be639 --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/DynamicStringBuilder.hpp @@ -0,0 +1,35 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../StringTraits/StringTraits.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A Print implementation that allows to write in a String +template +class DynamicStringBuilder { + public: + DynamicStringBuilder(TString &str) : _str(str) {} + + size_t print(char c) { + StringTraits::append(_str, c); + return 1; + } + + size_t print(const char *s) { + size_t initialLen = _str.length(); + StringTraits::append(_str, s); + return _str.length() - initialLen; + } + + private: + DynamicStringBuilder &operator=(const DynamicStringBuilder &); + + TString &_str; +}; +} +} diff --git a/include/lib/ArduinoJson/Serialization/FloatParts.hpp b/include/lib/ArduinoJson/Serialization/FloatParts.hpp new file mode 100644 index 0000000..c14e3b5 --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/FloatParts.hpp @@ -0,0 +1,89 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../Configuration.hpp" +#include "../Polyfills/math.hpp" +#include "../TypeTraits/FloatTraits.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +struct FloatParts { + uint32_t integral; + uint32_t decimal; + int16_t exponent; + int8_t decimalPlaces; + + FloatParts(TFloat value) { + uint32_t maxDecimalPart = sizeof(TFloat) >= 8 ? 1000000000 : 1000000; + decimalPlaces = sizeof(TFloat) >= 8 ? 9 : 6; + + exponent = normalize(value); + + integral = uint32_t(value); + // reduce number of decimal places by the number of integral places + for (uint32_t tmp = integral; tmp >= 10; tmp /= 10) { + maxDecimalPart /= 10; + decimalPlaces--; + } + + TFloat remainder = (value - TFloat(integral)) * TFloat(maxDecimalPart); + + decimal = uint32_t(remainder); + remainder = remainder - TFloat(decimal); + + // rounding: + // increment by 1 if remainder >= 0.5 + decimal += uint32_t(remainder * 2); + if (decimal >= maxDecimalPart) { + decimal = 0; + integral++; + if (exponent && integral >= 10) { + exponent++; + integral = 1; + } + } + + // remove trailing zeros + while (decimal % 10 == 0 && decimalPlaces > 0) { + decimal /= 10; + decimalPlaces--; + } + } + + static int16_t normalize(TFloat& value) { + typedef FloatTraits traits; + int16_t powersOf10 = 0; + + int8_t index = sizeof(TFloat) == 8 ? 8 : 5; + int bit = 1 << index; + + if (value >= ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD) { + for (; index >= 0; index--) { + if (value >= traits::positiveBinaryPowerOfTen(index)) { + value *= traits::negativeBinaryPowerOfTen(index); + powersOf10 = int16_t(powersOf10 + bit); + } + bit >>= 1; + } + } + + if (value > 0 && value <= ARDUINOJSON_NEGATIVE_EXPONENTIATION_THRESHOLD) { + for (; index >= 0; index--) { + if (value < traits::negativeBinaryPowerOfTenPlusOne(index)) { + value *= traits::positiveBinaryPowerOfTen(index); + powersOf10 = int16_t(powersOf10 - bit); + } + bit >>= 1; + } + } + + return powersOf10; + } +}; +} +} diff --git a/include/lib/ArduinoJson/Serialization/IndentedPrint.hpp b/include/lib/ArduinoJson/Serialization/IndentedPrint.hpp new file mode 100644 index 0000000..864f9aa --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/IndentedPrint.hpp @@ -0,0 +1,68 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// Decorator on top of Print to allow indented output. +// This class is used by JsonPrintable::prettyPrintTo() but can also be used +// for your own purpose, like logging. +template +class IndentedPrint { + public: + explicit IndentedPrint(Print &p) : sink(&p) { + level = 0; + tabSize = 2; + isNewLine = true; + } + + size_t print(char c) { + size_t n = 0; + if (isNewLine) n += writeTabs(); + n += sink->print(c); + isNewLine = c == '\n'; + return n; + } + + size_t print(const char *s) { + // TODO: optimize + size_t n = 0; + while (*s) n += print(*s++); + return n; + } + + // Adds one level of indentation + void indent() { + if (level < MAX_LEVEL) level++; + } + + // Removes one level of indentation + void unindent() { + if (level > 0) level--; + } + + // Set the number of space printed for each level of indentation + void setTabSize(uint8_t n) { + if (n < MAX_TAB_SIZE) tabSize = n & MAX_TAB_SIZE; + } + + private: + Print *sink; + uint8_t level : 4; + uint8_t tabSize : 3; + bool isNewLine : 1; + + size_t writeTabs() { + size_t n = 0; + for (int i = 0; i < level * tabSize; i++) n += sink->print(' '); + return n; + } + + static const int MAX_LEVEL = 15; // because it's only 4 bits + static const int MAX_TAB_SIZE = 7; // because it's only 3 bits +}; +} +} diff --git a/include/lib/ArduinoJson/Serialization/JsonPrintable.hpp b/include/lib/ArduinoJson/Serialization/JsonPrintable.hpp new file mode 100644 index 0000000..e73f8fb --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/JsonPrintable.hpp @@ -0,0 +1,117 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../Configuration.hpp" +#include "../TypeTraits/EnableIf.hpp" +#include "DummyPrint.hpp" +#include "DynamicStringBuilder.hpp" +#include "IndentedPrint.hpp" +#include "JsonSerializer.hpp" +#include "JsonWriter.hpp" +#include "Prettyfier.hpp" +#include "StaticStringBuilder.hpp" + +#if ARDUINOJSON_ENABLE_STD_STREAM +#include "StreamPrintAdapter.hpp" +#endif + +namespace ArduinoJson { +namespace Internals { + +// Implements all the overloads of printTo() and prettyPrintTo() +// Caution: this class use a template parameter to avoid virtual methods. +// This is a bit curious but allows to reduce the size of JsonVariant, JsonArray +// and JsonObject. +template +class JsonPrintable { + public: + template + typename EnableIf::has_append, size_t>::type printTo( + Print &print) const { + JsonWriter writer(print); + JsonSerializer >::serialize(downcast(), writer); + return writer.bytesWritten(); + } + +#if ARDUINOJSON_ENABLE_STD_STREAM + std::ostream &printTo(std::ostream &os) const { + StreamPrintAdapter adapter(os); + printTo(adapter); + return os; + } +#endif + + size_t printTo(char *buffer, size_t bufferSize) const { + StaticStringBuilder sb(buffer, bufferSize); + return printTo(sb); + } + + template + size_t printTo(char (&buffer)[TN]) const { + return printTo(buffer, TN); + } + + template + typename EnableIf::has_append, size_t>::type printTo( + TString &str) const { + DynamicStringBuilder sb(str); + return printTo(sb); + } + + template + size_t prettyPrintTo(IndentedPrint &print) const { + Prettyfier p(print); + return printTo(p); + } + + size_t prettyPrintTo(char *buffer, size_t bufferSize) const { + StaticStringBuilder sb(buffer, bufferSize); + return prettyPrintTo(sb); + } + + template + size_t prettyPrintTo(char (&buffer)[TN]) const { + return prettyPrintTo(buffer, TN); + } + + template + typename EnableIf::has_append, size_t>::type + prettyPrintTo(Print &print) const { + IndentedPrint indentedPrint(print); + return prettyPrintTo(indentedPrint); + } + + template + typename EnableIf::has_append, size_t>::type + prettyPrintTo(TString &str) const { + DynamicStringBuilder sb(str); + return prettyPrintTo(sb); + } + + size_t measureLength() const { + DummyPrint dp; + return printTo(dp); + } + + size_t measurePrettyLength() const { + DummyPrint dp; + return prettyPrintTo(dp); + } + + private: + const T &downcast() const { + return *static_cast(this); + } +}; + +#if ARDUINOJSON_ENABLE_STD_STREAM +template +inline std::ostream &operator<<(std::ostream &os, const JsonPrintable &v) { + return v.printTo(os); +} +#endif +} +} diff --git a/include/lib/ArduinoJson/Serialization/JsonSerializer.hpp b/include/lib/ArduinoJson/Serialization/JsonSerializer.hpp new file mode 100644 index 0000000..0cb537f --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/JsonSerializer.hpp @@ -0,0 +1,32 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "JsonWriter.hpp" + +namespace ArduinoJson { + +class JsonArray; +class JsonObject; +class JsonVariant; + +namespace Internals { + +class JsonArraySubscript; +template +class JsonObjectSubscript; + +template +class JsonSerializer { + public: + static void serialize(const JsonArray &, Writer &); + static void serialize(const JsonArraySubscript &, Writer &); + static void serialize(const JsonObject &, Writer &); + template + static void serialize(const JsonObjectSubscript &, Writer &); + static void serialize(const JsonVariant &, Writer &); +}; +} +} diff --git a/include/lib/ArduinoJson/Serialization/JsonSerializerImpl.hpp b/include/lib/ArduinoJson/Serialization/JsonSerializerImpl.hpp new file mode 100644 index 0000000..0faae27 --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/JsonSerializerImpl.hpp @@ -0,0 +1,103 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../JsonArray.hpp" +#include "../JsonArraySubscript.hpp" +#include "../JsonObject.hpp" +#include "../JsonObjectSubscript.hpp" +#include "../JsonVariant.hpp" +#include "JsonSerializer.hpp" + +template +inline void ArduinoJson::Internals::JsonSerializer::serialize( + const JsonArray& array, Writer& writer) { + writer.beginArray(); + + JsonArray::const_iterator it = array.begin(); + while (it != array.end()) { + serialize(*it, writer); + + ++it; + if (it == array.end()) break; + + writer.writeComma(); + } + + writer.endArray(); +} + +template +inline void ArduinoJson::Internals::JsonSerializer::serialize( + const JsonArraySubscript& arraySubscript, Writer& writer) { + serialize(arraySubscript.as(), writer); +} + +template +inline void ArduinoJson::Internals::JsonSerializer::serialize( + const JsonObject& object, Writer& writer) { + writer.beginObject(); + + JsonObject::const_iterator it = object.begin(); + while (it != object.end()) { + writer.writeString(it->key); + writer.writeColon(); + serialize(it->value, writer); + + ++it; + if (it == object.end()) break; + + writer.writeComma(); + } + + writer.endObject(); +} + +template +template +inline void ArduinoJson::Internals::JsonSerializer::serialize( + const JsonObjectSubscript& objectSubscript, Writer& writer) { + serialize(objectSubscript.template as(), writer); +} + +template +inline void ArduinoJson::Internals::JsonSerializer::serialize( + const JsonVariant& variant, Writer& writer) { + switch (variant._type) { + case JSON_FLOAT: + writer.writeFloat(variant._content.asFloat); + return; + + case JSON_ARRAY: + serialize(*variant._content.asArray, writer); + return; + + case JSON_OBJECT: + serialize(*variant._content.asObject, writer); + return; + + case JSON_STRING: + writer.writeString(variant._content.asString); + return; + + case JSON_UNPARSED: + writer.writeRaw(variant._content.asString); + return; + + case JSON_NEGATIVE_INTEGER: + writer.writeRaw('-'); // Falls through. + + case JSON_POSITIVE_INTEGER: + writer.writeInteger(variant._content.asInteger); + return; + + case JSON_BOOLEAN: + writer.writeBoolean(variant._content.asInteger != 0); + return; + + default: // JSON_UNDEFINED + return; + } +} diff --git a/include/lib/ArduinoJson/Serialization/JsonWriter.hpp b/include/lib/ArduinoJson/Serialization/JsonWriter.hpp new file mode 100644 index 0000000..146d51d --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/JsonWriter.hpp @@ -0,0 +1,155 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include +#include "../Data/Encoding.hpp" +#include "../Data/JsonInteger.hpp" +#include "../Polyfills/attributes.hpp" +#include "../Serialization/FloatParts.hpp" + +namespace ArduinoJson { +namespace Internals { + +// Writes the JSON tokens to a Print implementation +// This class is used by: +// - JsonArray::writeTo() +// - JsonObject::writeTo() +// - JsonVariant::writeTo() +// Its derived by PrettyJsonWriter that overrides some members to add +// indentation. +template +class JsonWriter { + public: + explicit JsonWriter(Print &sink) : _sink(sink), _length(0) {} + + // Returns the number of bytes sent to the Print implementation. + // This is very handy for implementations of printTo() that must return the + // number of bytes written. + size_t bytesWritten() const { + return _length; + } + + void beginArray() { + writeRaw('['); + } + void endArray() { + writeRaw(']'); + } + + void beginObject() { + writeRaw('{'); + } + void endObject() { + writeRaw('}'); + } + + void writeColon() { + writeRaw(':'); + } + void writeComma() { + writeRaw(','); + } + + void writeBoolean(bool value) { + writeRaw(value ? "true" : "false"); + } + + void writeString(const char *value) { + if (!value) { + writeRaw("null"); + } else { + writeRaw('\"'); + while (*value) writeChar(*value++); + writeRaw('\"'); + } + } + + void writeChar(char c) { + char specialChar = Encoding::escapeChar(c); + if (specialChar) { + writeRaw('\\'); + writeRaw(specialChar); + } else { + writeRaw(c); + } + } + + template + void writeFloat(TFloat value) { + if (isNaN(value)) return writeRaw("NaN"); + + if (value < 0.0) { + writeRaw('-'); + value = -value; + } + + if (isInfinity(value)) return writeRaw("Infinity"); + + FloatParts parts(value); + + writeInteger(parts.integral); + if (parts.decimalPlaces) writeDecimals(parts.decimal, parts.decimalPlaces); + + if (parts.exponent < 0) { + writeRaw("e-"); + writeInteger(-parts.exponent); + } + + if (parts.exponent > 0) { + writeRaw('e'); + writeInteger(parts.exponent); + } + } + + template + void writeInteger(UInt value) { + char buffer[22]; + char *end = buffer + sizeof(buffer) - 1; + char *ptr = end; + + *ptr = 0; + do { + *--ptr = char(value % 10 + '0'); + value = UInt(value / 10); + } while (value); + + writeRaw(ptr); + } + + void writeDecimals(uint32_t value, int8_t width) { + // buffer should be big enough for all digits, the dot and the null + // terminator + char buffer[16]; + char *ptr = buffer + sizeof(buffer) - 1; + + // write the string in reverse order + *ptr = 0; + while (width--) { + *--ptr = char(value % 10 + '0'); + value /= 10; + } + *--ptr = '.'; + + // and dump it in the right order + writeRaw(ptr); + } + + void writeRaw(const char *s) { + _length += _sink.print(s); + } + void writeRaw(char c) { + _length += _sink.print(c); + } + + protected: + Print &_sink; + size_t _length; + + private: + JsonWriter &operator=(const JsonWriter &); // cannot be assigned +}; +} +} diff --git a/include/lib/ArduinoJson/Serialization/Prettyfier.hpp b/include/lib/ArduinoJson/Serialization/Prettyfier.hpp new file mode 100644 index 0000000..8b4f0d2 --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/Prettyfier.hpp @@ -0,0 +1,133 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "IndentedPrint.hpp" + +namespace ArduinoJson { +namespace Internals { + +// Converts a compact JSON string into an indented one. +template +class Prettyfier { + public: + explicit Prettyfier(IndentedPrint& p) : _sink(p) { + _previousChar = 0; + _inString = false; + } + + size_t print(char c) { + size_t n = _inString ? handleStringChar(c) : handleMarkupChar(c); + _previousChar = c; + return n; + } + + size_t print(const char* s) { + // TODO: optimize + size_t n = 0; + while (*s) n += print(*s++); + return n; + } + + private: + Prettyfier& operator=(const Prettyfier&); // cannot be assigned + + bool inEmptyBlock() { + return _previousChar == '{' || _previousChar == '['; + } + + size_t handleStringChar(char c) { + bool isQuote = c == '"' && _previousChar != '\\'; + + if (isQuote) _inString = false; + + return _sink.print(c); + } + + size_t handleMarkupChar(char c) { + switch (c) { + case '{': + case '[': + return writeBlockOpen(c); + + case '}': + case ']': + return writeBlockClose(c); + + case ':': + return writeColon(); + + case ',': + return writeComma(); + + case '"': + return writeQuoteOpen(); + + default: + return writeNormalChar(c); + } + } + + size_t writeBlockClose(char c) { + size_t n = 0; + n += unindentIfNeeded(); + n += _sink.print(c); + return n; + } + + size_t writeBlockOpen(char c) { + size_t n = 0; + n += indentIfNeeded(); + n += _sink.print(c); + return n; + } + + size_t writeColon() { + size_t n = 0; + n += _sink.print(": "); + return n; + } + + size_t writeComma() { + size_t n = 0; + n += _sink.print(",\r\n"); + return n; + } + + size_t writeQuoteOpen() { + _inString = true; + size_t n = 0; + n += indentIfNeeded(); + n += _sink.print('"'); + return n; + } + + size_t writeNormalChar(char c) { + size_t n = 0; + n += indentIfNeeded(); + n += _sink.print(c); + return n; + } + + size_t indentIfNeeded() { + if (!inEmptyBlock()) return 0; + + _sink.indent(); + return _sink.print("\r\n"); + } + + size_t unindentIfNeeded() { + if (inEmptyBlock()) return 0; + + _sink.unindent(); + return _sink.print("\r\n"); + } + + char _previousChar; + IndentedPrint& _sink; + bool _inString; +}; +} +} diff --git a/include/lib/ArduinoJson/Serialization/StaticStringBuilder.hpp b/include/lib/ArduinoJson/Serialization/StaticStringBuilder.hpp new file mode 100644 index 0000000..9617bbd --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/StaticStringBuilder.hpp @@ -0,0 +1,36 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A Print implementation that allows to write in a char[] +class StaticStringBuilder { + public: + StaticStringBuilder(char *buf, size_t size) : end(buf + size - 1), p(buf) { + *p = '\0'; + } + + size_t print(char c) { + if (p >= end) return 0; + *p++ = c; + *p = '\0'; + return 1; + } + + size_t print(const char *s) { + char *begin = p; + while (p < end && *s) *p++ = *s++; + *p = '\0'; + return size_t(p - begin); + } + + private: + char *end; + char *p; +}; +} +} diff --git a/include/lib/ArduinoJson/Serialization/StreamPrintAdapter.hpp b/include/lib/ArduinoJson/Serialization/StreamPrintAdapter.hpp new file mode 100644 index 0000000..60f0af4 --- /dev/null +++ b/include/lib/ArduinoJson/Serialization/StreamPrintAdapter.hpp @@ -0,0 +1,39 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../Configuration.hpp" + +#if ARDUINOJSON_ENABLE_STD_STREAM + +#include + +namespace ArduinoJson { +namespace Internals { + +class StreamPrintAdapter { + public: + explicit StreamPrintAdapter(std::ostream& os) : _os(os) {} + + size_t print(char c) { + _os << c; + return 1; + } + + size_t print(const char* s) { + _os << s; + return strlen(s); + } + + private: + // cannot be assigned + StreamPrintAdapter& operator=(const StreamPrintAdapter&); + + std::ostream& _os; +}; +} +} + +#endif // ARDUINOJSON_ENABLE_STD_STREAM diff --git a/include/lib/ArduinoJson/StaticJsonBuffer.hpp b/include/lib/ArduinoJson/StaticJsonBuffer.hpp new file mode 100644 index 0000000..267d9d0 --- /dev/null +++ b/include/lib/ArduinoJson/StaticJsonBuffer.hpp @@ -0,0 +1,126 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "JsonBufferBase.hpp" + +namespace ArduinoJson { +namespace Internals { + +class StaticJsonBufferBase : public JsonBufferBase { + public: + class String { + public: + String(StaticJsonBufferBase* parent) : _parent(parent) { + _start = parent->_buffer + parent->_size; + } + + void append(char c) { + if (_parent->canAlloc(1)) { + char* last = static_cast(_parent->doAlloc(1)); + *last = c; + } + } + + const char* c_str() const { + if (_parent->canAlloc(1)) { + char* last = static_cast(_parent->doAlloc(1)); + *last = '\0'; + return _start; + } else { + return NULL; + } + } + + private: + StaticJsonBufferBase* _parent; + char* _start; + }; + + StaticJsonBufferBase(char* buffer, size_t capa) + : _buffer(buffer), _capacity(capa), _size(0) {} + + // Gets the capacity of the buffer in bytes + size_t capacity() const { + return _capacity; + } + + // Gets the current usage of the buffer in bytes + size_t size() const { + return _size; + } + + // Allocates the specified amount of bytes in the buffer + virtual void* alloc(size_t bytes) { + alignNextAlloc(); + if (!canAlloc(bytes)) return NULL; + return doAlloc(bytes); + } + + // Resets the buffer. + // USE WITH CAUTION: this invalidates all previously allocated data + void clear() { + _size = 0; + } + + String startString() { + return String(this); + } + + protected: + ~StaticJsonBufferBase() {} + + private: + void alignNextAlloc() { + _size = round_size_up(_size); + } + + bool canAlloc(size_t bytes) const { + return _size + bytes <= _capacity; + } + + void* doAlloc(size_t bytes) { + void* p = &_buffer[_size]; + _size += bytes; + return p; + } + + char* _buffer; + size_t _capacity; + size_t _size; +}; +} + +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wnon-virtual-dtor" +#elif defined(__GNUC__) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) +#pragma GCC diagnostic push +#endif +#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" +#endif + +// Implements a JsonBuffer with fixed memory allocation. +// The template paramenter CAPACITY specifies the capacity of the buffer in +// bytes. +template +class StaticJsonBuffer : public Internals::StaticJsonBufferBase { + public: + explicit StaticJsonBuffer() + : Internals::StaticJsonBufferBase(_buffer, CAPACITY) {} + + private: + char _buffer[CAPACITY]; +}; +} + +#if defined(__clang__) +#pragma clang diagnostic pop +#elif defined(__GNUC__) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) +#pragma GCC diagnostic pop +#endif +#endif diff --git a/include/lib/ArduinoJson/StringTraits/ArduinoStream.hpp b/include/lib/ArduinoJson/StringTraits/ArduinoStream.hpp new file mode 100644 index 0000000..5db0852 --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/ArduinoStream.hpp @@ -0,0 +1,61 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#if ARDUINOJSON_ENABLE_ARDUINO_STREAM + +#include + +namespace ArduinoJson { +namespace Internals { + +struct ArduinoStreamTraits { + class Reader { + Stream& _stream; + char _current, _next; + + public: + Reader(Stream& stream) : _stream(stream), _current(0), _next(0) {} + + void move() { + _current = _next; + _next = 0; + } + + char current() { + if (!_current) _current = read(); + return _current; + } + + char next() { + // assumes that current() has been called + if (!_next) _next = read(); + return _next; + } + + private: + char read() { + // don't use _stream.read() as it ignores the timeout + char c = 0; + _stream.readBytes(&c, 1); + return c; + } + }; + + static const bool has_append = false; + static const bool has_equals = false; +}; + +template +struct StringTraits< + TStream, + // match any type that is derived from Stream: + typename EnableIf< + IsBaseOf::type>::value>::type> + : ArduinoStreamTraits {}; +} +} + +#endif diff --git a/include/lib/ArduinoJson/StringTraits/CharPointer.hpp b/include/lib/ArduinoJson/StringTraits/CharPointer.hpp new file mode 100644 index 0000000..98896cc --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/CharPointer.hpp @@ -0,0 +1,64 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +template +struct CharPointerTraits { + class Reader { + const TChar* _ptr; + + public: + Reader(const TChar* ptr) + : _ptr(ptr ? ptr : reinterpret_cast("")) {} + + void move() { + ++_ptr; + } + + char current() const { + return char(_ptr[0]); + } + + char next() const { + return char(_ptr[1]); + } + }; + + static bool equals(const TChar* str, const char* expected) { + const char* actual = reinterpret_cast(str); + if (!actual || !expected) return actual == expected; + return strcmp(actual, expected) == 0; + } + + static bool is_null(const TChar* str) { + return !str; + } + + typedef const char* duplicate_t; + + template + static duplicate_t duplicate(const TChar* str, Buffer* buffer) { + if (!str) return NULL; + size_t size = strlen(reinterpret_cast(str)) + 1; + void* dup = buffer->alloc(size); + if (dup != NULL) memcpy(dup, str, size); + return static_cast(dup); + } + + static const bool has_append = false; + static const bool has_equals = true; + static const bool should_duplicate = !IsConst::value; +}; + +// char*, unsigned char*, signed char* +// const char*, const unsigned char*, const signed char* +template +struct StringTraits::value>::type> + : CharPointerTraits {}; +} // namespace Internals +} // namespace ArduinoJson diff --git a/include/lib/ArduinoJson/StringTraits/FlashString.hpp b/include/lib/ArduinoJson/StringTraits/FlashString.hpp new file mode 100644 index 0000000..0701b9b --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/FlashString.hpp @@ -0,0 +1,61 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#if ARDUINOJSON_ENABLE_PROGMEM + +namespace ArduinoJson { +namespace Internals { +template <> +struct StringTraits { + class Reader { + const char* _ptr; + + public: + Reader(const __FlashStringHelper* ptr) + : _ptr(reinterpret_cast(ptr)) {} + + void move() { + _ptr++; + } + + char current() const { + return pgm_read_byte_near(_ptr); + } + + char next() const { + return pgm_read_byte_near(_ptr + 1); + } + }; + + static bool equals(const __FlashStringHelper* str, const char* expected) { + const char* actual = reinterpret_cast(str); + if (!actual || !expected) return actual == expected; + return strcmp_P(expected, actual) == 0; + } + + static bool is_null(const __FlashStringHelper* str) { + return !str; + } + + typedef const char* duplicate_t; + + template + static duplicate_t duplicate(const __FlashStringHelper* str, Buffer* buffer) { + if (!str) return NULL; + size_t size = strlen_P((const char*)str) + 1; + void* dup = buffer->alloc(size); + if (dup != NULL) memcpy_P(dup, (const char*)str, size); + return static_cast(dup); + } + + static const bool has_append = false; + static const bool has_equals = true; + static const bool should_duplicate = true; +}; +} // namespace Internals +} // namespace ArduinoJson + +#endif diff --git a/include/lib/ArduinoJson/StringTraits/StdStream.hpp b/include/lib/ArduinoJson/StringTraits/StdStream.hpp new file mode 100644 index 0000000..227c744 --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/StdStream.hpp @@ -0,0 +1,60 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#if ARDUINOJSON_ENABLE_STD_STREAM + +#include + +namespace ArduinoJson { +namespace Internals { + +struct StdStreamTraits { + class Reader { + std::istream& _stream; + char _current, _next; + + public: + Reader(std::istream& stream) : _stream(stream), _current(0), _next(0) {} + + void move() { + _current = _next; + _next = 0; + } + + char current() { + if (!_current) _current = read(); + return _current; + } + + char next() { + // assumes that current() has been called + if (!_next) _next = read(); + return _next; + } + + private: + Reader& operator=(const Reader&); // Visual Studio C4512 + + char read() { + return _stream.eof() ? '\0' : static_cast(_stream.get()); + } + }; + + static const bool has_append = false; + static const bool has_equals = false; +}; + +template +struct StringTraits< + TStream, + // match any type that is derived from std::istream: + typename EnableIf::type>::value>::type> + : StdStreamTraits {}; +} +} + +#endif diff --git a/include/lib/ArduinoJson/StringTraits/StdString.hpp b/include/lib/ArduinoJson/StringTraits/StdString.hpp new file mode 100644 index 0000000..35f4461 --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/StdString.hpp @@ -0,0 +1,77 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#if ARDUINOJSON_ENABLE_STD_STRING || ARDUINOJSON_ENABLE_ARDUINO_STRING + +#if ARDUINOJSON_ENABLE_ARDUINO_STRING +#include +#endif + +#if ARDUINOJSON_ENABLE_STD_STRING +#include +#endif + +namespace ArduinoJson { +namespace Internals { + +template +struct StdStringTraits { + typedef const char* duplicate_t; + + template + static duplicate_t duplicate(const TString& str, Buffer* buffer) { + if (!str.c_str()) return NULL; // <- Arduino string can return NULL + size_t size = str.length() + 1; + void* dup = buffer->alloc(size); + if (dup != NULL) memcpy(dup, str.c_str(), size); + return static_cast(dup); + } + + static bool is_null(const TString& str) { + // Arduino's String::c_str() can return NULL + return !str.c_str(); + } + + struct Reader : CharPointerTraits::Reader { + Reader(const TString& str) : CharPointerTraits::Reader(str.c_str()) {} + }; + + static bool equals(const TString& str, const char* expected) { + // Arduino's String::c_str() can return NULL + const char* actual = str.c_str(); + if (!actual || !expected) return actual == expected; + return 0 == strcmp(actual, expected); + } + + static void append(TString& str, char c) { + str += c; + } + + static void append(TString& str, const char* s) { + str += s; + } + + static const bool has_append = true; + static const bool has_equals = true; + static const bool should_duplicate = true; +}; + +#if ARDUINOJSON_ENABLE_ARDUINO_STRING +template <> +struct StringTraits : StdStringTraits {}; +template <> +struct StringTraits : StdStringTraits { +}; +#endif + +#if ARDUINOJSON_ENABLE_STD_STRING +template <> +struct StringTraits : StdStringTraits {}; +#endif +} // namespace Internals +} // namespace ArduinoJson + +#endif diff --git a/include/lib/ArduinoJson/StringTraits/StringTraits.hpp b/include/lib/ArduinoJson/StringTraits/StringTraits.hpp new file mode 100644 index 0000000..dd5694b --- /dev/null +++ b/include/lib/ArduinoJson/StringTraits/StringTraits.hpp @@ -0,0 +1,36 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include +#include "../Configuration.hpp" +#include "../TypeTraits/EnableIf.hpp" +#include "../TypeTraits/IsBaseOf.hpp" +#include "../TypeTraits/IsChar.hpp" +#include "../TypeTraits/IsConst.hpp" +#include "../TypeTraits/RemoveReference.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +struct StringTraits { + static const bool has_append = false; + static const bool has_equals = false; +}; + +template +struct StringTraits : StringTraits {}; + +template +struct StringTraits : StringTraits {}; +} +} + +#include "ArduinoStream.hpp" +#include "CharPointer.hpp" +#include "FlashString.hpp" +#include "StdStream.hpp" +#include "StdString.hpp" diff --git a/include/lib/ArduinoJson/TypeTraits/EnableIf.hpp b/include/lib/ArduinoJson/TypeTraits/EnableIf.hpp new file mode 100644 index 0000000..83fc5e0 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/EnableIf.hpp @@ -0,0 +1,19 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that return the type T if Condition is true. +template +struct EnableIf {}; + +template +struct EnableIf { + typedef T type; +}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/FloatTraits.hpp b/include/lib/ArduinoJson/TypeTraits/FloatTraits.hpp new file mode 100644 index 0000000..648cc82 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/FloatTraits.hpp @@ -0,0 +1,171 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include +#include // for size_t +#include "../Configuration.hpp" +#include "../Polyfills/math.hpp" + +namespace ArduinoJson { +namespace Internals { + +template +struct FloatTraits {}; + +template +struct FloatTraits { + typedef int64_t mantissa_type; + static const short mantissa_bits = 52; + static const mantissa_type mantissa_max = + (static_cast(1) << mantissa_bits) - 1; + + typedef int16_t exponent_type; + static const exponent_type exponent_max = 308; + + template + static T make_float(T m, TExponent e) { + if (e > 0) { + for (uint8_t index = 0; e != 0; index++) { + if (e & 1) m *= positiveBinaryPowerOfTen(index); + e >>= 1; + } + } else { + e = TExponent(-e); + for (uint8_t index = 0; e != 0; index++) { + if (e & 1) m *= negativeBinaryPowerOfTen(index); + e >>= 1; + } + } + return m; + } + + static T positiveBinaryPowerOfTen(int index) { + static T factors[] = { + 1e1, + 1e2, + 1e4, + 1e8, + 1e16, + forge(0x4693B8B5, 0xB5056E17), // 1e32 + forge(0x4D384F03, 0xE93FF9F5), // 1e64 + forge(0x5A827748, 0xF9301D32), // 1e128 + forge(0x75154FDD, 0x7F73BF3C) // 1e256 + }; + return factors[index]; + } + + static T negativeBinaryPowerOfTen(int index) { + static T factors[] = { + forge(0x3FB99999, 0x9999999A), // 1e-1 + forge(0x3F847AE1, 0x47AE147B), // 1e-2 + forge(0x3F1A36E2, 0xEB1C432D), // 1e-4 + forge(0x3E45798E, 0xE2308C3A), // 1e-8 + forge(0x3C9CD2B2, 0x97D889BC), // 1e-16 + forge(0x3949F623, 0xD5A8A733), // 1e-32 + forge(0x32A50FFD, 0x44F4A73D), // 1e-64 + forge(0x255BBA08, 0xCF8C979D), // 1e-128 + forge(0x0AC80628, 0x64AC6F43) // 1e-256 + }; + return factors[index]; + } + + static T negativeBinaryPowerOfTenPlusOne(int index) { + static T factors[] = { + 1e0, + forge(0x3FB99999, 0x9999999A), // 1e-1 + forge(0x3F50624D, 0xD2F1A9FC), // 1e-3 + forge(0x3E7AD7F2, 0x9ABCAF48), // 1e-7 + forge(0x3CD203AF, 0x9EE75616), // 1e-15 + forge(0x398039D6, 0x65896880), // 1e-31 + forge(0x32DA53FC, 0x9631D10D), // 1e-63 + forge(0x25915445, 0x81B7DEC2), // 1e-127 + forge(0x0AFE07B2, 0x7DD78B14) // 1e-255 + }; + return factors[index]; + } + + static T nan() { + return forge(0x7ff80000, 0x00000000); + } + + static T inf() { + return forge(0x7ff00000, 0x00000000); + } + + // constructs a double floating point values from its binary representation + // we use this function to workaround platforms with single precision literals + // (for example, when -fsingle-precision-constant is passed to GCC) + static T forge(uint32_t msb, uint32_t lsb) { + union { + uint64_t integerBits; + T floatBits; + }; + integerBits = (uint64_t(msb) << 32) | lsb; + return floatBits; + } +}; + +template +struct FloatTraits { + typedef int32_t mantissa_type; + static const short mantissa_bits = 23; + static const mantissa_type mantissa_max = + (static_cast(1) << mantissa_bits) - 1; + + typedef int8_t exponent_type; + static const exponent_type exponent_max = 38; + + template + static T make_float(T m, TExponent e) { + if (e > 0) { + for (uint8_t index = 0; e != 0; index++) { + if (e & 1) m *= positiveBinaryPowerOfTen(index); + e >>= 1; + } + } else { + e = -e; + for (uint8_t index = 0; e != 0; index++) { + if (e & 1) m *= negativeBinaryPowerOfTen(index); + e >>= 1; + } + } + return m; + } + + static T positiveBinaryPowerOfTen(int index) { + static T factors[] = {1e1f, 1e2f, 1e4f, 1e8f, 1e16f, 1e32f}; + return factors[index]; + } + + static T negativeBinaryPowerOfTen(int index) { + static T factors[] = {1e-1f, 1e-2f, 1e-4f, 1e-8f, 1e-16f, 1e-32f}; + return factors[index]; + } + + static T negativeBinaryPowerOfTenPlusOne(int index) { + static T factors[] = {1e0f, 1e-1f, 1e-3f, 1e-7f, 1e-15f, 1e-31f}; + return factors[index]; + } + + static T forge(uint32_t bits) { + union { + uint32_t integerBits; + T floatBits; + }; + integerBits = bits; + return floatBits; + } + + static T nan() { + return forge(0x7fc00000); + } + + static T inf() { + return forge(0x7f800000); + } +}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/IsArray.hpp b/include/lib/ArduinoJson/TypeTraits/IsArray.hpp new file mode 100644 index 0000000..da42072 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/IsArray.hpp @@ -0,0 +1,24 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that return the type T without the const modifier +template +struct IsArray { + static const bool value = false; +}; +template +struct IsArray { + static const bool value = true; +}; +template +struct IsArray { + static const bool value = true; +}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/IsBaseOf.hpp b/include/lib/ArduinoJson/TypeTraits/IsBaseOf.hpp new file mode 100644 index 0000000..bf24e96 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/IsBaseOf.hpp @@ -0,0 +1,27 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that returns true if Derived inherits from TBase is an +// integral type. +template +class IsBaseOf { + protected: // <- to avoid GCC's "all member functions in class are private" + typedef char Yes[1]; + typedef char No[2]; + + static Yes &probe(const TBase *); + static No &probe(...); + + public: + enum { + value = sizeof(probe(reinterpret_cast(0))) == sizeof(Yes) + }; +}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/IsChar.hpp b/include/lib/ArduinoJson/TypeTraits/IsChar.hpp new file mode 100644 index 0000000..d97cec2 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/IsChar.hpp @@ -0,0 +1,23 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "IsSame.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that returns true if T is a charater +template +struct IsChar { + static const bool value = IsSame::value || + IsSame::value || + IsSame::value; +}; + +template +struct IsChar : IsChar {}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/IsConst.hpp b/include/lib/ArduinoJson/TypeTraits/IsConst.hpp new file mode 100644 index 0000000..512ee5c --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/IsConst.hpp @@ -0,0 +1,21 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that return the type T without the const modifier +template +struct IsConst { + static const bool value = false; +}; + +template +struct IsConst { + static const bool value = true; +}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/IsFloatingPoint.hpp b/include/lib/ArduinoJson/TypeTraits/IsFloatingPoint.hpp new file mode 100644 index 0000000..e41a682 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/IsFloatingPoint.hpp @@ -0,0 +1,18 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "IsSame.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that returns true if T is a floating point type +template +struct IsFloatingPoint { + static const bool value = IsSame::value || IsSame::value; +}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/IsIntegral.hpp b/include/lib/ArduinoJson/TypeTraits/IsIntegral.hpp new file mode 100644 index 0000000..17ae5f2 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/IsIntegral.hpp @@ -0,0 +1,26 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "IsSame.hpp" +#include "IsSignedIntegral.hpp" +#include "IsUnsignedIntegral.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that returns true if T is an integral type. +template +struct IsIntegral { + static const bool value = IsSignedIntegral::value || + IsUnsignedIntegral::value || + IsSame::value; + // CAUTION: differs from std::is_integral as it doesn't include bool +}; + +template +struct IsIntegral : IsIntegral {}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/IsSame.hpp b/include/lib/ArduinoJson/TypeTraits/IsSame.hpp new file mode 100644 index 0000000..06567c9 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/IsSame.hpp @@ -0,0 +1,21 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that returns true if types T and U are the same. +template +struct IsSame { + static const bool value = false; +}; + +template +struct IsSame { + static const bool value = true; +}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/IsSignedIntegral.hpp b/include/lib/ArduinoJson/TypeTraits/IsSignedIntegral.hpp new file mode 100644 index 0000000..7334eb9 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/IsSignedIntegral.hpp @@ -0,0 +1,28 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../Configuration.hpp" +#include "IsSame.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that returns true if T is an integral type. +template +struct IsSignedIntegral { + static const bool value = + IsSame::value || IsSame::value || + IsSame::value || IsSame::value || +#if ARDUINOJSON_USE_LONG_LONG + IsSame::value || +#endif +#if ARDUINOJSON_USE_INT64 + IsSame::value || +#endif + false; +}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/IsUnsignedIntegral.hpp b/include/lib/ArduinoJson/TypeTraits/IsUnsignedIntegral.hpp new file mode 100644 index 0000000..938423f --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/IsUnsignedIntegral.hpp @@ -0,0 +1,28 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../Configuration.hpp" +#include "IsSame.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that returns true if T is an integral type. +template +struct IsUnsignedIntegral { + static const bool value = + IsSame::value || IsSame::value || + IsSame::value || IsSame::value || +#if ARDUINOJSON_USE_LONG_LONG + IsSame::value || +#endif +#if ARDUINOJSON_USE_INT64 + IsSame::value || +#endif + false; +}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/IsVariant.hpp b/include/lib/ArduinoJson/TypeTraits/IsVariant.hpp new file mode 100644 index 0000000..f8b299f --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/IsVariant.hpp @@ -0,0 +1,17 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "IsBaseOf.hpp" + +namespace ArduinoJson { +namespace Internals { + +class JsonVariantTag {}; + +template +struct IsVariant : IsBaseOf {}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/RemoveConst.hpp b/include/lib/ArduinoJson/TypeTraits/RemoveConst.hpp new file mode 100644 index 0000000..39d4cb5 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/RemoveConst.hpp @@ -0,0 +1,20 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that return the type T without the const modifier +template +struct RemoveConst { + typedef T type; +}; +template +struct RemoveConst { + typedef T type; +}; +} +} diff --git a/include/lib/ArduinoJson/TypeTraits/RemoveReference.hpp b/include/lib/ArduinoJson/TypeTraits/RemoveReference.hpp new file mode 100644 index 0000000..395a128 --- /dev/null +++ b/include/lib/ArduinoJson/TypeTraits/RemoveReference.hpp @@ -0,0 +1,20 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +// A meta-function that return the type T without the reference modifier. +template +struct RemoveReference { + typedef T type; +}; +template +struct RemoveReference { + typedef T type; +}; +} +} diff --git a/include/lib/ArduinoJson/version.hpp b/include/lib/ArduinoJson/version.hpp new file mode 100644 index 0000000..a71c3ab --- /dev/null +++ b/include/lib/ArduinoJson/version.hpp @@ -0,0 +1,10 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#define ARDUINOJSON_VERSION "5.13.2" +#define ARDUINOJSON_VERSION_MAJOR 5 +#define ARDUINOJSON_VERSION_MINOR 13 +#define ARDUINOJSON_VERSION_REVISION 2 diff --git a/include/lib/binn.h b/include/lib/binn.h new file mode 100644 index 0000000..d8d1733 --- /dev/null +++ b/include/lib/binn.h @@ -0,0 +1,923 @@ + +// TO ENABLE INLINE FUNCTIONS: +// ON MSVC: enable the 'Inline Function Expansion' (/Ob2) compiler option, and maybe the +// 'Whole Program Optimitazion' (/GL), that requires the +// 'Link Time Code Generation' (/LTCG) linker option to be enabled too + +#ifndef BINN_H +#define BINN_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifndef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void *)0) +#endif +#endif + +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +#ifndef BOOL +typedef int BOOL; +#endif + +#ifndef APIENTRY + #ifdef _WIN32 + #define APIENTRY __stdcall + #else + //#define APIENTRY __attribute__((stdcall)) + #define APIENTRY + #endif +#endif + +#ifndef BINN_PRIVATE + #ifdef DEBUG + #define BINN_PRIVATE + #else + #define BINN_PRIVATE static + #endif +#endif + +#ifdef _MSC_VER + #define INLINE __inline + #define ALWAYS_INLINE __forceinline +#else + // you can change to 'extern inline' if using the gcc option -flto + #define INLINE static inline + #define ALWAYS_INLINE static inline __attribute__((always_inline)) +#endif + +#ifndef int64 +#if defined(_MSC_VER) || defined(__BORLANDC__) + typedef __int64 int64; + typedef unsigned __int64 uint64; +#else + typedef long long int int64; + typedef unsigned long long int uint64; +#endif +#endif + +#ifdef _WIN32 +#define INT64_FORMAT "I64i" +#define UINT64_FORMAT "I64u" +#define INT64_HEX_FORMAT "I64x" +#else +#define INT64_FORMAT "lli" +#define UINT64_FORMAT "llu" +#define INT64_HEX_FORMAT "llx" +#endif + + +// BINN CONSTANTS ---------------------------------------- + +#define INVALID_BINN 0 + +// Storage Data Types ------------------------------------ + +#define BINN_STORAGE_NOBYTES 0x00 +#define BINN_STORAGE_BYTE 0x20 // 8 bits +#define BINN_STORAGE_WORD 0x40 // 16 bits -- the endianess (byte order) is automatically corrected +#define BINN_STORAGE_DWORD 0x60 // 32 bits -- the endianess (byte order) is automatically corrected +#define BINN_STORAGE_QWORD 0x80 // 64 bits -- the endianess (byte order) is automatically corrected +#define BINN_STORAGE_STRING 0xA0 // Are stored with null termination +#define BINN_STORAGE_BLOB 0xC0 +#define BINN_STORAGE_CONTAINER 0xE0 +#define BINN_STORAGE_VIRTUAL 0x80000 +//-- +#define BINN_STORAGE_MIN BINN_STORAGE_NOBYTES +#define BINN_STORAGE_MAX BINN_STORAGE_CONTAINER + +#define BINN_STORAGE_MASK 0xE0 +#define BINN_STORAGE_MASK16 0xE000 +#define BINN_STORAGE_HAS_MORE 0x10 +#define BINN_TYPE_MASK 0x0F +#define BINN_TYPE_MASK16 0x0FFF + +#define BINN_MAX_VALUE_MASK 0xFFFFF +//++ + +// Data Formats ------------------------------------------ + +#define BINN_LIST 0xE0 +#define BINN_MAP 0xE1 +#define BINN_OBJECT 0xE2 + +#define BINN_NULL 0x00 +#define BINN_TRUE 0x01 +#define BINN_FALSE 0x02 + +#define BINN_UINT8 0x20 // (BYTE) (unsigned byte) Is the default format for the BYTE type +#define BINN_INT8 0x21 // (BYTE) (signed byte, from -128 to +127. The 0x80 is the sign bit, so the range in hex is from 0x80 [-128] to 0x7F [127], being 0x00 = 0 and 0xFF = -1) +#define BINN_UINT16 0x40 // (WORD) (unsigned integer) Is the default format for the WORD type +#define BINN_INT16 0x41 // (WORD) (signed integer) +#define BINN_UINT32 0x60 // (DWORD) (unsigned integer) Is the default format for the DWORD type +#define BINN_INT32 0x61 // (DWORD) (signed integer) +#define BINN_UINT64 0x80 // (QWORD) (unsigned integer) Is the default format for the QWORD type +#define BINN_INT64 0x81 // (QWORD) (signed integer) + +#define BINN_SCHAR BINN_INT8 +#define BINN_UCHAR BINN_UINT8 + +#define BINN_STRING 0xA0 // (STRING) Raw String +#define BINN_DATETIME 0xA1 // (STRING) iso8601 format -- YYYY-MM-DD HH:MM:SS +#define BINN_DATE 0xA2 // (STRING) iso8601 format -- YYYY-MM-DD +#define BINN_TIME 0xA3 // (STRING) iso8601 format -- HH:MM:SS +#define BINN_DECIMAL 0xA4 // (STRING) High precision number - used for generic decimal values and for those ones that cannot be represented in the float64 format. +#define BINN_CURRENCYSTR 0xA5 // (STRING) With currency unit/symbol - check for some iso standard format +#define BINN_SINGLE_STR 0xA6 // (STRING) Can be restored to float32 +#define BINN_DOUBLE_STR 0xA7 // (STRING) May be restored to float64 + +#define BINN_FLOAT32 0x62 // (DWORD) +#define BINN_FLOAT64 0x82 // (QWORD) +#define BINN_FLOAT BINN_FLOAT32 +#define BINN_SINGLE BINN_FLOAT32 +#define BINN_DOUBLE BINN_FLOAT64 + +#define BINN_CURRENCY 0x83 // (QWORD) + +#define BINN_BLOB 0xC0 // (BLOB) Raw Blob + + +// virtual types: + +#define BINN_BOOL 0x80061 // (DWORD) The value may be 0 or 1 + +#ifdef BINN_EXTENDED +//#define BINN_SINGLE 0x800A1 // (STRING) Can be restored to float32 +//#define BINN_DOUBLE 0x800A2 // (STRING) May be restored to float64 +#endif + +//#define BINN_BINN 0x800E1 // (CONTAINER) +//#define BINN_BINN_BUFFER 0x800C1 // (BLOB) user binn. it's not open by the parser + + +// extended content types: + +// strings: + +#define BINN_HTML 0xB001 +#define BINN_XML 0xB002 +#define BINN_JSON 0xB003 +#define BINN_JAVASCRIPT 0xB004 +#define BINN_CSS 0xB005 + +// blobs: + +#define BINN_JPEG 0xD001 +#define BINN_GIF 0xD002 +#define BINN_PNG 0xD003 +#define BINN_BMP 0xD004 + + + +//-- + +// type families +#define BINN_FAMILY_NONE 0x00 +#define BINN_FAMILY_NULL 0xf1 +#define BINN_FAMILY_INT 0xf2 +#define BINN_FAMILY_FLOAT 0xf3 +#define BINN_FAMILY_STRING 0xf4 +#define BINN_FAMILY_BLOB 0xf5 +#define BINN_FAMILY_BOOL 0xf6 +#define BINN_FAMILY_BINN 0xf7 + +// integer types related to signal +#define BINN_SIGNED_INT 11 +#define BINN_UNSIGNED_INT 22 + +//++ + + +typedef void (*binn_mem_free)(void*); +#define BINN_STATIC ((binn_mem_free)0) +#define BINN_TRANSIENT ((binn_mem_free)-1) + + +// --- BINN STRUCTURE -------------------------------------------------------------- + + +struct binn_struct { + int header; // this struct header holds the magic number (BINN_MAGIC) that identifies this memory block as a binn structure + BOOL allocated; // the struct can be allocated using malloc_fn() or can be on the stack + BOOL writable; // did it was create for writing? it can use the pbuf if not unified with ptr + BOOL dirty; // the container header is not written to the buffer + // + void *pbuf; // use *ptr below? + BOOL pre_allocated; + int alloc_size; + int used_size; + // + int type; + void *ptr; + int size; + int count; + // + binn_mem_free freefn; // used only when type == BINN_STRING or BINN_BLOB + // + union { + signed char vint8; + signed short vint16; + signed int vint32; + int64 vint64; + unsigned char vuint8; + unsigned short vuint16; + unsigned int vuint32; + uint64 vuint64; + // + signed char vchar; + unsigned char vuchar; + signed short vshort; + unsigned short vushort; + signed int vint; + unsigned int vuint; + // + float vfloat; + double vdouble; + // + BOOL vbool; + }; + // + BOOL disable_int_compression; +}; + +typedef struct binn_struct binn; + + + +// --- GENERAL FUNCTIONS ---------------------------------------------------------- + + +void APIENTRY binn_set_alloc_functions(void* (*new_malloc)(size_t), void* (*new_realloc)(void*,size_t), void (*new_free)(void*)); + +int APIENTRY binn_create_type(int storage_type, int data_type_index); +BOOL APIENTRY binn_get_type_info(int long_type, int *pstorage_type, int *pextra_type); + +int APIENTRY binn_get_write_storage(int type); +int APIENTRY binn_get_read_storage(int type); + +BOOL APIENTRY binn_is_container(binn *item); + + +// --- WRITE FUNCTIONS ------------------------------------------------------------ + +// create a new binn allocating memory for the structure +binn * APIENTRY binn_new(int type, int size, void *buffer); +binn * APIENTRY binn_list(); +binn * APIENTRY binn_map(); +binn * APIENTRY binn_object(); + +// create a new binn storing the structure on the stack +BOOL APIENTRY binn_create(binn *item, int type, int size, void *buffer); +BOOL APIENTRY binn_create_list(binn *list); +BOOL APIENTRY binn_create_map(binn *map); +BOOL APIENTRY binn_create_object(binn *object); + + +BOOL APIENTRY binn_list_add_new(binn *list, binn *value); +BOOL APIENTRY binn_map_set_new(binn *map, int id, binn *value); +BOOL APIENTRY binn_object_set_new(binn *obj, char *key, binn *value); + + +// extended interface + +BOOL APIENTRY binn_list_add(binn *list, int type, void *pvalue, int size); +BOOL APIENTRY binn_map_set(binn *map, int id, int type, void *pvalue, int size); +BOOL APIENTRY binn_object_set(binn *obj, char *key, int type, void *pvalue, int size); + + +// release memory + +void APIENTRY binn_free(binn *item); +void * APIENTRY binn_release(binn *item); // free the binn structure but keeps the binn buffer allocated, returning a pointer to it. use the free function to release the buffer later + + +// --- CREATING VALUES --------------------------------------------------- + +binn * APIENTRY binn_value(int type, void *pvalue, int size, binn_mem_free freefn); + +ALWAYS_INLINE binn * binn_int8(signed char value) { + return binn_value(BINN_INT8, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_int16(short value) { + return binn_value(BINN_INT16, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_int32(int value) { + return binn_value(BINN_INT32, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_int64(int64 value) { + return binn_value(BINN_INT64, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_uint8(unsigned char value) { + return binn_value(BINN_UINT8, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_uint16(unsigned short value) { + return binn_value(BINN_UINT16, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_uint32(unsigned int value) { + return binn_value(BINN_UINT32, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_uint64(uint64 value) { + return binn_value(BINN_UINT64, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_float(float value) { + return binn_value(BINN_FLOAT, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_double(double value) { + return binn_value(BINN_DOUBLE, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_bool(BOOL value) { + return binn_value(BINN_BOOL, &value, 0, NULL); +} +ALWAYS_INLINE binn * binn_null() { + return binn_value(BINN_NULL, NULL, 0, NULL); +} +ALWAYS_INLINE binn * binn_string(char *str, binn_mem_free freefn) { + return binn_value(BINN_STRING, str, 0, freefn); +} +ALWAYS_INLINE binn * binn_blob(void *ptr, int size, binn_mem_free freefn) { + return binn_value(BINN_BLOB, ptr, size, freefn); +} + + +// --- READ FUNCTIONS ------------------------------------------------------------- + +// these functions accept pointer to the binn structure and pointer to the binn buffer +void * APIENTRY binn_ptr(void *ptr); +int APIENTRY binn_size(void *ptr); +int APIENTRY binn_type(void *ptr); +int APIENTRY binn_count(void *ptr); + +BOOL APIENTRY binn_is_valid(void *ptr, int *ptype, int *pcount, int *psize); +/* the function returns the values (type, count and size) and they don't need to be + initialized. these values are read from the buffer. example: + + int type, count, size; + result = binn_is_valid(ptr, &type, &count, &size); +*/ +BOOL APIENTRY binn_is_valid_ex(void *ptr, int *ptype, int *pcount, int *psize); +/* if some value is informed (type, count or size) then the function will check if + the value returned from the serialized data matches the informed value. otherwise + the values must be initialized to zero. example: + + int type=0, count=0, size = known_size; + result = binn_is_valid_ex(ptr, &type, &count, &size); +*/ + +BOOL APIENTRY binn_is_struct(void *ptr); + + +// Loading a binn buffer into a binn value - this is optional + +BOOL APIENTRY binn_load(void *data, binn *item); // on stack +binn * APIENTRY binn_open(void *data); // allocated + + +// easiest interface to use, but don't check if the value is there + +signed char APIENTRY binn_list_int8(void *list, int pos); +short APIENTRY binn_list_int16(void *list, int pos); +int APIENTRY binn_list_int32(void *list, int pos); +int64 APIENTRY binn_list_int64(void *list, int pos); +unsigned char APIENTRY binn_list_uint8(void *list, int pos); +unsigned short APIENTRY binn_list_uint16(void *list, int pos); +unsigned int APIENTRY binn_list_uint32(void *list, int pos); +uint64 APIENTRY binn_list_uint64(void *list, int pos); +float APIENTRY binn_list_float(void *list, int pos); +double APIENTRY binn_list_double(void *list, int pos); +BOOL APIENTRY binn_list_bool(void *list, int pos); +BOOL APIENTRY binn_list_null(void *list, int pos); +char * APIENTRY binn_list_str(void *list, int pos); +void * APIENTRY binn_list_blob(void *list, int pos, int *psize); +void * APIENTRY binn_list_list(void *list, int pos); +void * APIENTRY binn_list_map(void *list, int pos); +void * APIENTRY binn_list_object(void *list, int pos); + +signed char APIENTRY binn_map_int8(void *map, int id); +short APIENTRY binn_map_int16(void *map, int id); +int APIENTRY binn_map_int32(void *map, int id); +int64 APIENTRY binn_map_int64(void *map, int id); +unsigned char APIENTRY binn_map_uint8(void *map, int id); +unsigned short APIENTRY binn_map_uint16(void *map, int id); +unsigned int APIENTRY binn_map_uint32(void *map, int id); +uint64 APIENTRY binn_map_uint64(void *map, int id); +float APIENTRY binn_map_float(void *map, int id); +double APIENTRY binn_map_double(void *map, int id); +BOOL APIENTRY binn_map_bool(void *map, int id); +BOOL APIENTRY binn_map_null(void *map, int id); +char * APIENTRY binn_map_str(void *map, int id); +void * APIENTRY binn_map_blob(void *map, int id, int *psize); +void * APIENTRY binn_map_list(void *map, int id); +void * APIENTRY binn_map_map(void *map, int id); +void * APIENTRY binn_map_object(void *map, int id); + +signed char APIENTRY binn_object_int8(void *obj, char *key); +short APIENTRY binn_object_int16(void *obj, char *key); +int APIENTRY binn_object_int32(void *obj, char *key); +int64 APIENTRY binn_object_int64(void *obj, char *key); +unsigned char APIENTRY binn_object_uint8(void *obj, char *key); +unsigned short APIENTRY binn_object_uint16(void *obj, char *key); +unsigned int APIENTRY binn_object_uint32(void *obj, char *key); +uint64 APIENTRY binn_object_uint64(void *obj, char *key); +float APIENTRY binn_object_float(void *obj, char *key); +double APIENTRY binn_object_double(void *obj, char *key); +BOOL APIENTRY binn_object_bool(void *obj, char *key); +BOOL APIENTRY binn_object_null(void *obj, char *key); +char * APIENTRY binn_object_str(void *obj, char *key); +void * APIENTRY binn_object_blob(void *obj, char *key, int *psize); +void * APIENTRY binn_object_list(void *obj, char *key); +void * APIENTRY binn_object_map(void *obj, char *key); +void * APIENTRY binn_object_object(void *obj, char *key); + + +// return a pointer to an allocated binn structure - must be released with the free() function or equivalent set in binn_set_alloc_functions() +binn * APIENTRY binn_list_value(void *list, int pos); +binn * APIENTRY binn_map_value(void *map, int id); +binn * APIENTRY binn_object_value(void *obj, char *key); + +// read the value to a binn structure on the stack +BOOL APIENTRY binn_list_get_value(void* list, int pos, binn *value); +BOOL APIENTRY binn_map_get_value(void* map, int id, binn *value); +BOOL APIENTRY binn_object_get_value(void *obj, char *key, binn *value); + +// single interface - these functions check the data type +BOOL APIENTRY binn_list_get(void *list, int pos, int type, void *pvalue, int *psize); +BOOL APIENTRY binn_map_get(void *map, int id, int type, void *pvalue, int *psize); +BOOL APIENTRY binn_object_get(void *obj, char *key, int type, void *pvalue, int *psize); + +// these 3 functions return a pointer to the value and the data type +// they are thread-safe on big-endian devices +// on little-endian devices they are thread-safe only to return pointers to list, map, object, blob and strings +// the returned pointer to 16, 32 and 64 bits values must be used only by single-threaded applications +void * APIENTRY binn_list_read(void *list, int pos, int *ptype, int *psize); +void * APIENTRY binn_map_read(void *map, int id, int *ptype, int *psize); +void * APIENTRY binn_object_read(void *obj, char *key, int *ptype, int *psize); + + +// READ PAIR FUNCTIONS + +// these functions use base 1 in the 'pos' argument + +// on stack +BOOL APIENTRY binn_map_get_pair(void *map, int pos, int *pid, binn *value); +BOOL APIENTRY binn_object_get_pair(void *obj, int pos, char *pkey, binn *value); // must free the memory returned in the pkey + +// allocated +binn * APIENTRY binn_map_pair(void *map, int pos, int *pid); +binn * APIENTRY binn_object_pair(void *obj, int pos, char *pkey); // must free the memory returned in the pkey + +// these 2 functions return a pointer to the value and the data type +// they are thread-safe on big-endian devices +// on little-endian devices they are thread-safe only to return pointers to list, map, object, blob and strings +// the returned pointer to 16, 32 and 64 bits values must be used only by single-threaded applications +void * APIENTRY binn_map_read_pair(void *ptr, int pos, int *pid, int *ptype, int *psize); +void * APIENTRY binn_object_read_pair(void *ptr, int pos, char *pkey, int *ptype, int *psize); + + +// SEQUENTIAL READ FUNCTIONS + +typedef struct binn_iter_struct { + unsigned char *pnext; + unsigned char *plimit; + int type; + int count; + int current; +} binn_iter; + +BOOL APIENTRY binn_iter_init(binn_iter *iter, void *pbuf, int type); + +// allocated +binn * APIENTRY binn_list_next_value(binn_iter *iter); +binn * APIENTRY binn_map_next_value(binn_iter *iter, int *pid); +binn * APIENTRY binn_object_next_value(binn_iter *iter, char *pkey); // the key must be declared as: char key[256]; + +// on stack +BOOL APIENTRY binn_list_next(binn_iter *iter, binn *value); +BOOL APIENTRY binn_map_next(binn_iter *iter, int *pid, binn *value); +BOOL APIENTRY binn_object_next(binn_iter *iter, char *pkey, binn *value); // the key must be declared as: char key[256]; + +// these 3 functions return a pointer to the value and the data type +// they are thread-safe on big-endian devices +// on little-endian devices they are thread-safe only to return pointers to list, map, object, blob and strings +// the returned pointer to 16, 32 and 64 bits values must be used only by single-threaded applications +void * APIENTRY binn_list_read_next(binn_iter *iter, int *ptype, int *psize); +void * APIENTRY binn_map_read_next(binn_iter *iter, int *pid, int *ptype, int *psize); +void * APIENTRY binn_object_read_next(binn_iter *iter, char *pkey, int *ptype, int *psize); // the key must be declared as: char key[256]; + + +// --- MACROS ------------------------------------------------------------ + + +#define binn_is_writable(item) (item)->writable; + + +// set values on stack allocated binn structures + +#define binn_set_null(item) do { (item)->type = BINN_NULL; } while (0) + +#define binn_set_bool(item,value) do { (item)->type = BINN_BOOL; (item)->vbool = value; (item)->ptr = &((item)->vbool); } while (0) + +#define binn_set_int(item,value) do { (item)->type = BINN_INT32; (item)->vint32 = value; (item)->ptr = &((item)->vint32); } while (0) +#define binn_set_int64(item,value) do { (item)->type = BINN_INT64; (item)->vint64 = value; (item)->ptr = &((item)->vint64); } while (0) + +#define binn_set_uint(item,value) do { (item)->type = BINN_UINT32; (item)->vuint32 = value; (item)->ptr = &((item)->vuint32); } while (0) +#define binn_set_uint64(item,value) do { (item)->type = BINN_UINT64; (item)->vuint64 = value; (item)->ptr = &((item)->vuint64); } while (0) + +#define binn_set_float(item,value) do { (item)->type = BINN_FLOAT; (item)->vfloat = value; (item)->ptr = &((item)->vfloat); } while (0) +#define binn_set_double(item,value) do { (item)->type = BINN_DOUBLE; (item)->vdouble = value; (item)->ptr = &((item)->vdouble); } while (0) + +//#define binn_set_string(item,str,pfree) do { (item)->type = BINN_STRING; (item)->ptr = str; (item)->freefn = pfree; } while (0) +//#define binn_set_blob(item,ptr,size,pfree) do { (item)->type = BINN_BLOB; (item)->ptr = ptr; (item)->freefn = pfree; (item)->size = size; } while (0) +BOOL APIENTRY binn_set_string(binn *item, char *str, binn_mem_free pfree); +BOOL APIENTRY binn_set_blob(binn *item, void *ptr, int size, binn_mem_free pfree); + + +//#define binn_double(value) { (item)->type = BINN_DOUBLE; (item)->vdouble = value; (item)->ptr = &((item)->vdouble) } + + + +// FOREACH MACROS +// must use these declarations in the function that will use them: +// binn_iter iter; +// char key[256]; // only for the object +// int id; // only for the map +// binn value; + +#define binn_object_foreach(object, key, value) \ + binn_iter_init(&iter, object, BINN_OBJECT); \ + while (binn_object_next(&iter, key, &value)) + +#define binn_map_foreach(map, id, value) \ + binn_iter_init(&iter, map, BINN_MAP); \ + while (binn_map_next(&iter, &id, &value)) + +#define binn_list_foreach(list, value) \ + binn_iter_init(&iter, list, BINN_LIST); \ + while (binn_list_next(&iter, &value)) + + + +/*************************************************************************************/ +/*** SET FUNCTIONS *******************************************************************/ +/*************************************************************************************/ + +ALWAYS_INLINE BOOL binn_list_add_int8(binn *list, signed char value) { + return binn_list_add(list, BINN_INT8, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_int16(binn *list, short value) { + return binn_list_add(list, BINN_INT16, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_int32(binn *list, int value) { + return binn_list_add(list, BINN_INT32, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_int64(binn *list, int64 value) { + return binn_list_add(list, BINN_INT64, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_uint8(binn *list, unsigned char value) { + return binn_list_add(list, BINN_UINT8, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_uint16(binn *list, unsigned short value) { + return binn_list_add(list, BINN_UINT16, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_uint32(binn *list, unsigned int value) { + return binn_list_add(list, BINN_UINT32, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_uint64(binn *list, uint64 value) { + return binn_list_add(list, BINN_UINT64, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_float(binn *list, float value) { + return binn_list_add(list, BINN_FLOAT32, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_double(binn *list, double value) { + return binn_list_add(list, BINN_FLOAT64, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_bool(binn *list, BOOL value) { + return binn_list_add(list, BINN_BOOL, &value, 0); +} +ALWAYS_INLINE BOOL binn_list_add_null(binn *list) { + return binn_list_add(list, BINN_NULL, NULL, 0); +} +ALWAYS_INLINE BOOL binn_list_add_str(binn *list, char *str) { + return binn_list_add(list, BINN_STRING, str, 0); +} +ALWAYS_INLINE BOOL binn_list_add_blob(binn *list, void *ptr, int size) { + return binn_list_add(list, BINN_BLOB, ptr, size); +} +ALWAYS_INLINE BOOL binn_list_add_list(binn *list, void *list2) { + return binn_list_add(list, BINN_LIST, binn_ptr(list2), binn_size(list2)); +} +ALWAYS_INLINE BOOL binn_list_add_map(binn *list, void *map) { + return binn_list_add(list, BINN_MAP, binn_ptr(map), binn_size(map)); +} +ALWAYS_INLINE BOOL binn_list_add_object(binn *list, void *obj) { + return binn_list_add(list, BINN_OBJECT, binn_ptr(obj), binn_size(obj)); +} +ALWAYS_INLINE BOOL binn_list_add_value(binn *list, binn *value) { + return binn_list_add(list, value->type, binn_ptr(value), binn_size(value)); +} + +/*************************************************************************************/ + +ALWAYS_INLINE BOOL binn_map_set_int8(binn *map, int id, signed char value) { + return binn_map_set(map, id, BINN_INT8, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_int16(binn *map, int id, short value) { + return binn_map_set(map, id, BINN_INT16, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_int32(binn *map, int id, int value) { + return binn_map_set(map, id, BINN_INT32, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_int64(binn *map, int id, int64 value) { + return binn_map_set(map, id, BINN_INT64, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_uint8(binn *map, int id, unsigned char value) { + return binn_map_set(map, id, BINN_UINT8, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_uint16(binn *map, int id, unsigned short value) { + return binn_map_set(map, id, BINN_UINT16, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_uint32(binn *map, int id, unsigned int value) { + return binn_map_set(map, id, BINN_UINT32, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_uint64(binn *map, int id, uint64 value) { + return binn_map_set(map, id, BINN_UINT64, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_float(binn *map, int id, float value) { + return binn_map_set(map, id, BINN_FLOAT32, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_double(binn *map, int id, double value) { + return binn_map_set(map, id, BINN_FLOAT64, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_bool(binn *map, int id, BOOL value) { + return binn_map_set(map, id, BINN_BOOL, &value, 0); +} +ALWAYS_INLINE BOOL binn_map_set_null(binn *map, int id) { + return binn_map_set(map, id, BINN_NULL, NULL, 0); +} +ALWAYS_INLINE BOOL binn_map_set_str(binn *map, int id, char *str) { + return binn_map_set(map, id, BINN_STRING, str, 0); +} +ALWAYS_INLINE BOOL binn_map_set_blob(binn *map, int id, void *ptr, int size) { + return binn_map_set(map, id, BINN_BLOB, ptr, size); +} +ALWAYS_INLINE BOOL binn_map_set_list(binn *map, int id, void *list) { + return binn_map_set(map, id, BINN_LIST, binn_ptr(list), binn_size(list)); +} +ALWAYS_INLINE BOOL binn_map_set_map(binn *map, int id, void *map2) { + return binn_map_set(map, id, BINN_MAP, binn_ptr(map2), binn_size(map2)); +} +ALWAYS_INLINE BOOL binn_map_set_object(binn *map, int id, void *obj) { + return binn_map_set(map, id, BINN_OBJECT, binn_ptr(obj), binn_size(obj)); +} +ALWAYS_INLINE BOOL binn_map_set_value(binn *map, int id, binn *value) { + return binn_map_set(map, id, value->type, binn_ptr(value), binn_size(value)); +} + +/*************************************************************************************/ + +ALWAYS_INLINE BOOL binn_object_set_int8(binn *obj, char *key, signed char value) { + return binn_object_set(obj, key, BINN_INT8, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_int16(binn *obj, char *key, short value) { + return binn_object_set(obj, key, BINN_INT16, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_int32(binn *obj, char *key, int value) { + return binn_object_set(obj, key, BINN_INT32, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_int64(binn *obj, char *key, int64 value) { + return binn_object_set(obj, key, BINN_INT64, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_uint8(binn *obj, char *key, unsigned char value) { + return binn_object_set(obj, key, BINN_UINT8, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_uint16(binn *obj, char *key, unsigned short value) { + return binn_object_set(obj, key, BINN_UINT16, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_uint32(binn *obj, char *key, unsigned int value) { + return binn_object_set(obj, key, BINN_UINT32, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_uint64(binn *obj, char *key, uint64 value) { + return binn_object_set(obj, key, BINN_UINT64, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_float(binn *obj, char *key, float value) { + return binn_object_set(obj, key, BINN_FLOAT32, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_double(binn *obj, char *key, double value) { + return binn_object_set(obj, key, BINN_FLOAT64, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_bool(binn *obj, char *key, BOOL value) { + return binn_object_set(obj, key, BINN_BOOL, &value, 0); +} +ALWAYS_INLINE BOOL binn_object_set_null(binn *obj, char *key) { + return binn_object_set(obj, key, BINN_NULL, NULL, 0); +} +ALWAYS_INLINE BOOL binn_object_set_str(binn *obj, char *key, char *str) { + return binn_object_set(obj, key, BINN_STRING, str, 0); +} +ALWAYS_INLINE BOOL binn_object_set_blob(binn *obj, char *key, void *ptr, int size) { + return binn_object_set(obj, key, BINN_BLOB, ptr, size); +} +ALWAYS_INLINE BOOL binn_object_set_list(binn *obj, char *key, void *list) { + return binn_object_set(obj, key, BINN_LIST, binn_ptr(list), binn_size(list)); +} +ALWAYS_INLINE BOOL binn_object_set_map(binn *obj, char *key, void *map) { + return binn_object_set(obj, key, BINN_MAP, binn_ptr(map), binn_size(map)); +} +ALWAYS_INLINE BOOL binn_object_set_object(binn *obj, char *key, void *obj2) { + return binn_object_set(obj, key, BINN_OBJECT, binn_ptr(obj2), binn_size(obj2)); +} +ALWAYS_INLINE BOOL binn_object_set_value(binn *obj, char *key, binn *value) { + return binn_object_set(obj, key, value->type, binn_ptr(value), binn_size(value)); +} + +/*************************************************************************************/ +/*** GET FUNCTIONS *******************************************************************/ +/*************************************************************************************/ + +ALWAYS_INLINE BOOL binn_list_get_int8(void *list, int pos, signed char *pvalue) { + return binn_list_get(list, pos, BINN_INT8, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_int16(void *list, int pos, short *pvalue) { + return binn_list_get(list, pos, BINN_INT16, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_int32(void *list, int pos, int *pvalue) { + return binn_list_get(list, pos, BINN_INT32, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_int64(void *list, int pos, int64 *pvalue) { + return binn_list_get(list, pos, BINN_INT64, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_uint8(void *list, int pos, unsigned char *pvalue) { + return binn_list_get(list, pos, BINN_UINT8, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_uint16(void *list, int pos, unsigned short *pvalue) { + return binn_list_get(list, pos, BINN_UINT16, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_uint32(void *list, int pos, unsigned int *pvalue) { + return binn_list_get(list, pos, BINN_UINT32, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_uint64(void *list, int pos, uint64 *pvalue) { + return binn_list_get(list, pos, BINN_UINT64, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_float(void *list, int pos, float *pvalue) { + return binn_list_get(list, pos, BINN_FLOAT32, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_double(void *list, int pos, double *pvalue) { + return binn_list_get(list, pos, BINN_FLOAT64, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_bool(void *list, int pos, BOOL *pvalue) { + return binn_list_get(list, pos, BINN_BOOL, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_str(void *list, int pos, char **pvalue) { + return binn_list_get(list, pos, BINN_STRING, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_blob(void *list, int pos, void **pvalue, int *psize) { + return binn_list_get(list, pos, BINN_BLOB, pvalue, psize); +} +ALWAYS_INLINE BOOL binn_list_get_list(void *list, int pos, void **pvalue) { + return binn_list_get(list, pos, BINN_LIST, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_map(void *list, int pos, void **pvalue) { + return binn_list_get(list, pos, BINN_MAP, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_list_get_object(void *list, int pos, void **pvalue) { + return binn_list_get(list, pos, BINN_OBJECT, pvalue, NULL); +} + +/***************************************************************************/ + +ALWAYS_INLINE BOOL binn_map_get_int8(void *map, int id, signed char *pvalue) { + return binn_map_get(map, id, BINN_INT8, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_int16(void *map, int id, short *pvalue) { + return binn_map_get(map, id, BINN_INT16, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_int32(void *map, int id, int *pvalue) { + return binn_map_get(map, id, BINN_INT32, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_int64(void *map, int id, int64 *pvalue) { + return binn_map_get(map, id, BINN_INT64, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_uint8(void *map, int id, unsigned char *pvalue) { + return binn_map_get(map, id, BINN_UINT8, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_uint16(void *map, int id, unsigned short *pvalue) { + return binn_map_get(map, id, BINN_UINT16, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_uint32(void *map, int id, unsigned int *pvalue) { + return binn_map_get(map, id, BINN_UINT32, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_uint64(void *map, int id, uint64 *pvalue) { + return binn_map_get(map, id, BINN_UINT64, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_float(void *map, int id, float *pvalue) { + return binn_map_get(map, id, BINN_FLOAT32, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_double(void *map, int id, double *pvalue) { + return binn_map_get(map, id, BINN_FLOAT64, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_bool(void *map, int id, BOOL *pvalue) { + return binn_map_get(map, id, BINN_BOOL, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_str(void *map, int id, char **pvalue) { + return binn_map_get(map, id, BINN_STRING, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_blob(void *map, int id, void **pvalue, int *psize) { + return binn_map_get(map, id, BINN_BLOB, pvalue, psize); +} +ALWAYS_INLINE BOOL binn_map_get_list(void *map, int id, void **pvalue) { + return binn_map_get(map, id, BINN_LIST, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_map(void *map, int id, void **pvalue) { + return binn_map_get(map, id, BINN_MAP, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_map_get_object(void *map, int id, void **pvalue) { + return binn_map_get(map, id, BINN_OBJECT, pvalue, NULL); +} + +/***************************************************************************/ + +// usage: +// if (binn_object_get_int32(obj, "key", &value) == FALSE) xxx; + +ALWAYS_INLINE BOOL binn_object_get_int8(void *obj, char *key, signed char *pvalue) { + return binn_object_get(obj, key, BINN_INT8, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_int16(void *obj, char *key, short *pvalue) { + return binn_object_get(obj, key, BINN_INT16, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_int32(void *obj, char *key, int *pvalue) { + return binn_object_get(obj, key, BINN_INT32, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_int64(void *obj, char *key, int64 *pvalue) { + return binn_object_get(obj, key, BINN_INT64, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_uint8(void *obj, char *key, unsigned char *pvalue) { + return binn_object_get(obj, key, BINN_UINT8, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_uint16(void *obj, char *key, unsigned short *pvalue) { + return binn_object_get(obj, key, BINN_UINT16, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_uint32(void *obj, char *key, unsigned int *pvalue) { + return binn_object_get(obj, key, BINN_UINT32, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_uint64(void *obj, char *key, uint64 *pvalue) { + return binn_object_get(obj, key, BINN_UINT64, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_float(void *obj, char *key, float *pvalue) { + return binn_object_get(obj, key, BINN_FLOAT32, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_double(void *obj, char *key, double *pvalue) { + return binn_object_get(obj, key, BINN_FLOAT64, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_bool(void *obj, char *key, BOOL *pvalue) { + return binn_object_get(obj, key, BINN_BOOL, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_str(void *obj, char *key, char **pvalue) { + return binn_object_get(obj, key, BINN_STRING, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_blob(void *obj, char *key, void **pvalue, int *psize) { + return binn_object_get(obj, key, BINN_BLOB, pvalue, psize); +} +ALWAYS_INLINE BOOL binn_object_get_list(void *obj, char *key, void **pvalue) { + return binn_object_get(obj, key, BINN_LIST, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_map(void *obj, char *key, void **pvalue) { + return binn_object_get(obj, key, BINN_MAP, pvalue, NULL); +} +ALWAYS_INLINE BOOL binn_object_get_object(void *obj, char *key, void **pvalue) { + return binn_object_get(obj, key, BINN_OBJECT, pvalue, NULL); +} + +/***************************************************************************/ + +BOOL APIENTRY binn_get_int32(binn *value, int *pint); +BOOL APIENTRY binn_get_int64(binn *value, int64 *pint); +BOOL APIENTRY binn_get_double(binn *value, double *pfloat); +BOOL APIENTRY binn_get_bool(binn *value, BOOL *pbool); +char * APIENTRY binn_get_str(binn *value); + +// boolean string values: +// 1, true, yes, on +// 0, false, no, off + +// boolean number values: +// !=0 [true] +// ==0 [false] + + +#ifdef __cplusplus +} +#endif + +#endif //BINN_H diff --git a/include/lib/capnp-c/capn-list.inc b/include/lib/capnp-c/capn-list.inc new file mode 100644 index 0000000..ad94ec8 --- /dev/null +++ b/include/lib/capnp-c/capn-list.inc @@ -0,0 +1,162 @@ +/* capn-list.inc + * + * Copyright (C) 2013 James McKaskill + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ + +#define CAT2(A,B) A ## B +#define CAT(A,B) CAT2(A, B) +#define UINT_T CAT(CAT(uint, SZ), _t) +#define LIST_T CAT(capn_list, SZ) +#define FLIP CAT(capn_flip, SZ) + +UINT_T CAT(capn_get,SZ) (LIST_T l, int off) { + char *d; + capn_ptr p = l.p; + if (off >= p.len) { + return 0; + } + + switch (p.type) { + case CAPN_LIST: + if (p.datasz < SZ/8) + return 0; + d = p.data + off * (p.datasz + 8*p.ptrs); + return FLIP(*(UINT_T*)d); + + case CAPN_PTR_LIST: + d = struct_ptr(p.seg, p.data + 8*off, SZ/8); + if (d) { + return FLIP(*(UINT_T*)d); + } else { + return 0; + } + + default: + return 0; + } +} + +int CAT(capn_getv,SZ) (LIST_T l, int off, UINT_T *to, int sz) { + int i; + capn_ptr p; + capn_resolve(&l.p); + p = l.p; + if (off + sz > p.len) { + sz = p.len - off; + } + + switch (p.type) { + case CAPN_LIST: + if (p.datasz == SZ/8 && !p.ptrs && (SZ == 8 || CAPN_LITTLE)) { + memcpy(to, p.data + off, sz * (SZ/8)); + return sz; + } else if (p.datasz < SZ/8) { + return -1; + } + + for (i = 0; i < sz; i++) { + char *d = p.data + (i + off) * (p.datasz + 8*p.ptrs); + to[i] = FLIP(*(UINT_T*)d); + } + return sz; + + case CAPN_PTR_LIST: + for (i = 0; i < sz; i++) { + char *d = struct_ptr(p.seg, p.data + 8*(i+off), SZ/8); + if (d) { + to[i] = FLIP(*(UINT_T*)d); + } else { + return -1; + } + } + return sz; + + default: + return -1; + } +} + +int CAT(capn_set,SZ) (LIST_T l, int off, UINT_T v) { + char *d; + capn_ptr p = l.p; + if (off >= p.len) { + return -1; + } + + switch (p.type) { + case CAPN_LIST: + if (p.datasz < SZ/8) + return -1; + d = p.data + off * (p.datasz + 8*p.ptrs); + *(UINT_T*) d = FLIP(v); + return 0; + + case CAPN_PTR_LIST: + d = struct_ptr(p.seg, p.data + 8*off, SZ/8); + if (!d) { + return -1; + } + *(UINT_T*) d = FLIP(v); + return 0; + + default: + return -1; + } +} + +int CAT(capn_setv,SZ) (LIST_T l, int off, const UINT_T *from, int sz) { + int i; + capn_ptr p = l.p; + if (off + sz > p.len) { + sz = p.len - off; + } + + switch (p.type) { + case CAPN_LIST: + if (p.datasz == SZ/8 && !p.ptrs && (SZ == 8 || CAPN_LITTLE)) { + memcpy(p.data + off, from, sz * (SZ/8)); + return sz; + } else if (p.datasz < SZ/8) { + return -1; + } + + for (i = 0; i < sz; i++) { + char *d = p.data + (i + off) * (p.datasz + 8*p.ptrs); + *(UINT_T*) d = FLIP(from[i]); + } + return sz; + + case CAPN_PTR_LIST: + for (i = 0; i < sz; i++) { + char *d = struct_ptr(p.seg, p.data + 8*(i+off), SZ/8); + if (d) { + *(UINT_T*) d = FLIP(from[i]); + } else { + return -1; + } + } + return sz; + + default: + return -1; + } +} + +LIST_T CAT(capn_new_list,SZ) (struct capn_segment *seg, int sz) { + LIST_T l = {{CAPN_LIST}}; + l.p.seg = seg; + l.p.len = sz; + l.p.datasz = SZ/8; + new_object(&l.p, sz*(SZ/8)); + return l; +} + +#undef CAT2 +#undef CAT +#undef UINT_T +#undef LIST_T +#undef FLIP + diff --git a/include/lib/capnp-c/capnp_c.h b/include/lib/capnp-c/capnp_c.h new file mode 100644 index 0000000..28f171d --- /dev/null +++ b/include/lib/capnp-c/capnp_c.h @@ -0,0 +1,446 @@ +/* vim: set sw=8 ts=8 sts=8 noet: */ +/* capnp_c.h + * + * Copyright (C) 2013 James McKaskill + * Copyright (C) 2014 Steve Dee + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ + +#ifndef CAPNP_C_H +#define CAPNP_C_H + +#include +#include +#include + +#if defined(unix) && !defined(__APPLE__) +#include +#endif + +/* ssize_t is a POSIX type, not an ISO C one... + * Windows seems to only have SSIZE_T in BaseTsd.h + */ +#ifdef _MSC_VER +typedef intmax_t ssize_t; +#else +#include +#endif + +// AVR does not have ssize_T either +#ifdef MULTIPASS_ARCH_arduino_nano +typedef intmax_t ssize_t; +#endif + +// Cross-platform macro ALIGNED_(x) aligns a struct by `x` bytes. +#ifdef _MSC_VER +#define ALIGNED_(x) __declspec(align(x)) +#endif +#ifdef __GNUC__ +#define ALIGNED_(x) __attribute__ ((aligned(x))) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(__cplusplus) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) +#define CAPN_INLINE static inline +#else +#define CAPN_INLINE static +#endif + +#define CAPN_VERSION 1 + +/* struct capn is a common structure shared between segments in the same + * session/context so that far pointers between segments will be created. + * + * lookup is used to lookup segments by id when derefencing a far pointer + * + * create is used to create or lookup an alternate segment that has at least + * sz available (ie returned seg->len + sz <= seg->cap) + * + * create_local is used to create a segment for the copy tree and should be + * allocated in the local memory space. + * + * Allocated segments must be zero initialized. + * + * create and lookup can be NULL if you don't need multiple segments and don't + * want to support copying + * + * seglist and copylist are linked lists which can be used to free up segments + * on cleanup, but should not be modified by the user. + * + * lookup, create, create_local, and user can be set by the user. Other values + * should be zero initialized. + */ +struct capn { + /* user settable */ + struct capn_segment *(*lookup)(void* /*user*/, uint32_t /*id */); + struct capn_segment *(*create)(void* /*user*/, uint32_t /*id */, int /*sz*/); + struct capn_segment *(*create_local)(void* /*user*/, int /*sz*/); + void *user; + /* zero initialized, user should not modify */ + uint32_t segnum; + struct capn_tree *copy; + struct capn_tree *segtree; + struct capn_segment *seglist, *lastseg; + struct capn_segment *copylist; +}; + +/* struct capn_tree is a rb tree header used internally for the segment id + * lookup and copy tree */ +struct capn_tree { + struct capn_tree *parent, *link[2]; + unsigned int red : 1; +}; + +struct capn_tree *capn_tree_insert(struct capn_tree *root, struct capn_tree *n); + +/* struct capn_segment contains the information about a single segment. + * + * capn points to a struct capn that is shared between segments in the + * same session + * + * id specifies the segment id, used for far pointers + * + * data specifies the segment data. This should not move after creation. + * + * len specifies the current segment length. This is 0 for a blank + * segment. + * + * cap specifies the segment capacity. + * + * When creating new structures len will be incremented until it reaches cap, + * at which point a new segment will be requested via capn->create. The + * create callback can either create a new segment or expand an existing + * one by incrementing cap and returning the expanded segment. + * + * data, len, and cap must all be 8 byte aligned, hence the ALIGNED_(8) macro + * on the struct definition. + * + * data, len, cap, and user should all be set by the user. Other values + * should be zero initialized. + */ + +struct ALIGNED_(8) capn_segment { + struct capn_tree hdr; + struct capn_segment *next; + struct capn *capn; + uint32_t id; + /* user settable */ + char *data; + size_t len, cap; + void *user; +}; + +enum CAPN_TYPE { + CAPN_NULL = 0, + CAPN_STRUCT = 1, + CAPN_LIST = 2, + CAPN_PTR_LIST = 3, + CAPN_BIT_LIST = 4, + CAPN_FAR_POINTER = 5, +}; + +struct capn_ptr { + unsigned int type : 4; + unsigned int has_ptr_tag : 1; + unsigned int is_list_member : 1; + unsigned int is_composite_list : 1; + unsigned int datasz : 19; + unsigned int ptrs : 16; + int len; + char *data; + struct capn_segment *seg; +}; + +struct capn_text { + int len; + const char *str; + struct capn_segment *seg; +}; + +typedef struct capn_ptr capn_ptr; +typedef struct capn_text capn_text; +typedef struct {capn_ptr p;} capn_data; +typedef struct {capn_ptr p;} capn_list1; +typedef struct {capn_ptr p;} capn_list8; +typedef struct {capn_ptr p;} capn_list16; +typedef struct {capn_ptr p;} capn_list32; +typedef struct {capn_ptr p;} capn_list64; + +struct capn_msg { + struct capn_segment *seg; + uint64_t iface; + uint16_t method; + capn_ptr args; +}; + +/* capn_append_segment appends a segment to a session */ +void capn_append_segment(struct capn*, struct capn_segment*); + +capn_ptr capn_root(struct capn *c); +void capn_resolve(capn_ptr *p); + +#define capn_len(list) ((list).p.type == CAPN_FAR_POINTER ? (capn_resolve(&(list).p), (list).p.len) : (list).p.len) + +/* capn_getp|setp functions get/set ptrs in list/structs + * off is the list index or pointer index in a struct + * capn_setp will copy the data, create far pointers, etc if the target + * is in a different segment/context. + * Both of these will use/return inner pointers for composite lists. + */ +capn_ptr capn_getp(capn_ptr p, int off, int resolve); +int capn_setp(capn_ptr p, int off, capn_ptr tgt); + +capn_text capn_get_text(capn_ptr p, int off, capn_text def); +capn_data capn_get_data(capn_ptr p, int off); +int capn_set_text(capn_ptr p, int off, capn_text tgt); +/* there is no set_data -- use capn_new_list8 + capn_setv8 instead + * and set data.p = list.p */ + +/* capn_get* functions get data from a list + * The length of the list is given by p->size + * off specifies how far into the list to start + * sz indicates the number of elements to get + * The function returns the number of elements read or -1 on an error. + * off must be byte aligned for capn_getv1 + */ +int capn_get1(capn_list1 p, int off); +uint8_t capn_get8(capn_list8 p, int off); +uint16_t capn_get16(capn_list16 p, int off); +uint32_t capn_get32(capn_list32 p, int off); +uint64_t capn_get64(capn_list64 p, int off); +int capn_getv1(capn_list1 p, int off, uint8_t *data, int sz); +int capn_getv8(capn_list8 p, int off, uint8_t *data, int sz); +int capn_getv16(capn_list16 p, int off, uint16_t *data, int sz); +int capn_getv32(capn_list32 p, int off, uint32_t *data, int sz); +int capn_getv64(capn_list64 p, int off, uint64_t *data, int sz); + +/* capn_set* functions set data in a list + * off specifies how far into the list to start + * sz indicates the number of elements to write + * The function returns the number of elemnts written or -1 on an error. + * off must be byte aligned for capn_setv1 + */ +int capn_set1(capn_list1 p, int off, int v); +int capn_set8(capn_list8 p, int off, uint8_t v); +int capn_set16(capn_list16 p, int off, uint16_t v); +int capn_set32(capn_list32 p, int off, uint32_t v); +int capn_set64(capn_list64 p, int off, uint64_t v); +int capn_setv1(capn_list1 p, int off, const uint8_t *data, int sz); +int capn_setv8(capn_list8 p, int off, const uint8_t *data, int sz); +int capn_setv16(capn_list16 p, int off, const uint16_t *data, int sz); +int capn_setv32(capn_list32 p, int off, const uint32_t *data, int sz); +int capn_setv64(capn_list64 p, int off, const uint64_t *data, int sz); + +/* capn_new_* functions create a new object + * datasz is in bytes, ptrs is # of pointers, sz is # of elements in the list + * On an error a CAPN_NULL pointer is returned + */ +capn_ptr capn_new_string(struct capn_segment *seg, const char *str, ssize_t sz); +capn_ptr capn_new_struct(struct capn_segment *seg, int datasz, int ptrs); +capn_ptr capn_new_interface(struct capn_segment *seg, int datasz, int ptrs); +capn_ptr capn_new_ptr_list(struct capn_segment *seg, int sz); +capn_ptr capn_new_list(struct capn_segment *seg, int sz, int datasz, int ptrs); +capn_list1 capn_new_list1(struct capn_segment *seg, int sz); +capn_list8 capn_new_list8(struct capn_segment *seg, int sz); +capn_list16 capn_new_list16(struct capn_segment *seg, int sz); +capn_list32 capn_new_list32(struct capn_segment *seg, int sz); +capn_list64 capn_new_list64(struct capn_segment *seg, int sz); + +/* capn_read|write* functions read/write struct values + * off is the offset into the structure in bytes + * Rarely should these be called directly, instead use the generated code. + * Data must be xored with the default value + * These are inlined + */ +CAPN_INLINE uint8_t capn_read8(capn_ptr p, int off); +CAPN_INLINE uint16_t capn_read16(capn_ptr p, int off); +CAPN_INLINE uint32_t capn_read32(capn_ptr p, int off); +CAPN_INLINE uint64_t capn_read64(capn_ptr p, int off); +CAPN_INLINE int capn_write1(capn_ptr p, int off, int val); +CAPN_INLINE int capn_write8(capn_ptr p, int off, uint8_t val); +CAPN_INLINE int capn_write16(capn_ptr p, int off, uint16_t val); +CAPN_INLINE int capn_write32(capn_ptr p, int off, uint32_t val); +CAPN_INLINE int capn_write64(capn_ptr p, int off, uint64_t val); + +/* capn_init_malloc inits the capn struct with a create function which + * allocates segments on the heap using malloc + * + * capn_init_(fp|mem) inits by reading segments in from the file/memory buffer + * in serialized form (optionally packed). It will then setup the create + * function ala capn_init_malloc so that further segments can be created. + * + * capn_free frees all the segment headers and data created by the create + * function setup by capn_init_* + */ +void capn_init_malloc(struct capn *c); +int capn_init_fp(struct capn *c, FILE *f, int packed); +int capn_init_mem(struct capn *c, const uint8_t *p, size_t sz, int packed); + +/* capn_write_(fp|mem) writes segments to the file/memory buffer in + * serialized form and returns the number of bytes written. + */ +/* TODO */ +/*int capn_write_fp(struct capn *c, FILE *f, int packed);*/ +int capn_write_fd(struct capn *c, ssize_t (*write_fd)(int fd, const void *p, size_t count), int fd, int packed); +int capn_write_mem(struct capn *c, uint8_t *p, size_t sz, int packed); + +void capn_free(struct capn *c); +void capn_reset_copy(struct capn *c); + +/* Inline functions */ + + +CAPN_INLINE uint8_t capn_flip8(uint8_t v) { + return v; +} +CAPN_INLINE uint16_t capn_flip16(uint16_t v) { +#if defined(__BYTE_ORDER) && (__BYTE_ORDER == __LITTLE_ENDIAN) + return v; +#elif defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN) && \ + defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 + return __builtin_bswap16(v); +#else + union { uint16_t u; uint8_t v[2]; } s; + s.v[0] = (uint8_t)v; + s.v[1] = (uint8_t)(v>>8); + return s.u; +#endif +} +CAPN_INLINE uint32_t capn_flip32(uint32_t v) { +#if defined(__BYTE_ORDER) && (__BYTE_ORDER == __LITTLE_ENDIAN) + return v; +#elif defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN) && \ + defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 + return __builtin_bswap32(v); +#else + union { uint32_t u; uint8_t v[4]; } s; + s.v[0] = (uint8_t)v; + s.v[1] = (uint8_t)(v>>8); + s.v[2] = (uint8_t)(v>>16); + s.v[3] = (uint8_t)(v>>24); + return s.u; +#endif +} +CAPN_INLINE uint64_t capn_flip64(uint64_t v) { +#if defined(__BYTE_ORDER) && (__BYTE_ORDER == __LITTLE_ENDIAN) + return v; +#elif defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN) && \ + defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 + return __builtin_bswap64(v); +#else + union { uint64_t u; uint8_t v[8]; } s; + s.v[0] = (uint8_t)v; + s.v[1] = (uint8_t)(v>>8); + s.v[2] = (uint8_t)(v>>16); + s.v[3] = (uint8_t)(v>>24); + s.v[4] = (uint8_t)(v>>32); + s.v[5] = (uint8_t)(v>>40); + s.v[6] = (uint8_t)(v>>48); + s.v[7] = (uint8_t)(v>>56); + return s.u; +#endif +} + +CAPN_INLINE int capn_write1(capn_ptr p, int off, int val) { + if (off >= p.datasz*8) { + return -1; + } else if (val) { + uint8_t tmp = (uint8_t)(1 << (off & 7)); + ((uint8_t*) p.data)[off >> 3] |= tmp; + return 0; + } else { + uint8_t tmp = (uint8_t)(~(1 << (off & 7))); + ((uint8_t*) p.data)[off >> 3] &= tmp; + return 0; + } +} + +CAPN_INLINE uint8_t capn_read8(capn_ptr p, int off) { + return off+1 <= p.datasz ? capn_flip8(*(uint8_t*) (p.data+off)) : 0; +} +CAPN_INLINE int capn_write8(capn_ptr p, int off, uint8_t val) { + if (off+1 <= p.datasz) { + *(uint8_t*) (p.data+off) = capn_flip8(val); + return 0; + } else { + return -1; + } +} + +CAPN_INLINE uint16_t capn_read16(capn_ptr p, int off) { + return off+2 <= p.datasz ? capn_flip16(*(uint16_t*) (p.data+off)) : 0; +} +CAPN_INLINE int capn_write16(capn_ptr p, int off, uint16_t val) { + if (off+2 <= p.datasz) { + *(uint16_t*) (p.data+off) = capn_flip16(val); + return 0; + } else { + return -1; + } +} + +CAPN_INLINE uint32_t capn_read32(capn_ptr p, int off) { + return off+4 <= p.datasz ? capn_flip32(*(uint32_t*) (p.data+off)) : 0; +} +CAPN_INLINE int capn_write32(capn_ptr p, int off, uint32_t val) { + if (off+4 <= p.datasz) { + *(uint32_t*) (p.data+off) = capn_flip32(val); + return 0; + } else { + return -1; + } +} + +CAPN_INLINE uint64_t capn_read64(capn_ptr p, int off) { + return off+8 <= p.datasz ? capn_flip64(*(uint64_t*) (p.data+off)) : 0; +} +CAPN_INLINE int capn_write64(capn_ptr p, int off, uint64_t val) { + if (off+8 <= p.datasz) { + *(uint64_t*) (p.data+off) = capn_flip64(val); + return 0; + } else { + return -1; + } +} + +union capn_conv_f32 { + uint32_t u; + float f; +}; + +union capn_conv_f64 { + uint64_t u; + double f; +}; + +CAPN_INLINE float capn_to_f32(uint32_t v) { + union capn_conv_f32 u; + u.u = v; + return u.f; +} +CAPN_INLINE double capn_to_f64(uint64_t v) { + union capn_conv_f64 u; + u.u = v; + return u.f; +} +CAPN_INLINE uint32_t capn_from_f32(float v) { + union capn_conv_f32 u; + u.f = v; + return u.u; +} +CAPN_INLINE uint64_t capn_from_f64(double v) { + union capn_conv_f64 u; + u.f = v; + return u.u; +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/lib/capnp-c/capnp_priv.h b/include/lib/capnp-c/capnp_priv.h new file mode 100644 index 0000000..6c03763 --- /dev/null +++ b/include/lib/capnp-c/capnp_priv.h @@ -0,0 +1,62 @@ +/* vim: set sw=8 ts=8 sts=8 noet: */ +/* capnp_c.h + * + * Copyright (C) 2013 James McKaskill + * Copyright (C) 2014 Steve Dee + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ + +/* + * functions / structures in this header are private to the capnproto-c + * library; applications should not call or use them. + */ + +#ifndef CAPNP_PRIV_H +#define CAPNP_PRIV_H + +#include "capnp_c.h" + +#if defined(__GNUC__) && __GNUC__ >= 4 +# define intern __attribute__((visibility ("internal"))) +#else +# define intern /**/ +#endif + +/* capn_stream encapsulates the needed fields for capn_(deflate|inflate) in a + * similar manner to z_stream from zlib + * + * The user should set next_in, avail_in, next_out, avail_out to the + * available in/out buffers before calling capn_(deflate|inflate). + * + * Other fields should be zero initialized. + */ +struct capn_stream { + const uint8_t *next_in; + size_t avail_in; + uint8_t *next_out; + size_t avail_out; + unsigned zeros, raw; + + uint8_t inflate_buf[8]; + size_t avail_buf; +}; + +#define CAPN_MISALIGNED -1 +#define CAPN_NEED_MORE -2 + +/* capn_deflate deflates a stream to the packed format + * capn_inflate inflates a stream from the packed format + * + * Returns: + * CAPN_MISALIGNED - if the unpacked data is not 8 byte aligned + * CAPN_NEED_MORE - more packed data/room is required (out for inflate, in for + * deflate) + * 0 - success, all output for inflate, all input for deflate processed + */ +intern int capn_deflate(struct capn_stream*); +intern int capn_inflate(struct capn_stream*); + + +#endif /* CAPNP_PRIV_H */ diff --git a/include/lib/modernjson/adl_serializer.hpp b/include/lib/modernjson/adl_serializer.hpp new file mode 100644 index 0000000..45f2452 --- /dev/null +++ b/include/lib/modernjson/adl_serializer.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include + +#include +#include + +namespace nlohmann +{ + +template +struct adl_serializer +{ + /*! + @brief convert a JSON value to any value type + + This function is usually called by the `get()` function of the + @ref basic_json class (either explicit or via conversion operators). + + @param[in] j JSON value to read from + @param[in,out] val value to write to + */ + template + static auto from_json(BasicJsonType&& j, ValueType& val) noexcept( + noexcept(::nlohmann::from_json(std::forward(j), val))) + -> decltype(::nlohmann::from_json(std::forward(j), val), void()) + { + ::nlohmann::from_json(std::forward(j), val); + } + + /*! + @brief convert any value type to a JSON value + + This function is usually called by the constructors of the @ref basic_json + class. + + @param[in,out] j JSON value to write to + @param[in] val value to read from + */ + template + static auto to_json(BasicJsonType& j, ValueType&& val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) + -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) + { + ::nlohmann::to_json(j, std::forward(val)); + } +}; + +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/conversions/from_json.hpp b/include/lib/modernjson/detail/conversions/from_json.hpp new file mode 100644 index 0000000..817d4b7 --- /dev/null +++ b/include/lib/modernjson/detail/conversions/from_json.hpp @@ -0,0 +1,370 @@ +#pragma once + +#include // transform +#include // array +#include // and, not +#include // forward_list +#include // inserter, front_inserter, end +#include // map +#include // string +#include // tuple, make_tuple +#include // is_arithmetic, is_same, is_enum, underlying_type, is_convertible +#include // unordered_map +#include // pair, declval +#include // valarray + +#include +#include +#include +#include +#include + +namespace nlohmann +{ +namespace detail +{ +template +void from_json(const BasicJsonType& j, typename std::nullptr_t& n) +{ + if (JSON_UNLIKELY(not j.is_null())) + { + JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()))); + } + n = nullptr; +} + +// overloads for basic_json template parameters +template::value and + not std::is_same::value, + int> = 0> +void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) +{ + switch (static_cast(j)) + { + case value_t::number_unsigned: + { + val = static_cast(*j.template get_ptr()); + break; + } + case value_t::number_integer: + { + val = static_cast(*j.template get_ptr()); + break; + } + case value_t::number_float: + { + val = static_cast(*j.template get_ptr()); + break; + } + + default: + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); + } +} + +template +void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) +{ + if (JSON_UNLIKELY(not j.is_boolean())) + { + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()))); + } + b = *j.template get_ptr(); +} + +template +void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) +{ + if (JSON_UNLIKELY(not j.is_string())) + { + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + } + s = *j.template get_ptr(); +} + +template < + typename BasicJsonType, typename ConstructibleStringType, + enable_if_t < + is_constructible_string_type::value and + not std::is_same::value, + int > = 0 > +void from_json(const BasicJsonType& j, ConstructibleStringType& s) +{ + if (JSON_UNLIKELY(not j.is_string())) + { + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + } + + s = *j.template get_ptr(); +} + +template +void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val) +{ + get_arithmetic_value(j, val); +} + +template +void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val) +{ + get_arithmetic_value(j, val); +} + +template +void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val) +{ + get_arithmetic_value(j, val); +} + +template::value, int> = 0> +void from_json(const BasicJsonType& j, EnumType& e) +{ + typename std::underlying_type::type val; + get_arithmetic_value(j, val); + e = static_cast(val); +} + +// forward_list doesn't have an insert method +template::value, int> = 0> +void from_json(const BasicJsonType& j, std::forward_list& l) +{ + if (JSON_UNLIKELY(not j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + } + std::transform(j.rbegin(), j.rend(), + std::front_inserter(l), [](const BasicJsonType & i) + { + return i.template get(); + }); +} + +// valarray doesn't have an insert method +template::value, int> = 0> +void from_json(const BasicJsonType& j, std::valarray& l) +{ + if (JSON_UNLIKELY(not j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + } + l.resize(j.size()); + std::copy(j.m_value.array->begin(), j.m_value.array->end(), std::begin(l)); +} + +template +void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/) +{ + arr = *j.template get_ptr(); +} + +template +auto from_json_array_impl(const BasicJsonType& j, std::array& arr, + priority_tag<2> /*unused*/) +-> decltype(j.template get(), void()) +{ + for (std::size_t i = 0; i < N; ++i) + { + arr[i] = j.at(i).template get(); + } +} + +template +auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/) +-> decltype( + arr.reserve(std::declval()), + j.template get(), + void()) +{ + using std::end; + + arr.reserve(j.size()); + std::transform(j.begin(), j.end(), + std::inserter(arr, end(arr)), [](const BasicJsonType & i) + { + // get() returns *this, this won't call a from_json + // method when value_type is BasicJsonType + return i.template get(); + }); +} + +template +void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, + priority_tag<0> /*unused*/) +{ + using std::end; + + std::transform( + j.begin(), j.end(), std::inserter(arr, end(arr)), + [](const BasicJsonType & i) + { + // get() returns *this, this won't call a from_json + // method when value_type is BasicJsonType + return i.template get(); + }); +} + +template ::value and + not is_constructible_object_type::value and + not is_constructible_string_type::value and + not is_basic_json::value, + int > = 0 > + +auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr) +-> decltype(from_json_array_impl(j, arr, priority_tag<3> {}), +j.template get(), +void()) +{ + if (JSON_UNLIKELY(not j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + from_json_array_impl(j, arr, priority_tag<3> {}); +} + +template::value, int> = 0> +void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) +{ + if (JSON_UNLIKELY(not j.is_object())) + { + JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()))); + } + + auto inner_object = j.template get_ptr(); + using value_type = typename ConstructibleObjectType::value_type; + std::transform( + inner_object->begin(), inner_object->end(), + std::inserter(obj, obj.begin()), + [](typename BasicJsonType::object_t::value_type const & p) + { + return value_type(p.first, p.second.template get()); + }); +} + +// overload for arithmetic types, not chosen for basic_json template arguments +// (BooleanType, etc..); note: Is it really necessary to provide explicit +// overloads for boolean_t etc. in case of a custom BooleanType which is not +// an arithmetic type? +template::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value, + int> = 0> +void from_json(const BasicJsonType& j, ArithmeticType& val) +{ + switch (static_cast(j)) + { + case value_t::number_unsigned: + { + val = static_cast(*j.template get_ptr()); + break; + } + case value_t::number_integer: + { + val = static_cast(*j.template get_ptr()); + break; + } + case value_t::number_float: + { + val = static_cast(*j.template get_ptr()); + break; + } + case value_t::boolean: + { + val = static_cast(*j.template get_ptr()); + break; + } + + default: + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); + } +} + +template +void from_json(const BasicJsonType& j, std::pair& p) +{ + p = {j.at(0).template get(), j.at(1).template get()}; +} + +template +void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence /*unused*/) +{ + t = std::make_tuple(j.at(Idx).template get::type>()...); +} + +template +void from_json(const BasicJsonType& j, std::tuple& t) +{ + from_json_tuple_impl(j, t, index_sequence_for {}); +} + +template ::value>> +void from_json(const BasicJsonType& j, std::map& m) +{ + if (JSON_UNLIKELY(not j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + } + for (const auto& p : j) + { + if (JSON_UNLIKELY(not p.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()))); + } + m.emplace(p.at(0).template get(), p.at(1).template get()); + } +} + +template ::value>> +void from_json(const BasicJsonType& j, std::unordered_map& m) +{ + if (JSON_UNLIKELY(not j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + } + for (const auto& p : j) + { + if (JSON_UNLIKELY(not p.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()))); + } + m.emplace(p.at(0).template get(), p.at(1).template get()); + } +} + +struct from_json_fn +{ + template + auto operator()(const BasicJsonType& j, T& val) const + noexcept(noexcept(from_json(j, val))) + -> decltype(from_json(j, val), void()) + { + return from_json(j, val); + } +}; +} // namespace detail + +/// namespace to hold default `from_json` function +/// to see why this is required: +/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html +namespace +{ +constexpr const auto& from_json = detail::static_const::value; +} // namespace +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/conversions/to_chars.hpp b/include/lib/modernjson/detail/conversions/to_chars.hpp new file mode 100644 index 0000000..b32e176 --- /dev/null +++ b/include/lib/modernjson/detail/conversions/to_chars.hpp @@ -0,0 +1,1094 @@ +#pragma once + +#include // assert +#include // or, and, not +#include // signbit, isfinite +#include // intN_t, uintN_t +#include // memcpy, memmove + +namespace nlohmann +{ +namespace detail +{ + +/*! +@brief implements the Grisu2 algorithm for binary to decimal floating-point +conversion. + +This implementation is a slightly modified version of the reference +implementation which may be obtained from +http://florian.loitsch.com/publications (bench.tar.gz). + +The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch. + +For a detailed description of the algorithm see: + +[1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with + Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming + Language Design and Implementation, PLDI 2010 +[2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately", + Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language + Design and Implementation, PLDI 1996 +*/ +namespace dtoa_impl +{ + +template +Target reinterpret_bits(const Source source) +{ + static_assert(sizeof(Target) == sizeof(Source), "size mismatch"); + + Target target; + std::memcpy(&target, &source, sizeof(Source)); + return target; +} + +struct diyfp // f * 2^e +{ + static constexpr int kPrecision = 64; // = q + + uint64_t f = 0; + int e = 0; + + constexpr diyfp(uint64_t f_, int e_) noexcept : f(f_), e(e_) {} + + /*! + @brief returns x - y + @pre x.e == y.e and x.f >= y.f + */ + static diyfp sub(const diyfp& x, const diyfp& y) noexcept + { + assert(x.e == y.e); + assert(x.f >= y.f); + + return {x.f - y.f, x.e}; + } + + /*! + @brief returns x * y + @note The result is rounded. (Only the upper q bits are returned.) + */ + static diyfp mul(const diyfp& x, const diyfp& y) noexcept + { + static_assert(kPrecision == 64, "internal error"); + + // Computes: + // f = round((x.f * y.f) / 2^q) + // e = x.e + y.e + q + + // Emulate the 64-bit * 64-bit multiplication: + // + // p = u * v + // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi) + // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi ) + // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 ) + // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 ) + // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3) + // = (p0_lo ) + 2^32 (Q ) + 2^64 (H ) + // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H ) + // + // (Since Q might be larger than 2^32 - 1) + // + // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H) + // + // (Q_hi + H does not overflow a 64-bit int) + // + // = p_lo + 2^64 p_hi + + const uint64_t u_lo = x.f & 0xFFFFFFFF; + const uint64_t u_hi = x.f >> 32; + const uint64_t v_lo = y.f & 0xFFFFFFFF; + const uint64_t v_hi = y.f >> 32; + + const uint64_t p0 = u_lo * v_lo; + const uint64_t p1 = u_lo * v_hi; + const uint64_t p2 = u_hi * v_lo; + const uint64_t p3 = u_hi * v_hi; + + const uint64_t p0_hi = p0 >> 32; + const uint64_t p1_lo = p1 & 0xFFFFFFFF; + const uint64_t p1_hi = p1 >> 32; + const uint64_t p2_lo = p2 & 0xFFFFFFFF; + const uint64_t p2_hi = p2 >> 32; + + uint64_t Q = p0_hi + p1_lo + p2_lo; + + // The full product might now be computed as + // + // p_hi = p3 + p2_hi + p1_hi + (Q >> 32) + // p_lo = p0_lo + (Q << 32) + // + // But in this particular case here, the full p_lo is not required. + // Effectively we only need to add the highest bit in p_lo to p_hi (and + // Q_hi + 1 does not overflow). + + Q += uint64_t{1} << (64 - 32 - 1); // round, ties up + + const uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32); + + return {h, x.e + y.e + 64}; + } + + /*! + @brief normalize x such that the significand is >= 2^(q-1) + @pre x.f != 0 + */ + static diyfp normalize(diyfp x) noexcept + { + assert(x.f != 0); + + while ((x.f >> 63) == 0) + { + x.f <<= 1; + x.e--; + } + + return x; + } + + /*! + @brief normalize x such that the result has the exponent E + @pre e >= x.e and the upper e - x.e bits of x.f must be zero. + */ + static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept + { + const int delta = x.e - target_exponent; + + assert(delta >= 0); + assert(((x.f << delta) >> delta) == x.f); + + return {x.f << delta, target_exponent}; + } +}; + +struct boundaries +{ + diyfp w; + diyfp minus; + diyfp plus; +}; + +/*! +Compute the (normalized) diyfp representing the input number 'value' and its +boundaries. + +@pre value must be finite and positive +*/ +template +boundaries compute_boundaries(FloatType value) +{ + assert(std::isfinite(value)); + assert(value > 0); + + // Convert the IEEE representation into a diyfp. + // + // If v is denormal: + // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1)) + // If v is normalized: + // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1)) + + static_assert(std::numeric_limits::is_iec559, + "internal error: dtoa_short requires an IEEE-754 floating-point implementation"); + + constexpr int kPrecision = std::numeric_limits::digits; // = p (includes the hidden bit) + constexpr int kBias = std::numeric_limits::max_exponent - 1 + (kPrecision - 1); + constexpr int kMinExp = 1 - kBias; + constexpr uint64_t kHiddenBit = uint64_t{1} << (kPrecision - 1); // = 2^(p-1) + + using bits_type = typename std::conditional< kPrecision == 24, uint32_t, uint64_t >::type; + + const uint64_t bits = reinterpret_bits(value); + const uint64_t E = bits >> (kPrecision - 1); + const uint64_t F = bits & (kHiddenBit - 1); + + const bool is_denormal = (E == 0); + const diyfp v = is_denormal + ? diyfp(F, kMinExp) + : diyfp(F + kHiddenBit, static_cast(E) - kBias); + + // Compute the boundaries m- and m+ of the floating-point value + // v = f * 2^e. + // + // Determine v- and v+, the floating-point predecessor and successor if v, + // respectively. + // + // v- = v - 2^e if f != 2^(p-1) or e == e_min (A) + // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B) + // + // v+ = v + 2^e + // + // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_ + // between m- and m+ round to v, regardless of how the input rounding + // algorithm breaks ties. + // + // ---+-------------+-------------+-------------+-------------+--- (A) + // v- m- v m+ v+ + // + // -----------------+------+------+-------------+-------------+--- (B) + // v- m- v m+ v+ + + const bool lower_boundary_is_closer = (F == 0 and E > 1); + const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1); + const diyfp m_minus = lower_boundary_is_closer + ? diyfp(4 * v.f - 1, v.e - 2) // (B) + : diyfp(2 * v.f - 1, v.e - 1); // (A) + + // Determine the normalized w+ = m+. + const diyfp w_plus = diyfp::normalize(m_plus); + + // Determine w- = m- such that e_(w-) = e_(w+). + const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e); + + return {diyfp::normalize(v), w_minus, w_plus}; +} + +// Given normalized diyfp w, Grisu needs to find a (normalized) cached +// power-of-ten c, such that the exponent of the product c * w = f * 2^e lies +// within a certain range [alpha, gamma] (Definition 3.2 from [1]) +// +// alpha <= e = e_c + e_w + q <= gamma +// +// or +// +// f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q +// <= f_c * f_w * 2^gamma +// +// Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies +// +// 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma +// +// or +// +// 2^(q - 2 + alpha) <= c * w < 2^(q + gamma) +// +// The choice of (alpha,gamma) determines the size of the table and the form of +// the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well +// in practice: +// +// The idea is to cut the number c * w = f * 2^e into two parts, which can be +// processed independently: An integral part p1, and a fractional part p2: +// +// f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e +// = (f div 2^-e) + (f mod 2^-e) * 2^e +// = p1 + p2 * 2^e +// +// The conversion of p1 into decimal form requires a series of divisions and +// modulos by (a power of) 10. These operations are faster for 32-bit than for +// 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be +// achieved by choosing +// +// -e >= 32 or e <= -32 := gamma +// +// In order to convert the fractional part +// +// p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ... +// +// into decimal form, the fraction is repeatedly multiplied by 10 and the digits +// d[-i] are extracted in order: +// +// (10 * p2) div 2^-e = d[-1] +// (10 * p2) mod 2^-e = d[-2] / 10^1 + ... +// +// The multiplication by 10 must not overflow. It is sufficient to choose +// +// 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64. +// +// Since p2 = f mod 2^-e < 2^-e, +// +// -e <= 60 or e >= -60 := alpha + +constexpr int kAlpha = -60; +constexpr int kGamma = -32; + +struct cached_power // c = f * 2^e ~= 10^k +{ + uint64_t f; + int e; + int k; +}; + +/*! +For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached +power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c +satisfies (Definition 3.2 from [1]) + + alpha <= e_c + e + q <= gamma. +*/ +inline cached_power get_cached_power_for_binary_exponent(int e) +{ + // Now + // + // alpha <= e_c + e + q <= gamma (1) + // ==> f_c * 2^alpha <= c * 2^e * 2^q + // + // and since the c's are normalized, 2^(q-1) <= f_c, + // + // ==> 2^(q - 1 + alpha) <= c * 2^(e + q) + // ==> 2^(alpha - e - 1) <= c + // + // If c were an exakt power of ten, i.e. c = 10^k, one may determine k as + // + // k = ceil( log_10( 2^(alpha - e - 1) ) ) + // = ceil( (alpha - e - 1) * log_10(2) ) + // + // From the paper: + // "In theory the result of the procedure could be wrong since c is rounded, + // and the computation itself is approximated [...]. In practice, however, + // this simple function is sufficient." + // + // For IEEE double precision floating-point numbers converted into + // normalized diyfp's w = f * 2^e, with q = 64, + // + // e >= -1022 (min IEEE exponent) + // -52 (p - 1) + // -52 (p - 1, possibly normalize denormal IEEE numbers) + // -11 (normalize the diyfp) + // = -1137 + // + // and + // + // e <= +1023 (max IEEE exponent) + // -52 (p - 1) + // -11 (normalize the diyfp) + // = 960 + // + // This binary exponent range [-1137,960] results in a decimal exponent + // range [-307,324]. One does not need to store a cached power for each + // k in this range. For each such k it suffices to find a cached power + // such that the exponent of the product lies in [alpha,gamma]. + // This implies that the difference of the decimal exponents of adjacent + // table entries must be less than or equal to + // + // floor( (gamma - alpha) * log_10(2) ) = 8. + // + // (A smaller distance gamma-alpha would require a larger table.) + + // NB: + // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34. + + constexpr int kCachedPowersSize = 79; + constexpr int kCachedPowersMinDecExp = -300; + constexpr int kCachedPowersDecStep = 8; + + static constexpr cached_power kCachedPowers[] = + { + { 0xAB70FE17C79AC6CA, -1060, -300 }, + { 0xFF77B1FCBEBCDC4F, -1034, -292 }, + { 0xBE5691EF416BD60C, -1007, -284 }, + { 0x8DD01FAD907FFC3C, -980, -276 }, + { 0xD3515C2831559A83, -954, -268 }, + { 0x9D71AC8FADA6C9B5, -927, -260 }, + { 0xEA9C227723EE8BCB, -901, -252 }, + { 0xAECC49914078536D, -874, -244 }, + { 0x823C12795DB6CE57, -847, -236 }, + { 0xC21094364DFB5637, -821, -228 }, + { 0x9096EA6F3848984F, -794, -220 }, + { 0xD77485CB25823AC7, -768, -212 }, + { 0xA086CFCD97BF97F4, -741, -204 }, + { 0xEF340A98172AACE5, -715, -196 }, + { 0xB23867FB2A35B28E, -688, -188 }, + { 0x84C8D4DFD2C63F3B, -661, -180 }, + { 0xC5DD44271AD3CDBA, -635, -172 }, + { 0x936B9FCEBB25C996, -608, -164 }, + { 0xDBAC6C247D62A584, -582, -156 }, + { 0xA3AB66580D5FDAF6, -555, -148 }, + { 0xF3E2F893DEC3F126, -529, -140 }, + { 0xB5B5ADA8AAFF80B8, -502, -132 }, + { 0x87625F056C7C4A8B, -475, -124 }, + { 0xC9BCFF6034C13053, -449, -116 }, + { 0x964E858C91BA2655, -422, -108 }, + { 0xDFF9772470297EBD, -396, -100 }, + { 0xA6DFBD9FB8E5B88F, -369, -92 }, + { 0xF8A95FCF88747D94, -343, -84 }, + { 0xB94470938FA89BCF, -316, -76 }, + { 0x8A08F0F8BF0F156B, -289, -68 }, + { 0xCDB02555653131B6, -263, -60 }, + { 0x993FE2C6D07B7FAC, -236, -52 }, + { 0xE45C10C42A2B3B06, -210, -44 }, + { 0xAA242499697392D3, -183, -36 }, + { 0xFD87B5F28300CA0E, -157, -28 }, + { 0xBCE5086492111AEB, -130, -20 }, + { 0x8CBCCC096F5088CC, -103, -12 }, + { 0xD1B71758E219652C, -77, -4 }, + { 0x9C40000000000000, -50, 4 }, + { 0xE8D4A51000000000, -24, 12 }, + { 0xAD78EBC5AC620000, 3, 20 }, + { 0x813F3978F8940984, 30, 28 }, + { 0xC097CE7BC90715B3, 56, 36 }, + { 0x8F7E32CE7BEA5C70, 83, 44 }, + { 0xD5D238A4ABE98068, 109, 52 }, + { 0x9F4F2726179A2245, 136, 60 }, + { 0xED63A231D4C4FB27, 162, 68 }, + { 0xB0DE65388CC8ADA8, 189, 76 }, + { 0x83C7088E1AAB65DB, 216, 84 }, + { 0xC45D1DF942711D9A, 242, 92 }, + { 0x924D692CA61BE758, 269, 100 }, + { 0xDA01EE641A708DEA, 295, 108 }, + { 0xA26DA3999AEF774A, 322, 116 }, + { 0xF209787BB47D6B85, 348, 124 }, + { 0xB454E4A179DD1877, 375, 132 }, + { 0x865B86925B9BC5C2, 402, 140 }, + { 0xC83553C5C8965D3D, 428, 148 }, + { 0x952AB45CFA97A0B3, 455, 156 }, + { 0xDE469FBD99A05FE3, 481, 164 }, + { 0xA59BC234DB398C25, 508, 172 }, + { 0xF6C69A72A3989F5C, 534, 180 }, + { 0xB7DCBF5354E9BECE, 561, 188 }, + { 0x88FCF317F22241E2, 588, 196 }, + { 0xCC20CE9BD35C78A5, 614, 204 }, + { 0x98165AF37B2153DF, 641, 212 }, + { 0xE2A0B5DC971F303A, 667, 220 }, + { 0xA8D9D1535CE3B396, 694, 228 }, + { 0xFB9B7CD9A4A7443C, 720, 236 }, + { 0xBB764C4CA7A44410, 747, 244 }, + { 0x8BAB8EEFB6409C1A, 774, 252 }, + { 0xD01FEF10A657842C, 800, 260 }, + { 0x9B10A4E5E9913129, 827, 268 }, + { 0xE7109BFBA19C0C9D, 853, 276 }, + { 0xAC2820D9623BF429, 880, 284 }, + { 0x80444B5E7AA7CF85, 907, 292 }, + { 0xBF21E44003ACDD2D, 933, 300 }, + { 0x8E679C2F5E44FF8F, 960, 308 }, + { 0xD433179D9C8CB841, 986, 316 }, + { 0x9E19DB92B4E31BA9, 1013, 324 }, + }; + + // This computation gives exactly the same results for k as + // k = ceil((kAlpha - e - 1) * 0.30102999566398114) + // for |e| <= 1500, but doesn't require floating-point operations. + // NB: log_10(2) ~= 78913 / 2^18 + assert(e >= -1500); + assert(e <= 1500); + const int f = kAlpha - e - 1; + const int k = (f * 78913) / (1 << 18) + static_cast(f > 0); + + const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep; + assert(index >= 0); + assert(index < kCachedPowersSize); + static_cast(kCachedPowersSize); // Fix warning. + + const cached_power cached = kCachedPowers[index]; + assert(kAlpha <= cached.e + e + 64); + assert(kGamma >= cached.e + e + 64); + + return cached; +} + +/*! +For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k. +For n == 0, returns 1 and sets pow10 := 1. +*/ +inline int find_largest_pow10(const uint32_t n, uint32_t& pow10) +{ + // LCOV_EXCL_START + if (n >= 1000000000) + { + pow10 = 1000000000; + return 10; + } + // LCOV_EXCL_STOP + else if (n >= 100000000) + { + pow10 = 100000000; + return 9; + } + else if (n >= 10000000) + { + pow10 = 10000000; + return 8; + } + else if (n >= 1000000) + { + pow10 = 1000000; + return 7; + } + else if (n >= 100000) + { + pow10 = 100000; + return 6; + } + else if (n >= 10000) + { + pow10 = 10000; + return 5; + } + else if (n >= 1000) + { + pow10 = 1000; + return 4; + } + else if (n >= 100) + { + pow10 = 100; + return 3; + } + else if (n >= 10) + { + pow10 = 10; + return 2; + } + else + { + pow10 = 1; + return 1; + } +} + +inline void grisu2_round(char* buf, int len, uint64_t dist, uint64_t delta, + uint64_t rest, uint64_t ten_k) +{ + assert(len >= 1); + assert(dist <= delta); + assert(rest <= delta); + assert(ten_k > 0); + + // <--------------------------- delta ----> + // <---- dist ---------> + // --------------[------------------+-------------------]-------------- + // M- w M+ + // + // ten_k + // <------> + // <---- rest ----> + // --------------[------------------+----+--------------]-------------- + // w V + // = buf * 10^k + // + // ten_k represents a unit-in-the-last-place in the decimal representation + // stored in buf. + // Decrement buf by ten_k while this takes buf closer to w. + + // The tests are written in this order to avoid overflow in unsigned + // integer arithmetic. + + while (rest < dist + and delta - rest >= ten_k + and (rest + ten_k < dist or dist - rest > rest + ten_k - dist)) + { + assert(buf[len - 1] != '0'); + buf[len - 1]--; + rest += ten_k; + } +} + +/*! +Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+. +M- and M+ must be normalized and share the same exponent -60 <= e <= -32. +*/ +inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, + diyfp M_minus, diyfp w, diyfp M_plus) +{ + static_assert(kAlpha >= -60, "internal error"); + static_assert(kGamma <= -32, "internal error"); + + // Generates the digits (and the exponent) of a decimal floating-point + // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's + // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma. + // + // <--------------------------- delta ----> + // <---- dist ---------> + // --------------[------------------+-------------------]-------------- + // M- w M+ + // + // Grisu2 generates the digits of M+ from left to right and stops as soon as + // V is in [M-,M+]. + + assert(M_plus.e >= kAlpha); + assert(M_plus.e <= kGamma); + + uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e) + uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e) + + // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0): + // + // M+ = f * 2^e + // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e + // = ((p1 ) * 2^-e + (p2 )) * 2^e + // = p1 + p2 * 2^e + + const diyfp one(uint64_t{1} << -M_plus.e, M_plus.e); + + auto p1 = static_cast(M_plus.f >> -one.e); // p1 = f div 2^-e (Since -e >= 32, p1 fits into a 32-bit int.) + uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e + + // 1) + // + // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0] + + assert(p1 > 0); + + uint32_t pow10; + const int k = find_largest_pow10(p1, pow10); + + // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) + // + // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1)) + // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1)) + // + // M+ = p1 + p2 * 2^e + // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e + // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e + // = d[k-1] * 10^(k-1) + ( rest) * 2^e + // + // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0) + // + // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0] + // + // but stop as soon as + // + // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e + + int n = k; + while (n > 0) + { + // Invariants: + // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k) + // pow10 = 10^(n-1) <= p1 < 10^n + // + const uint32_t d = p1 / pow10; // d = p1 div 10^(n-1) + const uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1) + // + // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e + // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e) + // + assert(d <= 9); + buffer[length++] = static_cast('0' + d); // buffer := buffer * 10 + d + // + // M+ = buffer * 10^(n-1) + (r + p2 * 2^e) + // + p1 = r; + n--; + // + // M+ = buffer * 10^n + (p1 + p2 * 2^e) + // pow10 = 10^n + // + + // Now check if enough digits have been generated. + // Compute + // + // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e + // + // Note: + // Since rest and delta share the same exponent e, it suffices to + // compare the significands. + const uint64_t rest = (uint64_t{p1} << -one.e) + p2; + if (rest <= delta) + { + // V = buffer * 10^n, with M- <= V <= M+. + + decimal_exponent += n; + + // We may now just stop. But instead look if the buffer could be + // decremented to bring V closer to w. + // + // pow10 = 10^n is now 1 ulp in the decimal representation V. + // The rounding procedure works with diyfp's with an implicit + // exponent of e. + // + // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e + // + const uint64_t ten_n = uint64_t{pow10} << -one.e; + grisu2_round(buffer, length, dist, delta, rest, ten_n); + + return; + } + + pow10 /= 10; + // + // pow10 = 10^(n-1) <= p1 < 10^n + // Invariants restored. + } + + // 2) + // + // The digits of the integral part have been generated: + // + // M+ = d[k-1]...d[1]d[0] + p2 * 2^e + // = buffer + p2 * 2^e + // + // Now generate the digits of the fractional part p2 * 2^e. + // + // Note: + // No decimal point is generated: the exponent is adjusted instead. + // + // p2 actually represents the fraction + // + // p2 * 2^e + // = p2 / 2^-e + // = d[-1] / 10^1 + d[-2] / 10^2 + ... + // + // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...) + // + // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m + // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...) + // + // using + // + // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e) + // = ( d) * 2^-e + ( r) + // + // or + // 10^m * p2 * 2^e = d + r * 2^e + // + // i.e. + // + // M+ = buffer + p2 * 2^e + // = buffer + 10^-m * (d + r * 2^e) + // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e + // + // and stop as soon as 10^-m * r * 2^e <= delta * 2^e + + assert(p2 > delta); + + int m = 0; + for (;;) + { + // Invariant: + // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e + // = buffer * 10^-m + 10^-m * (p2 ) * 2^e + // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e + // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e + // + assert(p2 <= UINT64_MAX / 10); + p2 *= 10; + const uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e + const uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e + // + // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e + // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e)) + // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e + // + assert(d <= 9); + buffer[length++] = static_cast('0' + d); // buffer := buffer * 10 + d + // + // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e + // + p2 = r; + m++; + // + // M+ = buffer * 10^-m + 10^-m * p2 * 2^e + // Invariant restored. + + // Check if enough digits have been generated. + // + // 10^-m * p2 * 2^e <= delta * 2^e + // p2 * 2^e <= 10^m * delta * 2^e + // p2 <= 10^m * delta + delta *= 10; + dist *= 10; + if (p2 <= delta) + { + break; + } + } + + // V = buffer * 10^-m, with M- <= V <= M+. + + decimal_exponent -= m; + + // 1 ulp in the decimal representation is now 10^-m. + // Since delta and dist are now scaled by 10^m, we need to do the + // same with ulp in order to keep the units in sync. + // + // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e + // + const uint64_t ten_m = one.f; + grisu2_round(buffer, length, dist, delta, p2, ten_m); + + // By construction this algorithm generates the shortest possible decimal + // number (Loitsch, Theorem 6.2) which rounds back to w. + // For an input number of precision p, at least + // + // N = 1 + ceil(p * log_10(2)) + // + // decimal digits are sufficient to identify all binary floating-point + // numbers (Matula, "In-and-Out conversions"). + // This implies that the algorithm does not produce more than N decimal + // digits. + // + // N = 17 for p = 53 (IEEE double precision) + // N = 9 for p = 24 (IEEE single precision) +} + +/*! +v = buf * 10^decimal_exponent +len is the length of the buffer (number of decimal digits) +The buffer must be large enough, i.e. >= max_digits10. +*/ +inline void grisu2(char* buf, int& len, int& decimal_exponent, + diyfp m_minus, diyfp v, diyfp m_plus) +{ + assert(m_plus.e == m_minus.e); + assert(m_plus.e == v.e); + + // --------(-----------------------+-----------------------)-------- (A) + // m- v m+ + // + // --------------------(-----------+-----------------------)-------- (B) + // m- v m+ + // + // First scale v (and m- and m+) such that the exponent is in the range + // [alpha, gamma]. + + const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e); + + const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k + + // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma] + const diyfp w = diyfp::mul(v, c_minus_k); + const diyfp w_minus = diyfp::mul(m_minus, c_minus_k); + const diyfp w_plus = diyfp::mul(m_plus, c_minus_k); + + // ----(---+---)---------------(---+---)---------------(---+---)---- + // w- w w+ + // = c*m- = c*v = c*m+ + // + // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and + // w+ are now off by a small amount. + // In fact: + // + // w - v * 10^k < 1 ulp + // + // To account for this inaccuracy, add resp. subtract 1 ulp. + // + // --------+---[---------------(---+---)---------------]---+-------- + // w- M- w M+ w+ + // + // Now any number in [M-, M+] (bounds included) will round to w when input, + // regardless of how the input rounding algorithm breaks ties. + // + // And digit_gen generates the shortest possible such number in [M-, M+]. + // Note that this does not mean that Grisu2 always generates the shortest + // possible number in the interval (m-, m+). + const diyfp M_minus(w_minus.f + 1, w_minus.e); + const diyfp M_plus (w_plus.f - 1, w_plus.e ); + + decimal_exponent = -cached.k; // = -(-k) = k + + grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus); +} + +/*! +v = buf * 10^decimal_exponent +len is the length of the buffer (number of decimal digits) +The buffer must be large enough, i.e. >= max_digits10. +*/ +template +void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value) +{ + static_assert(diyfp::kPrecision >= std::numeric_limits::digits + 3, + "internal error: not enough precision"); + + assert(std::isfinite(value)); + assert(value > 0); + + // If the neighbors (and boundaries) of 'value' are always computed for double-precision + // numbers, all float's can be recovered using strtod (and strtof). However, the resulting + // decimal representations are not exactly "short". + // + // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars) + // says "value is converted to a string as if by std::sprintf in the default ("C") locale" + // and since sprintf promotes float's to double's, I think this is exactly what 'std::to_chars' + // does. + // On the other hand, the documentation for 'std::to_chars' requires that "parsing the + // representation using the corresponding std::from_chars function recovers value exactly". That + // indicates that single precision floating-point numbers should be recovered using + // 'std::strtof'. + // + // NB: If the neighbors are computed for single-precision numbers, there is a single float + // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision + // value is off by 1 ulp. +#if 0 + const boundaries w = compute_boundaries(static_cast(value)); +#else + const boundaries w = compute_boundaries(value); +#endif + + grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus); +} + +/*! +@brief appends a decimal representation of e to buf +@return a pointer to the element following the exponent. +@pre -1000 < e < 1000 +*/ +inline char* append_exponent(char* buf, int e) +{ + assert(e > -1000); + assert(e < 1000); + + if (e < 0) + { + e = -e; + *buf++ = '-'; + } + else + { + *buf++ = '+'; + } + + auto k = static_cast(e); + if (k < 10) + { + // Always print at least two digits in the exponent. + // This is for compatibility with printf("%g"). + *buf++ = '0'; + *buf++ = static_cast('0' + k); + } + else if (k < 100) + { + *buf++ = static_cast('0' + k / 10); + k %= 10; + *buf++ = static_cast('0' + k); + } + else + { + *buf++ = static_cast('0' + k / 100); + k %= 100; + *buf++ = static_cast('0' + k / 10); + k %= 10; + *buf++ = static_cast('0' + k); + } + + return buf; +} + +/*! +@brief prettify v = buf * 10^decimal_exponent + +If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point +notation. Otherwise it will be printed in exponential notation. + +@pre min_exp < 0 +@pre max_exp > 0 +*/ +inline char* format_buffer(char* buf, int len, int decimal_exponent, + int min_exp, int max_exp) +{ + assert(min_exp < 0); + assert(max_exp > 0); + + const int k = len; + const int n = len + decimal_exponent; + + // v = buf * 10^(n-k) + // k is the length of the buffer (number of decimal digits) + // n is the position of the decimal point relative to the start of the buffer. + + if (k <= n and n <= max_exp) + { + // digits[000] + // len <= max_exp + 2 + + std::memset(buf + k, '0', static_cast(n - k)); + // Make it look like a floating-point number (#362, #378) + buf[n + 0] = '.'; + buf[n + 1] = '0'; + return buf + (n + 2); + } + + if (0 < n and n <= max_exp) + { + // dig.its + // len <= max_digits10 + 1 + + assert(k > n); + + std::memmove(buf + (n + 1), buf + n, static_cast(k - n)); + buf[n] = '.'; + return buf + (k + 1); + } + + if (min_exp < n and n <= 0) + { + // 0.[000]digits + // len <= 2 + (-min_exp - 1) + max_digits10 + + std::memmove(buf + (2 + -n), buf, static_cast(k)); + buf[0] = '0'; + buf[1] = '.'; + std::memset(buf + 2, '0', static_cast(-n)); + return buf + (2 + (-n) + k); + } + + if (k == 1) + { + // dE+123 + // len <= 1 + 5 + + buf += 1; + } + else + { + // d.igitsE+123 + // len <= max_digits10 + 1 + 5 + + std::memmove(buf + 2, buf + 1, static_cast(k - 1)); + buf[1] = '.'; + buf += 1 + k; + } + + *buf++ = 'e'; + return append_exponent(buf, n - 1); +} + +} // namespace dtoa_impl + +/*! +@brief generates a decimal representation of the floating-point number value in [first, last). + +The format of the resulting decimal representation is similar to printf's %g +format. Returns an iterator pointing past-the-end of the decimal representation. + +@note The input number must be finite, i.e. NaN's and Inf's are not supported. +@note The buffer must be large enough. +@note The result is NOT null-terminated. +*/ +template +char* to_chars(char* first, const char* last, FloatType value) +{ + static_cast(last); // maybe unused - fix warning + assert(std::isfinite(value)); + + // Use signbit(value) instead of (value < 0) since signbit works for -0. + if (std::signbit(value)) + { + value = -value; + *first++ = '-'; + } + + if (value == 0) // +-0 + { + *first++ = '0'; + // Make it look like a floating-point number (#362, #378) + *first++ = '.'; + *first++ = '0'; + return first; + } + + assert(last - first >= std::numeric_limits::max_digits10); + + // Compute v = buffer * 10^decimal_exponent. + // The decimal digits are stored in the buffer, which needs to be interpreted + // as an unsigned decimal integer. + // len is the length of the buffer, i.e. the number of decimal digits. + int len = 0; + int decimal_exponent = 0; + dtoa_impl::grisu2(first, len, decimal_exponent, value); + + assert(len <= std::numeric_limits::max_digits10); + + // Format the buffer like printf("%.*g", prec, value) + constexpr int kMinExp = -4; + // Use digits10 here to increase compatibility with version 2. + constexpr int kMaxExp = std::numeric_limits::digits10; + + assert(last - first >= kMaxExp + 2); + assert(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits::max_digits10); + assert(last - first >= std::numeric_limits::max_digits10 + 6); + + return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp); +} + +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/conversions/to_json.hpp b/include/lib/modernjson/detail/conversions/to_json.hpp new file mode 100644 index 0000000..5c3669c --- /dev/null +++ b/include/lib/modernjson/detail/conversions/to_json.hpp @@ -0,0 +1,342 @@ +#pragma once + +#include // or, and, not +#include // begin, end +#include // tuple, get +#include // is_same, is_constructible, is_floating_point, is_enum, underlying_type +#include // move, forward, declval, pair +#include // valarray +#include // vector + +#include +#include +#include +#include + +namespace nlohmann +{ +namespace detail +{ +////////////////// +// constructors // +////////////////// + +template struct external_constructor; + +template<> +struct external_constructor +{ + template + static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept + { + j.m_type = value_t::boolean; + j.m_value = b; + j.assert_invariant(); + } +}; + +template<> +struct external_constructor +{ + template + static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s) + { + j.m_type = value_t::string; + j.m_value = s; + j.assert_invariant(); + } + + template + static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s) + { + j.m_type = value_t::string; + j.m_value = std::move(s); + j.assert_invariant(); + } + + template::value, + int> = 0> + static void construct(BasicJsonType& j, const CompatibleStringType& str) + { + j.m_type = value_t::string; + j.m_value.string = j.template create(str); + j.assert_invariant(); + } +}; + +template<> +struct external_constructor +{ + template + static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept + { + j.m_type = value_t::number_float; + j.m_value = val; + j.assert_invariant(); + } +}; + +template<> +struct external_constructor +{ + template + static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept + { + j.m_type = value_t::number_unsigned; + j.m_value = val; + j.assert_invariant(); + } +}; + +template<> +struct external_constructor +{ + template + static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept + { + j.m_type = value_t::number_integer; + j.m_value = val; + j.assert_invariant(); + } +}; + +template<> +struct external_constructor +{ + template + static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr) + { + j.m_type = value_t::array; + j.m_value = arr; + j.assert_invariant(); + } + + template + static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr) + { + j.m_type = value_t::array; + j.m_value = std::move(arr); + j.assert_invariant(); + } + + template::value, + int> = 0> + static void construct(BasicJsonType& j, const CompatibleArrayType& arr) + { + using std::begin; + using std::end; + j.m_type = value_t::array; + j.m_value.array = j.template create(begin(arr), end(arr)); + j.assert_invariant(); + } + + template + static void construct(BasicJsonType& j, const std::vector& arr) + { + j.m_type = value_t::array; + j.m_value = value_t::array; + j.m_value.array->reserve(arr.size()); + for (const bool x : arr) + { + j.m_value.array->push_back(x); + } + j.assert_invariant(); + } + + template::value, int> = 0> + static void construct(BasicJsonType& j, const std::valarray& arr) + { + j.m_type = value_t::array; + j.m_value = value_t::array; + j.m_value.array->resize(arr.size()); + std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin()); + j.assert_invariant(); + } +}; + +template<> +struct external_constructor +{ + template + static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj) + { + j.m_type = value_t::object; + j.m_value = obj; + j.assert_invariant(); + } + + template + static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj) + { + j.m_type = value_t::object; + j.m_value = std::move(obj); + j.assert_invariant(); + } + + template::value, int> = 0> + static void construct(BasicJsonType& j, const CompatibleObjectType& obj) + { + using std::begin; + using std::end; + + j.m_type = value_t::object; + j.m_value.object = j.template create(begin(obj), end(obj)); + j.assert_invariant(); + } +}; + +///////////// +// to_json // +///////////// + +template::value, int> = 0> +void to_json(BasicJsonType& j, T b) noexcept +{ + external_constructor::construct(j, b); +} + +template::value, int> = 0> +void to_json(BasicJsonType& j, const CompatibleString& s) +{ + external_constructor::construct(j, s); +} + +template +void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s) +{ + external_constructor::construct(j, std::move(s)); +} + +template::value, int> = 0> +void to_json(BasicJsonType& j, FloatType val) noexcept +{ + external_constructor::construct(j, static_cast(val)); +} + +template::value, int> = 0> +void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept +{ + external_constructor::construct(j, static_cast(val)); +} + +template::value, int> = 0> +void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept +{ + external_constructor::construct(j, static_cast(val)); +} + +template::value, int> = 0> +void to_json(BasicJsonType& j, EnumType e) noexcept +{ + using underlying_type = typename std::underlying_type::type; + external_constructor::construct(j, static_cast(e)); +} + +template +void to_json(BasicJsonType& j, const std::vector& e) +{ + external_constructor::construct(j, e); +} + +template ::value and + not is_compatible_object_type< + BasicJsonType, CompatibleArrayType>::value and + not is_compatible_string_type::value and + not is_basic_json::value, + int> = 0> +void to_json(BasicJsonType& j, const CompatibleArrayType& arr) +{ + external_constructor::construct(j, arr); +} + +template::value, int> = 0> +void to_json(BasicJsonType& j, const std::valarray& arr) +{ + external_constructor::construct(j, std::move(arr)); +} + +template +void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr) +{ + external_constructor::construct(j, std::move(arr)); +} + +template::value and not is_basic_json::value, int> = 0> +void to_json(BasicJsonType& j, const CompatibleObjectType& obj) +{ + external_constructor::construct(j, obj); +} + +template +void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj) +{ + external_constructor::construct(j, std::move(obj)); +} + +template < + typename BasicJsonType, typename T, std::size_t N, + enable_if_t::value, + int> = 0 > +void to_json(BasicJsonType& j, const T (&arr)[N]) +{ + external_constructor::construct(j, arr); +} + +template +void to_json(BasicJsonType& j, const std::pair& p) +{ + j = {p.first, p.second}; +} + +// for https://github.com/nlohmann/json/pull/1134 +template::iteration_proxy_internal>::value, int> = 0> +void to_json(BasicJsonType& j, const T& b) +{ + j = {{b.key(), b.value()}}; +} + +template +void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence /*unused*/) +{ + j = {std::get(t)...}; +} + +template +void to_json(BasicJsonType& j, const std::tuple& t) +{ + to_json_tuple_impl(j, t, index_sequence_for {}); +} + +struct to_json_fn +{ + template + auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward(val)))) + -> decltype(to_json(j, std::forward(val)), void()) + { + return to_json(j, std::forward(val)); + } +}; +} // namespace detail + +/// namespace to hold default `to_json` function +namespace +{ +constexpr const auto& to_json = detail::static_const::value; +} // namespace +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/exceptions.hpp b/include/lib/modernjson/detail/exceptions.hpp new file mode 100644 index 0000000..bcb1f2e --- /dev/null +++ b/include/lib/modernjson/detail/exceptions.hpp @@ -0,0 +1,349 @@ +#pragma once + +#include // exception +#include // runtime_error +#include // to_string + +#include + +namespace nlohmann +{ +namespace detail +{ +//////////////// +// exceptions // +//////////////// + +/*! +@brief general exception of the @ref basic_json class + +This class is an extension of `std::exception` objects with a member @a id for +exception ids. It is used as the base class for all exceptions thrown by the +@ref basic_json class. This class can hence be used as "wildcard" to catch +exceptions. + +Subclasses: +- @ref parse_error for exceptions indicating a parse error +- @ref invalid_iterator for exceptions indicating errors with iterators +- @ref type_error for exceptions indicating executing a member function with + a wrong type +- @ref out_of_range for exceptions indicating access out of the defined range +- @ref other_error for exceptions indicating other library errors + +@internal +@note To have nothrow-copy-constructible exceptions, we internally use + `std::runtime_error` which can cope with arbitrary-length error messages. + Intermediate strings are built with static functions and then passed to + the actual constructor. +@endinternal + +@liveexample{The following code shows how arbitrary library exceptions can be +caught.,exception} + +@since version 3.0.0 +*/ +class exception : public std::exception +{ + public: + /// returns the explanatory string + const char* what() const noexcept override + { + return m.what(); + } + + /// the id of the exception + const int id; + + protected: + exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} + + static std::string name(const std::string& ename, int id_) + { + return "[json.exception." + ename + "." + std::to_string(id_) + "] "; + } + + private: + /// an exception object as storage for error messages + std::runtime_error m; +}; + +/*! +@brief exception indicating a parse error + +This exception is thrown by the library when a parse error occurs. Parse errors +can occur during the deserialization of JSON text, CBOR, MessagePack, as well +as when using JSON Patch. + +Member @a byte holds the byte index of the last read character in the input +file. + +Exceptions have ids 1xx. + +name / id | example message | description +------------------------------ | --------------- | ------------------------- +json.exception.parse_error.101 | parse error at 2: unexpected end of input; expected string literal | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member @a byte indicates the error position. +json.exception.parse_error.102 | parse error at 14: missing or wrong low surrogate | JSON uses the `\uxxxx` format to describe Unicode characters. Code points above above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point. +json.exception.parse_error.103 | parse error: code points above 0x10FFFF are invalid | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid. +json.exception.parse_error.104 | parse error: JSON patch must be an array of objects | [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects. +json.exception.parse_error.105 | parse error: operation must have string member 'op' | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors. +json.exception.parse_error.106 | parse error: array index '01' must not begin with '0' | An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number without a leading `0`. +json.exception.parse_error.107 | parse error: JSON pointer must be empty or begin with '/' - was: 'foo' | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character. +json.exception.parse_error.108 | parse error: escape character '~' must be followed with '0' or '1' | In a JSON Pointer, only `~0` and `~1` are valid escape sequences. +json.exception.parse_error.109 | parse error: array index 'one' is not a number | A JSON Pointer array index must be a number. +json.exception.parse_error.110 | parse error at 1: cannot read 2 bytes from vector | When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read. +json.exception.parse_error.112 | parse error at 1: error reading CBOR; last byte: 0xF8 | Not all types of CBOR or MessagePack are supported. This exception occurs if an unsupported byte was read. +json.exception.parse_error.113 | parse error at 2: expected a CBOR string; last byte: 0x98 | While parsing a map key, a value that is not a string has been read. +json.exception.parse_error.114 | parse error: Unsupported BSON record type 0x0F | The parsing of the corresponding BSON record type is not implemented (yet). + +@note For an input with n bytes, 1 is the index of the first character and n+1 + is the index of the terminating null byte or the end of file. This also + holds true when reading a byte vector (CBOR or MessagePack). + +@liveexample{The following code shows how a `parse_error` exception can be +caught.,parse_error} + +@sa @ref exception for the base class of the library exceptions +@sa @ref invalid_iterator for exceptions indicating errors with iterators +@sa @ref type_error for exceptions indicating executing a member function with + a wrong type +@sa @ref out_of_range for exceptions indicating access out of the defined range +@sa @ref other_error for exceptions indicating other library errors + +@since version 3.0.0 +*/ +class parse_error : public exception +{ + public: + /*! + @brief create a parse error exception + @param[in] id_ the id of the exception + @param[in] position the position where the error occurred (or with + chars_read_total=0 if the position cannot be + determined) + @param[in] what_arg the explanatory string + @return parse_error object + */ + static parse_error create(int id_, const position_t& pos, const std::string& what_arg) + { + std::string w = exception::name("parse_error", id_) + "parse error" + + position_string(pos) + ": " + what_arg; + return parse_error(id_, pos.chars_read_total, w.c_str()); + } + + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg) + { + std::string w = exception::name("parse_error", id_) + "parse error" + + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + + ": " + what_arg; + return parse_error(id_, byte_, w.c_str()); + } + + /*! + @brief byte index of the parse error + + The byte index of the last read character in the input file. + + @note For an input with n bytes, 1 is the index of the first character and + n+1 is the index of the terminating null byte or the end of file. + This also holds true when reading a byte vector (CBOR or MessagePack). + */ + const std::size_t byte; + + private: + parse_error(int id_, std::size_t byte_, const char* what_arg) + : exception(id_, what_arg), byte(byte_) {} + + static std::string position_string(const position_t& pos) + { + return " at line " + std::to_string(pos.lines_read + 1) + + ", column " + std::to_string(pos.chars_read_current_line); + } +}; + +/*! +@brief exception indicating errors with iterators + +This exception is thrown if iterators passed to a library function do not match +the expected semantics. + +Exceptions have ids 2xx. + +name / id | example message | description +----------------------------------- | --------------- | ------------------------- +json.exception.invalid_iterator.201 | iterators are not compatible | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid. +json.exception.invalid_iterator.202 | iterator does not fit current value | In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion. +json.exception.invalid_iterator.203 | iterators do not fit current value | Either iterator passed to function @ref erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from. +json.exception.invalid_iterator.204 | iterators out of range | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (@ref begin(), @ref end()), because this is the only way the single stored value is expressed. All other ranges are invalid. +json.exception.invalid_iterator.205 | iterator out of range | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the @ref begin() iterator, because it is the only way to address the stored value. All other iterators are invalid. +json.exception.invalid_iterator.206 | cannot construct with iterators from null | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range. +json.exception.invalid_iterator.207 | cannot use key() for non-object iterators | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key. +json.exception.invalid_iterator.208 | cannot use operator[] for object iterators | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. +json.exception.invalid_iterator.209 | cannot use offsets with object iterators | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. +json.exception.invalid_iterator.210 | iterators do not fit | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid. +json.exception.invalid_iterator.211 | passed iterators may not belong to container | The iterator range passed to the insert function must not be a subrange of the container to insert to. +json.exception.invalid_iterator.212 | cannot compare iterators of different containers | When two iterators are compared, they must belong to the same container. +json.exception.invalid_iterator.213 | cannot compare order of object iterators | The order of object iterators cannot be compared, because JSON objects are unordered. +json.exception.invalid_iterator.214 | cannot get value | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to @ref begin(). + +@liveexample{The following code shows how an `invalid_iterator` exception can be +caught.,invalid_iterator} + +@sa @ref exception for the base class of the library exceptions +@sa @ref parse_error for exceptions indicating a parse error +@sa @ref type_error for exceptions indicating executing a member function with + a wrong type +@sa @ref out_of_range for exceptions indicating access out of the defined range +@sa @ref other_error for exceptions indicating other library errors + +@since version 3.0.0 +*/ +class invalid_iterator : public exception +{ + public: + static invalid_iterator create(int id_, const std::string& what_arg) + { + std::string w = exception::name("invalid_iterator", id_) + what_arg; + return invalid_iterator(id_, w.c_str()); + } + + private: + invalid_iterator(int id_, const char* what_arg) + : exception(id_, what_arg) {} +}; + +/*! +@brief exception indicating executing a member function with a wrong type + +This exception is thrown in case of a type error; that is, a library function is +executed on a JSON value whose type does not match the expected semantics. + +Exceptions have ids 3xx. + +name / id | example message | description +----------------------------- | --------------- | ------------------------- +json.exception.type_error.301 | cannot create object from initializer list | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead. +json.exception.type_error.302 | type must be object, but is array | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types. +json.exception.type_error.303 | incompatible ReferenceType for get_ref, actual type is object | To retrieve a reference to a value stored in a @ref basic_json object with @ref get_ref, the type of the reference must match the value type. For instance, for a JSON array, the @a ReferenceType must be @ref array_t&. +json.exception.type_error.304 | cannot use at() with string | The @ref at() member functions can only be executed for certain JSON types. +json.exception.type_error.305 | cannot use operator[] with string | The @ref operator[] member functions can only be executed for certain JSON types. +json.exception.type_error.306 | cannot use value() with string | The @ref value() member functions can only be executed for certain JSON types. +json.exception.type_error.307 | cannot use erase() with string | The @ref erase() member functions can only be executed for certain JSON types. +json.exception.type_error.308 | cannot use push_back() with string | The @ref push_back() and @ref operator+= member functions can only be executed for certain JSON types. +json.exception.type_error.309 | cannot use insert() with | The @ref insert() member functions can only be executed for certain JSON types. +json.exception.type_error.310 | cannot use swap() with number | The @ref swap() member functions can only be executed for certain JSON types. +json.exception.type_error.311 | cannot use emplace_back() with string | The @ref emplace_back() member function can only be executed for certain JSON types. +json.exception.type_error.312 | cannot use update() with string | The @ref update() member functions can only be executed for certain JSON types. +json.exception.type_error.313 | invalid value to unflatten | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined. +json.exception.type_error.314 | only objects can be unflattened | The @ref unflatten function only works for an object whose keys are JSON Pointers. +json.exception.type_error.315 | values in object must be primitive | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive. +json.exception.type_error.316 | invalid UTF-8 byte at index 10: 0x7E | The @ref dump function only works with UTF-8 encoded strings; that is, if you assign a `std::string` to a JSON value, make sure it is UTF-8 encoded. | +json.exception.type_error.317 | JSON value cannot be serialized to requested format | The dynamic type of the object cannot be represented in the requested serialization format (e.g. a raw `true` or `null` JSON object cannot be serialized to BSON) | + +@liveexample{The following code shows how a `type_error` exception can be +caught.,type_error} + +@sa @ref exception for the base class of the library exceptions +@sa @ref parse_error for exceptions indicating a parse error +@sa @ref invalid_iterator for exceptions indicating errors with iterators +@sa @ref out_of_range for exceptions indicating access out of the defined range +@sa @ref other_error for exceptions indicating other library errors + +@since version 3.0.0 +*/ +class type_error : public exception +{ + public: + static type_error create(int id_, const std::string& what_arg) + { + std::string w = exception::name("type_error", id_) + what_arg; + return type_error(id_, w.c_str()); + } + + private: + type_error(int id_, const char* what_arg) : exception(id_, what_arg) {} +}; + +/*! +@brief exception indicating access out of the defined range + +This exception is thrown in case a library function is called on an input +parameter that exceeds the expected range, for instance in case of array +indices or nonexisting object keys. + +Exceptions have ids 4xx. + +name / id | example message | description +------------------------------- | --------------- | ------------------------- +json.exception.out_of_range.401 | array index 3 is out of range | The provided array index @a i is larger than @a size-1. +json.exception.out_of_range.402 | array index '-' (3) is out of range | The special array index `-` in a JSON Pointer never describes a valid element of the array, but the index past the end. That is, it can only be used to add elements at this position, but not to read it. +json.exception.out_of_range.403 | key 'foo' not found | The provided key was not found in the JSON object. +json.exception.out_of_range.404 | unresolved reference token 'foo' | A reference token in a JSON Pointer could not be resolved. +json.exception.out_of_range.405 | JSON pointer has no parent | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value. +json.exception.out_of_range.406 | number overflow parsing '10E1000' | A parsed number could not be stored as without changing it to NaN or INF. +json.exception.out_of_range.407 | number overflow serializing '9223372036854775808' | UBJSON and BSON only support integer numbers up to 9223372036854775807. | +json.exception.out_of_range.408 | excessive array size: 8658170730974374167 | The size (following `#`) of an UBJSON array or object exceeds the maximal capacity. | +json.exception.out_of_range.409 | BSON key cannot contain code point U+0000 (at byte 2) | Key identifiers to be serialized to BSON cannot contain code point U+0000, since the key is stored as zero-terminated c-string | + +@liveexample{The following code shows how an `out_of_range` exception can be +caught.,out_of_range} + +@sa @ref exception for the base class of the library exceptions +@sa @ref parse_error for exceptions indicating a parse error +@sa @ref invalid_iterator for exceptions indicating errors with iterators +@sa @ref type_error for exceptions indicating executing a member function with + a wrong type +@sa @ref other_error for exceptions indicating other library errors + +@since version 3.0.0 +*/ +class out_of_range : public exception +{ + public: + static out_of_range create(int id_, const std::string& what_arg) + { + std::string w = exception::name("out_of_range", id_) + what_arg; + return out_of_range(id_, w.c_str()); + } + + private: + out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {} +}; + +/*! +@brief exception indicating other library errors + +This exception is thrown in case of errors that cannot be classified with the +other exception types. + +Exceptions have ids 5xx. + +name / id | example message | description +------------------------------ | --------------- | ------------------------- +json.exception.other_error.501 | unsuccessful: {"op":"test","path":"/baz", "value":"bar"} | A JSON Patch operation 'test' failed. The unsuccessful operation is also printed. + +@sa @ref exception for the base class of the library exceptions +@sa @ref parse_error for exceptions indicating a parse error +@sa @ref invalid_iterator for exceptions indicating errors with iterators +@sa @ref type_error for exceptions indicating executing a member function with + a wrong type +@sa @ref out_of_range for exceptions indicating access out of the defined range + +@liveexample{The following code shows how an `other_error` exception can be +caught.,other_error} + +@since version 3.0.0 +*/ +class other_error : public exception +{ + public: + static other_error create(int id_, const std::string& what_arg) + { + std::string w = exception::name("other_error", id_) + what_arg; + return other_error(id_, w.c_str()); + } + + private: + other_error(int id_, const char* what_arg) : exception(id_, what_arg) {} +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/input/binary_reader.hpp b/include/lib/modernjson/detail/input/binary_reader.hpp new file mode 100644 index 0000000..637569a --- /dev/null +++ b/include/lib/modernjson/detail/input/binary_reader.hpp @@ -0,0 +1,1984 @@ +#pragma once + +#include // generate_n +#include // array +#include // assert +#include // ldexp +#include // size_t +#include // uint8_t, uint16_t, uint32_t, uint64_t +#include // snprintf +#include // memcpy +#include // back_inserter +#include // numeric_limits +#include // char_traits, string +#include // make_pair, move + +#include +#include +#include +#include +#include +#include + +namespace nlohmann +{ +namespace detail +{ +/////////////////// +// binary reader // +/////////////////// + +/*! +@brief deserialization of CBOR, MessagePack, and UBJSON values +*/ +template> +class binary_reader +{ + using number_integer_t = typename BasicJsonType::number_integer_t; + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + using number_float_t = typename BasicJsonType::number_float_t; + using string_t = typename BasicJsonType::string_t; + using json_sax_t = SAX; + + public: + /*! + @brief create a binary reader + + @param[in] adapter input adapter to read from + */ + explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter)) + { + (void)detail::is_sax_static_asserts {}; + assert(ia); + } + + /*! + @param[in] format the binary format to parse + @param[in] sax_ a SAX event processor + @param[in] strict whether to expect the input to be consumed completed + + @return + */ + bool sax_parse(const input_format_t format, + json_sax_t* sax_, + const bool strict = true) + { + sax = sax_; + bool result = false; + + switch (format) + { + case input_format_t::bson: + result = parse_bson_internal(); + break; + + case input_format_t::cbor: + result = parse_cbor_internal(); + break; + + case input_format_t::msgpack: + result = parse_msgpack_internal(); + break; + + case input_format_t::ubjson: + result = parse_ubjson_internal(); + break; + + // LCOV_EXCL_START + default: + assert(false); + // LCOV_EXCL_STOP + } + + // strict mode: next byte must be EOF + if (result and strict) + { + if (format == input_format_t::ubjson) + { + get_ignore_noop(); + } + else + { + get(); + } + + if (JSON_UNLIKELY(current != std::char_traits::eof())) + { + return sax->parse_error(chars_read, get_token_string(), + parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"))); + } + } + + return result; + } + + /*! + @brief determine system byte order + + @return true if and only if system's byte order is little endian + + @note from http://stackoverflow.com/a/1001328/266378 + */ + static constexpr bool little_endianess(int num = 1) noexcept + { + return (*reinterpret_cast(&num) == 1); + } + + private: + ////////// + // BSON // + ////////// + + /*! + @brief Reads in a BSON-object and passes it to the SAX-parser. + @return whether a valid BSON-value was passed to the SAX parser + */ + bool parse_bson_internal() + { + std::int32_t document_size; + get_number(input_format_t::bson, document_size); + + if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1)))) + { + return false; + } + + if (JSON_UNLIKELY(not parse_bson_element_list(/*is_array*/false))) + { + return false; + } + + return sax->end_object(); + } + + /*! + @brief Parses a C-style string from the BSON input. + @param[in, out] result A reference to the string variable where the read + string is to be stored. + @return `true` if the \x00-byte indicating the end of the string was + encountered before the EOF; false` indicates an unexpected EOF. + */ + bool get_bson_cstr(string_t& result) + { + auto out = std::back_inserter(result); + while (true) + { + get(); + if (JSON_UNLIKELY(not unexpect_eof(input_format_t::bson, "cstring"))) + { + return false; + } + if (current == 0x00) + { + return true; + } + *out++ = static_cast(current); + } + + return true; + } + + /*! + @brief Parses a zero-terminated string of length @a len from the BSON + input. + @param[in] len The length (including the zero-byte at the end) of the + string to be read. + @param[in, out] result A reference to the string variable where the read + string is to be stored. + @tparam NumberType The type of the length @a len + @pre len >= 1 + @return `true` if the string was successfully parsed + */ + template + bool get_bson_string(const NumberType len, string_t& result) + { + if (JSON_UNLIKELY(len < 1)) + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"))); + } + + return get_string(input_format_t::bson, len - static_cast(1), result) and get() != std::char_traits::eof(); + } + + /*! + @brief Read a BSON document element of the given @a element_type. + @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html + @param[in] element_type_parse_position The position in the input stream, + where the `element_type` was read. + @warning Not all BSON element types are supported yet. An unsupported + @a element_type will give rise to a parse_error.114: + Unsupported BSON record type 0x... + @return whether a valid BSON-object/array was passed to the SAX parser + */ + bool parse_bson_element_internal(const int element_type, + const std::size_t element_type_parse_position) + { + switch (element_type) + { + case 0x01: // double + { + double number; + return get_number(input_format_t::bson, number) and sax->number_float(static_cast(number), ""); + } + + case 0x02: // string + { + std::int32_t len; + string_t value; + return get_number(input_format_t::bson, len) and get_bson_string(len, value) and sax->string(value); + } + + case 0x03: // object + { + return parse_bson_internal(); + } + + case 0x04: // array + { + return parse_bson_array(); + } + + case 0x08: // boolean + { + return sax->boolean(get() != 0); + } + + case 0x0A: // null + { + return sax->null(); + } + + case 0x10: // int32 + { + std::int32_t value; + return get_number(input_format_t::bson, value) and sax->number_integer(value); + } + + case 0x12: // int64 + { + std::int64_t value; + return get_number(input_format_t::bson, value) and sax->number_integer(value); + } + + default: // anything else not supported (yet) + { + char cr[3]; + (std::snprintf)(cr, sizeof(cr), "%.2hhX", static_cast(element_type)); + return sax->parse_error(element_type_parse_position, std::string(cr), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr))); + } + } + } + + /*! + @brief Read a BSON element list (as specified in the BSON-spec) + + The same binary layout is used for objects and arrays, hence it must be + indicated with the argument @a is_array which one is expected + (true --> array, false --> object). + + @param[in] is_array Determines if the element list being read is to be + treated as an object (@a is_array == false), or as an + array (@a is_array == true). + @return whether a valid BSON-object/array was passed to the SAX parser + */ + bool parse_bson_element_list(const bool is_array) + { + string_t key; + while (int element_type = get()) + { + if (JSON_UNLIKELY(not unexpect_eof(input_format_t::bson, "element list"))) + { + return false; + } + + const std::size_t element_type_parse_position = chars_read; + if (JSON_UNLIKELY(not get_bson_cstr(key))) + { + return false; + } + + if (not is_array) + { + if (not sax->key(key)) + { + return false; + } + } + + if (JSON_UNLIKELY(not parse_bson_element_internal(element_type, element_type_parse_position))) + { + return false; + } + + // get_bson_cstr only appends + key.clear(); + } + + return true; + } + + /*! + @brief Reads an array from the BSON input and passes it to the SAX-parser. + @return whether a valid BSON-array was passed to the SAX parser + */ + bool parse_bson_array() + { + std::int32_t document_size; + get_number(input_format_t::bson, document_size); + + if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1)))) + { + return false; + } + + if (JSON_UNLIKELY(not parse_bson_element_list(/*is_array*/true))) + { + return false; + } + + return sax->end_array(); + } + + ////////// + // CBOR // + ////////// + + /*! + @param[in] get_char whether a new character should be retrieved from the + input (true, default) or whether the last read + character should be considered instead + + @return whether a valid CBOR value was passed to the SAX parser + */ + bool parse_cbor_internal(const bool get_char = true) + { + switch (get_char ? get() : current) + { + // EOF + case std::char_traits::eof(): + return unexpect_eof(input_format_t::cbor, "value"); + + // Integer 0x00..0x17 (0..23) + case 0x00: + case 0x01: + case 0x02: + case 0x03: + case 0x04: + case 0x05: + case 0x06: + case 0x07: + case 0x08: + case 0x09: + case 0x0A: + case 0x0B: + case 0x0C: + case 0x0D: + case 0x0E: + case 0x0F: + case 0x10: + case 0x11: + case 0x12: + case 0x13: + case 0x14: + case 0x15: + case 0x16: + case 0x17: + return sax->number_unsigned(static_cast(current)); + + case 0x18: // Unsigned integer (one-byte uint8_t follows) + { + uint8_t number; + return get_number(input_format_t::cbor, number) and sax->number_unsigned(number); + } + + case 0x19: // Unsigned integer (two-byte uint16_t follows) + { + uint16_t number; + return get_number(input_format_t::cbor, number) and sax->number_unsigned(number); + } + + case 0x1A: // Unsigned integer (four-byte uint32_t follows) + { + uint32_t number; + return get_number(input_format_t::cbor, number) and sax->number_unsigned(number); + } + + case 0x1B: // Unsigned integer (eight-byte uint64_t follows) + { + uint64_t number; + return get_number(input_format_t::cbor, number) and sax->number_unsigned(number); + } + + // Negative integer -1-0x00..-1-0x17 (-1..-24) + case 0x20: + case 0x21: + case 0x22: + case 0x23: + case 0x24: + case 0x25: + case 0x26: + case 0x27: + case 0x28: + case 0x29: + case 0x2A: + case 0x2B: + case 0x2C: + case 0x2D: + case 0x2E: + case 0x2F: + case 0x30: + case 0x31: + case 0x32: + case 0x33: + case 0x34: + case 0x35: + case 0x36: + case 0x37: + return sax->number_integer(static_cast(0x20 - 1 - current)); + + case 0x38: // Negative integer (one-byte uint8_t follows) + { + uint8_t number; + return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast(-1) - number); + } + + case 0x39: // Negative integer -1-n (two-byte uint16_t follows) + { + uint16_t number; + return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast(-1) - number); + } + + case 0x3A: // Negative integer -1-n (four-byte uint32_t follows) + { + uint32_t number; + return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast(-1) - number); + } + + case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows) + { + uint64_t number; + return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast(-1) + - static_cast(number)); + } + + // UTF-8 string (0x00..0x17 bytes follow) + case 0x60: + case 0x61: + case 0x62: + case 0x63: + case 0x64: + case 0x65: + case 0x66: + case 0x67: + case 0x68: + case 0x69: + case 0x6A: + case 0x6B: + case 0x6C: + case 0x6D: + case 0x6E: + case 0x6F: + case 0x70: + case 0x71: + case 0x72: + case 0x73: + case 0x74: + case 0x75: + case 0x76: + case 0x77: + case 0x78: // UTF-8 string (one-byte uint8_t for n follows) + case 0x79: // UTF-8 string (two-byte uint16_t for n follow) + case 0x7A: // UTF-8 string (four-byte uint32_t for n follow) + case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow) + case 0x7F: // UTF-8 string (indefinite length) + { + string_t s; + return get_cbor_string(s) and sax->string(s); + } + + // array (0x00..0x17 data items follow) + case 0x80: + case 0x81: + case 0x82: + case 0x83: + case 0x84: + case 0x85: + case 0x86: + case 0x87: + case 0x88: + case 0x89: + case 0x8A: + case 0x8B: + case 0x8C: + case 0x8D: + case 0x8E: + case 0x8F: + case 0x90: + case 0x91: + case 0x92: + case 0x93: + case 0x94: + case 0x95: + case 0x96: + case 0x97: + return get_cbor_array(static_cast(current & 0x1F)); + + case 0x98: // array (one-byte uint8_t for n follows) + { + uint8_t len; + return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast(len)); + } + + case 0x99: // array (two-byte uint16_t for n follow) + { + uint16_t len; + return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast(len)); + } + + case 0x9A: // array (four-byte uint32_t for n follow) + { + uint32_t len; + return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast(len)); + } + + case 0x9B: // array (eight-byte uint64_t for n follow) + { + uint64_t len; + return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast(len)); + } + + case 0x9F: // array (indefinite length) + return get_cbor_array(std::size_t(-1)); + + // map (0x00..0x17 pairs of data items follow) + case 0xA0: + case 0xA1: + case 0xA2: + case 0xA3: + case 0xA4: + case 0xA5: + case 0xA6: + case 0xA7: + case 0xA8: + case 0xA9: + case 0xAA: + case 0xAB: + case 0xAC: + case 0xAD: + case 0xAE: + case 0xAF: + case 0xB0: + case 0xB1: + case 0xB2: + case 0xB3: + case 0xB4: + case 0xB5: + case 0xB6: + case 0xB7: + return get_cbor_object(static_cast(current & 0x1F)); + + case 0xB8: // map (one-byte uint8_t for n follows) + { + uint8_t len; + return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast(len)); + } + + case 0xB9: // map (two-byte uint16_t for n follow) + { + uint16_t len; + return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast(len)); + } + + case 0xBA: // map (four-byte uint32_t for n follow) + { + uint32_t len; + return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast(len)); + } + + case 0xBB: // map (eight-byte uint64_t for n follow) + { + uint64_t len; + return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast(len)); + } + + case 0xBF: // map (indefinite length) + return get_cbor_object(std::size_t(-1)); + + case 0xF4: // false + return sax->boolean(false); + + case 0xF5: // true + return sax->boolean(true); + + case 0xF6: // null + return sax->null(); + + case 0xF9: // Half-Precision Float (two-byte IEEE 754) + { + const int byte1_raw = get(); + if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number"))) + { + return false; + } + const int byte2_raw = get(); + if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number"))) + { + return false; + } + + const auto byte1 = static_cast(byte1_raw); + const auto byte2 = static_cast(byte2_raw); + + // code from RFC 7049, Appendix D, Figure 3: + // As half-precision floating-point numbers were only added + // to IEEE 754 in 2008, today's programming platforms often + // still only have limited support for them. It is very + // easy to include at least decoding support for them even + // without such support. An example of a small decoder for + // half-precision floating-point numbers in the C language + // is shown in Fig. 3. + const int half = (byte1 << 8) + byte2; + const double val = [&half] + { + const int exp = (half >> 10) & 0x1F; + const int mant = half & 0x3FF; + assert(0 <= exp and exp <= 32); + assert(0 <= mant and mant <= 1024); + switch (exp) + { + case 0: + return std::ldexp(mant, -24); + case 31: + return (mant == 0) + ? std::numeric_limits::infinity() + : std::numeric_limits::quiet_NaN(); + default: + return std::ldexp(mant + 1024, exp - 25); + } + }(); + return sax->number_float((half & 0x8000) != 0 + ? static_cast(-val) + : static_cast(val), ""); + } + + case 0xFA: // Single-Precision Float (four-byte IEEE 754) + { + float number; + return get_number(input_format_t::cbor, number) and sax->number_float(static_cast(number), ""); + } + + case 0xFB: // Double-Precision Float (eight-byte IEEE 754) + { + double number; + return get_number(input_format_t::cbor, number) and sax->number_float(static_cast(number), ""); + } + + default: // anything else (0xFF is handled inside the other types) + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + } + } + } + + /*! + @brief reads a CBOR string + + This function first reads starting bytes to determine the expected + string length and then copies this number of bytes into a string. + Additionally, CBOR's strings with indefinite lengths are supported. + + @param[out] result created string + + @return whether string creation completed + */ + bool get_cbor_string(string_t& result) + { + if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "string"))) + { + return false; + } + + switch (current) + { + // UTF-8 string (0x00..0x17 bytes follow) + case 0x60: + case 0x61: + case 0x62: + case 0x63: + case 0x64: + case 0x65: + case 0x66: + case 0x67: + case 0x68: + case 0x69: + case 0x6A: + case 0x6B: + case 0x6C: + case 0x6D: + case 0x6E: + case 0x6F: + case 0x70: + case 0x71: + case 0x72: + case 0x73: + case 0x74: + case 0x75: + case 0x76: + case 0x77: + { + return get_string(input_format_t::cbor, current & 0x1F, result); + } + + case 0x78: // UTF-8 string (one-byte uint8_t for n follows) + { + uint8_t len; + return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result); + } + + case 0x79: // UTF-8 string (two-byte uint16_t for n follow) + { + uint16_t len; + return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result); + } + + case 0x7A: // UTF-8 string (four-byte uint32_t for n follow) + { + uint32_t len; + return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result); + } + + case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow) + { + uint64_t len; + return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result); + } + + case 0x7F: // UTF-8 string (indefinite length) + { + while (get() != 0xFF) + { + string_t chunk; + if (not get_cbor_string(chunk)) + { + return false; + } + result.append(chunk); + } + return true; + } + + default: + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"))); + } + } + } + + /*! + @param[in] len the length of the array or std::size_t(-1) for an + array of indefinite size + @return whether array creation completed + */ + bool get_cbor_array(const std::size_t len) + { + if (JSON_UNLIKELY(not sax->start_array(len))) + { + return false; + } + + if (len != std::size_t(-1)) + { + for (std::size_t i = 0; i < len; ++i) + { + if (JSON_UNLIKELY(not parse_cbor_internal())) + { + return false; + } + } + } + else + { + while (get() != 0xFF) + { + if (JSON_UNLIKELY(not parse_cbor_internal(false))) + { + return false; + } + } + } + + return sax->end_array(); + } + + /*! + @param[in] len the length of the object or std::size_t(-1) for an + object of indefinite size + @return whether object creation completed + */ + bool get_cbor_object(const std::size_t len) + { + if (not JSON_UNLIKELY(sax->start_object(len))) + { + return false; + } + + string_t key; + if (len != std::size_t(-1)) + { + for (std::size_t i = 0; i < len; ++i) + { + get(); + if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key))) + { + return false; + } + + if (JSON_UNLIKELY(not parse_cbor_internal())) + { + return false; + } + key.clear(); + } + } + else + { + while (get() != 0xFF) + { + if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key))) + { + return false; + } + + if (JSON_UNLIKELY(not parse_cbor_internal())) + { + return false; + } + key.clear(); + } + } + + return sax->end_object(); + } + + ///////////// + // MsgPack // + ///////////// + + /*! + @return whether a valid MessagePack value was passed to the SAX parser + */ + bool parse_msgpack_internal() + { + switch (get()) + { + // EOF + case std::char_traits::eof(): + return unexpect_eof(input_format_t::msgpack, "value"); + + // positive fixint + case 0x00: + case 0x01: + case 0x02: + case 0x03: + case 0x04: + case 0x05: + case 0x06: + case 0x07: + case 0x08: + case 0x09: + case 0x0A: + case 0x0B: + case 0x0C: + case 0x0D: + case 0x0E: + case 0x0F: + case 0x10: + case 0x11: + case 0x12: + case 0x13: + case 0x14: + case 0x15: + case 0x16: + case 0x17: + case 0x18: + case 0x19: + case 0x1A: + case 0x1B: + case 0x1C: + case 0x1D: + case 0x1E: + case 0x1F: + case 0x20: + case 0x21: + case 0x22: + case 0x23: + case 0x24: + case 0x25: + case 0x26: + case 0x27: + case 0x28: + case 0x29: + case 0x2A: + case 0x2B: + case 0x2C: + case 0x2D: + case 0x2E: + case 0x2F: + case 0x30: + case 0x31: + case 0x32: + case 0x33: + case 0x34: + case 0x35: + case 0x36: + case 0x37: + case 0x38: + case 0x39: + case 0x3A: + case 0x3B: + case 0x3C: + case 0x3D: + case 0x3E: + case 0x3F: + case 0x40: + case 0x41: + case 0x42: + case 0x43: + case 0x44: + case 0x45: + case 0x46: + case 0x47: + case 0x48: + case 0x49: + case 0x4A: + case 0x4B: + case 0x4C: + case 0x4D: + case 0x4E: + case 0x4F: + case 0x50: + case 0x51: + case 0x52: + case 0x53: + case 0x54: + case 0x55: + case 0x56: + case 0x57: + case 0x58: + case 0x59: + case 0x5A: + case 0x5B: + case 0x5C: + case 0x5D: + case 0x5E: + case 0x5F: + case 0x60: + case 0x61: + case 0x62: + case 0x63: + case 0x64: + case 0x65: + case 0x66: + case 0x67: + case 0x68: + case 0x69: + case 0x6A: + case 0x6B: + case 0x6C: + case 0x6D: + case 0x6E: + case 0x6F: + case 0x70: + case 0x71: + case 0x72: + case 0x73: + case 0x74: + case 0x75: + case 0x76: + case 0x77: + case 0x78: + case 0x79: + case 0x7A: + case 0x7B: + case 0x7C: + case 0x7D: + case 0x7E: + case 0x7F: + return sax->number_unsigned(static_cast(current)); + + // fixmap + case 0x80: + case 0x81: + case 0x82: + case 0x83: + case 0x84: + case 0x85: + case 0x86: + case 0x87: + case 0x88: + case 0x89: + case 0x8A: + case 0x8B: + case 0x8C: + case 0x8D: + case 0x8E: + case 0x8F: + return get_msgpack_object(static_cast(current & 0x0F)); + + // fixarray + case 0x90: + case 0x91: + case 0x92: + case 0x93: + case 0x94: + case 0x95: + case 0x96: + case 0x97: + case 0x98: + case 0x99: + case 0x9A: + case 0x9B: + case 0x9C: + case 0x9D: + case 0x9E: + case 0x9F: + return get_msgpack_array(static_cast(current & 0x0F)); + + // fixstr + case 0xA0: + case 0xA1: + case 0xA2: + case 0xA3: + case 0xA4: + case 0xA5: + case 0xA6: + case 0xA7: + case 0xA8: + case 0xA9: + case 0xAA: + case 0xAB: + case 0xAC: + case 0xAD: + case 0xAE: + case 0xAF: + case 0xB0: + case 0xB1: + case 0xB2: + case 0xB3: + case 0xB4: + case 0xB5: + case 0xB6: + case 0xB7: + case 0xB8: + case 0xB9: + case 0xBA: + case 0xBB: + case 0xBC: + case 0xBD: + case 0xBE: + case 0xBF: + { + string_t s; + return get_msgpack_string(s) and sax->string(s); + } + + case 0xC0: // nil + return sax->null(); + + case 0xC2: // false + return sax->boolean(false); + + case 0xC3: // true + return sax->boolean(true); + + case 0xCA: // float 32 + { + float number; + return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast(number), ""); + } + + case 0xCB: // float 64 + { + double number; + return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast(number), ""); + } + + case 0xCC: // uint 8 + { + uint8_t number; + return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number); + } + + case 0xCD: // uint 16 + { + uint16_t number; + return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number); + } + + case 0xCE: // uint 32 + { + uint32_t number; + return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number); + } + + case 0xCF: // uint 64 + { + uint64_t number; + return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number); + } + + case 0xD0: // int 8 + { + int8_t number; + return get_number(input_format_t::msgpack, number) and sax->number_integer(number); + } + + case 0xD1: // int 16 + { + int16_t number; + return get_number(input_format_t::msgpack, number) and sax->number_integer(number); + } + + case 0xD2: // int 32 + { + int32_t number; + return get_number(input_format_t::msgpack, number) and sax->number_integer(number); + } + + case 0xD3: // int 64 + { + int64_t number; + return get_number(input_format_t::msgpack, number) and sax->number_integer(number); + } + + case 0xD9: // str 8 + case 0xDA: // str 16 + case 0xDB: // str 32 + { + string_t s; + return get_msgpack_string(s) and sax->string(s); + } + + case 0xDC: // array 16 + { + uint16_t len; + return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast(len)); + } + + case 0xDD: // array 32 + { + uint32_t len; + return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast(len)); + } + + case 0xDE: // map 16 + { + uint16_t len; + return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast(len)); + } + + case 0xDF: // map 32 + { + uint32_t len; + return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast(len)); + } + + // negative fixint + case 0xE0: + case 0xE1: + case 0xE2: + case 0xE3: + case 0xE4: + case 0xE5: + case 0xE6: + case 0xE7: + case 0xE8: + case 0xE9: + case 0xEA: + case 0xEB: + case 0xEC: + case 0xED: + case 0xEE: + case 0xEF: + case 0xF0: + case 0xF1: + case 0xF2: + case 0xF3: + case 0xF4: + case 0xF5: + case 0xF6: + case 0xF7: + case 0xF8: + case 0xF9: + case 0xFA: + case 0xFB: + case 0xFC: + case 0xFD: + case 0xFE: + case 0xFF: + return sax->number_integer(static_cast(current)); + + default: // anything else + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"))); + } + } + } + + /*! + @brief reads a MessagePack string + + This function first reads starting bytes to determine the expected + string length and then copies this number of bytes into a string. + + @param[out] result created string + + @return whether string creation completed + */ + bool get_msgpack_string(string_t& result) + { + if (JSON_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "string"))) + { + return false; + } + + switch (current) + { + // fixstr + case 0xA0: + case 0xA1: + case 0xA2: + case 0xA3: + case 0xA4: + case 0xA5: + case 0xA6: + case 0xA7: + case 0xA8: + case 0xA9: + case 0xAA: + case 0xAB: + case 0xAC: + case 0xAD: + case 0xAE: + case 0xAF: + case 0xB0: + case 0xB1: + case 0xB2: + case 0xB3: + case 0xB4: + case 0xB5: + case 0xB6: + case 0xB7: + case 0xB8: + case 0xB9: + case 0xBA: + case 0xBB: + case 0xBC: + case 0xBD: + case 0xBE: + case 0xBF: + { + return get_string(input_format_t::msgpack, current & 0x1F, result); + } + + case 0xD9: // str 8 + { + uint8_t len; + return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result); + } + + case 0xDA: // str 16 + { + uint16_t len; + return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result); + } + + case 0xDB: // str 32 + { + uint32_t len; + return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result); + } + + default: + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"))); + } + } + } + + /*! + @param[in] len the length of the array + @return whether array creation completed + */ + bool get_msgpack_array(const std::size_t len) + { + if (JSON_UNLIKELY(not sax->start_array(len))) + { + return false; + } + + for (std::size_t i = 0; i < len; ++i) + { + if (JSON_UNLIKELY(not parse_msgpack_internal())) + { + return false; + } + } + + return sax->end_array(); + } + + /*! + @param[in] len the length of the object + @return whether object creation completed + */ + bool get_msgpack_object(const std::size_t len) + { + if (JSON_UNLIKELY(not sax->start_object(len))) + { + return false; + } + + string_t key; + for (std::size_t i = 0; i < len; ++i) + { + get(); + if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(key))) + { + return false; + } + + if (JSON_UNLIKELY(not parse_msgpack_internal())) + { + return false; + } + key.clear(); + } + + return sax->end_object(); + } + + //////////// + // UBJSON // + //////////// + + /*! + @param[in] get_char whether a new character should be retrieved from the + input (true, default) or whether the last read + character should be considered instead + + @return whether a valid UBJSON value was passed to the SAX parser + */ + bool parse_ubjson_internal(const bool get_char = true) + { + return get_ubjson_value(get_char ? get_ignore_noop() : current); + } + + /*! + @brief reads a UBJSON string + + This function is either called after reading the 'S' byte explicitly + indicating a string, or in case of an object key where the 'S' byte can be + left out. + + @param[out] result created string + @param[in] get_char whether a new character should be retrieved from the + input (true, default) or whether the last read + character should be considered instead + + @return whether string creation completed + */ + bool get_ubjson_string(string_t& result, const bool get_char = true) + { + if (get_char) + { + get(); // TODO: may we ignore N here? + } + + if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value"))) + { + return false; + } + + switch (current) + { + case 'U': + { + uint8_t len; + return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result); + } + + case 'i': + { + int8_t len; + return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result); + } + + case 'I': + { + int16_t len; + return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result); + } + + case 'l': + { + int32_t len; + return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result); + } + + case 'L': + { + int64_t len; + return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result); + } + + default: + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"))); + } + } + + /*! + @param[out] result determined size + @return whether size determination completed + */ + bool get_ubjson_size_value(std::size_t& result) + { + switch (get_ignore_noop()) + { + case 'U': + { + uint8_t number; + if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number))) + { + return false; + } + result = static_cast(number); + return true; + } + + case 'i': + { + int8_t number; + if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number))) + { + return false; + } + result = static_cast(number); + return true; + } + + case 'I': + { + int16_t number; + if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number))) + { + return false; + } + result = static_cast(number); + return true; + } + + case 'l': + { + int32_t number; + if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number))) + { + return false; + } + result = static_cast(number); + return true; + } + + case 'L': + { + int64_t number; + if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number))) + { + return false; + } + result = static_cast(number); + return true; + } + + default: + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"))); + } + } + } + + /*! + @brief determine the type and size for a container + + In the optimized UBJSON format, a type and a size can be provided to allow + for a more compact representation. + + @param[out] result pair of the size and the type + + @return whether pair creation completed + */ + bool get_ubjson_size_type(std::pair& result) + { + result.first = string_t::npos; // size + result.second = 0; // type + + get_ignore_noop(); + + if (current == '$') + { + result.second = get(); // must not ignore 'N', because 'N' maybe the type + if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "type"))) + { + return false; + } + + get_ignore_noop(); + if (JSON_UNLIKELY(current != '#')) + { + if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value"))) + { + return false; + } + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"))); + } + + return get_ubjson_size_value(result.first); + } + else if (current == '#') + { + return get_ubjson_size_value(result.first); + } + return true; + } + + /*! + @param prefix the previously read or set type prefix + @return whether value creation completed + */ + bool get_ubjson_value(const int prefix) + { + switch (prefix) + { + case std::char_traits::eof(): // EOF + return unexpect_eof(input_format_t::ubjson, "value"); + + case 'T': // true + return sax->boolean(true); + case 'F': // false + return sax->boolean(false); + + case 'Z': // null + return sax->null(); + + case 'U': + { + uint8_t number; + return get_number(input_format_t::ubjson, number) and sax->number_unsigned(number); + } + + case 'i': + { + int8_t number; + return get_number(input_format_t::ubjson, number) and sax->number_integer(number); + } + + case 'I': + { + int16_t number; + return get_number(input_format_t::ubjson, number) and sax->number_integer(number); + } + + case 'l': + { + int32_t number; + return get_number(input_format_t::ubjson, number) and sax->number_integer(number); + } + + case 'L': + { + int64_t number; + return get_number(input_format_t::ubjson, number) and sax->number_integer(number); + } + + case 'd': + { + float number; + return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast(number), ""); + } + + case 'D': + { + double number; + return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast(number), ""); + } + + case 'C': // char + { + get(); + if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "char"))) + { + return false; + } + if (JSON_UNLIKELY(current > 127)) + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"))); + } + string_t s(1, static_cast(current)); + return sax->string(s); + } + + case 'S': // string + { + string_t s; + return get_ubjson_string(s) and sax->string(s); + } + + case '[': // array + return get_ubjson_array(); + + case '{': // object + return get_ubjson_object(); + + default: // anything else + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"))); + } + } + } + + /*! + @return whether array creation completed + */ + bool get_ubjson_array() + { + std::pair size_and_type; + if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type))) + { + return false; + } + + if (size_and_type.first != string_t::npos) + { + if (JSON_UNLIKELY(not sax->start_array(size_and_type.first))) + { + return false; + } + + if (size_and_type.second != 0) + { + if (size_and_type.second != 'N') + { + for (std::size_t i = 0; i < size_and_type.first; ++i) + { + if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second))) + { + return false; + } + } + } + } + else + { + for (std::size_t i = 0; i < size_and_type.first; ++i) + { + if (JSON_UNLIKELY(not parse_ubjson_internal())) + { + return false; + } + } + } + } + else + { + if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1)))) + { + return false; + } + + while (current != ']') + { + if (JSON_UNLIKELY(not parse_ubjson_internal(false))) + { + return false; + } + get_ignore_noop(); + } + } + + return sax->end_array(); + } + + /*! + @return whether object creation completed + */ + bool get_ubjson_object() + { + std::pair size_and_type; + if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type))) + { + return false; + } + + string_t key; + if (size_and_type.first != string_t::npos) + { + if (JSON_UNLIKELY(not sax->start_object(size_and_type.first))) + { + return false; + } + + if (size_and_type.second != 0) + { + for (std::size_t i = 0; i < size_and_type.first; ++i) + { + if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key))) + { + return false; + } + if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second))) + { + return false; + } + key.clear(); + } + } + else + { + for (std::size_t i = 0; i < size_and_type.first; ++i) + { + if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key))) + { + return false; + } + if (JSON_UNLIKELY(not parse_ubjson_internal())) + { + return false; + } + key.clear(); + } + } + } + else + { + if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1)))) + { + return false; + } + + while (current != '}') + { + if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key))) + { + return false; + } + if (JSON_UNLIKELY(not parse_ubjson_internal())) + { + return false; + } + get_ignore_noop(); + key.clear(); + } + } + + return sax->end_object(); + } + + /////////////////////// + // Utility functions // + /////////////////////// + + /*! + @brief get next character from the input + + This function provides the interface to the used input adapter. It does + not throw in case the input reached EOF, but returns a -'ve valued + `std::char_traits::eof()` in that case. + + @return character read from the input + */ + int get() + { + ++chars_read; + return (current = ia->get_character()); + } + + /*! + @return character read from the input after ignoring all 'N' entries + */ + int get_ignore_noop() + { + do + { + get(); + } + while (current == 'N'); + + return current; + } + + /* + @brief read a number from the input + + @tparam NumberType the type of the number + @param[in] format the current format (for diagnostics) + @param[out] result number of type @a NumberType + + @return whether conversion completed + + @note This function needs to respect the system's endianess, because + bytes in CBOR, MessagePack, and UBJSON are stored in network order + (big endian) and therefore need reordering on little endian systems. + */ + template + bool get_number(const input_format_t format, NumberType& result) + { + // step 1: read input into array with system's byte order + std::array vec; + for (std::size_t i = 0; i < sizeof(NumberType); ++i) + { + get(); + if (JSON_UNLIKELY(not unexpect_eof(format, "number"))) + { + return false; + } + + // reverse byte order prior to conversion if necessary + if (is_little_endian && !InputIsLittleEndian) + { + vec[sizeof(NumberType) - i - 1] = static_cast(current); + } + else + { + vec[i] = static_cast(current); // LCOV_EXCL_LINE + } + } + + // step 2: convert array into number of type T and return + std::memcpy(&result, vec.data(), sizeof(NumberType)); + return true; + } + + /*! + @brief create a string by reading characters from the input + + @tparam NumberType the type of the number + @param[in] format the current format (for diagnostics) + @param[in] len number of characters to read + @param[out] result string created by reading @a len bytes + + @return whether string creation completed + + @note We can not reserve @a len bytes for the result, because @a len + may be too large. Usually, @ref unexpect_eof() detects the end of + the input before we run out of string memory. + */ + template + bool get_string(const input_format_t format, + const NumberType len, + string_t& result) + { + bool success = true; + std::generate_n(std::back_inserter(result), len, [this, &success, &format]() + { + get(); + if (JSON_UNLIKELY(not unexpect_eof(format, "string"))) + { + success = false; + } + return static_cast(current); + }); + return success; + } + + /*! + @param[in] format the current format (for diagnostics) + @param[in] context further context information (for diagnostics) + @return whether the last read character is not EOF + */ + bool unexpect_eof(const input_format_t format, const char* context) const + { + if (JSON_UNLIKELY(current == std::char_traits::eof())) + { + return sax->parse_error(chars_read, "", + parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context))); + } + return true; + } + + /*! + @return a string representation of the last read byte + */ + std::string get_token_string() const + { + char cr[3]; + (std::snprintf)(cr, 3, "%.2hhX", static_cast(current)); + return std::string{cr}; + } + + /*! + @param[in] format the current format + @param[in] detail a detailed error message + @param[in] context further contect information + @return a message string to use in the parse_error exceptions + */ + std::string exception_message(const input_format_t format, + const std::string& detail, + const std::string& context) const + { + std::string error_msg = "syntax error while parsing "; + + switch (format) + { + case input_format_t::cbor: + error_msg += "CBOR"; + break; + + case input_format_t::msgpack: + error_msg += "MessagePack"; + break; + + case input_format_t::ubjson: + error_msg += "UBJSON"; + break; + + case input_format_t::bson: + error_msg += "BSON"; + break; + + // LCOV_EXCL_START + default: + assert(false); + // LCOV_EXCL_STOP + } + + return error_msg + " " + context + ": " + detail; + } + + private: + /// input adapter + input_adapter_t ia = nullptr; + + /// the current character + int current = std::char_traits::eof(); + + /// the number of characters read + std::size_t chars_read = 0; + + /// whether we can assume little endianess + const bool is_little_endian = little_endianess(); + + /// the SAX parser + json_sax_t* sax = nullptr; +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/input/input_adapters.hpp b/include/lib/modernjson/detail/input/input_adapters.hpp new file mode 100644 index 0000000..dfb8caf --- /dev/null +++ b/include/lib/modernjson/detail/input/input_adapters.hpp @@ -0,0 +1,398 @@ +#pragma once + +#include // assert +#include // size_t +#include // strlen +#include // istream +#include // begin, end, iterator_traits, random_access_iterator_tag, distance, next +#include // shared_ptr, make_shared, addressof +#include // accumulate +#include // string, char_traits +#include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer +#include // pair, declval + +#include + +namespace nlohmann +{ +namespace detail +{ +/// the supported input formats +enum class input_format_t { json, cbor, msgpack, ubjson, bson }; + +//////////////////// +// input adapters // +//////////////////// + +/*! +@brief abstract input adapter interface + +Produces a stream of std::char_traits::int_type characters from a +std::istream, a buffer, or some other input type. Accepts the return of +exactly one non-EOF character for future input. The int_type characters +returned consist of all valid char values as positive values (typically +unsigned char), plus an EOF value outside that range, specified by the value +of the function std::char_traits::eof(). This value is typically -1, but +could be any arbitrary value which is not a valid char value. +*/ +struct input_adapter_protocol +{ + /// get a character [0,255] or std::char_traits::eof(). + virtual std::char_traits::int_type get_character() = 0; + virtual ~input_adapter_protocol() = default; +}; + +/// a type to simplify interfaces +using input_adapter_t = std::shared_ptr; + +/*! +Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at +beginning of input. Does not support changing the underlying std::streambuf +in mid-input. Maintains underlying std::istream and std::streambuf to support +subsequent use of standard std::istream operations to process any input +characters following those used in parsing the JSON input. Clears the +std::istream flags; any input errors (e.g., EOF) will be detected by the first +subsequent call for input from the std::istream. +*/ +class input_stream_adapter : public input_adapter_protocol +{ + public: + ~input_stream_adapter() override + { + // clear stream flags; we use underlying streambuf I/O, do not + // maintain ifstream flags, except eof + is.clear(is.rdstate() & std::ios::eofbit); + } + + explicit input_stream_adapter(std::istream& i) + : is(i), sb(*i.rdbuf()) + {} + + // delete because of pointer members + input_stream_adapter(const input_stream_adapter&) = delete; + input_stream_adapter& operator=(input_stream_adapter&) = delete; + input_stream_adapter(input_stream_adapter&&) = delete; + input_stream_adapter& operator=(input_stream_adapter&&) = delete; + + // std::istream/std::streambuf use std::char_traits::to_int_type, to + // ensure that std::char_traits::eof() and the character 0xFF do not + // end up as the same value, eg. 0xFFFFFFFF. + std::char_traits::int_type get_character() override + { + auto res = sb.sbumpc(); + // set eof manually, as we don't use the istream interface. + if (res == EOF) + { + is.clear(is.rdstate() | std::ios::eofbit); + } + return res; + } + + private: + /// the associated input stream + std::istream& is; + std::streambuf& sb; +}; + +/// input adapter for buffer input +class input_buffer_adapter : public input_adapter_protocol +{ + public: + input_buffer_adapter(const char* b, const std::size_t l) noexcept + : cursor(b), limit(b + l) + {} + + // delete because of pointer members + input_buffer_adapter(const input_buffer_adapter&) = delete; + input_buffer_adapter& operator=(input_buffer_adapter&) = delete; + input_buffer_adapter(input_buffer_adapter&&) = delete; + input_buffer_adapter& operator=(input_buffer_adapter&&) = delete; + ~input_buffer_adapter() override = default; + + std::char_traits::int_type get_character() noexcept override + { + if (JSON_LIKELY(cursor < limit)) + { + return std::char_traits::to_int_type(*(cursor++)); + } + + return std::char_traits::eof(); + } + + private: + /// pointer to the current character + const char* cursor; + /// pointer past the last character + const char* const limit; +}; + +template +struct wide_string_input_helper +{ + // UTF-32 + static void fill_buffer(const WideStringType& str, size_t& current_wchar, std::array::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled) + { + utf8_bytes_index = 0; + + if (current_wchar == str.size()) + { + utf8_bytes[0] = std::char_traits::eof(); + utf8_bytes_filled = 1; + } + else + { + // get the current character + const auto wc = static_cast(str[current_wchar++]); + + // UTF-32 to UTF-8 encoding + if (wc < 0x80) + { + utf8_bytes[0] = wc; + utf8_bytes_filled = 1; + } + else if (wc <= 0x7FF) + { + utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F); + utf8_bytes[1] = 0x80 | (wc & 0x3F); + utf8_bytes_filled = 2; + } + else if (wc <= 0xFFFF) + { + utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F); + utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F); + utf8_bytes[2] = 0x80 | (wc & 0x3F); + utf8_bytes_filled = 3; + } + else if (wc <= 0x10FFFF) + { + utf8_bytes[0] = 0xF0 | ((wc >> 18) & 0x07); + utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F); + utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F); + utf8_bytes[3] = 0x80 | (wc & 0x3F); + utf8_bytes_filled = 4; + } + else + { + // unknown character + utf8_bytes[0] = wc; + utf8_bytes_filled = 1; + } + } + } +}; + +template +struct wide_string_input_helper +{ + // UTF-16 + static void fill_buffer(const WideStringType& str, size_t& current_wchar, std::array::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled) + { + utf8_bytes_index = 0; + + if (current_wchar == str.size()) + { + utf8_bytes[0] = std::char_traits::eof(); + utf8_bytes_filled = 1; + } + else + { + // get the current character + const auto wc = static_cast(str[current_wchar++]); + + // UTF-16 to UTF-8 encoding + if (wc < 0x80) + { + utf8_bytes[0] = wc; + utf8_bytes_filled = 1; + } + else if (wc <= 0x7FF) + { + utf8_bytes[0] = 0xC0 | ((wc >> 6)); + utf8_bytes[1] = 0x80 | (wc & 0x3F); + utf8_bytes_filled = 2; + } + else if (0xD800 > wc or wc >= 0xE000) + { + utf8_bytes[0] = 0xE0 | ((wc >> 12)); + utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F); + utf8_bytes[2] = 0x80 | (wc & 0x3F); + utf8_bytes_filled = 3; + } + else + { + if (current_wchar < str.size()) + { + const auto wc2 = static_cast(str[current_wchar++]); + const int charcode = 0x10000 + (((wc & 0x3FF) << 10) | (wc2 & 0x3FF)); + utf8_bytes[0] = 0xf0 | (charcode >> 18); + utf8_bytes[1] = 0x80 | ((charcode >> 12) & 0x3F); + utf8_bytes[2] = 0x80 | ((charcode >> 6) & 0x3F); + utf8_bytes[3] = 0x80 | (charcode & 0x3F); + utf8_bytes_filled = 4; + } + else + { + // unknown character + ++current_wchar; + utf8_bytes[0] = wc; + utf8_bytes_filled = 1; + } + } + } + } +}; + +template +class wide_string_input_adapter : public input_adapter_protocol +{ + public: + explicit wide_string_input_adapter(const WideStringType& w) noexcept + : str(w) + {} + + std::char_traits::int_type get_character() noexcept override + { + // check if buffer needs to be filled + if (utf8_bytes_index == utf8_bytes_filled) + { + fill_buffer(); + + assert(utf8_bytes_filled > 0); + assert(utf8_bytes_index == 0); + } + + // use buffer + assert(utf8_bytes_filled > 0); + assert(utf8_bytes_index < utf8_bytes_filled); + return utf8_bytes[utf8_bytes_index++]; + } + + private: + template + void fill_buffer() + { + wide_string_input_helper::fill_buffer(str, current_wchar, utf8_bytes, utf8_bytes_index, utf8_bytes_filled); + } + + /// the wstring to process + const WideStringType& str; + + /// index of the current wchar in str + std::size_t current_wchar = 0; + + /// a buffer for UTF-8 bytes + std::array::int_type, 4> utf8_bytes = {{0, 0, 0, 0}}; + + /// index to the utf8_codes array for the next valid byte + std::size_t utf8_bytes_index = 0; + /// number of valid bytes in the utf8_codes array + std::size_t utf8_bytes_filled = 0; +}; + +class input_adapter +{ + public: + // native support + + /// input adapter for input stream + input_adapter(std::istream& i) + : ia(std::make_shared(i)) {} + + /// input adapter for input stream + input_adapter(std::istream&& i) + : ia(std::make_shared(i)) {} + + input_adapter(const std::wstring& ws) + : ia(std::make_shared>(ws)) {} + + input_adapter(const std::u16string& ws) + : ia(std::make_shared>(ws)) {} + + input_adapter(const std::u32string& ws) + : ia(std::make_shared>(ws)) {} + + /// input adapter for buffer + template::value and + std::is_integral::type>::value and + sizeof(typename std::remove_pointer::type) == 1, + int>::type = 0> + input_adapter(CharT b, std::size_t l) + : ia(std::make_shared(reinterpret_cast(b), l)) {} + + // derived support + + /// input adapter for string literal + template::value and + std::is_integral::type>::value and + sizeof(typename std::remove_pointer::type) == 1, + int>::type = 0> + input_adapter(CharT b) + : input_adapter(reinterpret_cast(b), + std::strlen(reinterpret_cast(b))) {} + + /// input adapter for iterator range with contiguous storage + template::iterator_category, std::random_access_iterator_tag>::value, + int>::type = 0> + input_adapter(IteratorType first, IteratorType last) + { +#ifndef NDEBUG + // assertion to check that the iterator range is indeed contiguous, + // see http://stackoverflow.com/a/35008842/266378 for more discussion + const auto is_contiguous = std::accumulate( + first, last, std::pair(true, 0), + [&first](std::pair res, decltype(*first) val) + { + res.first &= (val == *(std::next(std::addressof(*first), res.second++))); + return res; + }).first; + assert(is_contiguous); +#endif + + // assertion to check that each element is 1 byte long + static_assert( + sizeof(typename std::iterator_traits::value_type) == 1, + "each element in the iterator range must have the size of 1 byte"); + + const auto len = static_cast(std::distance(first, last)); + if (JSON_LIKELY(len > 0)) + { + // there is at least one element: use the address of first + ia = std::make_shared(reinterpret_cast(&(*first)), len); + } + else + { + // the address of first cannot be used: use nullptr + ia = std::make_shared(nullptr, len); + } + } + + /// input adapter for array + template + input_adapter(T (&array)[N]) + : input_adapter(std::begin(array), std::end(array)) {} + + /// input adapter for contiguous container + template::value and + std::is_base_of()))>::iterator_category>::value, + int>::type = 0> + input_adapter(const ContiguousContainer& c) + : input_adapter(std::begin(c), std::end(c)) {} + + operator input_adapter_t() + { + return ia; + } + + private: + /// the actual adapter + input_adapter_t ia = nullptr; +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/input/json_sax.hpp b/include/lib/modernjson/detail/input/json_sax.hpp new file mode 100644 index 0000000..e1d48a2 --- /dev/null +++ b/include/lib/modernjson/detail/input/json_sax.hpp @@ -0,0 +1,701 @@ +#pragma once + +#include +#include +#include + +#include +#include + +namespace nlohmann +{ + +/*! +@brief SAX interface + +This class describes the SAX interface used by @ref nlohmann::json::sax_parse. +Each function is called in different situations while the input is parsed. The +boolean return value informs the parser whether to continue processing the +input. +*/ +template +struct json_sax +{ + /// type for (signed) integers + using number_integer_t = typename BasicJsonType::number_integer_t; + /// type for unsigned integers + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + /// type for floating-point numbers + using number_float_t = typename BasicJsonType::number_float_t; + /// type for strings + using string_t = typename BasicJsonType::string_t; + + /*! + @brief a null value was read + @return whether parsing should proceed + */ + virtual bool null() = 0; + + /*! + @brief a boolean value was read + @param[in] val boolean value + @return whether parsing should proceed + */ + virtual bool boolean(bool val) = 0; + + /*! + @brief an integer number was read + @param[in] val integer value + @return whether parsing should proceed + */ + virtual bool number_integer(number_integer_t val) = 0; + + /*! + @brief an unsigned integer number was read + @param[in] val unsigned integer value + @return whether parsing should proceed + */ + virtual bool number_unsigned(number_unsigned_t val) = 0; + + /*! + @brief an floating-point number was read + @param[in] val floating-point value + @param[in] s raw token value + @return whether parsing should proceed + */ + virtual bool number_float(number_float_t val, const string_t& s) = 0; + + /*! + @brief a string was read + @param[in] val string value + @return whether parsing should proceed + @note It is safe to move the passed string. + */ + virtual bool string(string_t& val) = 0; + + /*! + @brief the beginning of an object was read + @param[in] elements number of object elements or -1 if unknown + @return whether parsing should proceed + @note binary formats may report the number of elements + */ + virtual bool start_object(std::size_t elements) = 0; + + /*! + @brief an object key was read + @param[in] val object key + @return whether parsing should proceed + @note It is safe to move the passed string. + */ + virtual bool key(string_t& val) = 0; + + /*! + @brief the end of an object was read + @return whether parsing should proceed + */ + virtual bool end_object() = 0; + + /*! + @brief the beginning of an array was read + @param[in] elements number of array elements or -1 if unknown + @return whether parsing should proceed + @note binary formats may report the number of elements + */ + virtual bool start_array(std::size_t elements) = 0; + + /*! + @brief the end of an array was read + @return whether parsing should proceed + */ + virtual bool end_array() = 0; + + /*! + @brief a parse error occurred + @param[in] position the position in the input where the error occurs + @param[in] last_token the last read token + @param[in] ex an exception object describing the error + @return whether parsing should proceed (must return false) + */ + virtual bool parse_error(std::size_t position, + const std::string& last_token, + const detail::exception& ex) = 0; + + virtual ~json_sax() = default; +}; + + +namespace detail +{ +/*! +@brief SAX implementation to create a JSON value from SAX events + +This class implements the @ref json_sax interface and processes the SAX events +to create a JSON value which makes it basically a DOM parser. The structure or +hierarchy of the JSON value is managed by the stack `ref_stack` which contains +a pointer to the respective array or object for each recursion depth. + +After successful parsing, the value that is passed by reference to the +constructor contains the parsed value. + +@tparam BasicJsonType the JSON type +*/ +template +class json_sax_dom_parser +{ + public: + using number_integer_t = typename BasicJsonType::number_integer_t; + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + using number_float_t = typename BasicJsonType::number_float_t; + using string_t = typename BasicJsonType::string_t; + + /*! + @param[in, out] r reference to a JSON value that is manipulated while + parsing + @param[in] allow_exceptions_ whether parse errors yield exceptions + */ + explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true) + : root(r), allow_exceptions(allow_exceptions_) + {} + + bool null() + { + handle_value(nullptr); + return true; + } + + bool boolean(bool val) + { + handle_value(val); + return true; + } + + bool number_integer(number_integer_t val) + { + handle_value(val); + return true; + } + + bool number_unsigned(number_unsigned_t val) + { + handle_value(val); + return true; + } + + bool number_float(number_float_t val, const string_t& /*unused*/) + { + handle_value(val); + return true; + } + + bool string(string_t& val) + { + handle_value(val); + return true; + } + + bool start_object(std::size_t len) + { + ref_stack.push_back(handle_value(BasicJsonType::value_t::object)); + + if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size())) + { + JSON_THROW(out_of_range::create(408, + "excessive object size: " + std::to_string(len))); + } + + return true; + } + + bool key(string_t& val) + { + // add null at given key and store the reference for later + object_element = &(ref_stack.back()->m_value.object->operator[](val)); + return true; + } + + bool end_object() + { + ref_stack.pop_back(); + return true; + } + + bool start_array(std::size_t len) + { + ref_stack.push_back(handle_value(BasicJsonType::value_t::array)); + + if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size())) + { + JSON_THROW(out_of_range::create(408, + "excessive array size: " + std::to_string(len))); + } + + return true; + } + + bool end_array() + { + ref_stack.pop_back(); + return true; + } + + bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, + const detail::exception& ex) + { + errored = true; + if (allow_exceptions) + { + // determine the proper exception type from the id + switch ((ex.id / 100) % 100) + { + case 1: + JSON_THROW(*reinterpret_cast(&ex)); + case 4: + JSON_THROW(*reinterpret_cast(&ex)); + // LCOV_EXCL_START + case 2: + JSON_THROW(*reinterpret_cast(&ex)); + case 3: + JSON_THROW(*reinterpret_cast(&ex)); + case 5: + JSON_THROW(*reinterpret_cast(&ex)); + default: + assert(false); + // LCOV_EXCL_STOP + } + } + return false; + } + + constexpr bool is_errored() const + { + return errored; + } + + private: + /*! + @invariant If the ref stack is empty, then the passed value will be the new + root. + @invariant If the ref stack contains a value, then it is an array or an + object to which we can add elements + */ + template + BasicJsonType* handle_value(Value&& v) + { + if (ref_stack.empty()) + { + root = BasicJsonType(std::forward(v)); + return &root; + } + + assert(ref_stack.back()->is_array() or ref_stack.back()->is_object()); + + if (ref_stack.back()->is_array()) + { + ref_stack.back()->m_value.array->emplace_back(std::forward(v)); + return &(ref_stack.back()->m_value.array->back()); + } + else + { + assert(object_element); + *object_element = BasicJsonType(std::forward(v)); + return object_element; + } + } + + /// the parsed JSON value + BasicJsonType& root; + /// stack to model hierarchy of values + std::vector ref_stack; + /// helper to hold the reference for the next object element + BasicJsonType* object_element = nullptr; + /// whether a syntax error occurred + bool errored = false; + /// whether to throw exceptions in case of errors + const bool allow_exceptions = true; +}; + +template +class json_sax_dom_callback_parser +{ + public: + using number_integer_t = typename BasicJsonType::number_integer_t; + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + using number_float_t = typename BasicJsonType::number_float_t; + using string_t = typename BasicJsonType::string_t; + using parser_callback_t = typename BasicJsonType::parser_callback_t; + using parse_event_t = typename BasicJsonType::parse_event_t; + + json_sax_dom_callback_parser(BasicJsonType& r, + const parser_callback_t cb, + const bool allow_exceptions_ = true) + : root(r), callback(cb), allow_exceptions(allow_exceptions_) + { + keep_stack.push_back(true); + } + + bool null() + { + handle_value(nullptr); + return true; + } + + bool boolean(bool val) + { + handle_value(val); + return true; + } + + bool number_integer(number_integer_t val) + { + handle_value(val); + return true; + } + + bool number_unsigned(number_unsigned_t val) + { + handle_value(val); + return true; + } + + bool number_float(number_float_t val, const string_t& /*unused*/) + { + handle_value(val); + return true; + } + + bool string(string_t& val) + { + handle_value(val); + return true; + } + + bool start_object(std::size_t len) + { + // check callback for object start + const bool keep = callback(static_cast(ref_stack.size()), parse_event_t::object_start, discarded); + keep_stack.push_back(keep); + + auto val = handle_value(BasicJsonType::value_t::object, true); + ref_stack.push_back(val.second); + + // check object limit + if (ref_stack.back()) + { + if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size())) + { + JSON_THROW(out_of_range::create(408, + "excessive object size: " + std::to_string(len))); + } + } + + return true; + } + + bool key(string_t& val) + { + BasicJsonType k = BasicJsonType(val); + + // check callback for key + const bool keep = callback(static_cast(ref_stack.size()), parse_event_t::key, k); + key_keep_stack.push_back(keep); + + // add discarded value at given key and store the reference for later + if (keep and ref_stack.back()) + { + object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded); + } + + return true; + } + + bool end_object() + { + if (ref_stack.back()) + { + if (not callback(static_cast(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back())) + { + // discard object + *ref_stack.back() = discarded; + } + } + + assert(not ref_stack.empty()); + assert(not keep_stack.empty()); + ref_stack.pop_back(); + keep_stack.pop_back(); + + if (not ref_stack.empty() and ref_stack.back()) + { + // remove discarded value + if (ref_stack.back()->is_object()) + { + for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it) + { + if (it->is_discarded()) + { + ref_stack.back()->erase(it); + break; + } + } + } + } + + return true; + } + + bool start_array(std::size_t len) + { + const bool keep = callback(static_cast(ref_stack.size()), parse_event_t::array_start, discarded); + keep_stack.push_back(keep); + + auto val = handle_value(BasicJsonType::value_t::array, true); + ref_stack.push_back(val.second); + + // check array limit + if (ref_stack.back()) + { + if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size())) + { + JSON_THROW(out_of_range::create(408, + "excessive array size: " + std::to_string(len))); + } + } + + return true; + } + + bool end_array() + { + bool keep = true; + + if (ref_stack.back()) + { + keep = callback(static_cast(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back()); + if (not keep) + { + // discard array + *ref_stack.back() = discarded; + } + } + + assert(not ref_stack.empty()); + assert(not keep_stack.empty()); + ref_stack.pop_back(); + keep_stack.pop_back(); + + // remove discarded value + if (not keep and not ref_stack.empty()) + { + if (ref_stack.back()->is_array()) + { + ref_stack.back()->m_value.array->pop_back(); + } + } + + return true; + } + + bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, + const detail::exception& ex) + { + errored = true; + if (allow_exceptions) + { + // determine the proper exception type from the id + switch ((ex.id / 100) % 100) + { + case 1: + JSON_THROW(*reinterpret_cast(&ex)); + case 4: + JSON_THROW(*reinterpret_cast(&ex)); + // LCOV_EXCL_START + case 2: + JSON_THROW(*reinterpret_cast(&ex)); + case 3: + JSON_THROW(*reinterpret_cast(&ex)); + case 5: + JSON_THROW(*reinterpret_cast(&ex)); + default: + assert(false); + // LCOV_EXCL_STOP + } + } + return false; + } + + constexpr bool is_errored() const + { + return errored; + } + + private: + /*! + @param[in] v value to add to the JSON value we build during parsing + @param[in] skip_callback whether we should skip calling the callback + function; this is required after start_array() and + start_object() SAX events, because otherwise we would call the + callback function with an empty array or object, respectively. + + @invariant If the ref stack is empty, then the passed value will be the new + root. + @invariant If the ref stack contains a value, then it is an array or an + object to which we can add elements + + @return pair of boolean (whether value should be kept) and pointer (to the + passed value in the ref_stack hierarchy; nullptr if not kept) + */ + template + std::pair handle_value(Value&& v, const bool skip_callback = false) + { + assert(not keep_stack.empty()); + + // do not handle this value if we know it would be added to a discarded + // container + if (not keep_stack.back()) + { + return {false, nullptr}; + } + + // create value + auto value = BasicJsonType(std::forward(v)); + + // check callback + const bool keep = skip_callback or callback(static_cast(ref_stack.size()), parse_event_t::value, value); + + // do not handle this value if we just learnt it shall be discarded + if (not keep) + { + return {false, nullptr}; + } + + if (ref_stack.empty()) + { + root = std::move(value); + return {true, &root}; + } + + // skip this value if we already decided to skip the parent + // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360) + if (not ref_stack.back()) + { + return {false, nullptr}; + } + + // we now only expect arrays and objects + assert(ref_stack.back()->is_array() or ref_stack.back()->is_object()); + + if (ref_stack.back()->is_array()) + { + ref_stack.back()->m_value.array->push_back(std::move(value)); + return {true, &(ref_stack.back()->m_value.array->back())}; + } + else + { + // check if we should store an element for the current key + assert(not key_keep_stack.empty()); + const bool store_element = key_keep_stack.back(); + key_keep_stack.pop_back(); + + if (not store_element) + { + return {false, nullptr}; + } + + assert(object_element); + *object_element = std::move(value); + return {true, object_element}; + } + } + + /// the parsed JSON value + BasicJsonType& root; + /// stack to model hierarchy of values + std::vector ref_stack; + /// stack to manage which values to keep + std::vector keep_stack; + /// stack to manage which object keys to keep + std::vector key_keep_stack; + /// helper to hold the reference for the next object element + BasicJsonType* object_element = nullptr; + /// whether a syntax error occurred + bool errored = false; + /// callback function + const parser_callback_t callback = nullptr; + /// whether to throw exceptions in case of errors + const bool allow_exceptions = true; + /// a discarded value for the callback + BasicJsonType discarded = BasicJsonType::value_t::discarded; +}; + +template +class json_sax_acceptor +{ + public: + using number_integer_t = typename BasicJsonType::number_integer_t; + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + using number_float_t = typename BasicJsonType::number_float_t; + using string_t = typename BasicJsonType::string_t; + + bool null() + { + return true; + } + + bool boolean(bool /*unused*/) + { + return true; + } + + bool number_integer(number_integer_t /*unused*/) + { + return true; + } + + bool number_unsigned(number_unsigned_t /*unused*/) + { + return true; + } + + bool number_float(number_float_t /*unused*/, const string_t& /*unused*/) + { + return true; + } + + bool string(string_t& /*unused*/) + { + return true; + } + + bool start_object(std::size_t /*unused*/ = std::size_t(-1)) + { + return true; + } + + bool key(string_t& /*unused*/) + { + return true; + } + + bool end_object() + { + return true; + } + + bool start_array(std::size_t /*unused*/ = std::size_t(-1)) + { + return true; + } + + bool end_array() + { + return true; + } + + bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/) + { + return false; + } +}; +} // namespace detail + +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/input/lexer.hpp b/include/lib/modernjson/detail/input/lexer.hpp new file mode 100644 index 0000000..b61e289 --- /dev/null +++ b/include/lib/modernjson/detail/input/lexer.hpp @@ -0,0 +1,1506 @@ +#pragma once + +#include // localeconv +#include // size_t +#include // strtof, strtod, strtold, strtoll, strtoull +#include // snprintf +#include // initializer_list +#include // char_traits, string +#include // vector + +#include +#include +#include + +namespace nlohmann +{ +namespace detail +{ +/////////// +// lexer // +/////////// + +/*! +@brief lexical analysis + +This class organizes the lexical analysis during JSON deserialization. +*/ +template +class lexer +{ + using number_integer_t = typename BasicJsonType::number_integer_t; + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + using number_float_t = typename BasicJsonType::number_float_t; + using string_t = typename BasicJsonType::string_t; + + public: + /// token types for the parser + enum class token_type + { + uninitialized, ///< indicating the scanner is uninitialized + literal_true, ///< the `true` literal + literal_false, ///< the `false` literal + literal_null, ///< the `null` literal + value_string, ///< a string -- use get_string() for actual value + value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value + value_integer, ///< a signed integer -- use get_number_integer() for actual value + value_float, ///< an floating point number -- use get_number_float() for actual value + begin_array, ///< the character for array begin `[` + begin_object, ///< the character for object begin `{` + end_array, ///< the character for array end `]` + end_object, ///< the character for object end `}` + name_separator, ///< the name separator `:` + value_separator, ///< the value separator `,` + parse_error, ///< indicating a parse error + end_of_input, ///< indicating the end of the input buffer + literal_or_value ///< a literal or the begin of a value (only for diagnostics) + }; + + /// return name of values of type token_type (only used for errors) + static const char* token_type_name(const token_type t) noexcept + { + switch (t) + { + case token_type::uninitialized: + return ""; + case token_type::literal_true: + return "true literal"; + case token_type::literal_false: + return "false literal"; + case token_type::literal_null: + return "null literal"; + case token_type::value_string: + return "string literal"; + case lexer::token_type::value_unsigned: + case lexer::token_type::value_integer: + case lexer::token_type::value_float: + return "number literal"; + case token_type::begin_array: + return "'['"; + case token_type::begin_object: + return "'{'"; + case token_type::end_array: + return "']'"; + case token_type::end_object: + return "'}'"; + case token_type::name_separator: + return "':'"; + case token_type::value_separator: + return "','"; + case token_type::parse_error: + return ""; + case token_type::end_of_input: + return "end of input"; + case token_type::literal_or_value: + return "'[', '{', or a literal"; + // LCOV_EXCL_START + default: // catch non-enum values + return "unknown token"; + // LCOV_EXCL_STOP + } + } + + explicit lexer(detail::input_adapter_t&& adapter) + : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {} + + // delete because of pointer members + lexer(const lexer&) = delete; + lexer(lexer&&) = delete; + lexer& operator=(lexer&) = delete; + lexer& operator=(lexer&&) = delete; + ~lexer() = default; + + private: + ///////////////////// + // locales + ///////////////////// + + /// return the locale-dependent decimal point + static char get_decimal_point() noexcept + { + const auto loc = localeconv(); + assert(loc != nullptr); + return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point); + } + + ///////////////////// + // scan functions + ///////////////////// + + /*! + @brief get codepoint from 4 hex characters following `\u` + + For input "\u c1 c2 c3 c4" the codepoint is: + (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4 + = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0) + + Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f' + must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The + conversion is done by subtracting the offset (0x30, 0x37, and 0x57) + between the ASCII value of the character and the desired integer value. + + @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or + non-hex character) + */ + int get_codepoint() + { + // this function only makes sense after reading `\u` + assert(current == 'u'); + int codepoint = 0; + + const auto factors = { 12, 8, 4, 0 }; + for (const auto factor : factors) + { + get(); + + if (current >= '0' and current <= '9') + { + codepoint += ((current - 0x30) << factor); + } + else if (current >= 'A' and current <= 'F') + { + codepoint += ((current - 0x37) << factor); + } + else if (current >= 'a' and current <= 'f') + { + codepoint += ((current - 0x57) << factor); + } + else + { + return -1; + } + } + + assert(0x0000 <= codepoint and codepoint <= 0xFFFF); + return codepoint; + } + + /*! + @brief check if the next byte(s) are inside a given range + + Adds the current byte and, for each passed range, reads a new byte and + checks if it is inside the range. If a violation was detected, set up an + error message and return false. Otherwise, return true. + + @param[in] ranges list of integers; interpreted as list of pairs of + inclusive lower and upper bound, respectively + + @pre The passed list @a ranges must have 2, 4, or 6 elements; that is, + 1, 2, or 3 pairs. This precondition is enforced by an assertion. + + @return true if and only if no range violation was detected + */ + bool next_byte_in_range(std::initializer_list ranges) + { + assert(ranges.size() == 2 or ranges.size() == 4 or ranges.size() == 6); + add(current); + + for (auto range = ranges.begin(); range != ranges.end(); ++range) + { + get(); + if (JSON_LIKELY(*range <= current and current <= *(++range))) + { + add(current); + } + else + { + error_message = "invalid string: ill-formed UTF-8 byte"; + return false; + } + } + + return true; + } + + /*! + @brief scan a string literal + + This function scans a string according to Sect. 7 of RFC 7159. While + scanning, bytes are escaped and copied into buffer token_buffer. Then the + function returns successfully, token_buffer is *not* null-terminated (as it + may contain \0 bytes), and token_buffer.size() is the number of bytes in the + string. + + @return token_type::value_string if string could be successfully scanned, + token_type::parse_error otherwise + + @note In case of errors, variable error_message contains a textual + description. + */ + token_type scan_string() + { + // reset token_buffer (ignore opening quote) + reset(); + + // we entered the function by reading an open quote + assert(current == '\"'); + + while (true) + { + // get next character + switch (get()) + { + // end of file while parsing string + case std::char_traits::eof(): + { + error_message = "invalid string: missing closing quote"; + return token_type::parse_error; + } + + // closing quote + case '\"': + { + return token_type::value_string; + } + + // escapes + case '\\': + { + switch (get()) + { + // quotation mark + case '\"': + add('\"'); + break; + // reverse solidus + case '\\': + add('\\'); + break; + // solidus + case '/': + add('/'); + break; + // backspace + case 'b': + add('\b'); + break; + // form feed + case 'f': + add('\f'); + break; + // line feed + case 'n': + add('\n'); + break; + // carriage return + case 'r': + add('\r'); + break; + // tab + case 't': + add('\t'); + break; + + // unicode escapes + case 'u': + { + const int codepoint1 = get_codepoint(); + int codepoint = codepoint1; // start with codepoint1 + + if (JSON_UNLIKELY(codepoint1 == -1)) + { + error_message = "invalid string: '\\u' must be followed by 4 hex digits"; + return token_type::parse_error; + } + + // check if code point is a high surrogate + if (0xD800 <= codepoint1 and codepoint1 <= 0xDBFF) + { + // expect next \uxxxx entry + if (JSON_LIKELY(get() == '\\' and get() == 'u')) + { + const int codepoint2 = get_codepoint(); + + if (JSON_UNLIKELY(codepoint2 == -1)) + { + error_message = "invalid string: '\\u' must be followed by 4 hex digits"; + return token_type::parse_error; + } + + // check if codepoint2 is a low surrogate + if (JSON_LIKELY(0xDC00 <= codepoint2 and codepoint2 <= 0xDFFF)) + { + // overwrite codepoint + codepoint = + // high surrogate occupies the most significant 22 bits + (codepoint1 << 10) + // low surrogate occupies the least significant 15 bits + + codepoint2 + // there is still the 0xD800, 0xDC00 and 0x10000 noise + // in the result so we have to subtract with: + // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00 + - 0x35FDC00; + } + else + { + error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF"; + return token_type::parse_error; + } + } + else + { + error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF"; + return token_type::parse_error; + } + } + else + { + if (JSON_UNLIKELY(0xDC00 <= codepoint1 and codepoint1 <= 0xDFFF)) + { + error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF"; + return token_type::parse_error; + } + } + + // result of the above calculation yields a proper codepoint + assert(0x00 <= codepoint and codepoint <= 0x10FFFF); + + // translate codepoint into bytes + if (codepoint < 0x80) + { + // 1-byte characters: 0xxxxxxx (ASCII) + add(codepoint); + } + else if (codepoint <= 0x7FF) + { + // 2-byte characters: 110xxxxx 10xxxxxx + add(0xC0 | (codepoint >> 6)); + add(0x80 | (codepoint & 0x3F)); + } + else if (codepoint <= 0xFFFF) + { + // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx + add(0xE0 | (codepoint >> 12)); + add(0x80 | ((codepoint >> 6) & 0x3F)); + add(0x80 | (codepoint & 0x3F)); + } + else + { + // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + add(0xF0 | (codepoint >> 18)); + add(0x80 | ((codepoint >> 12) & 0x3F)); + add(0x80 | ((codepoint >> 6) & 0x3F)); + add(0x80 | (codepoint & 0x3F)); + } + + break; + } + + // other characters after escape + default: + error_message = "invalid string: forbidden character after backslash"; + return token_type::parse_error; + } + + break; + } + + // invalid control characters + case 0x00: + { + error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000"; + return token_type::parse_error; + } + + case 0x01: + { + error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001"; + return token_type::parse_error; + } + + case 0x02: + { + error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002"; + return token_type::parse_error; + } + + case 0x03: + { + error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003"; + return token_type::parse_error; + } + + case 0x04: + { + error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004"; + return token_type::parse_error; + } + + case 0x05: + { + error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005"; + return token_type::parse_error; + } + + case 0x06: + { + error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006"; + return token_type::parse_error; + } + + case 0x07: + { + error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007"; + return token_type::parse_error; + } + + case 0x08: + { + error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b"; + return token_type::parse_error; + } + + case 0x09: + { + error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t"; + return token_type::parse_error; + } + + case 0x0A: + { + error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n"; + return token_type::parse_error; + } + + case 0x0B: + { + error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B"; + return token_type::parse_error; + } + + case 0x0C: + { + error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f"; + return token_type::parse_error; + } + + case 0x0D: + { + error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r"; + return token_type::parse_error; + } + + case 0x0E: + { + error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E"; + return token_type::parse_error; + } + + case 0x0F: + { + error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F"; + return token_type::parse_error; + } + + case 0x10: + { + error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010"; + return token_type::parse_error; + } + + case 0x11: + { + error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011"; + return token_type::parse_error; + } + + case 0x12: + { + error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012"; + return token_type::parse_error; + } + + case 0x13: + { + error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013"; + return token_type::parse_error; + } + + case 0x14: + { + error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014"; + return token_type::parse_error; + } + + case 0x15: + { + error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015"; + return token_type::parse_error; + } + + case 0x16: + { + error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016"; + return token_type::parse_error; + } + + case 0x17: + { + error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017"; + return token_type::parse_error; + } + + case 0x18: + { + error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018"; + return token_type::parse_error; + } + + case 0x19: + { + error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019"; + return token_type::parse_error; + } + + case 0x1A: + { + error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A"; + return token_type::parse_error; + } + + case 0x1B: + { + error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B"; + return token_type::parse_error; + } + + case 0x1C: + { + error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C"; + return token_type::parse_error; + } + + case 0x1D: + { + error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D"; + return token_type::parse_error; + } + + case 0x1E: + { + error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E"; + return token_type::parse_error; + } + + case 0x1F: + { + error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F"; + return token_type::parse_error; + } + + // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace)) + case 0x20: + case 0x21: + case 0x23: + case 0x24: + case 0x25: + case 0x26: + case 0x27: + case 0x28: + case 0x29: + case 0x2A: + case 0x2B: + case 0x2C: + case 0x2D: + case 0x2E: + case 0x2F: + case 0x30: + case 0x31: + case 0x32: + case 0x33: + case 0x34: + case 0x35: + case 0x36: + case 0x37: + case 0x38: + case 0x39: + case 0x3A: + case 0x3B: + case 0x3C: + case 0x3D: + case 0x3E: + case 0x3F: + case 0x40: + case 0x41: + case 0x42: + case 0x43: + case 0x44: + case 0x45: + case 0x46: + case 0x47: + case 0x48: + case 0x49: + case 0x4A: + case 0x4B: + case 0x4C: + case 0x4D: + case 0x4E: + case 0x4F: + case 0x50: + case 0x51: + case 0x52: + case 0x53: + case 0x54: + case 0x55: + case 0x56: + case 0x57: + case 0x58: + case 0x59: + case 0x5A: + case 0x5B: + case 0x5D: + case 0x5E: + case 0x5F: + case 0x60: + case 0x61: + case 0x62: + case 0x63: + case 0x64: + case 0x65: + case 0x66: + case 0x67: + case 0x68: + case 0x69: + case 0x6A: + case 0x6B: + case 0x6C: + case 0x6D: + case 0x6E: + case 0x6F: + case 0x70: + case 0x71: + case 0x72: + case 0x73: + case 0x74: + case 0x75: + case 0x76: + case 0x77: + case 0x78: + case 0x79: + case 0x7A: + case 0x7B: + case 0x7C: + case 0x7D: + case 0x7E: + case 0x7F: + { + add(current); + break; + } + + // U+0080..U+07FF: bytes C2..DF 80..BF + case 0xC2: + case 0xC3: + case 0xC4: + case 0xC5: + case 0xC6: + case 0xC7: + case 0xC8: + case 0xC9: + case 0xCA: + case 0xCB: + case 0xCC: + case 0xCD: + case 0xCE: + case 0xCF: + case 0xD0: + case 0xD1: + case 0xD2: + case 0xD3: + case 0xD4: + case 0xD5: + case 0xD6: + case 0xD7: + case 0xD8: + case 0xD9: + case 0xDA: + case 0xDB: + case 0xDC: + case 0xDD: + case 0xDE: + case 0xDF: + { + if (JSON_UNLIKELY(not next_byte_in_range({0x80, 0xBF}))) + { + return token_type::parse_error; + } + break; + } + + // U+0800..U+0FFF: bytes E0 A0..BF 80..BF + case 0xE0: + { + if (JSON_UNLIKELY(not (next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF})))) + { + return token_type::parse_error; + } + break; + } + + // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF + // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF + case 0xE1: + case 0xE2: + case 0xE3: + case 0xE4: + case 0xE5: + case 0xE6: + case 0xE7: + case 0xE8: + case 0xE9: + case 0xEA: + case 0xEB: + case 0xEC: + case 0xEE: + case 0xEF: + { + if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0xBF, 0x80, 0xBF})))) + { + return token_type::parse_error; + } + break; + } + + // U+D000..U+D7FF: bytes ED 80..9F 80..BF + case 0xED: + { + if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0x9F, 0x80, 0xBF})))) + { + return token_type::parse_error; + } + break; + } + + // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF + case 0xF0: + { + if (JSON_UNLIKELY(not (next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF})))) + { + return token_type::parse_error; + } + break; + } + + // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF + case 0xF1: + case 0xF2: + case 0xF3: + { + if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF})))) + { + return token_type::parse_error; + } + break; + } + + // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF + case 0xF4: + { + if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF})))) + { + return token_type::parse_error; + } + break; + } + + // remaining bytes (80..C1 and F5..FF) are ill-formed + default: + { + error_message = "invalid string: ill-formed UTF-8 byte"; + return token_type::parse_error; + } + } + } + } + + static void strtof(float& f, const char* str, char** endptr) noexcept + { + f = std::strtof(str, endptr); + } + + static void strtof(double& f, const char* str, char** endptr) noexcept + { + f = std::strtod(str, endptr); + } + + static void strtof(long double& f, const char* str, char** endptr) noexcept + { + f = std::strtold(str, endptr); + } + + /*! + @brief scan a number literal + + This function scans a string according to Sect. 6 of RFC 7159. + + The function is realized with a deterministic finite state machine derived + from the grammar described in RFC 7159. Starting in state "init", the + input is read and used to determined the next state. Only state "done" + accepts the number. State "error" is a trap state to model errors. In the + table below, "anything" means any character but the ones listed before. + + state | 0 | 1-9 | e E | + | - | . | anything + ---------|----------|----------|----------|---------|---------|----------|----------- + init | zero | any1 | [error] | [error] | minus | [error] | [error] + minus | zero | any1 | [error] | [error] | [error] | [error] | [error] + zero | done | done | exponent | done | done | decimal1 | done + any1 | any1 | any1 | exponent | done | done | decimal1 | done + decimal1 | decimal2 | [error] | [error] | [error] | [error] | [error] | [error] + decimal2 | decimal2 | decimal2 | exponent | done | done | done | done + exponent | any2 | any2 | [error] | sign | sign | [error] | [error] + sign | any2 | any2 | [error] | [error] | [error] | [error] | [error] + any2 | any2 | any2 | done | done | done | done | done + + The state machine is realized with one label per state (prefixed with + "scan_number_") and `goto` statements between them. The state machine + contains cycles, but any cycle can be left when EOF is read. Therefore, + the function is guaranteed to terminate. + + During scanning, the read bytes are stored in token_buffer. This string is + then converted to a signed integer, an unsigned integer, or a + floating-point number. + + @return token_type::value_unsigned, token_type::value_integer, or + token_type::value_float if number could be successfully scanned, + token_type::parse_error otherwise + + @note The scanner is independent of the current locale. Internally, the + locale's decimal point is used instead of `.` to work with the + locale-dependent converters. + */ + token_type scan_number() // lgtm [cpp/use-of-goto] + { + // reset token_buffer to store the number's bytes + reset(); + + // the type of the parsed number; initially set to unsigned; will be + // changed if minus sign, decimal point or exponent is read + token_type number_type = token_type::value_unsigned; + + // state (init): we just found out we need to scan a number + switch (current) + { + case '-': + { + add(current); + goto scan_number_minus; + } + + case '0': + { + add(current); + goto scan_number_zero; + } + + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + add(current); + goto scan_number_any1; + } + + // LCOV_EXCL_START + default: + { + // all other characters are rejected outside scan_number() + assert(false); + } + // LCOV_EXCL_STOP + } + +scan_number_minus: + // state: we just parsed a leading minus sign + number_type = token_type::value_integer; + switch (get()) + { + case '0': + { + add(current); + goto scan_number_zero; + } + + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + add(current); + goto scan_number_any1; + } + + default: + { + error_message = "invalid number; expected digit after '-'"; + return token_type::parse_error; + } + } + +scan_number_zero: + // state: we just parse a zero (maybe with a leading minus sign) + switch (get()) + { + case '.': + { + add(decimal_point_char); + goto scan_number_decimal1; + } + + case 'e': + case 'E': + { + add(current); + goto scan_number_exponent; + } + + default: + goto scan_number_done; + } + +scan_number_any1: + // state: we just parsed a number 0-9 (maybe with a leading minus sign) + switch (get()) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + add(current); + goto scan_number_any1; + } + + case '.': + { + add(decimal_point_char); + goto scan_number_decimal1; + } + + case 'e': + case 'E': + { + add(current); + goto scan_number_exponent; + } + + default: + goto scan_number_done; + } + +scan_number_decimal1: + // state: we just parsed a decimal point + number_type = token_type::value_float; + switch (get()) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + add(current); + goto scan_number_decimal2; + } + + default: + { + error_message = "invalid number; expected digit after '.'"; + return token_type::parse_error; + } + } + +scan_number_decimal2: + // we just parsed at least one number after a decimal point + switch (get()) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + add(current); + goto scan_number_decimal2; + } + + case 'e': + case 'E': + { + add(current); + goto scan_number_exponent; + } + + default: + goto scan_number_done; + } + +scan_number_exponent: + // we just parsed an exponent + number_type = token_type::value_float; + switch (get()) + { + case '+': + case '-': + { + add(current); + goto scan_number_sign; + } + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + add(current); + goto scan_number_any2; + } + + default: + { + error_message = + "invalid number; expected '+', '-', or digit after exponent"; + return token_type::parse_error; + } + } + +scan_number_sign: + // we just parsed an exponent sign + switch (get()) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + add(current); + goto scan_number_any2; + } + + default: + { + error_message = "invalid number; expected digit after exponent sign"; + return token_type::parse_error; + } + } + +scan_number_any2: + // we just parsed a number after the exponent or exponent sign + switch (get()) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + add(current); + goto scan_number_any2; + } + + default: + goto scan_number_done; + } + +scan_number_done: + // unget the character after the number (we only read it to know that + // we are done scanning a number) + unget(); + + char* endptr = nullptr; + errno = 0; + + // try to parse integers first and fall back to floats + if (number_type == token_type::value_unsigned) + { + const auto x = std::strtoull(token_buffer.data(), &endptr, 10); + + // we checked the number format before + assert(endptr == token_buffer.data() + token_buffer.size()); + + if (errno == 0) + { + value_unsigned = static_cast(x); + if (value_unsigned == x) + { + return token_type::value_unsigned; + } + } + } + else if (number_type == token_type::value_integer) + { + const auto x = std::strtoll(token_buffer.data(), &endptr, 10); + + // we checked the number format before + assert(endptr == token_buffer.data() + token_buffer.size()); + + if (errno == 0) + { + value_integer = static_cast(x); + if (value_integer == x) + { + return token_type::value_integer; + } + } + } + + // this code is reached if we parse a floating-point number or if an + // integer conversion above failed + strtof(value_float, token_buffer.data(), &endptr); + + // we checked the number format before + assert(endptr == token_buffer.data() + token_buffer.size()); + + return token_type::value_float; + } + + /*! + @param[in] literal_text the literal text to expect + @param[in] length the length of the passed literal text + @param[in] return_type the token type to return on success + */ + token_type scan_literal(const char* literal_text, const std::size_t length, + token_type return_type) + { + assert(current == literal_text[0]); + for (std::size_t i = 1; i < length; ++i) + { + if (JSON_UNLIKELY(get() != literal_text[i])) + { + error_message = "invalid literal"; + return token_type::parse_error; + } + } + return return_type; + } + + ///////////////////// + // input management + ///////////////////// + + /// reset token_buffer; current character is beginning of token + void reset() noexcept + { + token_buffer.clear(); + token_string.clear(); + token_string.push_back(std::char_traits::to_char_type(current)); + } + + /* + @brief get next character from the input + + This function provides the interface to the used input adapter. It does + not throw in case the input reached EOF, but returns a + `std::char_traits::eof()` in that case. Stores the scanned characters + for use in error messages. + + @return character read from the input + */ + std::char_traits::int_type get() + { + ++position.chars_read_total; + ++position.chars_read_current_line; + + if (next_unget) + { + // just reset the next_unget variable and work with current + next_unget = false; + } + else + { + current = ia->get_character(); + } + + if (JSON_LIKELY(current != std::char_traits::eof())) + { + token_string.push_back(std::char_traits::to_char_type(current)); + } + + if (current == '\n') + { + ++position.lines_read; + ++position.chars_read_current_line = 0; + } + + return current; + } + + /*! + @brief unget current character (read it again on next get) + + We implement unget by setting variable next_unget to true. The input is not + changed - we just simulate ungetting by modifying chars_read_total, + chars_read_current_line, and token_string. The next call to get() will + behave as if the unget character is read again. + */ + void unget() + { + next_unget = true; + + --position.chars_read_total; + + // in case we "unget" a newline, we have to also decrement the lines_read + if (position.chars_read_current_line == 0) + { + if (position.lines_read > 0) + { + --position.lines_read; + } + } + else + { + --position.chars_read_current_line; + } + + if (JSON_LIKELY(current != std::char_traits::eof())) + { + assert(token_string.size() != 0); + token_string.pop_back(); + } + } + + /// add a character to token_buffer + void add(int c) + { + token_buffer.push_back(std::char_traits::to_char_type(c)); + } + + public: + ///////////////////// + // value getters + ///////////////////// + + /// return integer value + constexpr number_integer_t get_number_integer() const noexcept + { + return value_integer; + } + + /// return unsigned integer value + constexpr number_unsigned_t get_number_unsigned() const noexcept + { + return value_unsigned; + } + + /// return floating-point value + constexpr number_float_t get_number_float() const noexcept + { + return value_float; + } + + /// return current string value (implicitly resets the token; useful only once) + string_t& get_string() + { + return token_buffer; + } + + ///////////////////// + // diagnostics + ///////////////////// + + /// return position of last read token + constexpr position_t get_position() const noexcept + { + return position; + } + + /// return the last read token (for errors only). Will never contain EOF + /// (an arbitrary value that is not a valid char value, often -1), because + /// 255 may legitimately occur. May contain NUL, which should be escaped. + std::string get_token_string() const + { + // escape control characters + std::string result; + for (const auto c : token_string) + { + if ('\x00' <= c and c <= '\x1F') + { + // escape control characters + char cs[9]; + (std::snprintf)(cs, 9, "", static_cast(c)); + result += cs; + } + else + { + // add character as is + result.push_back(c); + } + } + + return result; + } + + /// return syntax error message + constexpr const char* get_error_message() const noexcept + { + return error_message; + } + + ///////////////////// + // actual scanner + ///////////////////// + + /*! + @brief skip the UTF-8 byte order mark + @return true iff there is no BOM or the correct BOM has been skipped + */ + bool skip_bom() + { + if (get() == 0xEF) + { + // check if we completely parse the BOM + return get() == 0xBB and get() == 0xBF; + } + + // the first character is not the beginning of the BOM; unget it to + // process is later + unget(); + return true; + } + + token_type scan() + { + // initially, skip the BOM + if (position.chars_read_total == 0 and not skip_bom()) + { + error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given"; + return token_type::parse_error; + } + + // read next character and ignore whitespace + do + { + get(); + } + while (current == ' ' or current == '\t' or current == '\n' or current == '\r'); + + switch (current) + { + // structural characters + case '[': + return token_type::begin_array; + case ']': + return token_type::end_array; + case '{': + return token_type::begin_object; + case '}': + return token_type::end_object; + case ':': + return token_type::name_separator; + case ',': + return token_type::value_separator; + + // literals + case 't': + return scan_literal("true", 4, token_type::literal_true); + case 'f': + return scan_literal("false", 5, token_type::literal_false); + case 'n': + return scan_literal("null", 4, token_type::literal_null); + + // string + case '\"': + return scan_string(); + + // number + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return scan_number(); + + // end of input (the null byte is needed when parsing from + // string literals) + case '\0': + case std::char_traits::eof(): + return token_type::end_of_input; + + // error + default: + error_message = "invalid literal"; + return token_type::parse_error; + } + } + + private: + /// input adapter + detail::input_adapter_t ia = nullptr; + + /// the current character + std::char_traits::int_type current = std::char_traits::eof(); + + /// whether the next get() call should just return current + bool next_unget = false; + + /// the start position of the current token + position_t position; + + /// raw input token string (for error messages) + std::vector token_string {}; + + /// buffer for variable-length tokens (numbers, strings) + string_t token_buffer {}; + + /// a description of occurred lexer errors + const char* error_message = ""; + + // number values + number_integer_t value_integer = 0; + number_unsigned_t value_unsigned = 0; + number_float_t value_float = 0; + + /// the decimal point + const char decimal_point_char = '.'; +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/input/parser.hpp b/include/lib/modernjson/detail/input/parser.hpp new file mode 100644 index 0000000..d205bbb --- /dev/null +++ b/include/lib/modernjson/detail/input/parser.hpp @@ -0,0 +1,504 @@ +#pragma once + +#include // assert +#include // isfinite +#include // uint8_t +#include // function +#include // string +#include // move + +#include +#include +#include +#include +#include +#include +#include + +namespace nlohmann +{ +namespace detail +{ +//////////// +// parser // +//////////// + +/*! +@brief syntax analysis + +This class implements a recursive decent parser. +*/ +template +class parser +{ + using number_integer_t = typename BasicJsonType::number_integer_t; + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + using number_float_t = typename BasicJsonType::number_float_t; + using string_t = typename BasicJsonType::string_t; + using lexer_t = lexer; + using token_type = typename lexer_t::token_type; + + public: + enum class parse_event_t : uint8_t + { + /// the parser read `{` and started to process a JSON object + object_start, + /// the parser read `}` and finished processing a JSON object + object_end, + /// the parser read `[` and started to process a JSON array + array_start, + /// the parser read `]` and finished processing a JSON array + array_end, + /// the parser read a key of a value in an object + key, + /// the parser finished reading a JSON value + value + }; + + using parser_callback_t = + std::function; + + /// a parser reading from an input adapter + explicit parser(detail::input_adapter_t&& adapter, + const parser_callback_t cb = nullptr, + const bool allow_exceptions_ = true) + : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_) + { + // read first token + get_token(); + } + + /*! + @brief public parser interface + + @param[in] strict whether to expect the last token to be EOF + @param[in,out] result parsed JSON value + + @throw parse_error.101 in case of an unexpected token + @throw parse_error.102 if to_unicode fails or surrogate error + @throw parse_error.103 if to_unicode fails + */ + void parse(const bool strict, BasicJsonType& result) + { + if (callback) + { + json_sax_dom_callback_parser sdp(result, callback, allow_exceptions); + sax_parse_internal(&sdp); + result.assert_invariant(); + + // in strict mode, input must be completely read + if (strict and (get_token() != token_type::end_of_input)) + { + sdp.parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::end_of_input, "value"))); + } + + // in case of an error, return discarded value + if (sdp.is_errored()) + { + result = value_t::discarded; + return; + } + + // set top-level value to null if it was discarded by the callback + // function + if (result.is_discarded()) + { + result = nullptr; + } + } + else + { + json_sax_dom_parser sdp(result, allow_exceptions); + sax_parse_internal(&sdp); + result.assert_invariant(); + + // in strict mode, input must be completely read + if (strict and (get_token() != token_type::end_of_input)) + { + sdp.parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::end_of_input, "value"))); + } + + // in case of an error, return discarded value + if (sdp.is_errored()) + { + result = value_t::discarded; + return; + } + } + } + + /*! + @brief public accept interface + + @param[in] strict whether to expect the last token to be EOF + @return whether the input is a proper JSON text + */ + bool accept(const bool strict = true) + { + json_sax_acceptor sax_acceptor; + return sax_parse(&sax_acceptor, strict); + } + + template + bool sax_parse(SAX* sax, const bool strict = true) + { + (void)detail::is_sax_static_asserts {}; + const bool result = sax_parse_internal(sax); + + // strict mode: next byte must be EOF + if (result and strict and (get_token() != token_type::end_of_input)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::end_of_input, "value"))); + } + + return result; + } + + private: + template + bool sax_parse_internal(SAX* sax) + { + // stack to remember the hierarchy of structured values we are parsing + // true = array; false = object + std::vector states; + // value to avoid a goto (see comment where set to true) + bool skip_to_state_evaluation = false; + + while (true) + { + if (not skip_to_state_evaluation) + { + // invariant: get_token() was called before each iteration + switch (last_token) + { + case token_type::begin_object: + { + if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1)))) + { + return false; + } + + // closing } -> we are done + if (get_token() == token_type::end_object) + { + if (JSON_UNLIKELY(not sax->end_object())) + { + return false; + } + break; + } + + // parse key + if (JSON_UNLIKELY(last_token != token_type::value_string)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::value_string, "object key"))); + } + if (JSON_UNLIKELY(not sax->key(m_lexer.get_string()))) + { + return false; + } + + // parse separator (:) + if (JSON_UNLIKELY(get_token() != token_type::name_separator)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::name_separator, "object separator"))); + } + + // remember we are now inside an object + states.push_back(false); + + // parse values + get_token(); + continue; + } + + case token_type::begin_array: + { + if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1)))) + { + return false; + } + + // closing ] -> we are done + if (get_token() == token_type::end_array) + { + if (JSON_UNLIKELY(not sax->end_array())) + { + return false; + } + break; + } + + // remember we are now inside an array + states.push_back(true); + + // parse values (no need to call get_token) + continue; + } + + case token_type::value_float: + { + const auto res = m_lexer.get_number_float(); + + if (JSON_UNLIKELY(not std::isfinite(res))) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'")); + } + else + { + if (JSON_UNLIKELY(not sax->number_float(res, m_lexer.get_string()))) + { + return false; + } + break; + } + } + + case token_type::literal_false: + { + if (JSON_UNLIKELY(not sax->boolean(false))) + { + return false; + } + break; + } + + case token_type::literal_null: + { + if (JSON_UNLIKELY(not sax->null())) + { + return false; + } + break; + } + + case token_type::literal_true: + { + if (JSON_UNLIKELY(not sax->boolean(true))) + { + return false; + } + break; + } + + case token_type::value_integer: + { + if (JSON_UNLIKELY(not sax->number_integer(m_lexer.get_number_integer()))) + { + return false; + } + break; + } + + case token_type::value_string: + { + if (JSON_UNLIKELY(not sax->string(m_lexer.get_string()))) + { + return false; + } + break; + } + + case token_type::value_unsigned: + { + if (JSON_UNLIKELY(not sax->number_unsigned(m_lexer.get_number_unsigned()))) + { + return false; + } + break; + } + + case token_type::parse_error: + { + // using "uninitialized" to avoid "expected" message + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::uninitialized, "value"))); + } + + default: // the last token was unexpected + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::literal_or_value, "value"))); + } + } + } + else + { + skip_to_state_evaluation = false; + } + + // we reached this line after we successfully parsed a value + if (states.empty()) + { + // empty stack: we reached the end of the hierarchy: done + return true; + } + else + { + if (states.back()) // array + { + // comma -> next value + if (get_token() == token_type::value_separator) + { + // parse a new value + get_token(); + continue; + } + + // closing ] + if (JSON_LIKELY(last_token == token_type::end_array)) + { + if (JSON_UNLIKELY(not sax->end_array())) + { + return false; + } + + // We are done with this array. Before we can parse a + // new value, we need to evaluate the new state first. + // By setting skip_to_state_evaluation to false, we + // are effectively jumping to the beginning of this if. + assert(not states.empty()); + states.pop_back(); + skip_to_state_evaluation = true; + continue; + } + else + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::end_array, "array"))); + } + } + else // object + { + // comma -> next value + if (get_token() == token_type::value_separator) + { + // parse key + if (JSON_UNLIKELY(get_token() != token_type::value_string)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::value_string, "object key"))); + } + else + { + if (JSON_UNLIKELY(not sax->key(m_lexer.get_string()))) + { + return false; + } + } + + // parse separator (:) + if (JSON_UNLIKELY(get_token() != token_type::name_separator)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::name_separator, "object separator"))); + } + + // parse values + get_token(); + continue; + } + + // closing } + if (JSON_LIKELY(last_token == token_type::end_object)) + { + if (JSON_UNLIKELY(not sax->end_object())) + { + return false; + } + + // We are done with this object. Before we can parse a + // new value, we need to evaluate the new state first. + // By setting skip_to_state_evaluation to false, we + // are effectively jumping to the beginning of this if. + assert(not states.empty()); + states.pop_back(); + skip_to_state_evaluation = true; + continue; + } + else + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::end_object, "object"))); + } + } + } + } + } + + /// get next token from lexer + token_type get_token() + { + return (last_token = m_lexer.scan()); + } + + std::string exception_message(const token_type expected, const std::string& context) + { + std::string error_msg = "syntax error "; + + if (not context.empty()) + { + error_msg += "while parsing " + context + " "; + } + + error_msg += "- "; + + if (last_token == token_type::parse_error) + { + error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" + + m_lexer.get_token_string() + "'"; + } + else + { + error_msg += "unexpected " + std::string(lexer_t::token_type_name(last_token)); + } + + if (expected != token_type::uninitialized) + { + error_msg += "; expected " + std::string(lexer_t::token_type_name(expected)); + } + + return error_msg; + } + + private: + /// callback function + const parser_callback_t callback = nullptr; + /// the type of the last read token + token_type last_token = token_type::uninitialized; + /// the lexer + lexer_t m_lexer; + /// whether to throw exceptions in case of errors + const bool allow_exceptions = true; +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/input/position_t.hpp b/include/lib/modernjson/detail/input/position_t.hpp new file mode 100644 index 0000000..37f4ab1 --- /dev/null +++ b/include/lib/modernjson/detail/input/position_t.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include // size_t + +namespace nlohmann +{ +namespace detail +{ +/// struct to capture the start position of the current token +struct position_t +{ + /// the total number of characters read + std::size_t chars_read_total = 0; + /// the number of characters read in the current line + std::size_t chars_read_current_line = 0; + /// the number of lines read + std::size_t lines_read = 0; + + /// conversion to size_t to preserve SAX interface + constexpr operator size_t() const + { + return chars_read_total; + } +}; + +} +} diff --git a/include/lib/modernjson/detail/iterators/internal_iterator.hpp b/include/lib/modernjson/detail/iterators/internal_iterator.hpp new file mode 100644 index 0000000..e1c12a8 --- /dev/null +++ b/include/lib/modernjson/detail/iterators/internal_iterator.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +namespace nlohmann +{ +namespace detail +{ +/*! +@brief an iterator value + +@note This structure could easily be a union, but MSVC currently does not allow +unions members with complex constructors, see https://github.com/nlohmann/json/pull/105. +*/ +template struct internal_iterator +{ + /// iterator for JSON objects + typename BasicJsonType::object_t::iterator object_iterator {}; + /// iterator for JSON arrays + typename BasicJsonType::array_t::iterator array_iterator {}; + /// generic iterator for all other types + primitive_iterator_t primitive_iterator {}; +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/iterators/iter_impl.hpp b/include/lib/modernjson/detail/iterators/iter_impl.hpp new file mode 100644 index 0000000..4ad94db --- /dev/null +++ b/include/lib/modernjson/detail/iterators/iter_impl.hpp @@ -0,0 +1,614 @@ +#pragma once + +#include // not +#include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next +#include // conditional, is_const, remove_const + +#include +#include +#include +#include +#include +#include + +namespace nlohmann +{ +namespace detail +{ +// forward declare, to be able to friend it later on +template class iteration_proxy; + +/*! +@brief a template for a bidirectional iterator for the @ref basic_json class + +This class implements a both iterators (iterator and const_iterator) for the +@ref basic_json class. + +@note An iterator is called *initialized* when a pointer to a JSON value has + been set (e.g., by a constructor or a copy assignment). If the iterator is + default-constructed, it is *uninitialized* and most methods are undefined. + **The library uses assertions to detect calls on uninitialized iterators.** + +@requirement The class satisfies the following concept requirements: +- +[BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator): + The iterator that can be moved can be moved in both directions (i.e. + incremented and decremented). + +@since version 1.0.0, simplified in version 2.0.9, change to bidirectional + iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593) +*/ +template +class iter_impl +{ + /// allow basic_json to access private members + friend iter_impl::value, typename std::remove_const::type, const BasicJsonType>::type>; + friend BasicJsonType; + friend iteration_proxy; + + using object_t = typename BasicJsonType::object_t; + using array_t = typename BasicJsonType::array_t; + // make sure BasicJsonType is basic_json or const basic_json + static_assert(is_basic_json::type>::value, + "iter_impl only accepts (const) basic_json"); + + public: + + /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17. + /// The C++ Standard has never required user-defined iterators to derive from std::iterator. + /// A user-defined iterator should provide publicly accessible typedefs named + /// iterator_category, value_type, difference_type, pointer, and reference. + /// Note that value_type is required to be non-const, even for constant iterators. + using iterator_category = std::bidirectional_iterator_tag; + + /// the type of the values when the iterator is dereferenced + using value_type = typename BasicJsonType::value_type; + /// a type to represent differences between iterators + using difference_type = typename BasicJsonType::difference_type; + /// defines a pointer to the type iterated over (value_type) + using pointer = typename std::conditional::value, + typename BasicJsonType::const_pointer, + typename BasicJsonType::pointer>::type; + /// defines a reference to the type iterated over (value_type) + using reference = + typename std::conditional::value, + typename BasicJsonType::const_reference, + typename BasicJsonType::reference>::type; + + /// default constructor + iter_impl() = default; + + /*! + @brief constructor for a given JSON instance + @param[in] object pointer to a JSON object for this iterator + @pre object != nullptr + @post The iterator is initialized; i.e. `m_object != nullptr`. + */ + explicit iter_impl(pointer object) noexcept : m_object(object) + { + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + { + m_it.object_iterator = typename object_t::iterator(); + break; + } + + case value_t::array: + { + m_it.array_iterator = typename array_t::iterator(); + break; + } + + default: + { + m_it.primitive_iterator = primitive_iterator_t(); + break; + } + } + } + + /*! + @note The conventional copy constructor and copy assignment are implicitly + defined. Combined with the following converting constructor and + assignment, they support: (1) copy from iterator to iterator, (2) + copy from const iterator to const iterator, and (3) conversion from + iterator to const iterator. However conversion from const iterator + to iterator is not defined. + */ + + /*! + @brief converting constructor + @param[in] other non-const iterator to copy from + @note It is not checked whether @a other is initialized. + */ + iter_impl(const iter_impl::type>& other) noexcept + : m_object(other.m_object), m_it(other.m_it) {} + + /*! + @brief converting assignment + @param[in,out] other non-const iterator to copy from + @return const/non-const iterator + @note It is not checked whether @a other is initialized. + */ + iter_impl& operator=(const iter_impl::type>& other) noexcept + { + m_object = other.m_object; + m_it = other.m_it; + return *this; + } + + private: + /*! + @brief set the iterator to the first value + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + void set_begin() noexcept + { + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + { + m_it.object_iterator = m_object->m_value.object->begin(); + break; + } + + case value_t::array: + { + m_it.array_iterator = m_object->m_value.array->begin(); + break; + } + + case value_t::null: + { + // set to end so begin()==end() is true: null is empty + m_it.primitive_iterator.set_end(); + break; + } + + default: + { + m_it.primitive_iterator.set_begin(); + break; + } + } + } + + /*! + @brief set the iterator past the last value + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + void set_end() noexcept + { + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + { + m_it.object_iterator = m_object->m_value.object->end(); + break; + } + + case value_t::array: + { + m_it.array_iterator = m_object->m_value.array->end(); + break; + } + + default: + { + m_it.primitive_iterator.set_end(); + break; + } + } + } + + public: + /*! + @brief return a reference to the value pointed to by the iterator + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + reference operator*() const + { + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + { + assert(m_it.object_iterator != m_object->m_value.object->end()); + return m_it.object_iterator->second; + } + + case value_t::array: + { + assert(m_it.array_iterator != m_object->m_value.array->end()); + return *m_it.array_iterator; + } + + case value_t::null: + JSON_THROW(invalid_iterator::create(214, "cannot get value")); + + default: + { + if (JSON_LIKELY(m_it.primitive_iterator.is_begin())) + { + return *m_object; + } + + JSON_THROW(invalid_iterator::create(214, "cannot get value")); + } + } + } + + /*! + @brief dereference the iterator + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + pointer operator->() const + { + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + { + assert(m_it.object_iterator != m_object->m_value.object->end()); + return &(m_it.object_iterator->second); + } + + case value_t::array: + { + assert(m_it.array_iterator != m_object->m_value.array->end()); + return &*m_it.array_iterator; + } + + default: + { + if (JSON_LIKELY(m_it.primitive_iterator.is_begin())) + { + return m_object; + } + + JSON_THROW(invalid_iterator::create(214, "cannot get value")); + } + } + } + + /*! + @brief post-increment (it++) + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + iter_impl const operator++(int) + { + auto result = *this; + ++(*this); + return result; + } + + /*! + @brief pre-increment (++it) + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + iter_impl& operator++() + { + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + { + std::advance(m_it.object_iterator, 1); + break; + } + + case value_t::array: + { + std::advance(m_it.array_iterator, 1); + break; + } + + default: + { + ++m_it.primitive_iterator; + break; + } + } + + return *this; + } + + /*! + @brief post-decrement (it--) + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + iter_impl const operator--(int) + { + auto result = *this; + --(*this); + return result; + } + + /*! + @brief pre-decrement (--it) + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + iter_impl& operator--() + { + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + { + std::advance(m_it.object_iterator, -1); + break; + } + + case value_t::array: + { + std::advance(m_it.array_iterator, -1); + break; + } + + default: + { + --m_it.primitive_iterator; + break; + } + } + + return *this; + } + + /*! + @brief comparison: equal + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + bool operator==(const iter_impl& other) const + { + // if objects are not the same, the comparison is undefined + if (JSON_UNLIKELY(m_object != other.m_object)) + { + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers")); + } + + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + return (m_it.object_iterator == other.m_it.object_iterator); + + case value_t::array: + return (m_it.array_iterator == other.m_it.array_iterator); + + default: + return (m_it.primitive_iterator == other.m_it.primitive_iterator); + } + } + + /*! + @brief comparison: not equal + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + bool operator!=(const iter_impl& other) const + { + return not operator==(other); + } + + /*! + @brief comparison: smaller + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + bool operator<(const iter_impl& other) const + { + // if objects are not the same, the comparison is undefined + if (JSON_UNLIKELY(m_object != other.m_object)) + { + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers")); + } + + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators")); + + case value_t::array: + return (m_it.array_iterator < other.m_it.array_iterator); + + default: + return (m_it.primitive_iterator < other.m_it.primitive_iterator); + } + } + + /*! + @brief comparison: less than or equal + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + bool operator<=(const iter_impl& other) const + { + return not other.operator < (*this); + } + + /*! + @brief comparison: greater than + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + bool operator>(const iter_impl& other) const + { + return not operator<=(other); + } + + /*! + @brief comparison: greater than or equal + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + bool operator>=(const iter_impl& other) const + { + return not operator<(other); + } + + /*! + @brief add to iterator + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + iter_impl& operator+=(difference_type i) + { + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); + + case value_t::array: + { + std::advance(m_it.array_iterator, i); + break; + } + + default: + { + m_it.primitive_iterator += i; + break; + } + } + + return *this; + } + + /*! + @brief subtract from iterator + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + iter_impl& operator-=(difference_type i) + { + return operator+=(-i); + } + + /*! + @brief add to iterator + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + iter_impl operator+(difference_type i) const + { + auto result = *this; + result += i; + return result; + } + + /*! + @brief addition of distance and iterator + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + friend iter_impl operator+(difference_type i, const iter_impl& it) + { + auto result = it; + result += i; + return result; + } + + /*! + @brief subtract from iterator + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + iter_impl operator-(difference_type i) const + { + auto result = *this; + result -= i; + return result; + } + + /*! + @brief return difference + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + difference_type operator-(const iter_impl& other) const + { + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); + + case value_t::array: + return m_it.array_iterator - other.m_it.array_iterator; + + default: + return m_it.primitive_iterator - other.m_it.primitive_iterator; + } + } + + /*! + @brief access to successor + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + reference operator[](difference_type n) const + { + assert(m_object != nullptr); + + switch (m_object->m_type) + { + case value_t::object: + JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators")); + + case value_t::array: + return *std::next(m_it.array_iterator, n); + + case value_t::null: + JSON_THROW(invalid_iterator::create(214, "cannot get value")); + + default: + { + if (JSON_LIKELY(m_it.primitive_iterator.get_value() == -n)) + { + return *m_object; + } + + JSON_THROW(invalid_iterator::create(214, "cannot get value")); + } + } + } + + /*! + @brief return the key of an object iterator + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + const typename object_t::key_type& key() const + { + assert(m_object != nullptr); + + if (JSON_LIKELY(m_object->is_object())) + { + return m_it.object_iterator->first; + } + + JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators")); + } + + /*! + @brief return the value of an iterator + @pre The iterator is initialized; i.e. `m_object != nullptr`. + */ + reference value() const + { + return operator*(); + } + + private: + /// associated JSON instance + pointer m_object = nullptr; + /// the actual iterator of the associated instance + internal_iterator::type> m_it; +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/iterators/iteration_proxy.hpp b/include/lib/modernjson/detail/iterators/iteration_proxy.hpp new file mode 100644 index 0000000..31cc9f0 --- /dev/null +++ b/include/lib/modernjson/detail/iterators/iteration_proxy.hpp @@ -0,0 +1,125 @@ +#pragma once + +#include // size_t +#include // string, to_string +#include // input_iterator_tag + +#include + +namespace nlohmann +{ +namespace detail +{ +/// proxy class for the items() function +template class iteration_proxy +{ + private: + /// helper class for iteration + class iteration_proxy_internal + { + public: + using difference_type = std::ptrdiff_t; + using value_type = iteration_proxy_internal; + using pointer = iteration_proxy_internal*; + using reference = iteration_proxy_internal&; + using iterator_category = std::input_iterator_tag; + + private: + /// the iterator + IteratorType anchor; + /// an index for arrays (used to create key names) + std::size_t array_index = 0; + /// last stringified array index + mutable std::size_t array_index_last = 0; + /// a string representation of the array index + mutable std::string array_index_str = "0"; + /// an empty string (to return a reference for primitive values) + const std::string empty_str = ""; + + public: + explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {} + + /// dereference operator (needed for range-based for) + iteration_proxy_internal& operator*() + { + return *this; + } + + /// increment operator (needed for range-based for) + iteration_proxy_internal& operator++() + { + ++anchor; + ++array_index; + + return *this; + } + + /// equality operator (needed for InputIterator) + bool operator==(const iteration_proxy_internal& o) const noexcept + { + return anchor == o.anchor; + } + + /// inequality operator (needed for range-based for) + bool operator!=(const iteration_proxy_internal& o) const noexcept + { + return anchor != o.anchor; + } + + /// return key of the iterator + const std::string& key() const + { + assert(anchor.m_object != nullptr); + + switch (anchor.m_object->type()) + { + // use integer array index as key + case value_t::array: + { + if (array_index != array_index_last) + { + array_index_str = std::to_string(array_index); + array_index_last = array_index; + } + return array_index_str; + } + + // use key from the object + case value_t::object: + return anchor.key(); + + // use an empty key for all primitive types + default: + return empty_str; + } + } + + /// return value of the iterator + typename IteratorType::reference value() const + { + return anchor.value(); + } + }; + + /// the container to iterate + typename IteratorType::reference container; + + public: + /// construct iteration proxy from a container + explicit iteration_proxy(typename IteratorType::reference cont) noexcept + : container(cont) {} + + /// return iterator begin (needed for range-based for) + iteration_proxy_internal begin() noexcept + { + return iteration_proxy_internal(container.begin()); + } + + /// return iterator end (needed for range-based for) + iteration_proxy_internal end() noexcept + { + return iteration_proxy_internal(container.end()); + } +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/iterators/json_reverse_iterator.hpp b/include/lib/modernjson/detail/iterators/json_reverse_iterator.hpp new file mode 100644 index 0000000..f3b5b5d --- /dev/null +++ b/include/lib/modernjson/detail/iterators/json_reverse_iterator.hpp @@ -0,0 +1,119 @@ +#pragma once + +#include // ptrdiff_t +#include // reverse_iterator +#include // declval + +namespace nlohmann +{ +namespace detail +{ +////////////////////// +// reverse_iterator // +////////////////////// + +/*! +@brief a template for a reverse iterator class + +@tparam Base the base iterator type to reverse. Valid types are @ref +iterator (to create @ref reverse_iterator) and @ref const_iterator (to +create @ref const_reverse_iterator). + +@requirement The class satisfies the following concept requirements: +- +[BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator): + The iterator that can be moved can be moved in both directions (i.e. + incremented and decremented). +- [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator): + It is possible to write to the pointed-to element (only if @a Base is + @ref iterator). + +@since version 1.0.0 +*/ +template +class json_reverse_iterator : public std::reverse_iterator +{ + public: + using difference_type = std::ptrdiff_t; + /// shortcut to the reverse iterator adapter + using base_iterator = std::reverse_iterator; + /// the reference type for the pointed-to element + using reference = typename Base::reference; + + /// create reverse iterator from iterator + explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept + : base_iterator(it) {} + + /// create reverse iterator from base class + explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {} + + /// post-increment (it++) + json_reverse_iterator const operator++(int) + { + return static_cast(base_iterator::operator++(1)); + } + + /// pre-increment (++it) + json_reverse_iterator& operator++() + { + return static_cast(base_iterator::operator++()); + } + + /// post-decrement (it--) + json_reverse_iterator const operator--(int) + { + return static_cast(base_iterator::operator--(1)); + } + + /// pre-decrement (--it) + json_reverse_iterator& operator--() + { + return static_cast(base_iterator::operator--()); + } + + /// add to iterator + json_reverse_iterator& operator+=(difference_type i) + { + return static_cast(base_iterator::operator+=(i)); + } + + /// add to iterator + json_reverse_iterator operator+(difference_type i) const + { + return static_cast(base_iterator::operator+(i)); + } + + /// subtract from iterator + json_reverse_iterator operator-(difference_type i) const + { + return static_cast(base_iterator::operator-(i)); + } + + /// return difference + difference_type operator-(const json_reverse_iterator& other) const + { + return base_iterator(*this) - base_iterator(other); + } + + /// access to successor + reference operator[](difference_type n) const + { + return *(this->operator+(n)); + } + + /// return the key of an object iterator + auto key() const -> decltype(std::declval().key()) + { + auto it = --this->base(); + return it.key(); + } + + /// return the value of an iterator + reference value() const + { + auto it = --this->base(); + return it.operator * (); + } +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/iterators/primitive_iterator.hpp b/include/lib/modernjson/detail/iterators/primitive_iterator.hpp new file mode 100644 index 0000000..28d6f1a --- /dev/null +++ b/include/lib/modernjson/detail/iterators/primitive_iterator.hpp @@ -0,0 +1,120 @@ +#pragma once + +#include // ptrdiff_t +#include // numeric_limits + +namespace nlohmann +{ +namespace detail +{ +/* +@brief an iterator for primitive JSON types + +This class models an iterator for primitive JSON types (boolean, number, +string). It's only purpose is to allow the iterator/const_iterator classes +to "iterate" over primitive values. Internally, the iterator is modeled by +a `difference_type` variable. Value begin_value (`0`) models the begin, +end_value (`1`) models past the end. +*/ +class primitive_iterator_t +{ + private: + using difference_type = std::ptrdiff_t; + static constexpr difference_type begin_value = 0; + static constexpr difference_type end_value = begin_value + 1; + + /// iterator as signed integer type + difference_type m_it = (std::numeric_limits::min)(); + + public: + constexpr difference_type get_value() const noexcept + { + return m_it; + } + + /// set iterator to a defined beginning + void set_begin() noexcept + { + m_it = begin_value; + } + + /// set iterator to a defined past the end + void set_end() noexcept + { + m_it = end_value; + } + + /// return whether the iterator can be dereferenced + constexpr bool is_begin() const noexcept + { + return m_it == begin_value; + } + + /// return whether the iterator is at end + constexpr bool is_end() const noexcept + { + return m_it == end_value; + } + + friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept + { + return lhs.m_it == rhs.m_it; + } + + friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept + { + return lhs.m_it < rhs.m_it; + } + + primitive_iterator_t operator+(difference_type n) noexcept + { + auto result = *this; + result += n; + return result; + } + + friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept + { + return lhs.m_it - rhs.m_it; + } + + primitive_iterator_t& operator++() noexcept + { + ++m_it; + return *this; + } + + primitive_iterator_t const operator++(int) noexcept + { + auto result = *this; + ++m_it; + return result; + } + + primitive_iterator_t& operator--() noexcept + { + --m_it; + return *this; + } + + primitive_iterator_t const operator--(int) noexcept + { + auto result = *this; + --m_it; + return result; + } + + primitive_iterator_t& operator+=(difference_type n) noexcept + { + m_it += n; + return *this; + } + + primitive_iterator_t& operator-=(difference_type n) noexcept + { + m_it -= n; + return *this; + } +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/json_pointer.hpp b/include/lib/modernjson/detail/json_pointer.hpp new file mode 100644 index 0000000..a280c9d --- /dev/null +++ b/include/lib/modernjson/detail/json_pointer.hpp @@ -0,0 +1,696 @@ +#pragma once + +#include // assert +#include // accumulate +#include // string +#include // vector + +#include +#include +#include + +namespace nlohmann +{ +template +class json_pointer +{ + // allow basic_json to access private members + NLOHMANN_BASIC_JSON_TPL_DECLARATION + friend class basic_json; + + public: + /*! + @brief create JSON pointer + + Create a JSON pointer according to the syntax described in + [Section 3 of RFC6901](https://tools.ietf.org/html/rfc6901#section-3). + + @param[in] s string representing the JSON pointer; if omitted, the empty + string is assumed which references the whole JSON value + + @throw parse_error.107 if the given JSON pointer @a s is nonempty and does + not begin with a slash (`/`); see example below + + @throw parse_error.108 if a tilde (`~`) in the given JSON pointer @a s is + not followed by `0` (representing `~`) or `1` (representing `/`); see + example below + + @liveexample{The example shows the construction several valid JSON pointers + as well as the exceptional behavior.,json_pointer} + + @since version 2.0.0 + */ + explicit json_pointer(const std::string& s = "") + : reference_tokens(split(s)) + {} + + /*! + @brief return a string representation of the JSON pointer + + @invariant For each JSON pointer `ptr`, it holds: + @code {.cpp} + ptr == json_pointer(ptr.to_string()); + @endcode + + @return a string representation of the JSON pointer + + @liveexample{The example shows the result of `to_string`., + json_pointer__to_string} + + @since version 2.0.0 + */ + std::string to_string() const + { + return std::accumulate(reference_tokens.begin(), reference_tokens.end(), + std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + escape(b); + }); + } + + /// @copydoc to_string() + operator std::string() const + { + return to_string(); + } + + /*! + @param[in] s reference token to be converted into an array index + + @return integer representation of @a s + + @throw out_of_range.404 if string @a s could not be converted to an integer + */ + static int array_index(const std::string& s) + { + std::size_t processed_chars = 0; + const int res = std::stoi(s, &processed_chars); + + // check if the string was completely read + if (JSON_UNLIKELY(processed_chars != s.size())) + { + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + } + + return res; + } + + private: + /*! + @brief remove and return last reference pointer + @throw out_of_range.405 if JSON pointer has no parent + */ + std::string pop_back() + { + if (JSON_UNLIKELY(is_root())) + { + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + } + + auto last = reference_tokens.back(); + reference_tokens.pop_back(); + return last; + } + + /// return whether pointer points to the root document + bool is_root() const noexcept + { + return reference_tokens.empty(); + } + + json_pointer top() const + { + if (JSON_UNLIKELY(is_root())) + { + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + } + + json_pointer result = *this; + result.reference_tokens = {reference_tokens[0]}; + return result; + } + + /*! + @brief create and return a reference to the pointed to value + + @complexity Linear in the number of reference tokens. + + @throw parse_error.109 if array index is not a number + @throw type_error.313 if value cannot be unflattened + */ + BasicJsonType& get_and_create(BasicJsonType& j) const + { + using size_type = typename BasicJsonType::size_type; + auto result = &j; + + // in case no reference tokens exist, return a reference to the JSON value + // j which will be overwritten by a primitive value + for (const auto& reference_token : reference_tokens) + { + switch (result->m_type) + { + case detail::value_t::null: + { + if (reference_token == "0") + { + // start a new array if reference token is 0 + result = &result->operator[](0); + } + else + { + // start a new object otherwise + result = &result->operator[](reference_token); + } + break; + } + + case detail::value_t::object: + { + // create an entry in the object + result = &result->operator[](reference_token); + break; + } + + case detail::value_t::array: + { + // create an entry in the array + JSON_TRY + { + result = &result->operator[](static_cast(array_index(reference_token))); + } + JSON_CATCH(std::invalid_argument&) + { + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); + } + break; + } + + /* + The following code is only reached if there exists a reference + token _and_ the current value is primitive. In this case, we have + an error situation, because primitive values may only occur as + single value; that is, with an empty list of reference tokens. + */ + default: + JSON_THROW(detail::type_error::create(313, "invalid value to unflatten")); + } + } + + return *result; + } + + /*! + @brief return a reference to the pointed to value + + @note This version does not throw if a value is not present, but tries to + create nested values instead. For instance, calling this function + with pointer `"/this/that"` on a null value is equivalent to calling + `operator[]("this").operator[]("that")` on that value, effectively + changing the null value to an object. + + @param[in] ptr a JSON value + + @return reference to the JSON value pointed to by the JSON pointer + + @complexity Linear in the length of the JSON pointer. + + @throw parse_error.106 if an array index begins with '0' + @throw parse_error.109 if an array index was not a number + @throw out_of_range.404 if the JSON pointer can not be resolved + */ + BasicJsonType& get_unchecked(BasicJsonType* ptr) const + { + using size_type = typename BasicJsonType::size_type; + for (const auto& reference_token : reference_tokens) + { + // convert null values to arrays or objects before continuing + if (ptr->m_type == detail::value_t::null) + { + // check if reference token is a number + const bool nums = + std::all_of(reference_token.begin(), reference_token.end(), + [](const char x) + { + return (x >= '0' and x <= '9'); + }); + + // change value to array for numbers or "-" or to object otherwise + *ptr = (nums or reference_token == "-") + ? detail::value_t::array + : detail::value_t::object; + } + + switch (ptr->m_type) + { + case detail::value_t::object: + { + // use unchecked object access + ptr = &ptr->operator[](reference_token); + break; + } + + case detail::value_t::array: + { + // error condition (cf. RFC 6901, Sect. 4) + if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0')) + { + JSON_THROW(detail::parse_error::create(106, 0, + "array index '" + reference_token + + "' must not begin with '0'")); + } + + if (reference_token == "-") + { + // explicitly treat "-" as index beyond the end + ptr = &ptr->operator[](ptr->m_value.array->size()); + } + else + { + // convert array index to number; unchecked access + JSON_TRY + { + ptr = &ptr->operator[]( + static_cast(array_index(reference_token))); + } + JSON_CATCH(std::invalid_argument&) + { + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); + } + } + break; + } + + default: + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + } + } + + return *ptr; + } + + /*! + @throw parse_error.106 if an array index begins with '0' + @throw parse_error.109 if an array index was not a number + @throw out_of_range.402 if the array index '-' is used + @throw out_of_range.404 if the JSON pointer can not be resolved + */ + BasicJsonType& get_checked(BasicJsonType* ptr) const + { + using size_type = typename BasicJsonType::size_type; + for (const auto& reference_token : reference_tokens) + { + switch (ptr->m_type) + { + case detail::value_t::object: + { + // note: at performs range check + ptr = &ptr->at(reference_token); + break; + } + + case detail::value_t::array: + { + if (JSON_UNLIKELY(reference_token == "-")) + { + // "-" always fails the range check + JSON_THROW(detail::out_of_range::create(402, + "array index '-' (" + std::to_string(ptr->m_value.array->size()) + + ") is out of range")); + } + + // error condition (cf. RFC 6901, Sect. 4) + if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0')) + { + JSON_THROW(detail::parse_error::create(106, 0, + "array index '" + reference_token + + "' must not begin with '0'")); + } + + // note: at performs range check + JSON_TRY + { + ptr = &ptr->at(static_cast(array_index(reference_token))); + } + JSON_CATCH(std::invalid_argument&) + { + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); + } + break; + } + + default: + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + } + } + + return *ptr; + } + + /*! + @brief return a const reference to the pointed to value + + @param[in] ptr a JSON value + + @return const reference to the JSON value pointed to by the JSON + pointer + + @throw parse_error.106 if an array index begins with '0' + @throw parse_error.109 if an array index was not a number + @throw out_of_range.402 if the array index '-' is used + @throw out_of_range.404 if the JSON pointer can not be resolved + */ + const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const + { + using size_type = typename BasicJsonType::size_type; + for (const auto& reference_token : reference_tokens) + { + switch (ptr->m_type) + { + case detail::value_t::object: + { + // use unchecked object access + ptr = &ptr->operator[](reference_token); + break; + } + + case detail::value_t::array: + { + if (JSON_UNLIKELY(reference_token == "-")) + { + // "-" cannot be used for const access + JSON_THROW(detail::out_of_range::create(402, + "array index '-' (" + std::to_string(ptr->m_value.array->size()) + + ") is out of range")); + } + + // error condition (cf. RFC 6901, Sect. 4) + if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0')) + { + JSON_THROW(detail::parse_error::create(106, 0, + "array index '" + reference_token + + "' must not begin with '0'")); + } + + // use unchecked array access + JSON_TRY + { + ptr = &ptr->operator[]( + static_cast(array_index(reference_token))); + } + JSON_CATCH(std::invalid_argument&) + { + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); + } + break; + } + + default: + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + } + } + + return *ptr; + } + + /*! + @throw parse_error.106 if an array index begins with '0' + @throw parse_error.109 if an array index was not a number + @throw out_of_range.402 if the array index '-' is used + @throw out_of_range.404 if the JSON pointer can not be resolved + */ + const BasicJsonType& get_checked(const BasicJsonType* ptr) const + { + using size_type = typename BasicJsonType::size_type; + for (const auto& reference_token : reference_tokens) + { + switch (ptr->m_type) + { + case detail::value_t::object: + { + // note: at performs range check + ptr = &ptr->at(reference_token); + break; + } + + case detail::value_t::array: + { + if (JSON_UNLIKELY(reference_token == "-")) + { + // "-" always fails the range check + JSON_THROW(detail::out_of_range::create(402, + "array index '-' (" + std::to_string(ptr->m_value.array->size()) + + ") is out of range")); + } + + // error condition (cf. RFC 6901, Sect. 4) + if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0')) + { + JSON_THROW(detail::parse_error::create(106, 0, + "array index '" + reference_token + + "' must not begin with '0'")); + } + + // note: at performs range check + JSON_TRY + { + ptr = &ptr->at(static_cast(array_index(reference_token))); + } + JSON_CATCH(std::invalid_argument&) + { + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); + } + break; + } + + default: + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + } + } + + return *ptr; + } + + /*! + @brief split the string input to reference tokens + + @note This function is only called by the json_pointer constructor. + All exceptions below are documented there. + + @throw parse_error.107 if the pointer is not empty or begins with '/' + @throw parse_error.108 if character '~' is not followed by '0' or '1' + */ + static std::vector split(const std::string& reference_string) + { + std::vector result; + + // special case: empty reference string -> no reference tokens + if (reference_string.empty()) + { + return result; + } + + // check if nonempty reference string begins with slash + if (JSON_UNLIKELY(reference_string[0] != '/')) + { + JSON_THROW(detail::parse_error::create(107, 1, + "JSON pointer must be empty or begin with '/' - was: '" + + reference_string + "'")); + } + + // extract the reference tokens: + // - slash: position of the last read slash (or end of string) + // - start: position after the previous slash + for ( + // search for the first slash after the first character + std::size_t slash = reference_string.find_first_of('/', 1), + // set the beginning of the first reference token + start = 1; + // we can stop if start == 0 (if slash == std::string::npos) + start != 0; + // set the beginning of the next reference token + // (will eventually be 0 if slash == std::string::npos) + start = (slash == std::string::npos) ? 0 : slash + 1, + // find next slash + slash = reference_string.find_first_of('/', start)) + { + // use the text between the beginning of the reference token + // (start) and the last slash (slash). + auto reference_token = reference_string.substr(start, slash - start); + + // check reference tokens are properly escaped + for (std::size_t pos = reference_token.find_first_of('~'); + pos != std::string::npos; + pos = reference_token.find_first_of('~', pos + 1)) + { + assert(reference_token[pos] == '~'); + + // ~ must be followed by 0 or 1 + if (JSON_UNLIKELY(pos == reference_token.size() - 1 or + (reference_token[pos + 1] != '0' and + reference_token[pos + 1] != '1'))) + { + JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'")); + } + } + + // finally, store the reference token + unescape(reference_token); + result.push_back(reference_token); + } + + return result; + } + + /*! + @brief replace all occurrences of a substring by another string + + @param[in,out] s the string to manipulate; changed so that all + occurrences of @a f are replaced with @a t + @param[in] f the substring to replace with @a t + @param[in] t the string to replace @a f + + @pre The search string @a f must not be empty. **This precondition is + enforced with an assertion.** + + @since version 2.0.0 + */ + static void replace_substring(std::string& s, const std::string& f, + const std::string& t) + { + assert(not f.empty()); + for (auto pos = s.find(f); // find first occurrence of f + pos != std::string::npos; // make sure f was found + s.replace(pos, f.size(), t), // replace with t, and + pos = s.find(f, pos + t.size())) // find next occurrence of f + {} + } + + /// escape "~" to "~0" and "/" to "~1" + static std::string escape(std::string s) + { + replace_substring(s, "~", "~0"); + replace_substring(s, "/", "~1"); + return s; + } + + /// unescape "~1" to tilde and "~0" to slash (order is important!) + static void unescape(std::string& s) + { + replace_substring(s, "~1", "/"); + replace_substring(s, "~0", "~"); + } + + /*! + @param[in] reference_string the reference string to the current value + @param[in] value the value to consider + @param[in,out] result the result object to insert values to + + @note Empty objects or arrays are flattened to `null`. + */ + static void flatten(const std::string& reference_string, + const BasicJsonType& value, + BasicJsonType& result) + { + switch (value.m_type) + { + case detail::value_t::array: + { + if (value.m_value.array->empty()) + { + // flatten empty array as null + result[reference_string] = nullptr; + } + else + { + // iterate array and use index as reference string + for (std::size_t i = 0; i < value.m_value.array->size(); ++i) + { + flatten(reference_string + "/" + std::to_string(i), + value.m_value.array->operator[](i), result); + } + } + break; + } + + case detail::value_t::object: + { + if (value.m_value.object->empty()) + { + // flatten empty object as null + result[reference_string] = nullptr; + } + else + { + // iterate object and use keys as reference string + for (const auto& element : *value.m_value.object) + { + flatten(reference_string + "/" + escape(element.first), element.second, result); + } + } + break; + } + + default: + { + // add primitive value with its reference string + result[reference_string] = value; + break; + } + } + } + + /*! + @param[in] value flattened JSON + + @return unflattened JSON + + @throw parse_error.109 if array index is not a number + @throw type_error.314 if value is not an object + @throw type_error.315 if object values are not primitive + @throw type_error.313 if value cannot be unflattened + */ + static BasicJsonType + unflatten(const BasicJsonType& value) + { + if (JSON_UNLIKELY(not value.is_object())) + { + JSON_THROW(detail::type_error::create(314, "only objects can be unflattened")); + } + + BasicJsonType result; + + // iterate the JSON object values + for (const auto& element : *value.m_value.object) + { + if (JSON_UNLIKELY(not element.second.is_primitive())) + { + JSON_THROW(detail::type_error::create(315, "values in object must be primitive")); + } + + // assign value to reference pointed to by JSON pointer; Note that if + // the JSON pointer is "" (i.e., points to the whole value), function + // get_and_create returns a reference to result itself. An assignment + // will then create a primitive value. + json_pointer(element.first).get_and_create(result) = element.second; + } + + return result; + } + + friend bool operator==(json_pointer const& lhs, + json_pointer const& rhs) noexcept + { + return (lhs.reference_tokens == rhs.reference_tokens); + } + + friend bool operator!=(json_pointer const& lhs, + json_pointer const& rhs) noexcept + { + return not (lhs == rhs); + } + + /// the reference tokens + std::vector reference_tokens; +}; +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/json_ref.hpp b/include/lib/modernjson/detail/json_ref.hpp new file mode 100644 index 0000000..c44aa66 --- /dev/null +++ b/include/lib/modernjson/detail/json_ref.hpp @@ -0,0 +1,69 @@ +#pragma once + +#include +#include + +#include + +namespace nlohmann +{ +namespace detail +{ +template +class json_ref +{ + public: + using value_type = BasicJsonType; + + json_ref(value_type&& value) + : owned_value(std::move(value)), value_ref(&owned_value), is_rvalue(true) + {} + + json_ref(const value_type& value) + : value_ref(const_cast(&value)), is_rvalue(false) + {} + + json_ref(std::initializer_list init) + : owned_value(init), value_ref(&owned_value), is_rvalue(true) + {} + + template < + class... Args, + enable_if_t::value, int> = 0 > + json_ref(Args && ... args) + : owned_value(std::forward(args)...), value_ref(&owned_value), + is_rvalue(true) {} + + // class should be movable only + json_ref(json_ref&&) = default; + json_ref(const json_ref&) = delete; + json_ref& operator=(const json_ref&) = delete; + json_ref& operator=(json_ref&&) = delete; + ~json_ref() = default; + + value_type moved_or_copied() const + { + if (is_rvalue) + { + return std::move(*value_ref); + } + return *value_ref; + } + + value_type const& operator*() const + { + return *static_cast(value_ref); + } + + value_type const* operator->() const + { + return static_cast(value_ref); + } + + private: + mutable value_type owned_value = nullptr; + value_type* value_ref = nullptr; + const bool is_rvalue; +}; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/macro_scope.hpp b/include/lib/modernjson/detail/macro_scope.hpp new file mode 100644 index 0000000..26db667 --- /dev/null +++ b/include/lib/modernjson/detail/macro_scope.hpp @@ -0,0 +1,135 @@ +#pragma once + +// This file contains all internal macro definitions +// You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them + +// exclude unsupported compilers +#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) + #if defined(__clang__) + #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 + #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) + #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800 + #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #endif +#endif + +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + +// disable documentation warnings on clang +#if defined(__clang__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wdocumentation" +#endif + +// allow for portable deprecation warnings +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #define JSON_DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) + #define JSON_DEPRECATED __declspec(deprecated) +#else + #define JSON_DEPRECATED +#endif + +// allow to disable exceptions +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) + #define JSON_THROW(exception) throw exception + #define JSON_TRY try + #define JSON_CATCH(exception) catch(exception) + #define JSON_INTERNAL_CATCH(exception) catch(exception) +#else + #define JSON_THROW(exception) std::abort() + #define JSON_TRY if(true) + #define JSON_CATCH(exception) if(false) + #define JSON_INTERNAL_CATCH(exception) if(false) +#endif + +// override exception macros +#if defined(JSON_THROW_USER) + #undef JSON_THROW + #define JSON_THROW JSON_THROW_USER +#endif +#if defined(JSON_TRY_USER) + #undef JSON_TRY + #define JSON_TRY JSON_TRY_USER +#endif +#if defined(JSON_CATCH_USER) + #undef JSON_CATCH + #define JSON_CATCH JSON_CATCH_USER + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_CATCH_USER +#endif +#if defined(JSON_INTERNAL_CATCH_USER) + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER +#endif + +// manual branch prediction +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #define JSON_LIKELY(x) __builtin_expect(!!(x), 1) + #define JSON_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else + #define JSON_LIKELY(x) x + #define JSON_UNLIKELY(x) x +#endif + +// C++ language standard detection +#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 +#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) + #define JSON_HAS_CPP_14 +#endif + +/*! +@brief macro to briefly define a mapping between an enum and JSON +@def NLOHMANN_JSON_SERIALIZE_ENUM +@since version 3.4.0 +*/ +#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \ + template \ + inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [e](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.first == e; \ + }); \ + j = ((it != std::end(m)) ? it : std::begin(m))->second; \ + } \ + template \ + inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [j](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.second == j; \ + }); \ + e = ((it != std::end(m)) ? it : std::begin(m))->first; \ + } + +// Ugly macros to avoid uglier copy-paste when specializing basic_json. They +// may be removed in the future once the class is split. + +#define NLOHMANN_BASIC_JSON_TPL_DECLARATION \ + template class ObjectType, \ + template class ArrayType, \ + class StringType, class BooleanType, class NumberIntegerType, \ + class NumberUnsignedType, class NumberFloatType, \ + template class AllocatorType, \ + template class JSONSerializer> + +#define NLOHMANN_BASIC_JSON_TPL \ + basic_json diff --git a/include/lib/modernjson/detail/macro_unscope.hpp b/include/lib/modernjson/detail/macro_unscope.hpp new file mode 100644 index 0000000..4c5aa91 --- /dev/null +++ b/include/lib/modernjson/detail/macro_unscope.hpp @@ -0,0 +1,22 @@ +#pragma once + +// restore GCC/clang diagnostic settings +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic pop +#endif +#if defined(__clang__) + #pragma GCC diagnostic pop +#endif + +// clean up +#undef JSON_INTERNAL_CATCH +#undef JSON_CATCH +#undef JSON_THROW +#undef JSON_TRY +#undef JSON_LIKELY +#undef JSON_UNLIKELY +#undef JSON_DEPRECATED +#undef JSON_HAS_CPP_14 +#undef JSON_HAS_CPP_17 +#undef NLOHMANN_BASIC_JSON_TPL_DECLARATION +#undef NLOHMANN_BASIC_JSON_TPL diff --git a/include/lib/modernjson/detail/meta/cpp_future.hpp b/include/lib/modernjson/detail/meta/cpp_future.hpp new file mode 100644 index 0000000..948cd4f --- /dev/null +++ b/include/lib/modernjson/detail/meta/cpp_future.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include // not +#include // size_t +#include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type + +namespace nlohmann +{ +namespace detail +{ +// alias templates to reduce boilerplate +template +using enable_if_t = typename std::enable_if::type; + +template +using uncvref_t = typename std::remove_cv::type>::type; + +// implementation of C++14 index_sequence and affiliates +// source: https://stackoverflow.com/a/32223343 +template +struct index_sequence +{ + using type = index_sequence; + using value_type = std::size_t; + static constexpr std::size_t size() noexcept + { + return sizeof...(Ints); + } +}; + +template +struct merge_and_renumber; + +template +struct merge_and_renumber, index_sequence> + : index_sequence < I1..., (sizeof...(I1) + I2)... > {}; + +template +struct make_index_sequence + : merge_and_renumber < typename make_index_sequence < N / 2 >::type, + typename make_index_sequence < N - N / 2 >::type > {}; + +template<> struct make_index_sequence<0> : index_sequence<> {}; +template<> struct make_index_sequence<1> : index_sequence<0> {}; + +template +using index_sequence_for = make_index_sequence; + +// dispatch utility (taken from ranges-v3) +template struct priority_tag : priority_tag < N - 1 > {}; +template<> struct priority_tag<0> {}; + +// taken from ranges-v3 +template +struct static_const +{ + static constexpr T value{}; +}; + +template +constexpr T static_const::value; +} // namespace detail +} // namespace nlohmann diff --git a/include/lib/modernjson/detail/meta/detected.hpp b/include/lib/modernjson/detail/meta/detected.hpp new file mode 100644 index 0000000..8fb318a --- /dev/null +++ b/include/lib/modernjson/detail/meta/detected.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include + +#include + +// http://en.cppreference.com/w/cpp/experimental/is_detected +namespace nlohmann +{ +namespace detail +{ +struct nonesuch +{ + nonesuch() = delete; + ~nonesuch() = delete; + nonesuch(nonesuch const&) = delete; + void operator=(nonesuch const&) = delete; +}; + +template class Op, + class... Args> +struct detector +{ + using value_t = std::false_type; + using type = Default; +}; + +template class Op, class... Args> +struct detector>, Op, Args...> +{ + using value_t = std::true_type; + using type = Op; +}; + +template