xpack_out.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <sys/time.h> linux only
  17. #include <iostream>
  18. #include "xpack/json.h"
  19. #ifndef _MSC_VER
  20. #include <sys/time.h>
  21. #endif
  22. using namespace std;
  23. #ifdef _MSC_VER
  24. struct timeval {
  25. long tv_sec;
  26. long tv_usec;
  27. };
  28. #endif
  29. // timeval is thirdparty struct
  30. XPACK_OUT(timeval, O(tv_sec, tv_usec));
  31. struct T {
  32. int a;
  33. string b;
  34. timeval t;
  35. XPACK(O(a, b, t));
  36. };
  37. int main(int argc, char *argv[]) {
  38. (void)argc;
  39. (void)argv;
  40. T t;
  41. T r;
  42. t.a = 123;
  43. t.b = "xpack";
  44. t.t.tv_sec = 888;
  45. t.t.tv_usec = 999;
  46. string s = xpack::json::encode(t);
  47. cout<<s<<endl;
  48. xpack::json::decode(s, r);
  49. cout<<r.a<<','<<r.b<<','<<r.t.tv_sec<<','<<r.t.tv_usec<<endl;
  50. return 0;
  51. }