|
| 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 | + |
| 17 | +#include <iostream> |
| 18 | +#include "xpack/xtype.h" |
| 19 | +#include "xpack/json.h" |
| 20 | +#include "xpack/xml.h" |
| 21 | + |
| 22 | +using namespace std; |
| 23 | + |
| 24 | +struct Date { |
| 25 | + long unix_time; |
| 26 | +}; |
| 27 | + |
| 28 | +namespace xpack { // must define in namespace xpack |
| 29 | + |
| 30 | +template<> |
| 31 | +struct is_xpack_xtype<Date> {static bool const value = true;}; |
| 32 | + |
| 33 | +// implement decode |
| 34 | +template<class OBJ> |
| 35 | +bool xpack_xtype_decode(OBJ &obj, const char*key, Date &val, const Extend *ext) { |
| 36 | + std::string str; |
| 37 | + obj.decode(key, str, ext); |
| 38 | + if (str.empty()) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | +#ifndef _MSC_VER |
| 43 | + tm ttm; |
| 44 | + |
| 45 | + if (0 != strptime(str.c_str(), "%Y-%m-%d %H:%M:%S", &ttm)) { |
| 46 | + val.unix_time = mktime(&ttm); |
| 47 | + } else { |
| 48 | + val.unix_time = 0; |
| 49 | + } |
| 50 | +#else |
| 51 | + static int days[]={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 32}; |
| 52 | + struct tm ttm={0}; |
| 53 | + sscanf_s(str.c_str(), "%d-%d-%d %d:%d:%d", &ttm.tm_year, &ttm.tm_mon, &ttm.tm_mday, &ttm.tm_hour, &ttm.tm_min, &ttm.tm_sec); |
| 54 | + ttm.tm_mon-=1; // mon[0-11] |
| 55 | + ttm.tm_year-=1900; // since 1900 |
| 56 | + val.unix_time = mktime(&ttm); |
| 57 | +#endif |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +// implement encode |
| 62 | +template<class OBJ> |
| 63 | +bool xpack_xtype_encode(OBJ &obj, const char*key, const Date &val, const Extend *ext) { |
| 64 | + time_t tt = (time_t)val.unix_time; |
| 65 | + tm ttm; |
| 66 | + |
| 67 | +#ifndef _MSC_VER |
| 68 | + localtime_r(&tt, &ttm); |
| 69 | +#else |
| 70 | + localtime_s(&ttm, &tt); |
| 71 | +#endif |
| 72 | + |
| 73 | + char buf[64]; |
| 74 | + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &ttm); |
| 75 | + return obj.encode(key, buf, ext); |
| 76 | +} |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +struct User { |
| 81 | + int64_t id; |
| 82 | + string name; |
| 83 | + string mail; |
| 84 | + Date d; |
| 85 | + User(int64_t i=0, const string& n="", const string& m="", long _d=0):id(i),name(n),mail(m){d.unix_time = _d;} |
| 86 | + XPACK(O(id, name, mail, d)); |
| 87 | +}; |
| 88 | + |
| 89 | +struct Group { |
| 90 | + string name; |
| 91 | + int64_t master; |
| 92 | + vector<User> members; |
| 93 | + XPACK(O(name, master, members)); |
| 94 | +}; |
| 95 | + |
| 96 | +int main(int argc, char *argv[]) { |
| 97 | + Group g; |
| 98 | + g.name = "C++"; |
| 99 | + g.master = 2019; |
| 100 | + g.members.resize(2); |
| 101 | + g.members[0] = User(1, "Jack", "jack@xpack.com", 1); |
| 102 | + g.members[1] = User(2, "Pony", "pony@xpack.com", 1609249232); |
| 103 | + |
| 104 | + string json = xpack::json::encode(g, 0, 2, ' '); |
| 105 | + cout<<json<<endl; |
| 106 | + |
| 107 | + string xml = xpack::xml::encode(g, "root", 0, 2, ' '); |
| 108 | + cout<<xml<<endl; |
| 109 | + |
| 110 | + Group n1; |
| 111 | + xpack::json::decode(json, n1); |
| 112 | + cout<<n1.name<<';'<<n1.members[0].name<<';'<<n1.members[0].d.unix_time<<endl; |
| 113 | + |
| 114 | + Group n2; |
| 115 | + xpack::xml::decode(xml, n2); |
| 116 | + cout<<n2.name<<';'<<n2.members[1].name<<';'<<n2.members[1].d.unix_time<<endl; |
| 117 | + |
| 118 | + return 0; |
| 119 | +} |
0 commit comments