QT : Bson\Json互转

简介

Bson 介绍

(1)官网 BSON (Binary JSON) Serialization

(2)百度百科 BSON_百度百科

Json介绍

菜鸟教程 JSON 教程 | 菜鸟教程

转换

QT /CPP

cpp 复制代码
#include <QCoreApplication>
#include <QJsonDocument>
#include <QFile>
#include <QDebug>
 
#include "bson/Value.h"
 
BSON::Value gbsonDoc = BSON::Object{{"undefined", BSON::Value{}},
                               {"int32", (BSON::int32)1},
                               {"int64", (BSON::int64)1},
                               {"double", 3.14},
                               {"true", true},
                               {"false", false},
                               {"string", "foobar"},
                               {"datetime", std::chrono::milliseconds{123}},
                               {"object", BSON::Object{{"foo", "bar"}}},
                               {"array", BSON::Array{1, 2, 3}}};
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
 
    std::string bsonString = gbsonDoc.toBSON(); // bson结构->bson字符串
    BSON::Value bsonDoc = BSON::Value::fromBSON(bsonString); // bson字符串->bson结构
 
    QString jsonString = QString::fromLocal8Bit(gbsonDoc.toJSON().c_str()); // bson结构->json字符串
    QJsonDocument jsonDoc = QJsonDocument::fromJson(QByteArray::fromStdString(bsonDoc.toJSON()));  // bson结构->QJsonDocument
 
    BSON::Value bsonDocfromJson = BSON::Value::fromJSON(std::string(const_cast<const char *>(jsonString.toLocal8Bit().constData()))); // json字符串->bson结构
 
 
    qDebug() << "Bson String: " << QByteArray::fromStdString(bsonDoc.toJSON());
    qDebug() << "Json String: " << jsonString;
    qDebug() << "Json Document: " << jsonDoc;
 
    return a.exec();
}

​参考:

https://github.com/trusch/libbson

资源:

QT 例子代码 - https://download.csdn.net/download/halo_hsuh/12284550

相关推荐
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner2 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Venuslite3 天前
从 Unexpected token < 到 Extra data:一次讲清 JSON 解析错误的排查思路
json
Quz7 天前
QML Hello World 入门示例
qt
疯狂SQL9 天前
手写高性能在线 JSON 工具|Web Worker 工程化打包 + 语法自动修复 + 多语言代码生成实战
typescript·json·next.js·web worker·前端性能优化·esbuild·源码实战
xcyxiner10 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner11 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner11 天前
DicomViewer (添加模型类)3
qt
xcyxiner12 天前
DicomViewer (目录调整) 2
qt
xcyxiner12 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt