From 24d993dcd23f258a69276a0dd2537ec6cd120ba8 Mon Sep 17 00:00:00 2001 From: Hiroyuki Kobayashi Date: Sat, 9 May 2015 00:24:29 +0900 Subject: [PATCH] Support picojson library --- CMakeLists.txt | 7 + README.md | 3 +- .../valijson/adapters/picojson_adapter.hpp | 691 +++++++++++ include/valijson/utils/picojson_utils.hpp | 34 + tests/test_adapter_comparison.cpp | 30 + tests/test_picojson_adapter.cpp | 85 ++ tests/test_validator.cpp | 3 + thirdparty/picojson-1.3.0/.gitignore | 4 + thirdparty/picojson-1.3.0/.gitmodules | 0 thirdparty/picojson-1.3.0/.travis.yml | 8 + thirdparty/picojson-1.3.0/Changes | 25 + thirdparty/picojson-1.3.0/LICENSE | 25 + thirdparty/picojson-1.3.0/Makefile | 26 + thirdparty/picojson-1.3.0/README.mkdn | 195 ++++ .../picojson-1.3.0/examples/github-issues.cc | 110 ++ .../picojson-1.3.0/examples/iostream.cc | 70 ++ .../picojson-1.3.0/examples/streaming.cc | 76 ++ thirdparty/picojson-1.3.0/picojson.h | 1010 +++++++++++++++++ thirdparty/picojson-1.3.0/picotest/picotest.c | 99 ++ thirdparty/picojson-1.3.0/picotest/picotest.h | 39 + thirdparty/picojson-1.3.0/test.cc | 312 +++++ 21 files changed, 2851 insertions(+), 1 deletion(-) create mode 100644 include/valijson/adapters/picojson_adapter.hpp create mode 100644 include/valijson/utils/picojson_utils.hpp create mode 100644 tests/test_picojson_adapter.cpp create mode 100644 thirdparty/picojson-1.3.0/.gitignore create mode 100644 thirdparty/picojson-1.3.0/.gitmodules create mode 100644 thirdparty/picojson-1.3.0/.travis.yml create mode 100644 thirdparty/picojson-1.3.0/Changes create mode 100644 thirdparty/picojson-1.3.0/LICENSE create mode 100644 thirdparty/picojson-1.3.0/Makefile create mode 100644 thirdparty/picojson-1.3.0/README.mkdn create mode 100644 thirdparty/picojson-1.3.0/examples/github-issues.cc create mode 100644 thirdparty/picojson-1.3.0/examples/iostream.cc create mode 100644 thirdparty/picojson-1.3.0/examples/streaming.cc create mode 100644 thirdparty/picojson-1.3.0/picojson.h create mode 100644 thirdparty/picojson-1.3.0/picotest/picotest.c create mode 100644 thirdparty/picojson-1.3.0/picotest/picotest.h create mode 100644 thirdparty/picojson-1.3.0/test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index a69fffc..13a9772 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ include_directories( thirdparty/gtest-1.7.0/include thirdparty/jsoncpp-0.9.4/include thirdparty/rapidjson-0.1/include + thirdparty/picojson-1.3.0 ${Boost_INCLUDE_DIRS} ) @@ -44,11 +45,17 @@ add_executable(test_suite tests/test_jsoncpp_adapter.cpp tests/test_property_tree_adapter.cpp tests/test_rapidjson_adapter.cpp + tests/test_picojson_adapter.cpp tests/test_uri_resolution.cpp tests/test_validation_errors.cpp tests/test_validator.cpp ) +# Definition for using picojson +set_target_properties(test_suite + PROPERTIES COMPILE_DEFINITIONS "PICOJSON_USE_INT64" +) + set(TEST_LIBS gtest gtest_main jsoncpp) target_link_libraries(test_suite ${TEST_LIBS} ${Boost_LIBRARIES}) diff --git a/README.md b/README.md index 4d4aa83..0f7aec2 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,12 @@ Required: Later versions of boost (up to 1.57) are also known to work correctly. -Valijson supports JSON documents loaded using JsonCpp, RapidJson and Boost Property Tree. It has been tested against the following versions of these libraries: +Valijson supports JSON documents loaded using JsonCpp, RapidJson, Boost Property Tree and PicoJSON. It has been tested against the following versions of these libraries: - [boost::property_tree 1.54](http://www.boost.org/doc/libs/1_54_0/doc/html/boost_propertytree/synopsis.html) - [jsoncpp 0.9.4](https://github.com/open-source-parsers/jsoncpp/archive/0.9.4.tar.gz) - [rapidjson 0.1](https://code.google.com/p/rapidjson/downloads/detail?name=rapidjson-0.1.zip) + - [PicoJSON 1.3.0](https://github.com/kazuho/picojson/archive/v1.3.0.tar.gz) Version of JsonCpp going back to 0.5.0 should also work correctly, but versions from 1.0 onwards have not yet been tested. diff --git a/include/valijson/adapters/picojson_adapter.hpp b/include/valijson/adapters/picojson_adapter.hpp new file mode 100644 index 0000000..3a143eb --- /dev/null +++ b/include/valijson/adapters/picojson_adapter.hpp @@ -0,0 +1,691 @@ +/** + * @file + * + * @brief Adapter implementation for the PicoJson parser library. + * + * Include this file in your program to enable support for PicoJson. + * + * This file defines the following classes (not in this order): + * - PicoJsonAdapter + * - PicoJsonArray + * - PicoJsonArrayValueIterator + * - PicoJsonFrozenValue + * - PicoJsonObject + * - PicoJsonObjectMember + * - PicoJsonObjectMemberIterator + * - PicoJsonValue + * + * Due to the dependencies that exist between these classes, the ordering of + * class declarations and definitions may be a bit confusing. The best place to + * start is PicoJsonAdapter. This class definition is actually very small, + * since most of the functionality is inherited from the BasicAdapter class. + * Most of the classes in this file are provided as template arguments to the + * inherited BasicAdapter class. + */ + +#ifndef __VALIJSON_ADAPTERS_PICOJSON_ADAPTER_HPP +#define __VALIJSON_ADAPTERS_PICOJSON_ADAPTER_HPP + +#include +#include +#include +#include + +#include + +#include +#include +#include + +namespace valijson { +namespace adapters { + +class PicoJsonAdapter; +class PicoJsonArrayValueIterator; +class PicoJsonObjectMemberIterator; + +typedef std::pair PicoJsonObjectMember; + +/** + * @brief Light weight wrapper for a PicoJson array value. + * + * This class is light weight wrapper for a PicoJson array. It provides a + * minimum set of container functions and typedefs that allow it to be used as + * an iterable container. + * + * An instance of this class contains a single reference to the underlying + * PicoJson value, assumed to be an array, so there is very little overhead + * associated with copy construction and passing by value. + */ +class PicoJsonArray +{ +public: + + typedef PicoJsonArrayValueIterator const_iterator; + typedef PicoJsonArrayValueIterator iterator; + + /// Construct a PicoJsonArray referencing an empty array. + PicoJsonArray() + : value(emptyArray()) { } + + /** + * @brief Construct a PicoJsonArray referencing a specific PicoJson + * value. + * + * @param value reference to a PicoJson value + * + * Note that this constructor will throw an exception if the value is not + * an array. + */ + PicoJsonArray(const picojson::value &value) + : value(value) + { + if (!value.is()) { + throw std::runtime_error("Value is not an array."); + } + } + + /** + * @brief Return an iterator for the first element of the array. + * + * The iterator return by this function is effectively the iterator + * returned by the underlying PicoJson implementation. + */ + PicoJsonArrayValueIterator begin() const; + + /** + * @brief Return an iterator for one-past the last element of the array. + * + * The iterator return by this function is effectively the iterator + * returned by the underlying PicoJson implementation. + */ + PicoJsonArrayValueIterator end() const; + + /// Return the number of elements in the array + size_t size() const + { + const picojson::array &array = value.get(); + return array.size(); + } + +private: + + /** + * @brief Return a reference to a PicoJson value that is an empty array. + * + * Note that the value returned by this function is a singleton. + */ + static const picojson::value & emptyArray() + { + static const picojson::value array(picojson::array_type, false); + return array; + } + + /// Reference to the contained value + const picojson::value &value; +}; + +/** + * @brief Light weight wrapper for a PicoJson object. + * + * This class is light weight wrapper for a PicoJson object. It provides a + * minimum set of container functions and typedefs that allow it to be used as + * an iterable container. + * + * An instance of this class contains a single reference to the underlying + * PicoJson value, assumed to be an object, so there is very little overhead + * associated with copy construction and passing by value. + */ +class PicoJsonObject +{ +public: + + typedef PicoJsonObjectMemberIterator const_iterator; + typedef PicoJsonObjectMemberIterator iterator; + + /// Construct a PicoJsonObject referencing an empty object singleton. + PicoJsonObject() + : value(emptyObject()) { } + + /** + * @brief Construct a PicoJsonObject referencing a specific PicoJson + * value. + * + * @param value reference to a PicoJson value + * + * Note that this constructor will throw an exception if the value is not + * an object. + */ + PicoJsonObject(const picojson::value &value) + : value(value) + { + if (!value.is()) { + throw std::runtime_error("Value is not an object."); + } + } + + /** + * @brief Return an iterator for this first object member + * + * The iterator return by this function is effectively a wrapper around + * the iterator value returned by the underlying PicoJson implementation. + */ + PicoJsonObjectMemberIterator begin() const; + + /** + * @brief Return an iterator for an invalid object member that indicates + * the end of the collection. + * + * The iterator return by this function is effectively a wrapper around + * the iterator value returned by the underlying PicoJson implementation. + */ + PicoJsonObjectMemberIterator end() const; + + /** + * @brief Return an iterator for the object member with the specified + * property name. + * + * If an object member with the specified name does not exist, the iterator + * returned will be the same as the iterator returned by the end() function. + * + * @param property property name to search for + */ + PicoJsonObjectMemberIterator find(const std::string &propertyName) const; + + /// Returns the number of members belonging to this object. + size_t size() const + { + const picojson::object &object = value.get(); + return object.size(); + } + +private: + + /** + * @brief Return a reference to a PicoJson value that is empty object. + * + * Note that the value returned by this function is a singleton. + */ + static const picojson::value & emptyObject() + { + static const picojson::value object(picojson::object_type, false); + return object; + } + + /// Reference to the contained object + const picojson::value &value; +}; + +/** + * @brief Stores an independent copy of a PicoJson value. + * + * This class allows a PicoJson value to be stored independent of its original + * document. PicoJson makes this easy to do, as it does not perform any + * custom memory management. + * + * @see FrozenValue + */ +class PicoJsonFrozenValue: public FrozenValue +{ +public: + + /** + * @brief Make a copy of a PicoJson value + * + * @param source the PicoJson value to be copied + */ + PicoJsonFrozenValue(const picojson::value &source) + : value(source) { } + + virtual FrozenValue * clone() const + { + return new PicoJsonFrozenValue(value); + } + + virtual bool equalTo(const Adapter &other, bool strict) const; + +private: + + /// Stored PicoJson value + picojson::value value; +}; + +/** + * @brief Light weight wrapper for a PicoJson value. + * + * This class is passed as an argument to the BasicAdapter template class, + * and is used to provide access to a PicoJson value. This class is responsible + * for the mechanics of actually reading a PicoJson value, whereas the + * BasicAdapter class is responsible for the semantics of type comparisons + * and conversions. + * + * The functions that need to be provided by this class are defined implicitly + * by the implementation of the BasicAdapter template class. + * + * @see BasicAdapter + */ +class PicoJsonValue +{ +public: + + /// Construct a wrapper for the empty object singleton + PicoJsonValue() + : value(emptyObject()) { } + + /// Construct a wrapper for a specific PicoJson value + PicoJsonValue(const picojson::value &value) + : value(value) { } + + /** + * @brief Create a new PicoJsonFrozenValue instance that contains the + * value referenced by this PicoJsonValue instance. + * + * @returns pointer to a new PicoJsonFrozenValue instance, belonging to the + * caller. + */ + FrozenValue * freeze() const + { + return new PicoJsonFrozenValue(value); + } + + /** + * @brief Optionally return a PicoJsonArray instance. + * + * If the referenced PicoJson value is an array, this function will return + * a boost::optional containing a PicoJsonArray instance referencing the + * array. + * + * Otherwise it will return boost::none. + */ + boost::optional getArrayOptional() const + { + if (value.is()) { + return boost::make_optional(PicoJsonArray(value)); + } + + return boost::none; + } + + /** + * @brief Retrieve the number of elements in the array + * + * If the referenced PicoJson value is an array, this function will + * retrieve the number of elements in the array and store it in the output + * variable provided. + * + * @param result reference to size_t to set with result + * + * @returns true if the number of elements was retrieved, false otherwise. + */ + bool getArraySize(size_t &result) const + { + if (value.is()) { + const picojson::array& array = value.get(); + result = array.size(); + return true; + } + + return false; + } + + bool getBool(bool &result) const + { + if (value.is()) { + result = value.get(); + return true; + } + + return false; + } + + bool getDouble(double &result) const + { + if (value.is()) { + result = value.get(); + return true; + } + + return false; + } + + bool getInteger(int64_t &result) const + { + if (value.is()) { + result = value.get(); + return true; + } + + return false; + } + + /** + * @brief Optionally return a PicoJsonObject instance. + * + * If the referenced PicoJson value is an object, this function will return a + * boost::optional containing a PicoJsonObject instance referencing the + * object. + * + * Otherwise it will return boost::none. + */ + boost::optional getObjectOptional() const + { + if (value.is()) { + return boost::make_optional(PicoJsonObject(value)); + } + + return boost::none; + } + + /** + * @brief Retrieve the number of members in the object + * + * If the referenced PicoJson value is an object, this function will + * retrieve the number of members in the object and store it in the output + * variable provided. + * + * @param result reference to size_t to set with result + * + * @returns true if the number of members was retrieved, false otherwise. + */ + bool getObjectSize(size_t &result) const + { + if (value.is()) { + const picojson::object &object = value.get(); + result = object.size(); + return true; + } + + return false; + } + + bool getString(std::string &result) const + { + if (value.is()) { + result = value.get(); + return true; + } + + return false; + } + + static bool hasStrictTypes() + { + return true; + } + + bool isArray() const + { + return value.is(); + } + + bool isBool() const + { + return value.is(); + } + + bool isDouble() const + { + if (value.is()) { + return false; + } + + return value.is(); + } + + bool isInteger() const + { + return value.is(); + } + + bool isNull() const + { + return value.is(); + } + + bool isNumber() const + { + return value.is(); + } + + bool isObject() const + { + return value.is(); + } + + bool isString() const + { + return value.is(); + } + +private: + + /// Return a reference to an empty object singleton + static const picojson::value & emptyObject() + { + static const picojson::value object(picojson::object_type, false); + return object; + } + + /// Reference to the contained PicoJson value. + const picojson::value &value; +}; + +/** + * @brief An implementation of the Adapter interface supporting PicoJson. + * + * This class is defined in terms of the BasicAdapter template class, which + * helps to ensure that all of the Adapter implementations behave consistently. + * + * @see Adapter + * @see BasicAdapter + */ +class PicoJsonAdapter: + public BasicAdapter +{ +public: + + /// Construct a PicoJsonAdapter that contains an empty object + PicoJsonAdapter() + : BasicAdapter() { } + + /// Construct a PicoJsonAdapter containing a specific PicoJson value + PicoJsonAdapter(const picojson::value &value) + : BasicAdapter(value) { } +}; + +/** + * @brief Class for iterating over values held in a JSON array. + * + * This class provides a JSON array iterator that dereferences as an instance of + * PicoJsonAdapter representing a value stored in the array. It has been + * implemented using the boost iterator_facade template. + * + * @see PicoJsonArray + */ +class PicoJsonArrayValueIterator: + public boost::iterator_facade< + PicoJsonArrayValueIterator, // name of derived type + PicoJsonAdapter, // value type + boost::bidirectional_traversal_tag, // bi-directional iterator + PicoJsonAdapter> // type returned when dereferenced +{ +public: + + /** + * @brief Construct a new PicoJsonArrayValueIterator using an existing + * PicoJson iterator. + * + * @param itr PicoJson iterator to store + */ + PicoJsonArrayValueIterator( + const picojson::array::const_iterator &itr) + : itr(itr) { } + + /// Returns a PicoJsonAdapter that contains the value of the current + /// element. + PicoJsonAdapter dereference() const + { + return PicoJsonAdapter(*itr); + } + + /** + * @brief Compare this iterator against another iterator. + * + * Note that this directly compares the iterators, not the underlying + * values, and assumes that two identical iterators will point to the same + * underlying object. + * + * @param rhs iterator to compare against + * + * @returns true if the iterators are equal, false otherwise. + */ + bool equal(const PicoJsonArrayValueIterator &other) const + { + return itr == other.itr; + } + + void increment() + { + itr++; + } + + void decrement() + { + itr--; + } + + void advance(std::ptrdiff_t n) + { + itr += n; + } + +private: + + picojson::array::const_iterator itr; +}; + +/** + * @brief Class for iterating over the members belonging to a JSON object. + * + * This class provides a JSON object iterator that dereferences as an instance + * of PicoJsonObjectMember representing one of the members of the object. It + * has been implemented using the boost iterator_facade template. + * + * @see PicoJsonObject + * @see PicoJsonObjectMember + */ +class PicoJsonObjectMemberIterator: + public boost::iterator_facade< + PicoJsonObjectMemberIterator, // name of derived type + PicoJsonObjectMember, // value type + boost::bidirectional_traversal_tag, // bi-directional iterator + PicoJsonObjectMember> // type returned when dereferenced +{ +public: + + /** + * @brief Construct an iterator from a PicoJson iterator. + * + * @param itr PicoJson iterator to store + */ + PicoJsonObjectMemberIterator( + const picojson::object::const_iterator &itr) + : itr(itr) { } + + /** + * @brief Returns a PicoJsonObjectMember that contains the key and value + * belonging to the object member identified by the iterator. + */ + PicoJsonObjectMember dereference() const + { + return PicoJsonObjectMember(itr->first, itr->second); + } + + /** + * @brief Compare this iterator with another iterator. + * + * Note that this directly compares the iterators, not the underlying + * values, and assumes that two identical iterators will point to the same + * underlying object. + * + * @param rhs Iterator to compare with + * + * @returns true if the underlying iterators are equal, false otherwise + */ + bool equal(const PicoJsonObjectMemberIterator &other) const + { + return itr == other.itr; + } + + void increment() + { + itr++; + } + + void decrement() + { + itr--; + } + +private: + + /// Iternal copy of the original PicoJson iterator + picojson::object::const_iterator itr; +}; + +/// Specialisation of the AdapterTraits template struct for PicoJsonAdapter. +template<> +struct AdapterTraits +{ + typedef picojson::value DocumentType; + + static std::string adapterName() + { + return "PicoJsonAdapter"; + } +}; + +inline bool PicoJsonFrozenValue::equalTo(const Adapter &other, bool strict) const +{ + return PicoJsonAdapter(value).equalTo(other, strict); +} + +inline PicoJsonArrayValueIterator PicoJsonArray::begin() const +{ + const picojson::array &array = value.get(); + return array.begin(); +} + +inline PicoJsonArrayValueIterator PicoJsonArray::end() const +{ + const picojson::array &array = value.get(); + return array.end(); +} + +inline PicoJsonObjectMemberIterator PicoJsonObject::begin() const +{ + const picojson::object &object = value.get(); + return object.begin(); +} + +inline PicoJsonObjectMemberIterator PicoJsonObject::end() const +{ + const picojson::object &object = value.get(); + return object.end(); +} + +inline PicoJsonObjectMemberIterator PicoJsonObject::find( + const std::string &propertyName) const +{ + const picojson::object &object = value.get(); + return object.find(propertyName); +} + +} // namespace adapters +} // namespace valijson + +#endif diff --git a/include/valijson/utils/picojson_utils.hpp b/include/valijson/utils/picojson_utils.hpp new file mode 100644 index 0000000..cbbd77d --- /dev/null +++ b/include/valijson/utils/picojson_utils.hpp @@ -0,0 +1,34 @@ +#ifndef __VALIJSON_UTILS_PICOJSON_UTILS_HPP +#define __VALIJSON_UTILS_PICOJSON_UTILS_HPP + +#include + +#include + +namespace valijson { +namespace utils { + +inline bool loadDocument(const std::string &path, picojson::value &document) +{ + // Load schema JSON from file + std::string file; + if (!loadFile(path, file)) { + std::cerr << "Failed to load json from file '" << path << "'." << std::endl; + return false; + } + + // Parse schema + std::string err = picojson::parse(document, file); + if (!err.empty()) { + std::cerr << "PicoJson failed to parse the document:" << std::endl + << "Parse error: " << err << std::endl; + return false; + } + + return true; +} + +} // namespace utils +} // namespace valijson + +#endif diff --git a/tests/test_adapter_comparison.cpp b/tests/test_adapter_comparison.cpp index 29830f9..a805145 100644 --- a/tests/test_adapter_comparison.cpp +++ b/tests/test_adapter_comparison.cpp @@ -6,9 +6,11 @@ #include #include #include +#include #include #include #include +#include #define TEST_DATA_DIR "../tests/data/documents/" @@ -135,6 +137,13 @@ TEST_F(TestAdapterComparison, JsonCppVsRapidJson) valijson::adapters::RapidJsonAdapter>(); } +TEST_F(TestAdapterComparison, JsonCppVsPicoJson) +{ + testComparison< + valijson::adapters::JsonCppAdapter, + valijson::adapters::PicoJsonAdapter>(); +} + TEST_F(TestAdapterComparison, PropertyTreeVsPropertyTree) { testComparison< @@ -149,9 +158,30 @@ TEST_F(TestAdapterComparison, PropertyTreeVsRapidJson) valijson::adapters::RapidJsonAdapter>(); } +TEST_F(TestAdapterComparison, PropertyTreeVsPicoJson) +{ + testComparison< + valijson::adapters::PropertyTreeAdapter, + valijson::adapters::PicoJsonAdapter>(); +} + TEST_F(TestAdapterComparison, RapidJsonVsRapidJson) { testComparison< valijson::adapters::RapidJsonAdapter, valijson::adapters::RapidJsonAdapter>(); } + +TEST_F(TestAdapterComparison, RapidJsonVsPicoJson) +{ + testComparison< + valijson::adapters::RapidJsonAdapter, + valijson::adapters::PicoJsonAdapter>(); +} + +TEST_F(TestAdapterComparison, PicoJsonVsPicoJson) +{ + testComparison< + valijson::adapters::PicoJsonAdapter, + valijson::adapters::PicoJsonAdapter>(); +} diff --git a/tests/test_picojson_adapter.cpp b/tests/test_picojson_adapter.cpp new file mode 100644 index 0000000..dea47e8 --- /dev/null +++ b/tests/test_picojson_adapter.cpp @@ -0,0 +1,85 @@ +#include +#include + +#include + +#include + +class TestPicoJsonAdapter : public testing::Test +{ + +}; + +TEST_F(TestPicoJsonAdapter, BasicArrayIteration) +{ + const unsigned int numElements = 10; + + // Create a picojson document that consists of an array of numbers + picojson::array array; + for (unsigned int i = 0; i < numElements; i++) { + picojson::value value(static_cast(i)); + array.push_back(value); + } + picojson::value document(array); + + // Ensure that wrapping the document preserves the array and does not allow + // it to be cast to other types + valijson::adapters::PicoJsonAdapter adapter(document); + ASSERT_NO_THROW( adapter.getArray() ); + ASSERT_ANY_THROW( adapter.getBool() ); + ASSERT_ANY_THROW( adapter.getDouble() ); + ASSERT_ANY_THROW( adapter.getObject() ); + ASSERT_ANY_THROW( adapter.getString() ); + + // Ensure that the array contains the expected number of elements + EXPECT_EQ( numElements, adapter.getArray().size() ); + + // Ensure that the elements are returned in the order they were inserted + unsigned int expectedValue = 0; + BOOST_FOREACH( const valijson::adapters::PicoJsonAdapter value, adapter.getArray() ) { + ASSERT_TRUE( value.isNumber() ); + EXPECT_EQ( double(expectedValue), value.getDouble() ); + expectedValue++; + } + + // Ensure that the correct number of elements were iterated over + EXPECT_EQ(numElements, expectedValue); +} + +TEST_F(TestPicoJsonAdapter, BasicObjectIteration) +{ + const unsigned int numElements = 10; + + // Create a picojson document that consists of an object that maps numeric + // strings their corresponding numeric values + picojson::object object; + for (unsigned int i = 0; i < numElements; i++) { + std::string name(boost::lexical_cast(i)); + object[name] = picojson::value(static_cast(i)); + } + picojson::value document(object); + + // Ensure that wrapping the document preserves the object and does not + // allow it to be cast to other types + valijson::adapters::PicoJsonAdapter adapter(document); + ASSERT_NO_THROW( adapter.getObject() ); + ASSERT_ANY_THROW( adapter.getArray() ); + ASSERT_ANY_THROW( adapter.getBool() ); + ASSERT_ANY_THROW( adapter.getDouble() ); + ASSERT_ANY_THROW( adapter.getString() ); + + // Ensure that the object contains the expected number of members + EXPECT_EQ( numElements, adapter.getObject().size() ); + + // Ensure that the members are returned in the order they were inserted + unsigned int expectedValue = 0; + BOOST_FOREACH( const valijson::adapters::PicoJsonAdapter::ObjectMember member, adapter.getObject() ) { + ASSERT_TRUE( member.second.isNumber() ); + EXPECT_EQ( boost::lexical_cast(expectedValue), member.first ); + EXPECT_EQ( double(expectedValue), member.second.getDouble() ); + expectedValue++; + } + + // Ensure that the correct number of elements were iterated over + EXPECT_EQ( numElements, expectedValue ); +} diff --git a/tests/test_validator.cpp b/tests/test_validator.cpp index 427b9f6..88f3c95 100644 --- a/tests/test_validator.cpp +++ b/tests/test_validator.cpp @@ -7,8 +7,10 @@ #include #include +#include #include #include +#include #include #include #include @@ -111,6 +113,7 @@ protected: { processTestFile(testFile, version); processTestFile(testFile, version); + processTestFile(testFile, version); } void processDraft3TestFile(const std::string &testFile) diff --git a/thirdparty/picojson-1.3.0/.gitignore b/thirdparty/picojson-1.3.0/.gitignore new file mode 100644 index 0000000..815b59f --- /dev/null +++ b/thirdparty/picojson-1.3.0/.gitignore @@ -0,0 +1,4 @@ +*~ +a.out +test-core +test-core-int64 diff --git a/thirdparty/picojson-1.3.0/.gitmodules b/thirdparty/picojson-1.3.0/.gitmodules new file mode 100644 index 0000000..e69de29 diff --git a/thirdparty/picojson-1.3.0/.travis.yml b/thirdparty/picojson-1.3.0/.travis.yml new file mode 100644 index 0000000..e2af2ae --- /dev/null +++ b/thirdparty/picojson-1.3.0/.travis.yml @@ -0,0 +1,8 @@ +language: cpp + +compiler: + - clang + - gcc + +script: + - make test diff --git a/thirdparty/picojson-1.3.0/Changes b/thirdparty/picojson-1.3.0/Changes new file mode 100644 index 0000000..d6017dd --- /dev/null +++ b/thirdparty/picojson-1.3.0/Changes @@ -0,0 +1,25 @@ +Revision history for picojson + +1.3.0 2015-02-25 13:05:00+0900 + - `make check` is now synonym of `make test` (#62) + - operator= is now safe when part of LHS is being assigned, as well as exception-safe (#66) + +1.2.1 2014-12-16 15:33:00+0900 + - bundle the contents of `picotest/` (#61) + +1.2.0 2014-12-15 16:20:00+0900 + - `make install` to install picojson.h (#58) + - two-argument `picojson::parse()` for ease-of-use (#57) + +1.1.1 2014-06-25 10:35:00+0900 + - tweaks to suppress compiler errors / warning (#38 #39) + - clarify the licenses of the files in exmaple/ (#42) + +1.1 2014-06-16 12:57:00+0900 + - added experimental support for int64 type (#34) + - by default, throw std::runtime_error instead of using assert for runtime errors (#33) + - refine compatibility regarding the use of isinf/isnan (#29, #36) + - remove `.get()` (#35) + +1.0 2014-06-05 12:54:00+0900 + - initial release with a version number diff --git a/thirdparty/picojson-1.3.0/LICENSE b/thirdparty/picojson-1.3.0/LICENSE new file mode 100644 index 0000000..72f3553 --- /dev/null +++ b/thirdparty/picojson-1.3.0/LICENSE @@ -0,0 +1,25 @@ +Copyright 2009-2010 Cybozu Labs, Inc. +Copyright 2011-2014 Kazuho Oku +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/thirdparty/picojson-1.3.0/Makefile b/thirdparty/picojson-1.3.0/Makefile new file mode 100644 index 0000000..40f6450 --- /dev/null +++ b/thirdparty/picojson-1.3.0/Makefile @@ -0,0 +1,26 @@ +prefix=/usr/local +includedir=$(prefix)/include + +check: test + +test: test-core test-core-int64 + ./test-core + ./test-core-int64 + +test-core: test.cc picotest/picotest.c picotest/picotest.h + $(CXX) -Wall test.cc picotest/picotest.c -o $@ + +test-core-int64: test.cc picotest/picotest.c picotest/picotest.h + $(CXX) -Wall -DPICOJSON_USE_INT64 test.cc picotest/picotest.c -o $@ + +clean: + rm -f test-core test-core-int64 + +install: + install -d $(DESTDIR)$(includedir) + install -p -m 0644 picojson.h $(DESTDIR)$(includedir) + +uninstall: + rm -f $(DESTDIR)$(includedir)/picojson.h + +.PHONY: test check clean install uninstall diff --git a/thirdparty/picojson-1.3.0/README.mkdn b/thirdparty/picojson-1.3.0/README.mkdn new file mode 100644 index 0000000..7db7fd6 --- /dev/null +++ b/thirdparty/picojson-1.3.0/README.mkdn @@ -0,0 +1,195 @@ +# PicoJSON - a C++ JSON parser / serializer + +Copyright © 2009-2010 Cybozu Labs, Inc. +Copyright © 2011-2015 Kazuho Oku + +Licensed under [2-clause BSD license](http://opensource.org/licenses/BSD-2-Clause) + +## Version + +1.3.0 [![Build Status](https://travis-ci.org/kazuho/picojson.svg?branch=rel/1.3.0)](https://travis-ci.org/kazuho/picojson) + +## Introduction + +PicoJSON is a tiny JSON parser / serializer for C++ with following properties: + +- header-file only +- no external dependencies (only uses standard C++ libraries) +- STL-frendly (arrays are represented by using std::vector, objects are std::map) +- provides both pull interface and streaming (event-based) interface + +## Reading JSON using the pull interface + +There are several ways to use the pull (DOM-like) interface of picojson. + +The easiest way is to use the two-argument `parse` function. + +``` +std::string json = "[ \"hello JSON\" ]"; +picojson::value v; +std::string err = picojson::parse(v, json); +if (! err.empty()) { + std:cerr << err << std::endl; +} +``` + +Four-argument `parse` function accepts a pair of iterators, and returns the end position of the input. + +``` +const char* json = "{\"a\":1}"; +picojson::value v; +std::string err; +const char* json_end = picojson::parse(v, json, json + strlen(json), &err); +if (! err.empty()) { + std::cerr << err << std::endl; +} +``` + +``` +std::istream_iterator input(std::cin); +picojson::value v; +std::string err; +input = picojson::parse(v, input, std::istream_iterator(), &err); +if (! err.empty()) { + std::cerr << err << std::endl; +} +``` + +It is also possible to use the `>>` operator to parse the input, however this interface is not thread-safe. + +``` +picosjon::value v; +std::cin >> v; +std::string err = picojson::get_last_error(); +``` + +## Accessing the values + +Values of a JSON object is represented as instances of picojson::value class. + +
+namespace picojson {
+
+  class value {
+    ...
+
+  public:
+
+    typedef std::vector<value> array;
+    typedef std::map<std::string, value> object;
+
+    value();                               // create a null object
+    explicit value(bool b);                // create a boolean object
+    explicit value(double n);              // create a number object
+    explicit value(const std::string& s);  // create a string object
+    explicit value(const array& a);        // create an array object
+    explicit value(const object& o);       // create an "object"
+
+    bool is<picojson::null>() const;       // check if the object is "null"
+
+    bool is<bool>() const;                 // check if the object is a boolean
+    const bool& get<bool>() const;         // const accessor (usable only if the object is a boolean)
+    bool& get<bool>();                     // non-const accessor (usable only if the object is a boolean)
+
+    bool is<double>() const;               // check if the object is a number
+    const double& get<double>() const;     // const accessor (usable only if the object is a number)
+    double& get<double>();                 // non-const accessor (usable only if the object is a number)
+
+    bool is<std::string>() const;          // check if the object is a string
+    const std::string& get<std::string>() const;
+                                           // const accessor (usable only if the object is a string)
+    std::string& get<std::string>();       // non-const accessor (usable only if the object is a string)
+
+    bool is<array>() const;                // check if the object is an array
+    const array& get<array>() const;       // const accessor (usable only if the object is an array)
+    array& get<array>();                   // non-const accessor (usable only if the object is an array)
+
+    bool is<object>() const;               // check if the object is an "object"
+    const object& get<object>() const;     // const accessor (usable only if the object is an object)
+    object& get<object>();                 // non-const accessor (usable only if the object is an array)
+
+    bool evaluate_as_boolean() const;      // evaluates the object as a boolean
+
+    std::string serialize() const;         // returns the object in JSON representation
+    template void serialize(Iter os) const;
+                                           // serializes the object in JSON representation through an output iterator
+
+    std::string to_str() const;            // returns the object in string (for casual use)
+
+  };
+
+}
+
+ +The code below parses a JSON string and prints the contents of the object. + +
+picojson::value v;
+
+// parse the input
+std::cin >> v;
+std::string err = picojson::get_last_error();
+if (! err.empty()) {
+  std::cerr << err << std::endl;
+  exit(1);
+}
+
+// check if the type of the value is "object"
+if (! v.is<picojson::object>()) {
+  std::cerr << "JSON is not an object" << std::endl;
+  exit(2);
+}
+
+// obtain a const reference to the map, and print the contents
+const picojson::value::object& obj = v.get<picojson::object>();
+for (picojson::value::object::const_iterator i = obj.begin();
+     i != obj.end();
+     ++i) {
+  std::cout << i->first << ': ' << i->second.to_str() << std::endl;
+}
+
+ +Please note that the type check is mandatory; do not forget to check the type of the object by calling is<type>() before accessing the value by calling get<type>(). + +## Reading JSON using the streaming (event-driven) interface + +Please refer to the implementation of picojson::default_parse_context and picojson::null_parse_context. There is also an example (examples/streaming.cc) . + +## Serializing to JSON + +Instances of the picojson::value class can be serialized in three ways, to ostream, to std::string, or to an output iterator. + +
+picojson::value v;
+...
+std::cout << v;
+
+ +
+picojson::value v;
+...
+std::string json = v.serialize();
+
+ +
+picojson::value v;
+...
+v.serialize(std::ostream_iterator(std::cout));
+
+ +## Experimental support for int64_t + +Experimental suport for int64_t becomes available if the code is compiled with preprocessor macro `PICOJSON_USE_INT64`. + +Turning on the feature will cause following changes to picojson: +- new constructor `picojson::value(int64_t)` is defined +- `is()` and `get()` become available +- numerics in JSON within the bounds of int64_t and not using `.` nor `e`/`E` are considered as int64 type + - the values are also avaliable as `double`s as well (i.e. all values which are `.is() == true` are also `.is() == true`) +- int64 values are converted to double once `get()` is called + +Enabling the feature should not cause compatibility problem with code that do not use the feature. + +## Further reading + +Examples can be found in the examples directory, and on the [Wiki](https://github.com/kazuho/picojson/wiki). Please add your favorite examples to the Wiki. diff --git a/thirdparty/picojson-1.3.0/examples/github-issues.cc b/thirdparty/picojson-1.3.0/examples/github-issues.cc new file mode 100644 index 0000000..3e05afd --- /dev/null +++ b/thirdparty/picojson-1.3.0/examples/github-issues.cc @@ -0,0 +1,110 @@ +/* + * Copyright 2009-2010 Cybozu Labs, Inc. + * Copyright 2011-2014 Kazuho Oku + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include +#include "../picojson.h" + +typedef struct { + char* data; // response data from server + size_t size; // response size of data +} MEMFILE; + +MEMFILE* +memfopen() { + MEMFILE* mf = (MEMFILE*) malloc(sizeof(MEMFILE)); + mf->data = NULL; + mf->size = 0; + return mf; +} + +void +memfclose(MEMFILE* mf) { + if (mf->data) free(mf->data); + free(mf); +} + +size_t +memfwrite(char* ptr, size_t size, size_t nmemb, void* stream) { + MEMFILE* mf = (MEMFILE*) stream; + int block = size * nmemb; + if (!mf->data) + mf->data = (char*) malloc(block); + else + mf->data = (char*) realloc(mf->data, mf->size + block); + if (mf->data) { + memcpy(mf->data + mf->size, ptr, block); + mf->size += block; + } + return block; +} + +char* +memfstrdup(MEMFILE* mf) { + char* buf = (char*)malloc(mf->size + 1); + memcpy(buf, mf->data, mf->size); + buf[mf->size] = 0; + return buf; +} + +using namespace std; +using namespace picojson; + +int +main(int argc, char* argv[]) { + char error[256]; + + MEMFILE* mf = memfopen(); + CURL* curl = curl_easy_init(); + curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repos/kazuho/picojson/issues"); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl"); + curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, mf); + if (curl_easy_perform(curl) != CURLE_OK) { + cerr << error << endl; + } else { + value v; + string err; + parse(v, mf->data, mf->data + mf->size, &err); + if (err.empty()) { + array arr = v.get(); + array::iterator it; + for (it = arr.begin(); it != arr.end(); it++) { + object obj = it->get(); + cout << "#" << obj["number"].to_str() << ": " << obj["title"].to_str() << endl; + cout << " " << obj["html_url"].to_str() << endl << endl; + } + } else { + cerr << err << endl; + } + } + curl_easy_cleanup(curl); + memfclose(mf); + + return 0; +} diff --git a/thirdparty/picojson-1.3.0/examples/iostream.cc b/thirdparty/picojson-1.3.0/examples/iostream.cc new file mode 100644 index 0000000..30cc93f --- /dev/null +++ b/thirdparty/picojson-1.3.0/examples/iostream.cc @@ -0,0 +1,70 @@ +/* + * Copyright 2009-2010 Cybozu Labs, Inc. + * Copyright 2011-2014 Kazuho Oku + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include "../picojson.h" + +int main(void) +{ + picojson::value v; + + // read json value from stream + std::cin >> v; + if (std::cin.fail()) { + std::cerr << picojson::get_last_error() << std::endl; + return 1; + } + + // dump json object + std::cout << "---- dump input ----" << std::endl; + std::cout << v << std::endl; + + // accessors + std::cout << "---- analyzing input ----" << std::endl; + if (v.is()) { + std::cout << "input is null" << std::endl; + } else if (v.is()) { + std::cout << "input is " << (v.get() ? "true" : "false") << std::endl; + } else if (v.is()) { + std::cout << "input is " << v.get() << std::endl; + } else if (v.is()) { + std::cout << "input is " << v.get() << std::endl; + } else if (v.is()) { + std::cout << "input is an array" << std::endl; + const picojson::array& a = v.get(); + for (picojson::array::const_iterator i = a.begin(); i != a.end(); ++i) { + std::cout << " " << *i << std::endl; + } + } else if (v.is()) { + std::cout << "input is an object" << std::endl; + const picojson::object& o = v.get(); + for (picojson::object::const_iterator i = o.begin(); i != o.end(); ++i) { + std::cout << i->first << " " << i->second << std::endl; + } + } + + return 0; +} diff --git a/thirdparty/picojson-1.3.0/examples/streaming.cc b/thirdparty/picojson-1.3.0/examples/streaming.cc new file mode 100644 index 0000000..8b1225f --- /dev/null +++ b/thirdparty/picojson-1.3.0/examples/streaming.cc @@ -0,0 +1,76 @@ +/* + * Copyright 2009-2010 Cybozu Labs, Inc. + * Copyright 2011-2014 Kazuho Oku + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include +#include +#include "../picojson.h" + +// this example reads a array of hashes (each item representing a 2D point), +// and prints the x and y values to stdout + +namespace { + + class root_context : public picojson::deny_parse_context { + public: + bool parse_array_start() { + return true; // only allow array as root + } + template bool parse_array_item(picojson::input& in, size_t) { + picojson::value item; + // parse the array item + picojson::default_parse_context ctx(&item); + if (! picojson::_parse(ctx, in)) { + return false; + } + // assert that the array item is a hash + if (! item.is()) { + return false; + } + // print x and y + std::cout << item.get("x") << ',' << item.get("y").to_str() + << std::endl; + return true; + } + }; + +} + +int main(void) +{ + root_context ctx; + std::string err; + + picojson::_parse(ctx, std::istream_iterator(std::cin), + std::istream_iterator(), &err); + + if (! err.empty()) { + std::cerr << err << std::endl; + return 1; + } + + return 0; +} diff --git a/thirdparty/picojson-1.3.0/picojson.h b/thirdparty/picojson-1.3.0/picojson.h new file mode 100644 index 0000000..48bb64e --- /dev/null +++ b/thirdparty/picojson-1.3.0/picojson.h @@ -0,0 +1,1010 @@ +/* + * Copyright 2009-2010 Cybozu Labs, Inc. + * Copyright 2011-2014 Kazuho Oku + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef picojson_h +#define picojson_h + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// for isnan/isinf +#if __cplusplus>=201103L +# include +#else +extern "C" { +# ifdef _MSC_VER +# include +# elif defined(__INTEL_COMPILER) +# include +# else +# include +# endif +} +#endif + +// experimental support for int64_t (see README.mkdn for detail) +#ifdef PICOJSON_USE_INT64 +# define __STDC_FORMAT_MACROS +# include +# include +#endif + +// to disable the use of localeconv(3), set PICOJSON_USE_LOCALE to 0 +#ifndef PICOJSON_USE_LOCALE +# define PICOJSON_USE_LOCALE 1 +#endif +#if PICOJSON_USE_LOCALE +extern "C" { +# include +} +#endif + +#ifndef PICOJSON_ASSERT +# define PICOJSON_ASSERT(e) do { if (! (e)) throw std::runtime_error(#e); } while (0) +#endif + +#ifdef _MSC_VER + #define SNPRINTF _snprintf_s + #pragma warning(push) + #pragma warning(disable : 4244) // conversion from int to char + #pragma warning(disable : 4127) // conditional expression is constant + #pragma warning(disable : 4702) // unreachable code +#else + #define SNPRINTF snprintf +#endif + +namespace picojson { + + enum { + null_type, + boolean_type, + number_type, + string_type, + array_type, + object_type +#ifdef PICOJSON_USE_INT64 + , int64_type +#endif + }; + + enum { + INDENT_WIDTH = 2 + }; + + struct null {}; + + class value { + public: + typedef std::vector array; + typedef std::map object; + union _storage { + bool boolean_; + double number_; +#ifdef PICOJSON_USE_INT64 + int64_t int64_; +#endif + std::string* string_; + array* array_; + object* object_; + }; + protected: + int type_; + _storage u_; + public: + value(); + value(int type, bool); + explicit value(bool b); +#ifdef PICOJSON_USE_INT64 + explicit value(int64_t i); +#endif + explicit value(double n); + explicit value(const std::string& s); + explicit value(const array& a); + explicit value(const object& o); + explicit value(const char* s); + value(const char* s, size_t len); + ~value(); + value(const value& x); + value& operator=(const value& x); + void swap(value& x); + template bool is() const; + template const T& get() const; + template T& get(); + bool evaluate_as_boolean() const; + const value& get(size_t idx) const; + const value& get(const std::string& key) const; + value& get(size_t idx); + value& get(const std::string& key); + + bool contains(size_t idx) const; + bool contains(const std::string& key) const; + std::string to_str() const; + template void serialize(Iter os, bool prettify = false) const; + std::string serialize(bool prettify = false) const; + private: + template value(const T*); // intentionally defined to block implicit conversion of pointer to bool + template static void _indent(Iter os, int indent); + template void _serialize(Iter os, int indent) const; + std::string _serialize(int indent) const; + }; + + typedef value::array array; + typedef value::object object; + + inline value::value() : type_(null_type) {} + + inline value::value(int type, bool) : type_(type) { + switch (type) { +#define INIT(p, v) case p##type: u_.p = v; break + INIT(boolean_, false); + INIT(number_, 0.0); +#ifdef PICOJSON_USE_INT64 + INIT(int64_, 0); +#endif + INIT(string_, new std::string()); + INIT(array_, new array()); + INIT(object_, new object()); +#undef INIT + default: break; + } + } + + inline value::value(bool b) : type_(boolean_type) { + u_.boolean_ = b; + } + +#ifdef PICOJSON_USE_INT64 + inline value::value(int64_t i) : type_(int64_type) { + u_.int64_ = i; + } +#endif + + inline value::value(double n) : type_(number_type) { + if ( +#ifdef _MSC_VER + ! _finite(n) +#elif __cplusplus>=201103L || !(defined(isnan) && defined(isinf)) + std::isnan(n) || std::isinf(n) +#else + isnan(n) || isinf(n) +#endif + ) { + throw std::overflow_error(""); + } + u_.number_ = n; + } + + inline value::value(const std::string& s) : type_(string_type) { + u_.string_ = new std::string(s); + } + + inline value::value(const array& a) : type_(array_type) { + u_.array_ = new array(a); + } + + inline value::value(const object& o) : type_(object_type) { + u_.object_ = new object(o); + } + + inline value::value(const char* s) : type_(string_type) { + u_.string_ = new std::string(s); + } + + inline value::value(const char* s, size_t len) : type_(string_type) { + u_.string_ = new std::string(s, len); + } + + inline value::~value() { + switch (type_) { +#define DEINIT(p) case p##type: delete u_.p; break + DEINIT(string_); + DEINIT(array_); + DEINIT(object_); +#undef DEINIT + default: break; + } + } + + inline value::value(const value& x) : type_(x.type_) { + switch (type_) { +#define INIT(p, v) case p##type: u_.p = v; break + INIT(string_, new std::string(*x.u_.string_)); + INIT(array_, new array(*x.u_.array_)); + INIT(object_, new object(*x.u_.object_)); +#undef INIT + default: + u_ = x.u_; + break; + } + } + + inline value& value::operator=(const value& x) { + if (this != &x) { + value t(x); + swap(t); + } + return *this; + } + + inline void value::swap(value& x) { + std::swap(type_, x.type_); + std::swap(u_, x.u_); + } + +#define IS(ctype, jtype) \ + template <> inline bool value::is() const { \ + return type_ == jtype##_type; \ + } + IS(null, null) + IS(bool, boolean) +#ifdef PICOJSON_USE_INT64 + IS(int64_t, int64) +#endif + IS(std::string, string) + IS(array, array) + IS(object, object) +#undef IS + template <> inline bool value::is() const { + return type_ == number_type +#ifdef PICOJSON_USE_INT64 + || type_ == int64_type +#endif + ; + } + +#define GET(ctype, var) \ + template <> inline const ctype& value::get() const { \ + PICOJSON_ASSERT("type mismatch! call is() before get()" \ + && is()); \ + return var; \ + } \ + template <> inline ctype& value::get() { \ + PICOJSON_ASSERT("type mismatch! call is() before get()" \ + && is()); \ + return var; \ + } + GET(bool, u_.boolean_) + GET(std::string, *u_.string_) + GET(array, *u_.array_) + GET(object, *u_.object_) +#ifdef PICOJSON_USE_INT64 + GET(double, (type_ == int64_type && (const_cast(this)->type_ = number_type, const_cast(this)->u_.number_ = u_.int64_), u_.number_)) + GET(int64_t, u_.int64_) +#else + GET(double, u_.number_) +#endif +#undef GET + + inline bool value::evaluate_as_boolean() const { + switch (type_) { + case null_type: + return false; + case boolean_type: + return u_.boolean_; + case number_type: + return u_.number_ != 0; + case string_type: + return ! u_.string_->empty(); + default: + return true; + } + } + + inline const value& value::get(size_t idx) const { + static value s_null; + PICOJSON_ASSERT(is()); + return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; + } + + inline value& value::get(size_t idx) { + static value s_null; + PICOJSON_ASSERT(is()); + return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; + } + + inline const value& value::get(const std::string& key) const { + static value s_null; + PICOJSON_ASSERT(is()); + object::const_iterator i = u_.object_->find(key); + return i != u_.object_->end() ? i->second : s_null; + } + + inline value& value::get(const std::string& key) { + static value s_null; + PICOJSON_ASSERT(is()); + object::iterator i = u_.object_->find(key); + return i != u_.object_->end() ? i->second : s_null; + } + + inline bool value::contains(size_t idx) const { + PICOJSON_ASSERT(is()); + return idx < u_.array_->size(); + } + + inline bool value::contains(const std::string& key) const { + PICOJSON_ASSERT(is()); + object::const_iterator i = u_.object_->find(key); + return i != u_.object_->end(); + } + + inline std::string value::to_str() const { + switch (type_) { + case null_type: return "null"; + case boolean_type: return u_.boolean_ ? "true" : "false"; +#ifdef PICOJSON_USE_INT64 + case int64_type: { + char buf[sizeof("-9223372036854775808")]; + SNPRINTF(buf, sizeof(buf), "%" PRId64, u_.int64_); + return buf; + } +#endif + case number_type: { + char buf[256]; + double tmp; + SNPRINTF(buf, sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ? "%.f" : "%.17g", u_.number_); +#if PICOJSON_USE_LOCALE + char *decimal_point = localeconv()->decimal_point; + if (strcmp(decimal_point, ".") != 0) { + size_t decimal_point_len = strlen(decimal_point); + for (char *p = buf; *p != '\0'; ++p) { + if (strncmp(p, decimal_point, decimal_point_len) == 0) { + return std::string(buf, p) + "." + (p + decimal_point_len); + } + } + } +#endif + return buf; + } + case string_type: return *u_.string_; + case array_type: return "array"; + case object_type: return "object"; + default: PICOJSON_ASSERT(0); +#ifdef _MSC_VER + __assume(0); +#endif + } + return std::string(); + } + + template void copy(const std::string& s, Iter oi) { + std::copy(s.begin(), s.end(), oi); + } + + template void serialize_str(const std::string& s, Iter oi) { + *oi++ = '"'; + for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { + switch (*i) { +#define MAP(val, sym) case val: copy(sym, oi); break + MAP('"', "\\\""); + MAP('\\', "\\\\"); + MAP('/', "\\/"); + MAP('\b', "\\b"); + MAP('\f', "\\f"); + MAP('\n', "\\n"); + MAP('\r', "\\r"); + MAP('\t', "\\t"); +#undef MAP + default: + if (static_cast(*i) < 0x20 || *i == 0x7f) { + char buf[7]; + SNPRINTF(buf, sizeof(buf), "\\u%04x", *i & 0xff); + copy(buf, buf + 6, oi); + } else { + *oi++ = *i; + } + break; + } + } + *oi++ = '"'; + } + + template void value::serialize(Iter oi, bool prettify) const { + return _serialize(oi, prettify ? 0 : -1); + } + + inline std::string value::serialize(bool prettify) const { + return _serialize(prettify ? 0 : -1); + } + + template void value::_indent(Iter oi, int indent) { + *oi++ = '\n'; + for (int i = 0; i < indent * INDENT_WIDTH; ++i) { + *oi++ = ' '; + } + } + + template void value::_serialize(Iter oi, int indent) const { + switch (type_) { + case string_type: + serialize_str(*u_.string_, oi); + break; + case array_type: { + *oi++ = '['; + if (indent != -1) { + ++indent; + } + for (array::const_iterator i = u_.array_->begin(); + i != u_.array_->end(); + ++i) { + if (i != u_.array_->begin()) { + *oi++ = ','; + } + if (indent != -1) { + _indent(oi, indent); + } + i->_serialize(oi, indent); + } + if (indent != -1) { + --indent; + if (! u_.array_->empty()) { + _indent(oi, indent); + } + } + *oi++ = ']'; + break; + } + case object_type: { + *oi++ = '{'; + if (indent != -1) { + ++indent; + } + for (object::const_iterator i = u_.object_->begin(); + i != u_.object_->end(); + ++i) { + if (i != u_.object_->begin()) { + *oi++ = ','; + } + if (indent != -1) { + _indent(oi, indent); + } + serialize_str(i->first, oi); + *oi++ = ':'; + if (indent != -1) { + *oi++ = ' '; + } + i->second._serialize(oi, indent); + } + if (indent != -1) { + --indent; + if (! u_.object_->empty()) { + _indent(oi, indent); + } + } + *oi++ = '}'; + break; + } + default: + copy(to_str(), oi); + break; + } + if (indent == 0) { + *oi++ = '\n'; + } + } + + inline std::string value::_serialize(int indent) const { + std::string s; + _serialize(std::back_inserter(s), indent); + return s; + } + + template class input { + protected: + Iter cur_, end_; + int last_ch_; + bool ungot_; + int line_; + public: + input(const Iter& first, const Iter& last) : cur_(first), end_(last), last_ch_(-1), ungot_(false), line_(1) {} + int getc() { + if (ungot_) { + ungot_ = false; + return last_ch_; + } + if (cur_ == end_) { + last_ch_ = -1; + return -1; + } + if (last_ch_ == '\n') { + line_++; + } + last_ch_ = *cur_ & 0xff; + ++cur_; + return last_ch_; + } + void ungetc() { + if (last_ch_ != -1) { + PICOJSON_ASSERT(! ungot_); + ungot_ = true; + } + } + Iter cur() const { return cur_; } + int line() const { return line_; } + void skip_ws() { + while (1) { + int ch = getc(); + if (! (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) { + ungetc(); + break; + } + } + } + bool expect(int expect) { + skip_ws(); + if (getc() != expect) { + ungetc(); + return false; + } + return true; + } + bool match(const std::string& pattern) { + for (std::string::const_iterator pi(pattern.begin()); + pi != pattern.end(); + ++pi) { + if (getc() != *pi) { + ungetc(); + return false; + } + } + return true; + } + }; + + template inline int _parse_quadhex(input &in) { + int uni_ch = 0, hex; + for (int i = 0; i < 4; i++) { + if ((hex = in.getc()) == -1) { + return -1; + } + if ('0' <= hex && hex <= '9') { + hex -= '0'; + } else if ('A' <= hex && hex <= 'F') { + hex -= 'A' - 0xa; + } else if ('a' <= hex && hex <= 'f') { + hex -= 'a' - 0xa; + } else { + in.ungetc(); + return -1; + } + uni_ch = uni_ch * 16 + hex; + } + return uni_ch; + } + + template inline bool _parse_codepoint(String& out, input& in) { + int uni_ch; + if ((uni_ch = _parse_quadhex(in)) == -1) { + return false; + } + if (0xd800 <= uni_ch && uni_ch <= 0xdfff) { + if (0xdc00 <= uni_ch) { + // a second 16-bit of a surrogate pair appeared + return false; + } + // first 16-bit of surrogate pair, get the next one + if (in.getc() != '\\' || in.getc() != 'u') { + in.ungetc(); + return false; + } + int second = _parse_quadhex(in); + if (! (0xdc00 <= second && second <= 0xdfff)) { + return false; + } + uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff); + uni_ch += 0x10000; + } + if (uni_ch < 0x80) { + out.push_back(uni_ch); + } else { + if (uni_ch < 0x800) { + out.push_back(0xc0 | (uni_ch >> 6)); + } else { + if (uni_ch < 0x10000) { + out.push_back(0xe0 | (uni_ch >> 12)); + } else { + out.push_back(0xf0 | (uni_ch >> 18)); + out.push_back(0x80 | ((uni_ch >> 12) & 0x3f)); + } + out.push_back(0x80 | ((uni_ch >> 6) & 0x3f)); + } + out.push_back(0x80 | (uni_ch & 0x3f)); + } + return true; + } + + template inline bool _parse_string(String& out, input& in) { + while (1) { + int ch = in.getc(); + if (ch < ' ') { + in.ungetc(); + return false; + } else if (ch == '"') { + return true; + } else if (ch == '\\') { + if ((ch = in.getc()) == -1) { + return false; + } + switch (ch) { +#define MAP(sym, val) case sym: out.push_back(val); break + MAP('"', '\"'); + MAP('\\', '\\'); + MAP('/', '/'); + MAP('b', '\b'); + MAP('f', '\f'); + MAP('n', '\n'); + MAP('r', '\r'); + MAP('t', '\t'); +#undef MAP + case 'u': + if (! _parse_codepoint(out, in)) { + return false; + } + break; + default: + return false; + } + } else { + out.push_back(ch); + } + } + return false; + } + + template inline bool _parse_array(Context& ctx, input& in) { + if (! ctx.parse_array_start()) { + return false; + } + size_t idx = 0; + if (in.expect(']')) { + return ctx.parse_array_stop(idx); + } + do { + if (! ctx.parse_array_item(in, idx)) { + return false; + } + idx++; + } while (in.expect(',')); + return in.expect(']') && ctx.parse_array_stop(idx); + } + + template inline bool _parse_object(Context& ctx, input& in) { + if (! ctx.parse_object_start()) { + return false; + } + if (in.expect('}')) { + return true; + } + do { + std::string key; + if (! in.expect('"') + || ! _parse_string(key, in) + || ! in.expect(':')) { + return false; + } + if (! ctx.parse_object_item(in, key)) { + return false; + } + } while (in.expect(',')); + return in.expect('}'); + } + + template inline std::string _parse_number(input& in) { + std::string num_str; + while (1) { + int ch = in.getc(); + if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' + || ch == 'e' || ch == 'E') { + num_str.push_back(ch); + } else if (ch == '.') { +#if PICOJSON_USE_LOCALE + num_str += localeconv()->decimal_point; +#else + num_str.push_back('.'); +#endif + } else { + in.ungetc(); + break; + } + } + return num_str; + } + + template inline bool _parse(Context& ctx, input& in) { + in.skip_ws(); + int ch = in.getc(); + switch (ch) { +#define IS(ch, text, op) case ch: \ + if (in.match(text) && op) { \ + return true; \ + } else { \ + return false; \ + } + IS('n', "ull", ctx.set_null()); + IS('f', "alse", ctx.set_bool(false)); + IS('t', "rue", ctx.set_bool(true)); +#undef IS + case '"': + return ctx.parse_string(in); + case '[': + return _parse_array(ctx, in); + case '{': + return _parse_object(ctx, in); + default: + if (('0' <= ch && ch <= '9') || ch == '-') { + double f; + char *endp; + in.ungetc(); + std::string num_str = _parse_number(in); + if (num_str.empty()) { + return false; + } +#ifdef PICOJSON_USE_INT64 + { + errno = 0; + intmax_t ival = strtoimax(num_str.c_str(), &endp, 10); + if (errno == 0 + && std::numeric_limits::min() <= ival + && ival <= std::numeric_limits::max() + && endp == num_str.c_str() + num_str.size()) { + ctx.set_int64(ival); + return true; + } + } +#endif + f = strtod(num_str.c_str(), &endp); + if (endp == num_str.c_str() + num_str.size()) { + ctx.set_number(f); + return true; + } + return false; + } + break; + } + in.ungetc(); + return false; + } + + class deny_parse_context { + public: + bool set_null() { return false; } + bool set_bool(bool) { return false; } +#ifdef PICOJSON_USE_INT64 + bool set_int64(int64_t) { return false; } +#endif + bool set_number(double) { return false; } + template bool parse_string(input&) { return false; } + bool parse_array_start() { return false; } + template bool parse_array_item(input&, size_t) { + return false; + } + bool parse_array_stop(size_t) { return false; } + bool parse_object_start() { return false; } + template bool parse_object_item(input&, const std::string&) { + return false; + } + }; + + class default_parse_context { + protected: + value* out_; + public: + default_parse_context(value* out) : out_(out) {} + bool set_null() { + *out_ = value(); + return true; + } + bool set_bool(bool b) { + *out_ = value(b); + return true; + } +#ifdef PICOJSON_USE_INT64 + bool set_int64(int64_t i) { + *out_ = value(i); + return true; + } +#endif + bool set_number(double f) { + *out_ = value(f); + return true; + } + template bool parse_string(input& in) { + *out_ = value(string_type, false); + return _parse_string(out_->get(), in); + } + bool parse_array_start() { + *out_ = value(array_type, false); + return true; + } + template bool parse_array_item(input& in, size_t) { + array& a = out_->get(); + a.push_back(value()); + default_parse_context ctx(&a.back()); + return _parse(ctx, in); + } + bool parse_array_stop(size_t) { return true; } + bool parse_object_start() { + *out_ = value(object_type, false); + return true; + } + template bool parse_object_item(input& in, const std::string& key) { + object& o = out_->get(); + default_parse_context ctx(&o[key]); + return _parse(ctx, in); + } + private: + default_parse_context(const default_parse_context&); + default_parse_context& operator=(const default_parse_context&); + }; + + class null_parse_context { + public: + struct dummy_str { + void push_back(int) {} + }; + public: + null_parse_context() {} + bool set_null() { return true; } + bool set_bool(bool) { return true; } +#ifdef PICOJSON_USE_INT64 + bool set_int64(int64_t) { return true; } +#endif + bool set_number(double) { return true; } + template bool parse_string(input& in) { + dummy_str s; + return _parse_string(s, in); + } + bool parse_array_start() { return true; } + template bool parse_array_item(input& in, size_t) { + return _parse(*this, in); + } + bool parse_array_stop(size_t) { return true; } + bool parse_object_start() { return true; } + template bool parse_object_item(input& in, const std::string&) { + return _parse(*this, in); + } + private: + null_parse_context(const null_parse_context&); + null_parse_context& operator=(const null_parse_context&); + }; + + // obsolete, use the version below + template inline std::string parse(value& out, Iter& pos, const Iter& last) { + std::string err; + pos = parse(out, pos, last, &err); + return err; + } + + template inline Iter _parse(Context& ctx, const Iter& first, const Iter& last, std::string* err) { + input in(first, last); + if (! _parse(ctx, in) && err != NULL) { + char buf[64]; + SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line()); + *err = buf; + while (1) { + int ch = in.getc(); + if (ch == -1 || ch == '\n') { + break; + } else if (ch >= ' ') { + err->push_back(ch); + } + } + } + return in.cur(); + } + + template inline Iter parse(value& out, const Iter& first, const Iter& last, std::string* err) { + default_parse_context ctx(&out); + return _parse(ctx, first, last, err); + } + + inline std::string parse(value& out, const std::string& s) { + std::string err; + parse(out, s.begin(), s.end(), &err); + return err; + } + + inline std::string parse(value& out, std::istream& is) { + std::string err; + parse(out, std::istreambuf_iterator(is.rdbuf()), + std::istreambuf_iterator(), &err); + return err; + } + + template struct last_error_t { + static std::string s; + }; + template std::string last_error_t::s; + + inline void set_last_error(const std::string& s) { + last_error_t::s = s; + } + + inline const std::string& get_last_error() { + return last_error_t::s; + } + + inline bool operator==(const value& x, const value& y) { + if (x.is()) + return y.is(); +#define PICOJSON_CMP(type) \ + if (x.is()) \ + return y.is() && x.get() == y.get() + PICOJSON_CMP(bool); + PICOJSON_CMP(double); + PICOJSON_CMP(std::string); + PICOJSON_CMP(array); + PICOJSON_CMP(object); +#undef PICOJSON_CMP + PICOJSON_ASSERT(0); +#ifdef _MSC_VER + __assume(0); +#endif + return false; + } + + inline bool operator!=(const value& x, const value& y) { + return ! (x == y); + } +} + +namespace std { + template<> inline void swap(picojson::value& x, picojson::value& y) + { + x.swap(y); + } +} + +inline std::istream& operator>>(std::istream& is, picojson::value& x) +{ + picojson::set_last_error(std::string()); + std::string err = picojson::parse(x, is); + if (! err.empty()) { + picojson::set_last_error(err); + is.setstate(std::ios::failbit); + } + return is; +} + +inline std::ostream& operator<<(std::ostream& os, const picojson::value& x) +{ + x.serialize(std::ostream_iterator(os)); + return os; +} +#ifdef _MSC_VER + #pragma warning(pop) +#endif + +#endif diff --git a/thirdparty/picojson-1.3.0/picotest/picotest.c b/thirdparty/picojson-1.3.0/picotest/picotest.c new file mode 100644 index 0000000..d1fe699 --- /dev/null +++ b/thirdparty/picojson-1.3.0/picotest/picotest.c @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2014 DeNA Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include +#include +#include +#include "picotest.h" + +struct test_t { + int num_tests; + int failed; +}; +struct test_t main_tests, *cur_tests = &main_tests; +static int test_level = 0; + +static void indent(void) +{ + int i; + for (i = 0; i != test_level; ++i) + printf(" "); +} + +__attribute__((format (printf, 1, 2))) +void note(const char *fmt, ...) +{ + va_list arg; + + indent(); + printf("# "); + + va_start(arg, fmt); + vprintf(fmt, arg); + va_end(arg); + + printf("\n"); +} + +__attribute__((format (printf, 2, 3))) +void _ok(int cond, const char *fmt, ...) +{ + va_list arg; + + if (! cond) + cur_tests->failed = 1; + indent(); + + printf("%s %d - ", cond ? "ok" : "not ok", ++cur_tests->num_tests); + va_start(arg, fmt); + vprintf(fmt, arg); + va_end(arg); + + printf("\n"); +} + +int done_testing(void) +{ + indent(); + printf("1..%d\n", cur_tests->num_tests); + return cur_tests->failed; +} + +void subtest(const char *name, void (*cb)(void)) +{ + struct test_t test = {}, *parent_tests; + + parent_tests = cur_tests; + cur_tests = &test; + ++test_level; + + note("Subtest: %s", name); + + cb(); + + done_testing(); + + --test_level; + cur_tests = parent_tests; + if (test.failed) + cur_tests->failed = 1; + _ok(! test.failed, "%s", name); +} diff --git a/thirdparty/picojson-1.3.0/picotest/picotest.h b/thirdparty/picojson-1.3.0/picotest/picotest.h new file mode 100644 index 0000000..22ab02f --- /dev/null +++ b/thirdparty/picojson-1.3.0/picotest/picotest.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014 DeNA Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#ifndef picotest_h +#define picotest_h + +#ifdef __cplusplus +extern "C" { +#endif + +void note(const char *fmt, ...) __attribute__((format (printf, 1, 2))); +void _ok(int cond, const char *fmt, ...) __attribute__((format (printf, 2, 3))); +#define ok(cond) _ok(cond, "%s %d", __FILE__, __LINE__) +int done_testing(void); +void subtest(const char *name, void (*cb)(void)); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/thirdparty/picojson-1.3.0/test.cc b/thirdparty/picojson-1.3.0/test.cc new file mode 100644 index 0000000..ed9656d --- /dev/null +++ b/thirdparty/picojson-1.3.0/test.cc @@ -0,0 +1,312 @@ +/* + * Copyright 2009-2010 Cybozu Labs, Inc. + * Copyright 2011-2014 Kazuho Oku, Yasuhiro Matsumoto, Shigeo Mitsunari + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include "picojson.h" +#include "picotest/picotest.h" + +#ifdef _MSC_VER + #pragma warning(disable : 4127) // conditional expression is constant +#endif + +using namespace std; + +#define is(x, y, name) _ok((x) == (y), name) + +#include +#include +#include +#include + +int main(void) +{ +#if PICOJSON_USE_LOCALE + setlocale(LC_ALL, ""); +#endif + + // constructors +#define TEST(expr, expected) \ + is(picojson::value expr .serialize(), string(expected), "picojson::value" #expr) + + TEST( (true), "true"); + TEST( (false), "false"); + TEST( (42.0), "42"); + TEST( (string("hello")), "\"hello\""); + TEST( ("hello"), "\"hello\""); + TEST( ("hello", 4), "\"hell\""); + + { + double a = 1; + for (int i = 0; i < 1024; i++) { + picojson::value vi(a); + std::stringstream ss; + ss << vi; + picojson::value vo; + ss >> vo; + double b = vo.get(); + if ((i < 53 && a != b) || fabs(a - b) / b > 1e-8) { + printf("ng i=%d a=%.18e b=%.18e\n", i, a, b); + } + a *= 2; + } + } + +#undef TEST + +#define TEST(in, type, cmp, serialize_test) { \ + picojson::value v; \ + const char* s = in; \ + string err = picojson::parse(v, s, s + strlen(s)); \ + _ok(err.empty(), in " no error"); \ + _ok(v.is(), in " check type"); \ + is(v.get(), static_cast(cmp), in " correct output"); \ + is(*s, '\0', in " read to eof"); \ + if (serialize_test) { \ + is(v.serialize(), string(in), in " serialize"); \ + } \ + } + TEST("false", bool, false, true); + TEST("true", bool, true, true); + TEST("90.5", double, 90.5, false); + TEST("1.7976931348623157e+308", double, DBL_MAX, false); + TEST("\"hello\"", string, string("hello"), true); + TEST("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"", string, string("\"\\/\b\f\n\r\t"), + true); + TEST("\"\\u0061\\u30af\\u30ea\\u30b9\"", string, + string("a\xe3\x82\xaf\xe3\x83\xaa\xe3\x82\xb9"), false); + TEST("\"\\ud840\\udc0b\"", string, string("\xf0\xa0\x80\x8b"), false); +#ifdef PICOJSON_USE_INT64 + TEST("0", int64_t, 0, true); + TEST("-9223372036854775808", int64_t, std::numeric_limits::min(), true); + TEST("9223372036854775807", int64_t, std::numeric_limits::max(), true); +#endif +#undef TEST + +#define TEST(type, expr) { \ + picojson::value v; \ + const char *s = expr; \ + string err = picojson::parse(v, s, s + strlen(s)); \ + _ok(err.empty(), "empty " #type " no error"); \ + _ok(v.is(), "empty " #type " check type"); \ + _ok(v.get().empty(), "check " #type " array size"); \ + } + TEST(array, "[]"); + TEST(object, "{}"); +#undef TEST + + { + picojson::value v; + const char *s = "[1,true,\"hello\"]"; + string err = picojson::parse(v, s, s + strlen(s)); + _ok(err.empty(), "array no error"); + _ok(v.is(), "array check type"); + is(v.get().size(), size_t(3), "check array size"); + _ok(v.contains(0), "check contains array[0]"); + _ok(v.get(0).is(), "check array[0] type"); + is(v.get(0).get(), 1.0, "check array[0] value"); + _ok(v.contains(1), "check contains array[1]"); + _ok(v.get(1).is(), "check array[1] type"); + _ok(v.get(1).get(), "check array[1] value"); + _ok(v.contains(2), "check contains array[2]"); + _ok(v.get(2).is(), "check array[2] type"); + is(v.get(2).get(), string("hello"), "check array[2] value"); + _ok(!v.contains(3), "check not contains array[3]"); + } + + { + picojson::value v; + const char *s = "{ \"a\": true }"; + string err = picojson::parse(v, s, s + strlen(s)); + _ok(err.empty(), "object no error"); + _ok(v.is(), "object check type"); + is(v.get().size(), size_t(1), "check object size"); + _ok(v.contains("a"), "check contains property"); + _ok(v.get("a").is(), "check bool property exists"); + is(v.get("a").get(), true, "check bool property value"); + is(v.serialize(), string("{\"a\":true}"), "serialize object"); + _ok(!v.contains("z"), "check not contains property"); + } + +#define TEST(json, msg) do { \ + picojson::value v; \ + const char *s = json; \ + string err = picojson::parse(v, s, s + strlen(s)); \ + is(err, string("syntax error at line " msg), msg); \ + } while (0) + TEST("falsoa", "1 near: oa"); + TEST("{]", "1 near: ]"); + TEST("\n\bbell", "2 near: bell"); + TEST("\"abc\nd\"", "1 near: "); +#undef TEST + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + s = "{ \"d\": 2.0, \"b\": true, \"a\": [1,2,\"three\"] }"; + err = picojson::parse(v2, s, s + strlen(s)); + _ok((v1 == v2), "check == operator in deep comparison"); + } + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + s = "{ \"d\": 2.0, \"a\": [1,\"three\"], \"b\": true }"; + err = picojson::parse(v2, s, s + strlen(s)); + _ok((v1 != v2), "check != operator for array in deep comparison"); + } + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + s = "{ \"d\": 2.0, \"a\": [1,2,\"three\"], \"b\": false }"; + err = picojson::parse(v2, s, s + strlen(s)); + _ok((v1 != v2), "check != operator for object in deep comparison"); + } + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + picojson::object& o = v1.get(); + o.erase("b"); + picojson::array& a = o["a"].get(); + picojson::array::iterator i; + i = std::remove(a.begin(), a.end(), picojson::value(std::string("three"))); + a.erase(i, a.end()); + s = "{ \"a\": [1,2], \"d\": 2 }"; + err = picojson::parse(v2, s, s + strlen(s)); + _ok((v1 == v2), "check erase()"); + } + + _ok(picojson::value(3.0).serialize() == "3", + "integral number should be serialized as a integer"); + + { + const char* s = "{ \"a\": [1,2], \"d\": 2 }"; + picojson::null_parse_context ctx; + string err; + picojson::_parse(ctx, s, s + strlen(s), &err); + _ok(err.empty(), "null_parse_context"); + } + + { + picojson::value v1, v2; + v1 = picojson::value(true); + swap(v1, v2); + _ok(v1.is(), "swap (null)"); + _ok(v2.get() == true, "swap (bool)"); + + v1 = picojson::value("a"); + v2 = picojson::value(1.0); + swap(v1, v2); + _ok(v1.get() == 1.0, "swap (dobule)"); + _ok(v2.get() == "a", "swap (string)"); + + v1 = picojson::value(picojson::object()); + v2 = picojson::value(picojson::array()); + swap(v1, v2); + _ok(v1.is(), "swap (array)"); + _ok(v2.is(), "swap (object)"); + } + + { + picojson::value v; + const char *s = "{ \"a\": 1, \"b\": [ 2, { \"b1\": \"abc\" } ], \"c\": {}, \"d\": [] }"; + string err; + err = picojson::parse(v, s, s + strlen(s)); + _ok(err.empty(), "parse test data for prettifying output"); + _ok(v.serialize() == "{\"a\":1,\"b\":[2,{\"b1\":\"abc\"}],\"c\":{},\"d\":[]}", "non-prettifying output"); + _ok(v.serialize(true) == "{\n \"a\": 1,\n \"b\": [\n 2,\n {\n \"b1\": \"abc\"\n }\n ],\n \"c\": {},\n \"d\": []\n}\n", "prettifying output"); + } + + try { + picojson::value v(std::numeric_limits::quiet_NaN()); + _ok(false, "should not accept NaN"); + } catch (std::overflow_error e) { + _ok(true, "should not accept NaN"); + } + + try { + picojson::value v(std::numeric_limits::infinity()); + _ok(false, "should not accept infinity"); + } catch (std::overflow_error e) { + _ok(true, "should not accept infinity"); + } + + try { + picojson::value v(123.); + _ok(! v.is(), "is() should return false"); + v.get(); + _ok(false, "get() should raise an error"); + } catch (std::runtime_error e) { + _ok(true, "get() should raise an error"); + } + +#ifdef PICOJSON_USE_INT64 + { + picojson::value v1((int64_t)123); + _ok(v1.is(), "is int64_t"); + _ok(v1.is(), "is double as well"); + _ok(v1.serialize() == "123", "serialize the value"); + _ok(v1.get() == 123, "value is correct as int64_t"); + _ok(v1.get(), "value is correct as double"); + + _ok(! v1.is(), "is no more int64_type once get() is called"); + _ok(v1.is(), "and is still a double"); + + const char *s = "-9223372036854775809"; + _ok(picojson::parse(v1, s, s + strlen(s)).empty(), "parse underflowing int64_t"); + _ok(! v1.is(), "underflowing int is not int64_t"); + _ok(v1.is(), "underflowing int is double"); + _ok(v1.get() + 9.22337203685478e+18 < 65536, "double value is somewhat correct"); + } +#endif + + { + picojson::value v; + std::string err = picojson::parse(v, "[ 1, \"abc\" ]"); + _ok(err.empty(), "simple API no error"); + _ok(v.is(), "simple API return type is array"); + is(v.get().size(), 2, "simple API array size"); + _ok(v.get()[0].is(), "simple API type #0"); + is(v.get()[0].get(), 1, "simple API value #0"); + _ok(v.get()[1].is(), "simple API type #1"); + is(v.get()[1].get(), "abc", "simple API value #1"); + } + + return done_testing(); +}