/* * Copyright (C) 2019 YY Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include "example.h" #include "bson_builder.h" using namespace std; using namespace x2struct; int main(int argc, char *argv[]) { // struct <--> string { bson_error_t err; memset(&err, 0, sizeof(err)); std::ifstream fs("test.json", std::ifstream::binary); std::string json((std::istreambuf_iterator(fs)), std::istreambuf_iterator()); bson_t * bson = bson_new_from_json((const uint8_t *)json.data(), json.length(), &err); cout<<"======== struct <-----> string ==========="< vstr; vstr.push_back("s1"); vstr.push_back("s2"); #if __GXX_EXPERIMENTAL_CXX0X__ // if support c++11 build map by initializer_list bb::vp m{{"$set", bb::vp{{"_id",200}, {"date",bb::dt(1512828045000)}, {"vs", vstr}}}}; bb::vp m1{{"_id", bb::vp{{"$in", vector{"a"}}}}}; #else bb::vp m; bb::vp up; up.push_back(std::make_pair("_id", 200)); up.push_back(std::make_pair("date", bb::dt(1512828045000))); up.push_back(std::make_pair("vs", vstr)); m.push_back(std::make_pair("$set", up)); #endif cout< m; m["hello"] = 1; m["good"] = 2; string bson = x2struct::X::tobson(m); map n; x2struct::X::loadbson((uint8_t*)bson.data(), bson.length(), n); cout<