Git Product home page Git Product logo

Comments (5)

xyz347 avatar xyz347 commented on July 29, 2024

支持结构体嵌套的,像你的这种需要这么来定义:
struct Collection {
float temperature;
std::string mac;
float humidity;
XTOSTRUCT(O(temperature, mac, humidity));
};

struct Data {
int64_t timestamp;
std::vector collections;
std::string address;
XTOSTRUCT(O(timestamp, collections, address));
};

struct Info {
int errorCode;
std::string message;
Data data;
XTOSTRUCT(O(errorCode, message, data));
};

int main(int argc, char *argv[]) {
Info in;
in.errorCode = 0;
in.data.timestamp = 1531710826;
in.data.collections.resize(2);
in.data.collections[0].temperature = 26.7;
in.data.collections[0].mac = "ABCDEF";
in.data.collections[0].humidity = 75.1;
in.data.collections[1].temperature = 25.9;
in.data.collections[1].mac = "ABCDE2";
in.data.collections[1].humidity = 74.6;
in.data.address = "DEFAB";
std::string json = x2struct::X::tojson(in);
std::cout<<json<<std::endl;
return 0;
}

from x2struct.

HsiangCheng avatar HsiangCheng commented on July 29, 2024

请问必须使用O()吗?我的同事使用M()好像会编译报错

from x2struct.

xyz347 avatar xyz347 commented on July 29, 2024

M也是可以的,O和M的不同在于解析json的时候。M包起来的字段必须存在。
比如O(a,b)可以解析{"a":1},但是M(a,b)解析{"a":1}就会抛出异常,因为b不存在

from x2struct.

xyz347 avatar xyz347 commented on July 29, 2024

O的运行结果
x@~$ ./xtest
{"errorCode":0,"message":"","data":{"timestamp":1531710826,"collections":[{"temperature":26.700000762939454,"mac":"ABCDEF","humidity":75.0999984741211},{"temperature":25.899999618530275,"mac":"ABCDE2","humidity":74.5999984741211}],"address":"DEFAB"}}

M的运行结果
x@~$ ./xtest
{"errorCode":0,"message":"","data":{"timestamp":1531710826,"collections":[{"temperature":26.700000762939454,"mac":"ABCDEF","humidity":75.0999984741211},{"temperature":25.899999618530275,"mac":"ABCDE2","humidity":74.5999984741211}],"address":"DEFAB"}}

from x2struct.

HsiangCheng avatar HsiangCheng commented on July 29, 2024

抱歉,未能及时回复。我的同事还在调试代码中,你的回复对我们确定方向很有帮助,谢谢(* ̄︶ ̄)

from x2struct.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.