C/C++ json库

文章目录

  • 一、介绍
    • 1.1 json 介绍
  • 二、C/C++ json 库选型
    • 2.1 选型范围
    • 2.2 jsoncpp
      • 2.2.2 jsoncpp 编译和交叉编译
    • 2.3 rapidjson
    • 2.4 nlohmann/json
    • 2.5 sonic-cpp
  • 五、常见问题
    • 5.1 jsoncpp 中关于浮点数的控制和中文显示问题
    • 5.2 jsoncpp序列化double类型时精度损失问题的解决办法

一、介绍

1.1 json 介绍

  • 官网:http://www.json.org/json-zh.html
  • JSON是什么?如何正确理解?

二、C/C++ json 库选型

2.1 选型范围

  • 资料
    • 官网:http://www.json.org/json-zh.html
    • 开源库比较:https://github.com/miloyip/nativejson-benchmark
    • C++中json库的选择
    • C/C++ 开源 JSON 程序库性能及标准符合程度评测
  • 开源库
    • Rapidjson、Rapidjson_FullPrec、Rapidjson_AutoUTF
    • nlohmann / json
    • jsoncpp
  • 结论:
    • 注重最佳性能,选 Rapidjson (cereal序列化库使用)
    • 注重易用性,选 jsoncpp (ros 使用)、nlohmann / json

在这里插入图片描述

2.2 jsoncpp

  • 精度控制:precision
    • 15,16,17:原值会变
    • 0-14:原值不变的情况下,四舍五入

2.2.2 jsoncpp 编译和交叉编译

  • jsoncpp 的编译和交叉编译

  • 编译

    mkdir build; cd build
    cmake -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_INSTALL_PREFIX=`pwd`/result \
    -DJSONCPP_WITH_TESTS=OFF \ ..
    make install -j4
    
  • 交叉编译

    mkdir build; cd build
    cmake -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_INSTALL_PREFIX=`pwd`/result \
    -DJSONCPP_WITH_TESTS=OFF \ ..
    -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake \ ..
    make install -j4# toolchain.cmake 定义交叉编译环境变量
    SET(CMAKE_SYSROOT "/opt/fslc-x11/2.4.4/sysroots/armv7at2hf-neon-fslc-linux-gnueabi")
    set(CMAKE_SYSTEM_NAME Linux)
    set(CMAKE_SYSTEM_PROCESSOR arm)
    set(TOOLS /opt/fslc-x11/2.4.4/sysroots/x86_64-fslcsdk-linux/usr/bin/arm-fslc-linux-gnueabi)
    set(CMAKE_C_COMPILER "${TOOLS}/arm-fslc-linux-gnueabi-gcc")
    set(CMAKE_CXX_COMPILER "${TOOLS}/arm-fslc-linux-gnueabi-g++")
    set(CMAKE_AR "${TOOLS}/arm-fslc-linux-gnueabi-ar")
    
  • include/json

  • lib/cmake/xxx.cmake

  • lib/pkgconfig/jsoncpp.pc

  • pkgconfig

    mayue@PC-MAYUE:/mnt/d/hik/opensource/jsoncpp-1.9.5/build/result/lib$ cat pkgconfig/jsoncpp.pc
    prefix=/mnt/d/hik/opensource/jsoncpp-1.9.5/build/result
    exec_prefix=/mnt/d/hik/opensource/jsoncpp-1.9.5/build/result
    libdir=${exec_prefix}/lib
    includedir=${prefix}/includeName: jsoncpp
    Description: A C++ library for interacting with JSON
    Version: 1.9.5
    URL: https://github.com/open-source-parsers/jsoncpp
    Libs: -L${libdir} -ljsoncpp
    Cflags: -I${includedir}
    

指定连接静态库

g++ jsoncpp-test.cpp -I./include -L ./lib -l:libjsoncpp.a

2.3 rapidjson

  • http://rapidjson.org/

2.4 nlohmann/json

  • https://github.com/nlohmann/json
  • nlohmann入门使用总结

2.5 sonic-cpp

  • 当前仅支持amd64
  • 开源 C++ JSON 库 sonic-cpp解析性能为 rapidjson 的 2.5 倍
  • 性能提升 2.5 倍!字节开源高性能 C++ JSON 库 sonic-cpp

五、常见问题

5.1 jsoncpp 中关于浮点数的控制和中文显示问题

  • jsoncpp 中关于浮点数的控制和中文显示问题

5.2 jsoncpp序列化double类型时精度损失问题的解决办法

  • jsoncpp序列化double类型时精度损失问题的解决办法

