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

相关推荐
ONE_PUNCH_Ge2 小时前
Go 语言变量
开发语言
幼稚园的山代王2 小时前
go语言了解
开发语言·后端·golang
晚风残2 小时前
【C++ Primer】第六章:函数
开发语言·c++·算法·c++ primer
满天星83035772 小时前
【C++】AVL树的模拟实现
开发语言·c++·算法·stl
weixin_456904272 小时前
基于.NET Framework 4.0的串口通信
开发语言·c#·.net
ss2732 小时前
手写MyBatis第107弹:@MapperScan原理与SqlSessionTemplate线程安全机制
java·开发语言·后端·mybatis
麦麦鸡腿堡3 小时前
Java的动态绑定机制(重要)
java·开发语言·算法
时间之里3 小时前
【c++】:Lambda 表达式介绍和使用
开发语言·c++
Tiger_shl3 小时前
C# 预处理指令 (# 指令) 详解
开发语言·c#
@Kerry~4 小时前
phpstudy .htaccess 文件内容
java·开发语言·前端