From 62f8f9e4ee066ecb562de7969180a07cb3d41f57 Mon Sep 17 00:00:00 2001 From: cheliequan <liequanche@126.com> Date: 星期二, 17 一月 2023 16:40:22 +0800 Subject: [PATCH] 增加根据protobuf转json函数 template <class T> bool bus_dbapi_get_json( T &list, string &json) --- dbapi.h | 2 main.cpp | 120 ++++++++++++++++++++++++++++++++++++++++ util.cpp | 2 util.h | 30 ++++++++++ camera.cpp | 10 +++ 5 files changed, 162 insertions(+), 2 deletions(-) diff --git a/camera.cpp b/camera.cpp index 638d4d2..e02a8db 100644 --- a/camera.cpp +++ b/camera.cpp @@ -68,4 +68,12 @@ params.clear(); return ret; -} \ No newline at end of file +} + + +bool dbapi_get_json_by_cameralist(std::vector<protomsg::Camera> &list, string &json) { + bool ret = bus_dbapi_get_json(list, json); + + return ret; +} + diff --git a/dbapi.h b/dbapi.h index e2821f5..7bcf746 100644 --- a/dbapi.h +++ b/dbapi.h @@ -27,4 +27,6 @@ bool dbapi_get_polygons(void *handle, std::vector<protomsg::CameraPolygon> &list); bool dbapi_get_polygon_relations(void *handle, std::vector<protomsg::CameraPolygonRelation> &list); bool dbapi_get_server_get_is_sys_expired(void *handle); +bool dbapi_get_json_by_cameralist(std::vector<protomsg::Camera> &list, string &json); + #endif diff --git a/main.cpp b/main.cpp index 0262e12..c653594 100644 --- a/main.cpp +++ b/main.cpp @@ -64,5 +64,125 @@ } } + string json; + dbapi_get_json_by_cameralist(list, json); + + printf("Camera list to json:%s\n", json.c_str()); + + + /* [ + { + "addr": "璐㈠姟瀹�", + "alias": "", + "brand": "", + "coordTransform": "", + "floor": -9999, + "id": "2bdee058-43d9-4e95-9ba1-2f56920f6d7c", + "ip": "", + "is_running": false, + "latitude": 0, + "linkCams": null, + "longitude": 0, + "name": "5.152锛堣储鍔″锛�", + "password": "", + "port": 0, + "reserved": "", + "resolution_height": 0, + "resolution_width": 0, + "rtsp": "rtsp://admin:a1234567@192.168.5.152:554/h264/ch1/main/av_stream", + "runServerName": "189鏈嶅姟", + "run_enable": false, + "run_server_id": "DSVAD010120190703", + "run_type": -1, + "snapshot_url": "192.168.20.189:6700/71,1c82608f5aab92?collection=DSVAD010120190703-persistent", + "status": -2, + "tasks": [ + { + "hasRule": true, + "taskname": "绂诲矖" + } + ], + "type": 0, + "username": "", + "voiceEnable": false, + "voiceId": "" + }, + { + "addr": "浜у搧鏃�2", + "alias": "", + "brand": "", + "coordTransform": "", + "floor": -9999, + "id": "d05618a4-7cdd-4b64-888c-2b1c29d3f32a", + "ip": "", + "is_running": false, + "latitude": 0, + "linkCams": null, + "longitude": 0, + "name": "5.151锛堜骇鍝佹梺锛�", + "password": "", + "port": 0, + "reserved": "", + "resolution_height": 0, + "resolution_width": 0, + "rtsp": "rtsp://admin:a1234567@192.168.5.151:554/h265/ch1/main/av_stream", + "runServerName": "189鏈嶅姟", + "run_enable": false, + "run_server_id": "DSVAD010120190703", + "run_type": -1, + "snapshot_url": "192.168.20.189:6700/72,1c9ea52ffe3560?collection=DSVAD010120190703-persistent", + "status": -2, + "tasks": [ + { + "hasRule": true, + "taskname": "鍏ヤ镜" + }, + { + "hasRule": true, + "taskname": "浜у搧鍖哄煙浜哄憳绂诲矖" + }, + { + "hasRule": true, + "taskname": "鑴卞矖" + } + ], + "type": 0, + "username": "", + "voiceEnable": false, + "voiceId": "" + }, + { + "addr": "娓f补瑁呰溅10鍙�", + "alias": "", + "brand": "", + "coordTransform": "", + "floor": -9999, + "id": "8be6055f-639c-4ea0-bcec-99b6b5b95499", + "ip": "", + "is_running": false, + "latitude": 0, + "linkCams": null, + "longitude": 0, + "name": "娓f补瑁呰溅10鍙�", + "password": "", + "port": 0, + "reserved": "", + "resolution_height": 0, + "resolution_width": 0, + "rtsp": "rtsp://admin:a1234567@192.168.5.53:554/h264/ch1/main/av_stream", + "runServerName": "189鏈嶅姟", + "run_enable": true, + "run_server_id": "DSVAD010120190703", + "run_type": -1, + "snapshot_url": "192.168.20.189:6700/72,1c8f82b46690a6?collection=DSVAD010120190703-persistent", + "status": -2, + "tasks": null, + "type": 0, + "username": "", + "voiceEnable": false, + "voiceId": "" + } +]*/ + return 0; } diff --git a/util.cpp b/util.cpp index 26a0dda..dcfa0eb 100644 --- a/util.cpp +++ b/util.cpp @@ -32,7 +32,7 @@ bool proto_to_json(const google::protobuf::Message &message, std::string &json) { google::protobuf::util::JsonPrintOptions options; - options.add_whitespace = true; + options.add_whitespace = false; options.always_print_primitive_fields = true; options.preserve_proto_field_names = true; diff --git a/util.h b/util.h index 5539387..fe470fa 100644 --- a/util.h +++ b/util.h @@ -38,6 +38,36 @@ bool bus_dbapi_get_str(void *handle, const char *topic, char **pptr_str); int get_value_by_key(char *json, const char *key, char **pptr_data); + +template <class T> +bool bus_dbapi_get_json( T &list, string &json) +{ + using TMPT = typename std::remove_reference<decltype(*list.begin())>::type; + TMPT t; + string jsonString; + int len = list.size(); + bRet = false; + + jsonString += "["; + + for ( int i= 0 ; i < len ; i++ ) + { + string tmpjsonString; + bRet = proto_to_json(list[i], tmpjsonString); + if ( (len > 1) &&(i < len - 1)) + { + tmpjsonString += ","; + } + jsonString += tmpjsonString; + } + + jsonString += "]"; + + json = jsonString; + +} + + template <class T> bool bus_dbapi_get_list(char *json, const char *flag_key, const char *data_key, T &list) { char *ptr_value = NULL; -- Gitblit v1.8.0