解决办法1:此法不需要改源码,使用StreamWriterBuilder进行序列化

#include <json/json.h>
#include <json/writer.h>
#include <iostream>
#include <string>void test_precision(int precision)
{Json::Value root;root["pi"] = 3.1415926;root["count"] = 43.32558674566;Json::StreamWriterBuilder builder;//设置精度 注意这个默认设置的是数字总长度 //如果想设置小数点后的位数需设置precisionType为decimalbuilder.settings_["precision"] = precision;//设置精度类型 只可设置2种字符串 significant精度位数为数字总长度(jsoncpp默认为此类型) decimal精度位数为小数点后的长度builder.settings_["precisionType"] = "decimal";// 设置输出为紧凑格式,不带换行和空格builder["commentStyle"] = "None";   // 防止输出注释,默认就是nonebuilder["indentation"] = "";        // 空字符串表示不缩进std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());std::ostringstream oss;writer->write(root, &oss);std::string jsonText = oss.str();// 输出 JSON 字符串std::cout << "specified precision: " << precision << ", content:" << jsonText << std::endl;
}int main() {for(int i=17; i>=0; i--){test_precision(i);}return 0;
}
specified precision: 17, content:{"count":43.32558674565999723,"pi":3.14159260000000007}
specified precision: 16, content:{"count":43.3255867456599972,"pi":3.1415926000000001}
specified precision: 15, content:{"count":43.325586745659997,"pi":3.1415926}
specified precision: 14, content:{"count":43.32558674566,"pi":3.1415926}
specified precision: 13, content:{"count":43.32558674566,"pi":3.1415926}
specified precision: 12, content:{"count":43.32558674566,"pi":3.1415926}
specified precision: 11, content:{"count":43.32558674566,"pi":3.1415926}
specified precision: 10, content:{"count":43.3255867457,"pi":3.1415926}
specified precision: 9, content:{"count":43.325586746,"pi":3.1415926}
specified precision: 8, content:{"count":43.32558675,"pi":3.1415926}
specified precision: 7, content:{"count":43.3255867,"pi":3.1415926}
specified precision: 6, content:{"count":43.325587,"pi":3.141593}
specified precision: 5, content:{"count":43.32559,"pi":3.14159}
specified precision: 4, content:{"count":43.3256,"pi":3.1416}
specified precision: 3, content:{"count":43.326,"pi":3.142}
specified precision: 2, content:{"count":43.33,"pi":3.14}
specified precision: 1, content:{"count":43.3,"pi":3.1}
specified precision: 0, content:{"count":43,"pi":3}
  • 精度控制:precision

    • 15,16,17:原值会变、四舍五入
    • 0-14:原值不变的情况下,四舍五入
  • 特别注意:精度设置一定要大于你需求的精度位数,比如需要三位可以设置4位或5位,因为最后一位可能会不准(做了四舍五入)

不足之处:
StreamWriterBuilder序列化的字符串是可读形式的,就像上面的输出,是有换行和缩进的(转换效率会比FastWrite低),我的服务端代码里其实不需要转换json为可读的,更需要的是效率,所以还有下面一种方法改FasetWrite源码

