sharedptr.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2021 Duowan Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,
  11. * software distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <iostream>
  17. #include "xpack/json.h"
  18. #include "xpack/xml.h"
  19. using namespace std;
  20. struct User {
  21. int64_t id;
  22. string name;
  23. string mail;
  24. std::shared_ptr<string> bio;
  25. User(int64_t i=0, const string& n="", const string& m=""):id(i),name(n),mail(m){}
  26. XPACK(O(id, name, mail, bio)); // 添加宏定义XPACK在结构体定义结尾
  27. };
  28. int main(int argc, char *argv[]) {
  29. (void)argc;
  30. (void)argv;
  31. User u1(1, "Jack", "jack@xpack.com");
  32. User u2;
  33. User u3;
  34. string json = xpack::json::encode(u1); // 结构体转json
  35. string xml = xpack::xml::encode(u1, "root");
  36. cout<<"========================"<<endl;
  37. cout<<json<<endl;
  38. cout<<xml<<endl;
  39. cout<<"========================"<<endl;
  40. xpack::json::decode(json, u2);
  41. cout<<xpack::json::encode(u2)<<endl;
  42. xpack::xml::decode(xml, u3);
  43. cout<<xpack::xml::encode(u3, "root")<<endl;
  44. cout<<"========================"<<endl;
  45. u1.bio.reset(new std::string("farmer"));
  46. json = xpack::json::encode(u1); // 结构体转json
  47. xml = xpack::xml::encode(u1, "root");
  48. cout<<json<<endl;
  49. cout<<xml<<endl;
  50. return 0;
  51. }