Skip to content

Q&A (zh)

haibo2.liu edited this page Sep 29, 2022 · 2 revisions

常见问题

Q:

抛出异常 config deserialization error: unexpected token 'end_of_input'

A:

往往是读取文件失败导致的,请检查文件路径是否正确。

Q:

Windows 下中文乱码

A:

这是由于在中文环境下,Visual Studio 和 Windows 终端使用的编码都是 gb2312,而 configor 仅支持 unicode。

Visual Studio 使用 utf-8 非常困难,建议直接忽略编码,对中文不做处理:

// 使用 encoding::ignore 忽略 parse 编码
json::parse("{\"chinese\":\"一些带有中文的JSON字符串\"}", { json::parser::with_encoding<encoding::ignore>() });

// 使用 encoding::ignore 忽略 dump 编码
json::dump(j, { json::serializer::with_encoding<encoding::ignore>() });

或使用自定义的json类:

using myjson = configor::basic_json<value_tplargs, encoding::ignore>;

Q:

如何保证 JSON 序列化时按 key 的插入顺序输出?

A:

configor 内部使用 std::map 存储 kv 对象,默认是按 key 的字符串大小排序的。

建议用第三方库替换 std::map,比如 nlohmann/fifo_map,然后声明 fifo_json 替换 json 来保证插入序

struct fifo_value_tpl_args : value_tplargs
{
    template <class _Kty, class _Ty, class... _Args>
    using object_type = nlohmann::fifo_map<_Kty, _Ty>;
};

// fifo_json 是按插入序排列的
using fifo_json = configor::basic_json<fifo_value_tpl_args>;

Overview

  • Quick start ( en | zh )
  • Examples ( en | zh )

Features

  • Value operation ( en | zh )
  • Serialization ( en | zh )
  • Custom conversion ( en | zh )
  • Encoding support ( en | zh )

Other

  • Q&A ( en | zh )
  • Advanced ( en | zh )
Clone this wiki locally