json C++开源库:JSON for Modern C++的几个小问题

JSON for Modern C++是nlohmann在GitHub上开源的读写json文件的C++库,使用者仅需包含一个json.hpp文件即可。

开源地址为:GitHub - nlohmann/json: JSON for Modern C++

使用起来比较简单的了,首先从GitHub上将源码下载到本地,使用CMake编译源码(确保勾选了JSON_BuildTests)

在编译工程中找到unit-readme.cpp、unit-serialization.cpp、unit-deserialization.cpp

根据这三个测试代码就可以快速入门了。

下面主要讲使用这个库遇到的几个问题:

1、输出顺序

在测试unit-readme.cpp时,会发现示例代码输出的json对象的顺序并不是想要的,而是自动按照字母表顺序进行了排列,此为第一大坑!

以下为unit-readme.cpp中声明json对象的代码,用std::cout 输出j2,会发现顺序被重新排列。

            json j2 =
            {
                {"pi", 3.141},
                {"happy", true},
                {"name", "Niels"},
                {"nothing", nullptr},
                {
                    "answer", {
                        {"everything", 42}
                    }
                },
                {"list", {1, 0, 2}},
                {
                    "object", {
                        {"currency", "USD"},
                        {"value", 42.99}
                    }
                }
            };

实际使用json时,对象的顺序是非常重要的,如何解决这个问题呢?

为了寻求这个问题的解决方法,我在他的GitHub开源页面中,用浏览器搜索“order”,定位最后一个“order”

Order of object keys

By default, the library does not preserve the insertion order of object elements. This is standards-compliant, as the JSON standard defines objects as "an unordered collection of zero or more name/value pairs".

If you do want to preserve the insertion order, you can try the type nlohmann::ordered_json. Alternatively, you can use a more sophisticated ordered map like tsl::ordered_map (integration) or nlohmann::fifo_map (integration).

 可以看到,其实,作者在GitHub中已经作了说明,可以采用nlohmann::ordered_json类来声明json对象,为方便起见,可在代码开头using Json = nlohmann::ordered_json; 这样用Json来声明对象就好了。

2. 浮点数输出精度问题

在C++中浮点变量打印时经常会出现这样的现象:

                      double a = 14.55;

输出字符串时就变成了14.549999999

并且在外部通过precision函数设置stringstream或者std::cout的精度位数并没有什么用。

在GitHub中的issue也有人提出类似的问题:https://github.com/nlohmann/json/issues/1421

我通过调试作者的源代码,发现他把所有数字都先转换为字符串再统一输出。

在single_include\nlohmann\json.hpp文件中,下列函数将浮点型的x先通过to_chars函数转换为字符串,再通过o输出。那么无论外部对o进行怎样的精度设置,实际上在to_chars中并没有起作用。

    void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
    {
        auto* begin = number_buffer.data();
        auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);

        o->write_characters(begin, static_cast<size_t>(end - begin));
    }

这里有两个思路:一是改写to_chars函数,使得它根据o(当我用std::cout或stringstream进行输出时,它是output_stream_adapter类的对象)的精度来对浮点数进行转换;二是直接修改这个dump_float函数,对于浮点型的数,不用先转到字符串再输出,二是直接用o的<<操作符输出浮点数。

由于to_chars函数代码写的极其难看懂,不好改造,所以我采用第二种方法:

首先在output_stream_adapter的基类output_adapter_protocol中增加一个write_characters(double lf)的重载函数,并声明为纯虚函数,然后在output_stream_adapter类中重写这个接口:

    void write_characters(double lf) override
    {
        stream<<lf;
    }

然后将以上的dump_float函数改为:

    void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
    {
        o->write_characters(x);
    }

在外部调用json库用std::cout或stringstream进行输出浮点数时,可先调用precision函数设置精度,这样就能控制浮点数输出的位数精度了

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值