├── .gitignore ├── .gitmodules ├── .travis.yml ├── README.md ├── CMakeLists.txt ├── src ├── main.cpp └── msgpack │ └── type │ ├── jsoncpp.hpp │ └── rapidjson.hpp └── LICENSE.txt /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "formats/rapidjson"] 2 | path = formats/rapidjson 3 | url = https://github.com/miloyip/rapidjson.git 4 | [submodule "formats/msgpack"] 5 | path = formats/msgpack 6 | url = https://github.com/msgpack/msgpack-c.git 7 | [submodule "formats/jsoncpp"] 8 | path = formats/jsoncpp 9 | url = https://github.com/open-source-parsers/jsoncpp.git 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | cache: 3 | - apt 4 | compiler: 5 | - gcc 6 | before_install: 7 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 8 | - sudo apt-get update -qq 9 | - if [ "$CXX" = "clang++" ]; then sudo apt-get install -qq libstdc++-4.8-dev; fi 10 | - if [ "$CXX" = "g++" ]; then sudo apt-get install -qq g++-4.8; fi 11 | - if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi 12 | - git clean -xdf 13 | script: 14 | - cmake -G "Unix Makefiles" -H. -Bbuild 15 | - cmake --build build --target xchange --config Release 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | xchange 2 | ======= 3 | 4 | [![Build Status](https://travis-ci.org/xpol/xchange.png)](https://travis-ci.org/xpol/xchange) 5 | 6 | This is a simple project that build a bridge between: 7 | 8 | * [msgpack-c](https://github.com/msgpack/msgpack-c) 9 | * [jsoncpp](https://github.com/open-source-parsers/jsoncpp) 10 | * [RapidJSON](https://github.com/miloyip/rapidjson). 11 | 12 | Source Code 13 | ----------- 14 | 15 | The `src/main.cpp` is a demo usage. 16 | The jsoncpp adapter for msgpack-c is `src/msgpack/type/jsoncpp.hpp`. 17 | The RapidJSON adapter for msgpack-c is `src/msgpack/type/rapidjson.hpp`. 18 | 19 | Usage 20 | ----- 21 | 22 | Just check out the `src/main.cpp`. 23 | 24 | *Maybe I will document usage here later.* 25 | 26 | Current status 27 | -------------- 28 | 29 | From \ To | msgpack-c | jsoncpp | RapidJSON 30 | ---------------|-------------|---------------------|------------ 31 | **msgpack-c** | \- | Done | Done 32 | **jsoncpp** | Done | \- | Done(via msgpack-c) 33 | **RapidJSON** | Done | Done(via msgpack-c) | \- 34 | 35 | Special Thanks 36 | -------------- 37 | 38 | * @miloyip 39 | * @pah 40 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.0 FATAL_ERROR) 2 | project(xchange) 3 | 4 | set(JSONCPP_WITH_TESTS OFF CACHE BOOL "Turn off JsonCpp tests") 5 | set(MSGPACK_CXX11 ON CACHE BOOL "Turn on C++11 for msgpack") 6 | set(MSGPACK_BUILD_EXAMPLES OFF CACHE BOOL "Turn off msgpack examples") 7 | set(MSGPACK_BUILD_TESTS OFF CACHE BOOL "Turn off msgpack tests") 8 | set(MSGPACK_ENABLE_SHARED OFF CACHE BOOL "Turn off msgpack shared library") 9 | 10 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 11 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 12 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 13 | 14 | add_subdirectory(formats/jsoncpp) 15 | add_subdirectory(formats/msgpack) 16 | 17 | include_directories( 18 | src 19 | formats/msgpack/include 20 | formats/rapidjson/include 21 | formats/jsoncpp/include 22 | ) 23 | 24 | link_directories(${CMAKE_BINARY_DIR}/lib) 25 | 26 | list(APPEND SOURCES 27 | src/main.cpp 28 | src/msgpack/type/rapidjson.hpp 29 | src/msgpack/type/jsoncpp.hpp 30 | ) 31 | 32 | add_definitions(-std=c++11) 33 | 34 | 35 | add_executable(xchange ${SOURCES}) 36 | 37 | target_link_libraries(xchange msgpack-static jsoncpp_lib_static) 38 | 39 | if (MSVC) 40 | set_property(TARGET xchange APPEND_STRING PROPERTY COMPILE_FLAGS "/wd4290") 41 | set_property(TARGET msgpack APPEND_STRING PROPERTY COMPILE_FLAGS "/wd4100 /wd4127 /wd4204 /wd4290") 42 | set_property(TARGET msgpack-static APPEND_STRING PROPERTY COMPILE_FLAGS "/wd4100 /wd4127 /wd4204 /wd4290") 43 | endif (MSVC) 44 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include "msgpack/type/rapidjson.hpp" 14 | #include "msgpack/type/jsoncpp.hpp" 15 | 16 | using namespace rapidjson; 17 | using namespace msgpack; 18 | 19 | 20 | struct FileFormat { 21 | enum Format { 22 | INVALID, 23 | JSON, 24 | MSGPACK, 25 | }; 26 | std::string filename; 27 | Format format; 28 | static Format formatForExtension(const std::string& extension) 29 | { 30 | if (extension == ".json") 31 | return JSON; 32 | if (extension == ".mpack") 33 | return MSGPACK; 34 | return INVALID; 35 | } 36 | }; 37 | 38 | struct Opt { 39 | FileFormat src; 40 | FileFormat dest; 41 | std::string executable; 42 | bool help; 43 | 44 | bool parse(int argc, char* argv[]) 45 | { 46 | executable = program(argv[0]); 47 | std::cout << executable << std::endl; 48 | 49 | if (argc != 4) 50 | { 51 | usage(); 52 | return false; 53 | } 54 | 55 | 56 | for (int i = 1; i < argc; i++) 57 | { 58 | std::string arg(argv[i]); 59 | if (arg == "-o") 60 | { 61 | if (i == argc - 1) // last arg should not be "-o" 62 | { 63 | usage(); 64 | return false; 65 | } 66 | 67 | dest.filename = argv[++i]; 68 | } 69 | else 70 | src.filename = arg; 71 | } 72 | 73 | if (src.filename.empty() && dest.filename.empty()) 74 | { 75 | usage(); 76 | return false; 77 | } 78 | if (!getFormat(dest.filename, &dest.format)) 79 | return false; 80 | if (!getFormat(src.filename, &src.format)) 81 | return false; 82 | 83 | if (src.format == dest.format) 84 | { 85 | std::cerr << "Same format, nothing to do: " << src.filename << ", " << dest.filename << std::endl; 86 | return false; 87 | } 88 | 89 | 90 | return true; 91 | } 92 | 93 | private: 94 | void usage() const 95 | { 96 | std::cerr << "Usage " << executable << " -o " << std::endl;; 97 | } 98 | bool getFormat(const std::string& filename, FileFormat::Format* f) 99 | { 100 | *f = FileFormat::formatForExtension(extension(filename)); 101 | bool rv = (*f != FileFormat::INVALID); 102 | if (!rv) 103 | std::cerr << "Unsupported extension:" << filename << std::endl; 104 | return rv; 105 | 106 | } 107 | static std::string extension(const std::string& filename) 108 | { 109 | size_t start = filename.find_last_of("."); 110 | if (start == std::string::npos) 111 | return ""; 112 | size_t check = filename.find_last_of("/\\"); 113 | if (check != std::string::npos && start < check) 114 | return ""; 115 | return filename.substr(start, filename.size() - start); 116 | } 117 | static std::string program(const std::string& argv0) 118 | { 119 | size_t start = argv0.find_last_of("/\\"); 120 | start = (start == std::string::npos) ? 0 : start + 1; 121 | size_t ends = argv0.rfind("."); 122 | if (ends == std::string::npos || ends < start) 123 | ends = argv0.size(); 124 | 125 | return argv0.substr(start, ends - start); 126 | } 127 | 128 | }; 129 | 130 | bool read_file_contents(const std::string& filename, std::string* contents) 131 | { 132 | std::ifstream in(filename.data(), std::ios::in | std::ios::binary); 133 | if (in.bad()) 134 | return false; 135 | 136 | in.seekg(0, std::ios::end); 137 | contents->resize(static_cast(in.tellg())); 138 | in.seekg(0, std::ios::beg); 139 | in.read(&(*contents)[0], contents->size()); 140 | in.close(); 141 | return true; 142 | } 143 | bool write_file_contents(const std::string& filename, const std::string& contents) 144 | { 145 | std::ofstream of(filename.data(), std::ios::out | std::ios::binary); 146 | if (of.bad()) 147 | return false; 148 | of.write(contents.data(), contents.size()); 149 | return true; 150 | } 151 | 152 | 153 | struct Msgpack { 154 | struct Document { 155 | std::string buffer; 156 | msgpack::unpacked unpacked; 157 | }; 158 | typedef Document document_type;; 159 | static bool load(Document& doc, const std::string& filename) 160 | { 161 | if (!read_file_contents(filename, &doc.buffer)) 162 | return false; 163 | msgpack::unpack(&doc.unpacked, doc.buffer.data(), doc.buffer.size()); 164 | return true; 165 | } 166 | static bool save(const Json::Value& doc, const std::string& filename) 167 | { 168 | msgpack::sbuffer sbuf; // simple buffer 169 | msgpack::pack(&sbuf, doc); 170 | 171 | return write_file_contents(filename, std::string(sbuf.data(), sbuf.size())); 172 | } 173 | static bool save(const rapidjson::Document& doc, const std::string& filename) 174 | { 175 | msgpack::sbuffer sbuf; // simple buffer 176 | msgpack::pack(&sbuf, doc); 177 | 178 | return write_file_contents(filename, std::string(sbuf.data(), sbuf.size())); 179 | } 180 | }; 181 | 182 | struct Jsoncpp { 183 | typedef Json::Value document_type;; 184 | 185 | static bool load(Json::Value& doc, const std::string& filename) 186 | { 187 | Json::Reader reader; 188 | std::ifstream in(filename.data(), std::ios::in | std::ios::binary); 189 | reader.parse(in, doc); 190 | return true; 191 | } 192 | static bool save(const Msgpack::Document& sdoc, const std::string& filename) 193 | { 194 | Json::Value doc; 195 | 196 | sdoc.unpacked.get().convert(&doc); 197 | std::ofstream of(filename.data(), std::ios::out | std::ios::binary); 198 | of << doc; 199 | return true; 200 | } 201 | }; 202 | 203 | struct RapidJSON { 204 | typedef rapidjson::Document document_type;; 205 | static bool load(rapidjson::Document& doc, const std::string& filename) 206 | { 207 | std::string contents; 208 | if (!read_file_contents(filename, &contents)) 209 | return false; 210 | doc.Parse(contents.data()); 211 | return true; 212 | } 213 | static bool save(const Msgpack::Document& sdoc, const std::string& filename) 214 | { 215 | rapidjson::Document doc; 216 | 217 | sdoc.unpacked.get().convert(&doc); 218 | 219 | StringBuffer buffer; 220 | Writer writer(buffer); 221 | doc.Accept(writer); 222 | 223 | return write_file_contents(filename, std::string(buffer.GetString(), buffer.GetSize())); 224 | } 225 | }; 226 | 227 | template 228 | bool convert(const std::string& sf, const std::string& df) 229 | { 230 | typename Src::document_type doc; 231 | if (!Src::load(doc, sf)) 232 | return false; 233 | return Dest::save(doc, df); 234 | } 235 | 236 | 237 | 238 | 239 | int main(int argc, char* argv[]) 240 | { 241 | Opt opt; 242 | if (!opt.parse(argc, argv)) 243 | return EXIT_FAILURE; 244 | if (opt.src.format == FileFormat::JSON && opt.dest.format == FileFormat::MSGPACK) 245 | return convert(opt.src.filename, opt.dest.filename) ? EXIT_SUCCESS : EXIT_FAILURE; 246 | if (opt.src.format == FileFormat::MSGPACK && opt.dest.format == FileFormat::JSON) 247 | return convert(opt.src.filename, opt.dest.filename) ? EXIT_SUCCESS : EXIT_FAILURE; 248 | 249 | return 0; 250 | } 251 | -------------------------------------------------------------------------------- /src/msgpack/type/jsoncpp.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MSGPACK_TYPE_JSONCPP_VALUE_HPP__ 2 | #define MSGPACK_TYPE_JSONCPP_VALUE_HPP__ 3 | 4 | #include 5 | #include 6 | 7 | namespace msgpack { MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { namespace adaptor { 8 | 9 | template <> 10 | struct convert { 11 | msgpack::object const& operator()(msgpack::object const& o, Json::Value& v) const { 12 | switch (o.type) 13 | { 14 | case msgpack::type::BOOLEAN: v = o.via.boolean; break;; 15 | case msgpack::type::POSITIVE_INTEGER: v = static_cast(o.via.u64); break; 16 | case msgpack::type::NEGATIVE_INTEGER: v = static_cast(o.via.i64); break; 17 | case msgpack::type::FLOAT: v = o.via.f64; break; 18 | case msgpack::type::BIN: v = Json::Value(o.via.bin.ptr, o.via.bin.ptr+o.via.bin.size); break; 19 | case msgpack::type::STR: v = Json::Value(o.via.str.ptr, o.via.str.ptr+o.via.str.size); break; 20 | case msgpack::type::ARRAY:{ 21 | msgpack::object* ptr = o.via.array.ptr; 22 | msgpack::object* END = ptr + o.via.array.size; 23 | for (; ptr < END; ++ptr) 24 | { 25 | Json::Value element; 26 | ptr->convert(&element); 27 | v.append(element); 28 | } 29 | } 30 | break; 31 | case msgpack::type::MAP: { 32 | msgpack::object_kv* ptr = o.via.map.ptr; 33 | msgpack::object_kv* END = ptr + o.via.map.size; 34 | for (; ptr < END; ++ptr) 35 | { 36 | std::string key(ptr->key.via.str.ptr, ptr->key.via.str.size); 37 | Json::Value& val = v[key]; 38 | ptr->val.convert(&val); 39 | } 40 | } 41 | break; 42 | case msgpack::type::NIL: 43 | default: 44 | v = Json::Value::null; break; 45 | } 46 | return o; 47 | } 48 | }; 49 | 50 | 51 | template <> 52 | struct pack { 53 | template 54 | msgpack::packer& operator()(msgpack::packer& o, Json::Value const& v) const { 55 | switch (v.type()) 56 | { 57 | default: 58 | case Json::nullValue: return o.pack_nil(); 59 | case Json::intValue: return o.pack_int64(v.asInt64()); 60 | case Json::uintValue: return o.pack_uint64(v.asUInt64()); 61 | case Json::realValue: return o.pack_double(v.asDouble()); 62 | case Json::stringValue:{ 63 | std::string s(v.asString()); 64 | return o.pack_str(s.size()).pack_str_body(s.data(), s.size()); 65 | } 66 | case Json::booleanValue:return v.asBool() ? o.pack_true() : o.pack_false(); 67 | case Json::arrayValue: { 68 | o.pack_array(v.size()); 69 | Json::Value::const_iterator i = v.begin(), END = v.end(); 70 | for (; i != END; ++i) 71 | o.pack(*i); 72 | return o; 73 | } 74 | case Json::objectValue:{ 75 | o.pack_map(v.size()); 76 | 77 | Json::Value::const_iterator i = v.begin(), END = v.end(); 78 | for (; i != END; ++i) 79 | { 80 | o.pack(i.key().asString()); 81 | o.pack(*i); 82 | } 83 | return o; 84 | } 85 | } 86 | } 87 | }; 88 | /* 89 | template <> 90 | struct object { 91 | void operator()(msgpack::object& o, Json::Value const& v) const { 92 | // your implementation 93 | } 94 | };*/ 95 | 96 | template <> 97 | struct object_with_zone { 98 | void operator()(msgpack::object::with_zone& o, Json::Value const& v) const { 99 | switch (v.type()) 100 | { 101 | default: 102 | case Json::nullValue: 103 | o.type = type::NIL; 104 | break; 105 | case Json::intValue: 106 | o.type = type::NEGATIVE_INTEGER; 107 | o.via.i64 = v.asInt64(); 108 | break; 109 | case Json::uintValue: 110 | o.type = type::POSITIVE_INTEGER; 111 | o.via.u64 = v.asUInt64(); 112 | break; 113 | case Json::realValue: 114 | o.type = type::FLOAT; 115 | o.via.f64 = v.asDouble(); 116 | break; 117 | case Json::stringValue: { 118 | o.type = type::STR; 119 | std::string s(v.asString()); 120 | 121 | char* ptr = (char*)o.zone.allocate_align(s.size()); 122 | o.via.str.ptr = ptr; 123 | o.via.str.size = (uint32_t)s.size(); 124 | memcpy(ptr, s.data(), s.size()); 125 | break; 126 | } 127 | case Json::booleanValue: 128 | o.type = type::BOOLEAN; 129 | o.via.boolean = v.asBool(); 130 | break; 131 | case Json::arrayValue:{ 132 | o.type = type::ARRAY; 133 | if (v.empty()) { 134 | o.via.array.ptr = NULL; 135 | o.via.array.size = 0; 136 | } 137 | else { 138 | size_t sz = v.size(); 139 | msgpack::object* p = (msgpack::object*)o.zone.allocate_align(sizeof(msgpack::object)*sz); 140 | msgpack::object* const pend = p + sz; 141 | o.via.array.ptr = p; 142 | o.via.array.size = sz; 143 | Json::Value::const_iterator it = v.begin(); 144 | do { 145 | *p = msgpack::object(*it, o.zone); 146 | ++p; 147 | ++it; 148 | } while (p < pend); 149 | } 150 | } 151 | case Json::objectValue:{ 152 | o.type = type::MAP; 153 | if (v.empty()) { 154 | o.via.map.ptr = NULL; 155 | o.via.map.size = 0; 156 | } 157 | else { 158 | size_t sz = v.size(); 159 | object_kv* p = (object_kv*)o.zone.allocate_align(sizeof(object_kv)*sz); 160 | object_kv* const pend = p + sz; 161 | o.via.map.ptr = p; 162 | o.via.map.size = sz; 163 | Json::Value::const_iterator it(v.begin()); 164 | do { 165 | p->key = msgpack::object(it.key().asString(), o.zone); 166 | p->val = msgpack::object(*it, o.zone); 167 | ++p; 168 | ++it; 169 | } while (p < pend); 170 | } 171 | break; 172 | } 173 | } 174 | } 175 | }; 176 | }}} 177 | 178 | 179 | #endif /* msgpack/type/jsoncpp/value.hpp */ 180 | 181 | -------------------------------------------------------------------------------- /src/msgpack/type/rapidjson.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MSGPACK_TYPE_RAPIDJSON_DOCUMENT_HPP__ 2 | #define MSGPACK_TYPE_RAPIDJSON_DOCUMENT_HPP__ 3 | 4 | #include 5 | #include 6 | 7 | namespace msgpack { MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { namespace adaptor { 8 | 9 | template 10 | struct convert< rapidjson::GenericDocument > { 11 | msgpack::object const& operator()(msgpack::object const& o, rapidjson::GenericDocument& v) const { 12 | switch (o.type) 13 | { 14 | case msgpack::type::BOOLEAN: v.SetBool(o.via.boolean); break;; 15 | case msgpack::type::POSITIVE_INTEGER: v.SetUint64(o.via.u64); break; 16 | case msgpack::type::NEGATIVE_INTEGER: v.SetInt64(o.via.i64); break; 17 | case msgpack::type::FLOAT: v.SetDouble(o.via.f64); break; 18 | case msgpack::type::BIN: // fall through 19 | case msgpack::type::STR: v.SetString(o.via.str.ptr, o.via.str.size); break; 20 | case msgpack::type::ARRAY:{ 21 | v.SetArray(); 22 | v.Reserve(o.via.array.size, v.GetAllocator()); 23 | msgpack::object* ptr = o.via.array.ptr; 24 | msgpack::object* END = ptr + o.via.array.size; 25 | for (; ptr < END; ++ptr) 26 | { 27 | rapidjson::GenericDocument element(&v.GetAllocator()); 28 | ptr->convert(&element); 29 | v.PushBack(static_cast&>(element), v.GetAllocator()); 30 | } 31 | } 32 | break; 33 | case msgpack::type::MAP: { 34 | v.SetObject(); 35 | msgpack::object_kv* ptr = o.via.map.ptr; 36 | msgpack::object_kv* END = ptr + o.via.map.size; 37 | for (; ptr < END; ++ptr) 38 | { 39 | rapidjson::GenericValue key(ptr->key.via.str.ptr, ptr->key.via.str.size, v.GetAllocator()); 40 | rapidjson::GenericDocument val(&v.GetAllocator()); 41 | ptr->val.convert(&val); 42 | 43 | v.AddMember(key, val, v.GetAllocator()); 44 | } 45 | } 46 | break; 47 | case msgpack::type::NIL: 48 | default: 49 | v.SetNull(); break; 50 | 51 | } 52 | return o; 53 | } 54 | }; 55 | 56 | 57 | template 58 | struct convert< rapidjson::GenericValue > { 59 | msgpack::object const& operator()(msgpack::object const& o, rapidjson::GenericValue& v) const { 60 | rapidjson::GenericDocument d; 61 | o >> d; 62 | return v = d; 63 | } 64 | }; 65 | 66 | template 67 | struct pack< rapidjson::GenericValue > { 68 | template 69 | msgpack::packer& operator()(msgpack::packer& o, rapidjson::GenericValue const& v) const { 70 | switch (v.GetType()) 71 | { 72 | case rapidjson::kNullType: 73 | return o.pack_nil(); 74 | case rapidjson::kFalseType: 75 | return o.pack_false(); 76 | case rapidjson::kTrueType: 77 | return o.pack_true(); 78 | case rapidjson::kObjectType: 79 | { 80 | o.pack_map(v.MemberCount()); 81 | typename rapidjson::GenericValue::ConstMemberIterator i = v.MemberBegin(), END = v.MemberEnd(); 82 | for (; i != END; ++i) 83 | { 84 | o.pack_str(i->name.GetStringLength()).pack_str_body(i->name.GetString(), i->name.GetStringLength()); 85 | o.pack(i->value); 86 | } 87 | return o; 88 | } 89 | case rapidjson::kArrayType: 90 | { 91 | o.pack_array(v.Size()); 92 | typename rapidjson::GenericValue::ConstValueIterator i = v.Begin(), END = v.End(); 93 | for (;i < END; ++i) 94 | o.pack(*i); 95 | return o; 96 | } 97 | case rapidjson::kStringType: 98 | return o.pack_str(v.GetStringLength()).pack_str_body(v.GetString(), v.GetStringLength()); 99 | case rapidjson::kNumberType: 100 | if (v.IsInt()) 101 | return o.pack_int(v.GetInt()); 102 | if (v.IsUint()) 103 | return o.pack_unsigned_int(v.GetUint()); 104 | if (v.IsInt64()) 105 | return o.pack_int64(v.GetInt64()); 106 | if (v.IsUint64()) 107 | return o.pack_uint64(v.GetUint64()); 108 | if (v.IsDouble()||v.IsNumber()) 109 | return o.pack_double(v.GetDouble()); 110 | default: 111 | return o; 112 | } 113 | } 114 | }; 115 | 116 | template 117 | struct pack< rapidjson::GenericDocument > { 118 | template 119 | msgpack::packer& operator()(msgpack::packer& o, rapidjson::GenericDocument const& v) const { 120 | o << static_cast&>(v); 121 | return o; 122 | } 123 | }; 124 | 125 | template 126 | struct object_with_zone< rapidjson::GenericValue > { 127 | void operator()(msgpack::object::with_zone& o, rapidjson::GenericValue const& v) const { 128 | switch (v.GetType()) 129 | { 130 | case rapidjson::kNullType: 131 | o.type = type::NIL; 132 | break; 133 | case rapidjson::kFalseType: 134 | o.type = type::BOOLEAN; 135 | o.via.boolean = false; 136 | break; 137 | case rapidjson::kTrueType: 138 | o.type = type::BOOLEAN; 139 | o.via.boolean = true; 140 | break; 141 | case rapidjson::kObjectType: 142 | { 143 | o.type = type::MAP; 144 | if (v.ObjectEmpty()) { 145 | o.via.map.ptr = NULL; 146 | o.via.map.size = 0; 147 | } 148 | else { 149 | size_t sz = v.MemberCount(); 150 | object_kv* p = (object_kv*)o.zone.allocate_align(sizeof(object_kv)*sz); 151 | object_kv* const pend = p + sz; 152 | o.via.map.ptr = p; 153 | o.via.map.size = sz; 154 | typename rapidjson::GenericValue::ConstMemberIterator it(v.MemberBegin()); 155 | do { 156 | p->key = msgpack::object(it->name, o.zone); 157 | p->val = msgpack::object(it->value, o.zone); 158 | ++p; 159 | ++it; 160 | } while (p < pend); 161 | } 162 | break; 163 | } 164 | case rapidjson::kArrayType: 165 | { 166 | o.type = type::ARRAY; 167 | if (v.Empty()) { 168 | o.via.array.ptr = NULL; 169 | o.via.array.size = 0; 170 | } 171 | else { 172 | msgpack::object* p = (msgpack::object*)o.zone.allocate_align(sizeof(msgpack::object)*v.Size()); 173 | msgpack::object* const pend = p + v.Size(); 174 | o.via.array.ptr = p; 175 | o.via.array.size = v.Size(); 176 | typename rapidjson::GenericValue::ConstValueIterator it(v.Begin()); 177 | do { 178 | *p = msgpack::object(*it, o.zone); 179 | ++p; 180 | ++it; 181 | } while (p < pend); 182 | } 183 | break; 184 | } 185 | case rapidjson::kStringType: 186 | { 187 | o.type = type::STR; 188 | size_t size = v.GetStringLength(); 189 | char* ptr = (char*)o.zone.allocate_align(size); 190 | memcpy(ptr, v.GetString(), size); 191 | o.via.str.ptr = ptr; 192 | o.via.str.size = size; 193 | break; 194 | } 195 | case rapidjson::kNumberType: 196 | if (v.IsInt()) 197 | { 198 | o.type = type::NEGATIVE_INTEGER; 199 | o.via.i64 = v.GetInt(); 200 | } 201 | else if (v.IsUint()) 202 | { 203 | o.type = type::POSITIVE_INTEGER; 204 | o.via.u64 = v.GetUint(); 205 | } 206 | else if (v.IsInt64()) 207 | { 208 | o.type = type::NEGATIVE_INTEGER; 209 | o.via.i64 = v.GetInt64(); 210 | } 211 | else if (v.IsUint64()) 212 | { 213 | o.type = type::POSITIVE_INTEGER; 214 | o.via.u64 = v.GetUint64(); 215 | } 216 | else if (v.IsDouble()) 217 | { 218 | o.type = type::FLOAT; 219 | o.via.f64 = v.GetDouble(); 220 | } 221 | break; 222 | default: 223 | break; 224 | 225 | } 226 | } 227 | }; 228 | 229 | template 230 | struct object_with_zone< rapidjson::GenericDocument > { 231 | void operator()(msgpack::object::with_zone& o, rapidjson::GenericDocument const& v) const { 232 | o << static_cast const&>(v); 233 | } 234 | }; 235 | }}} 236 | 237 | 238 | #endif /* msgpack/type/rapidjson/document.hpp */ 239 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | This Software is licenced under Boost Software License v1.0 2 | or Apache License v2.0. Just choose one of them as you whish. 3 | 4 | 5 | The linked source code under formats/ directory is licensed under their own licences. 6 | 7 | =========================================================================== 8 | Boost Software License - Version 1.0 - August 17th, 2003 9 | 10 | Permission is hereby granted, free of charge, to any person or organization 11 | obtaining a copy of the software and accompanying documentation covered by 12 | this license (the "Software") to use, reproduce, display, distribute, 13 | execute, and transmit the Software, and to prepare derivative works of the 14 | Software, and to permit third-parties to whom the Software is furnished to 15 | do so, all subject to the following: 16 | 17 | The copyright notices in the Software and this entire statement, including 18 | the above license grant, this restriction and the following disclaimer, 19 | must be included in all copies of the Software, in whole or in part, and 20 | all derivative works of the Software, unless such copies or derivative 21 | works are solely in the form of machine-executable object code generated by 22 | a source language processor. 23 | 24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 27 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 28 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 29 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 30 | DEALINGS IN THE SOFTWARE. 31 | 32 | 33 | =========================================================================== 34 | Apache License 35 | Version 2.0, January 2004 36 | http://www.apache.org/licenses/ 37 | 38 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 39 | 40 | 1. Definitions. 41 | 42 | "License" shall mean the terms and conditions for use, reproduction, 43 | and distribution as defined by Sections 1 through 9 of this document. 44 | 45 | "Licensor" shall mean the copyright owner or entity authorized by 46 | the copyright owner that is granting the License. 47 | 48 | "Legal Entity" shall mean the union of the acting entity and all 49 | other entities that control, are controlled by, or are under common 50 | control with that entity. For the purposes of this definition, 51 | "control" means (i) the power, direct or indirect, to cause the 52 | direction or management of such entity, whether by contract or 53 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 54 | outstanding shares, or (iii) beneficial ownership of such entity. 55 | 56 | "You" (or "Your") shall mean an individual or Legal Entity 57 | exercising permissions granted by this License. 58 | 59 | "Source" form shall mean the preferred form for making modifications, 60 | including but not limited to software source code, documentation 61 | source, and configuration files. 62 | 63 | "Object" form shall mean any form resulting from mechanical 64 | transformation or translation of a Source form, including but 65 | not limited to compiled object code, generated documentation, 66 | and conversions to other media types. 67 | 68 | "Work" shall mean the work of authorship, whether in Source or 69 | Object form, made available under the License, as indicated by a 70 | copyright notice that is included in or attached to the work 71 | (an example is provided in the Appendix below). 72 | 73 | "Derivative Works" shall mean any work, whether in Source or Object 74 | form, that is based on (or derived from) the Work and for which the 75 | editorial revisions, annotations, elaborations, or other modifications 76 | represent, as a whole, an original work of authorship. For the purposes 77 | of this License, Derivative Works shall not include works that remain 78 | separable from, or merely link (or bind by name) to the interfaces of, 79 | the Work and Derivative Works thereof. 80 | 81 | "Contribution" shall mean any work of authorship, including 82 | the original version of the Work and any modifications or additions 83 | to that Work or Derivative Works thereof, that is intentionally 84 | submitted to Licensor for inclusion in the Work by the copyright owner 85 | or by an individual or Legal Entity authorized to submit on behalf of 86 | the copyright owner. For the purposes of this definition, "submitted" 87 | means any form of electronic, verbal, or written communication sent 88 | to the Licensor or its representatives, including but not limited to 89 | communication on electronic mailing lists, source code control systems, 90 | and issue tracking systems that are managed by, or on behalf of, the 91 | Licensor for the purpose of discussing and improving the Work, but 92 | excluding communication that is conspicuously marked or otherwise 93 | designated in writing by the copyright owner as "Not a Contribution." 94 | 95 | "Contributor" shall mean Licensor and any individual or Legal Entity 96 | on behalf of whom a Contribution has been received by Licensor and 97 | subsequently incorporated within the Work. 98 | 99 | 2. Grant of Copyright License. Subject to the terms and conditions of 100 | this License, each Contributor hereby grants to You a perpetual, 101 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 102 | copyright license to reproduce, prepare Derivative Works of, 103 | publicly display, publicly perform, sublicense, and distribute the 104 | Work and such Derivative Works in Source or Object form. 105 | 106 | 3. Grant of Patent License. Subject to the terms and conditions of 107 | this License, each Contributor hereby grants to You a perpetual, 108 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 109 | (except as stated in this section) patent license to make, have made, 110 | use, offer to sell, sell, import, and otherwise transfer the Work, 111 | where such license applies only to those patent claims licensable 112 | by such Contributor that are necessarily infringed by their 113 | Contribution(s) alone or by combination of their Contribution(s) 114 | with the Work to which such Contribution(s) was submitted. If You 115 | institute patent litigation against any entity (including a 116 | cross-claim or counterclaim in a lawsuit) alleging that the Work 117 | or a Contribution incorporated within the Work constitutes direct 118 | or contributory patent infringement, then any patent licenses 119 | granted to You under this License for that Work shall terminate 120 | as of the date such litigation is filed. 121 | 122 | 4. Redistribution. You may reproduce and distribute copies of the 123 | Work or Derivative Works thereof in any medium, with or without 124 | modifications, and in Source or Object form, provided that You 125 | meet the following conditions: 126 | 127 | (a) You must give any other recipients of the Work or 128 | Derivative Works a copy of this License; and 129 | 130 | (b) You must cause any modified files to carry prominent notices 131 | stating that You changed the files; and 132 | 133 | (c) You must retain, in the Source form of any Derivative Works 134 | that You distribute, all copyright, patent, trademark, and 135 | attribution notices from the Source form of the Work, 136 | excluding those notices that do not pertain to any part of 137 | the Derivative Works; and 138 | 139 | (d) If the Work includes a "NOTICE" text file as part of its 140 | distribution, then any Derivative Works that You distribute must 141 | include a readable copy of the attribution notices contained 142 | within such NOTICE file, excluding those notices that do not 143 | pertain to any part of the Derivative Works, in at least one 144 | of the following places: within a NOTICE text file distributed 145 | as part of the Derivative Works; within the Source form or 146 | documentation, if provided along with the Derivative Works; or, 147 | within a display generated by the Derivative Works, if and 148 | wherever such third-party notices normally appear. The contents 149 | of the NOTICE file are for informational purposes only and 150 | do not modify the License. You may add Your own attribution 151 | notices within Derivative Works that You distribute, alongside 152 | or as an addendum to the NOTICE text from the Work, provided 153 | that such additional attribution notices cannot be construed 154 | as modifying the License. 155 | 156 | You may add Your own copyright statement to Your modifications and 157 | may provide additional or different license terms and conditions 158 | for use, reproduction, or distribution of Your modifications, or 159 | for any such Derivative Works as a whole, provided Your use, 160 | reproduction, and distribution of the Work otherwise complies with 161 | the conditions stated in this License. 162 | 163 | 5. Submission of Contributions. Unless You explicitly state otherwise, 164 | any Contribution intentionally submitted for inclusion in the Work 165 | by You to the Licensor shall be under the terms and conditions of 166 | this License, without any additional terms or conditions. 167 | Notwithstanding the above, nothing herein shall supersede or modify 168 | the terms of any separate license agreement you may have executed 169 | with Licensor regarding such Contributions. 170 | 171 | 6. Trademarks. This License does not grant permission to use the trade 172 | names, trademarks, service marks, or product names of the Licensor, 173 | except as required for reasonable and customary use in describing the 174 | origin of the Work and reproducing the content of the NOTICE file. 175 | 176 | 7. Disclaimer of Warranty. Unless required by applicable law or 177 | agreed to in writing, Licensor provides the Work (and each 178 | Contributor provides its Contributions) on an "AS IS" BASIS, 179 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 180 | implied, including, without limitation, any warranties or conditions 181 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 182 | PARTICULAR PURPOSE. You are solely responsible for determining the 183 | appropriateness of using or redistributing the Work and assume any 184 | risks associated with Your exercise of permissions under this License. 185 | 186 | 8. Limitation of Liability. In no event and under no legal theory, 187 | whether in tort (including negligence), contract, or otherwise, 188 | unless required by applicable law (such as deliberate and grossly 189 | negligent acts) or agreed to in writing, shall any Contributor be 190 | liable to You for damages, including any direct, indirect, special, 191 | incidental, or consequential damages of any character arising as a 192 | result of this License or out of the use or inability to use the 193 | Work (including but not limited to damages for loss of goodwill, 194 | work stoppage, computer failure or malfunction, or any and all 195 | other commercial damages or losses), even if such Contributor 196 | has been advised of the possibility of such damages. 197 | 198 | 9. Accepting Warranty or Additional Liability. While redistributing 199 | the Work or Derivative Works thereof, You may choose to offer, 200 | and charge a fee for, acceptance of support, warranty, indemnity, 201 | or other liability obligations and/or rights consistent with this 202 | License. However, in accepting such obligations, You may act only 203 | on Your own behalf and on Your sole responsibility, not on behalf 204 | of any other Contributor, and only if You agree to indemnify, 205 | defend, and hold each Contributor harmless for any liability 206 | incurred by, or claims asserted against, such Contributor by reason 207 | of your accepting any such warranty or additional liability. 208 | 209 | END OF TERMS AND CONDITIONS 210 | 211 | APPENDIX: How to apply the Apache License to your work. 212 | 213 | To apply the Apache License to your work, attach the following 214 | boilerplate notice, with the fields enclosed by brackets "[]" 215 | replaced with your own identifying information. (Don't include 216 | the brackets!) The text should be enclosed in the appropriate 217 | comment syntax for the file format. We also recommend that a 218 | file or class name and description of purpose be included on the 219 | same "printed page" as the copyright notice for easier 220 | identification within third-party archives. 221 | 222 | Copyright [yyyy] [name of copyright owner] 223 | 224 | Licensed under the Apache License, Version 2.0 (the "License"); 225 | you may not use this file except in compliance with the License. 226 | You may obtain a copy of the License at 227 | 228 | http://www.apache.org/licenses/LICENSE-2.0 229 | 230 | Unless required by applicable law or agreed to in writing, software 231 | distributed under the License is distributed on an "AS IS" BASIS, 232 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 233 | See the License for the specific language governing permissions and 234 | limitations under the License. 235 | --------------------------------------------------------------------------------