解决办法2:此法需要改源码,使用FastWriter进行序列化

  • 注意:需升级jsoncpp到最新版本1.9.5版本

  • 修改源码(writer.h):FastWriter类新增2个成员变量(precision_和precisionType_)和成员函数(set_precision和set_precisionType)

    #if defined(_MSC_VER)
    #pragma warning(push)
    #pragma warning(disable : 4996) // Deriving from deprecated class
    #endif
    class JSON_API FastWriter: public Writer {
    public:FastWriter();~FastWriter() override = default;void enableYAMLCompatibility();/** \brief Drop the "null" string from the writer's output for nullValues.* Strictly speaking, this is not valid JSON. But when the output is being* fed to a browser's JavaScript, it makes for smaller output and the* browser can handle the output just fine.*/void dropNullPlaceholders();void omitEndingLineFeed();public: // overridden from WriterString write(const Value& root) override;//设置精度位数void set_precision(unsigned int precision) { precision_ = (precision > 17)?17:precision; };//设置精度类型 默认为数字总长//入参:isDecimal true表示类型为小数点后长度 false表示类型为数字总长void set_precisionType(bool isDecimal) { isDecimal ? (precisionType_ = PrecisionType::decimalPlaces) : (precisionType_ = PrecisionType::significantDigits); };private:void writeValue(const Value& value);String document_;bool yamlCompatibilityEnabled_{false};bool dropNullPlaceholders_{false};bool omitEndingLineFeed_{false};int precision_{ 17 };//精度位数 默认17位PrecisionType precisionType_{ PrecisionType::significantDigits };//精度类型 默认为数字总长
    };
    #if defined(_MSC_VER)
    #pragma warning(pop)
    #endif
    
  • 修改源码(json_writer.cpp):只修改了1行代码,FastWriter::writeValue函数中case realValue的处理中调用的valueToString新增了2个参数传递(源码中没有传递用的函数默认值,现在传了并且可以通过新增的2个成员函数进行设置)

    void FastWriter::writeValue(const Value& value) {switch (value.type()) {case nullValue:if (!dropNullPlaceholders_)document_ += "null";break;case intValue:document_ += valueToString(value.asLargestInt());break;case uintValue:document_ += valueToString(value.asLargestUInt());break;case realValue://这里原先是document_ += valueToString(value.asDouble());//因为后2个参数没传,所以用的函数默认值即精度位数=17,精度类型=PrecisionType::significantDigits//修改后 现在会传这2个参数,具体值可以通过新增加的2个成员函数设置document_ += valueToString(value.asDouble(), precision_, precisionType_);break;case stringValue: {// Is NULL possible for value.string_? No.char const* str;char const* end;bool ok = value.getString(&str, &end);if (ok)document_ += valueToQuotedStringN(str, static_cast<size_t>(end - str));break;}case booleanValue:document_ += valueToString(value.asBool());break;case arrayValue: {document_ += '[';ArrayIndex size = value.size();for (ArrayIndex index = 0; index < size; ++index) {if (index > 0)document_ += ',';writeValue(value[index]);}document_ += ']';} break;case objectValue: {Value::Members members(value.getMemberNames());document_ += '{';for (auto it = members.begin(); it != members.end(); ++it) {const String& name = *it;if (it != members.begin())document_ += ',';document_ += valueToQuotedStringN(name.data(), name.length());document_ += yamlCompatibilityEnabled_ ? ": " : ":";writeValue(value[name]);}document_ += '}';} break;}
    }
    
  • 需要重新编译库,然后demo验证,使用的代码:

    #include <json/json.h>
    #include <json/writer.h>
    #include <iostream>
    #include <string>void test_precision(int precision)
    {Json::Value obj;Json::FastWriter write;write.set_precision(precision);write.set_precisionType(true);obj["d"] = 2.1;obj["d2"] = 9.111;obj["d3"] = 9.123456789;auto rtn = write.write(obj);std::cout << "specified precision: " << precision << ", rtn:" << rtn << std::endl;
    }int main() {for(int i=17; i>=0; i--){test_precision(i);}return 0;
    }
    
    specified precision: 17, rtn:{"d":2.10000000000000009,"d2":9.11100000000000065,"d3":9.12345678900000046}specified precision: 16, rtn:{"d":2.1000000000000001,"d2":9.1110000000000007,"d3":9.1234567890000005}specified precision: 15, rtn:{"d":2.1,"d2":9.111000000000001,"d3":9.123456789}specified precision: 14, rtn:{"d":2.1,"d2":9.111,"d3":9.123456789}specified precision: 13, rtn:{"d":2.1,"d2":9.111,"d3":9.123456789}specified precision: 12, rtn:{"d":2.1,"d2":9.111,"d3":9.123456789}specified precision: 11, rtn:{"d":2.1,"d2":9.111,"d3":9.123456789}specified precision: 10, rtn:{"d":2.1,"d2":9.111,"d3":9.123456789}specified precision: 9, rtn:{"d":2.1,"d2":9.111,"d3":9.123456789}specified precision: 8, rtn:{"d":2.1,"d2":9.111,"d3":9.12345679}specified precision: 7, rtn:{"d":2.1,"d2":9.111,"d3":9.1234568}specified precision: 6, rtn:{"d":2.1,"d2":9.111,"d3":9.123457}specified precision: 5, rtn:{"d":2.1,"d2":9.111,"d3":9.12346}specified precision: 4, rtn:{"d":2.1,"d2":9.111,"d3":9.1235}specified precision: 3, rtn:{"d":2.1,"d2":9.111,"d3":9.123}specified precision: 2, rtn:{"d":2.1,"d2":9.11,"d3":9.12}specified precision: 1, rtn:{"d":2.1,"d2":9.1,"d3":9.1}specified precision: 0, rtn:{"d":2,"d2":9,"d3":9}

C++ json序列化库有哪些,哪个性能最好

C++中有多种JSON序列化库可供选择,包括但不限于以下几种:

  1. Rapidjson:这是一个非常流行的C++ JSON库,以其高性能著称,由腾讯团队开发 。

  2. nlohmann/json:这是一个现代的、基于C++11的JSON库,以其易用性和直观的接口而受到许多C++程序员的青睐 。

  3. sonic-cpp:由字节跳动STE团队和服务框架团队共同研发的高效JSON库,它利用CPU硬件特性和向量化编程,大幅提高了序列化和反序列化的性能。据报道,其解析性能是rapidjson的2.5倍 。

  4. JsonCpp:这是一个成熟的库,提供了丰富的功能来处理JSON数据。

  5. simdjson:这是一个使用SIMD指令集来加速解析的库,它提供了快速的解析性能,但不支持修改解析后的JSON结构 。

  6. yyjson:这是一个追求解析性能的库,使用链表结构,但在查找数据时性能较差 。

在这些库中,sonic-cpp 被报道为性能最好的库,它不仅提供了高效的解析性能,还解决了其他一些库的缺点,如simdjson和yyjson的问题,并支持高效的增删改查操作 。此外,sonic-cpp已经在字节跳动的多个核心业务中大规模使用,并通过了工程化的考验 。

如果您对性能有极高的要求,sonic-cpp可能是一个不错的选择。然而,选择哪个库还应考虑其他因素,如易用性、社区支持、库的活跃度和维护情况。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/48152.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Rust编程-类面向对象编程

封装&#xff08;encapsulation&#xff09;&#xff1a; rust使用pub关键字来控制代码中哪些模块、类型、函数和方法是公开的&#xff0c;默认都是私有的 继承&#xff08;inheritance&#xff09;&#xff1a; rust里没有继承。 rust通过定义trait来实现代码共享。trait中也…

算法学习笔记(Hello算法)—— 初识算法

1、相关链接 Hello算法&#xff1a;Hello 算法 (hello-algo.com) 2、算法是什么 2.1 算法定义 算法是一系列明确、有限且有效的步骤或指令的集合&#xff0c;用于解决特定问题或执行特定任务。 算法具有以下基本特征&#xff1a; 输入&#xff1a;算法至少有一个输入&…

WPF中UI元素继承关系

在 WPF&#xff08;Windows Presentation Foundation&#xff09;框架中&#xff0c;UI 元素是基于一个层次化的类结构构建的&#xff0c;这个结构以 FrameworkElement 类为核心&#xff0c;大多数 UI 元素都是 FrameworkElement 或其派生类的子类。FrameworkElement 类本身又继…

libevent的event_config 配置相关接口

目录 3. event_config 配置相关3.1 event_config_new功能备注原型 3.2 event_config_free功能备注原型 3.2 event_config_set_flag功能备注原型 3.4 event_config_set_num_cpus_hint功能备注原型 3.5 event_config_set_max_dispatch_interval功能备注原型 3.6 event_config_req…

【JavaScript 算法】图的遍历:理解图的结构

&#x1f525; 个人主页&#xff1a;空白诗 文章目录 一、深度优先搜索&#xff08;DFS&#xff09;深度优先搜索的步骤深度优先搜索的JavaScript实现 二、广度优先搜索&#xff08;BFS&#xff09;广度优先搜索的步骤 三、应用场景四、总结 图的遍历是图论中的基本操作之一&am…

院内影像一体化平台PACS源码,C#语言的PACS/RIS系统,二级医院应用案例

全院级PACS系统源码&#xff0c;一体化应用系统整合&#xff0c;满足放射、超声、内窥镜中心、病理、检验等多个科室的工作流程和需求&#xff0c;为不同科室提供专业的解决方案&#xff0c;实现了全院乃至区域内信息互联互通、数据统一存储与管理等功能&#xff0c;做到以病人…

浪漫情怀:红酒中的诗意与情感

在生活的点滴细节中&#xff0c;总有些元素能触动我们内心较柔软的地方&#xff0c;唤起那份深深的浪漫情怀。而红酒&#xff0c;便是这其中的一种神奇媒介。它以其不同的色泽、香气和口感&#xff0c;让人沉醉其中&#xff0c;感受那份诗意与情感的交织。今天&#xff0c;就让…

C语言 | Leetcode C语言题解之第237题删除链表中的节点

题目&#xff1a; 题解&#xff1a; /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/void deleteNode(struct ListNode* node) {struct ListNode * p node->next;int temp;temp node->val;node->val…

Kotlin 协程简化回调

suspend 和 suspendCoroutine 实现 suspendCoroutine函数必须在协程作用域或挂起函数中才能调用&#xff0c;它接收一个Lambda表达式参数&#xff0c;主要作用是将当前协程立即挂起&#xff0c;然后在一个普通的线程中执行Lambda表达式中的代码。Lambda表达式的参数列表上会传…

常见排序算法总结

文章目录 比较排序冒泡排序选择排序插入排序归并排序快速排序堆排序希尔排序 非比较排序&#xff08;桶排序&#xff09;计数排序基数排序 比较排序 冒泡排序 嵌套循环&#xff0c;每次内层循环执行时&#xff0c;数组的每两个元素交换&#xff0c;将一个最大/小的数排到数组…

AIGC代码学习记录

本文主要记录不同的SD模型代码中实现的一些细节&#xff0c;如text2img,img2img,inpaint等等 1. 文生图 第一步&#xff1a;生成随机的latent feature (n,4,64,64)&#xff1b;n为生成的图片个数&#xff1b; 第二步&#xff1a;对于prompt用clip生成特征&#xff0c;正向提示…

PDF-Extract-Kit (PDF内容抽取开源项目)

Github 地址&#xff1a;https://github.com/opendatalab/PDF-Extract-Kit 整体介绍 PDF文档中包含大量知识信息&#xff0c;例如文本、表格、图像、公式等。此外&#xff0c;PDF的文档布局也相当复杂&#xff0c;页眉、页脚、表格标题、图片标题等等&#xff0c;提取高质量的…

C++:聚合类、嵌套类、局部类、union类详细介绍与分析

聚合类 (1)What&#xff08;什么是聚合类&#xff09; 本质是一个自定义类型的数据结构&#xff08;结构体或类&#xff09;&#xff0c;但聚合类有以下特性&#xff1a; 所有的成员都是public没有任何构造函数没有基类类内部没有初始值 (2)Why&#xff08;聚合类的作用&…

ES6 正则的扩展(十九)

1. 正则表达式字面量改进 特性&#xff1a;在 ES6 中&#xff0c;正则表达式字面量允许在字符串中使用斜杠&#xff08;/&#xff09;作为分隔符。 用法&#xff1a;简化正则表达式的书写。 const regex1 /foo/; const regex2 /foo/g; // 全局搜索2. u 修饰符&#xff08;U…

python调用chrome浏览器自动化如何选择元素

功能描述&#xff1a;在对话框输入文字&#xff0c;并发送。 注意&#xff1a; # 定位到多行文本输入框并输入内容。在selenium 4版本中&#xff0c;元素定位需要填写父元素和子元素名。 textarea driver.find_element(By.CSS_SELECTOR,textarea.el-textarea__inner) from …

前端出发能走多远——从一个网络请求开始

当通过浏览器输入一个网址时&#xff0c;浏览器经历了一些列的过程&#xff0c;最终将网页展示给用户。这个过程是如何进行的呢&#xff1f;我们可以通过一个简单的例子来了解这个过程。 假设我们输入了一个网址 https://www.example.com&#xff0c;我们可以通过浏览器的开发…

2024.04最新 鹦鹉优化算法 (PO) 开源MATLAB代码

!!!只需要代码的直接跳转到最后一节&#xff0c;有开源代码 1.算法简介2.灵感来源与核心思想2.1 灵感来源2.2 核心思想 3.主要步骤3.1 种群初始化3.2 觅食行为3.3 停留行为3.4 交流行为3.5 对陌生人的恐惧行为 4.伪代码与核心代码4.1流程图与伪代码流程图伪代码 4.2 核心代码(P…

nodejs下载+react安装

一、nodejs安装 1、nodejs下载 具体安装可参考连接&#xff1a;2023最新版Node.js下载安装及环境配置教程&#xff08;非常详细&#xff09;从零基础入门到精通&#xff0c;看完这一篇就够了_nodejs安装及环境配置-CSDN博客 下载地址&#xff1a;Node.js — 下载 Node.js 测…

ubuntu 更新源

前言 实现一键替换在线源 一键更新源 ubuntu 全球镜像站以下支持现有ubuntu 20&#xff0c;22&#xff0c;24 echo "Delete the default source" rm -rf /etc/apt/sources.listecho "Build a new source" cat <<EOF>>/etc/apt/sources.li…

微信小程序 button样式设置为图片的方法

微信小程序 button样式设置为图片的方法 background-image background-size与background-repeat与border:none;是button必须的 <view style" position: relative;"><button class"customer-service-btn" style"background-image: url(./st…