json_lite.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // 极简 JSON **只读**解析(零外部依赖)—— 只为"现场用配置文件改参数,不重编译"服务。
  2. //
  3. // 为什么不引第三方库(nlohmann/json 等):
  4. // * 交付形态要求核心零外部依赖(cpp/README.md §1):一个第三方头就会把这条打破,
  5. // 而现场只需要"读 configs/*.json 里的标量 / 数组 / 字符串";
  6. // * 自实现才能保证**确定性**(无随机、无 locale 依赖、无异常抛出),
  7. // 便于自检与现场排错(现场最怕"一个字母打错,整机起不来却看不出哪里错")。
  8. //
  9. // 与 Python 侧的一致性(避免同一个文件两种解释):
  10. // * 语法按 RFC 8259 **严格**解析(与 Python `json` 模块同口径):不支持注释、不支持尾逗号、
  11. // 字符串必须双引号、数字不接受前导 `+` 与 `01`;
  12. // * `\uXXXX` 按 UTF-8 输出(含代理对)→ 配置里写中文 `_note` 能原样保留;
  13. // * 重复键:后值覆盖前值(与 Python 一致);
  14. // * 解析失败返回**带行列号的错误文本**(不抛异常),调用点原样打印即可定位。
  15. #pragma once
  16. #include <cstddef>
  17. #include <string>
  18. #include <utility>
  19. #include <vector>
  20. namespace portcv {
  21. namespace json {
  22. /// JSON 值类型。
  23. enum class Type { Null, Bool, Number, String, Array, Object };
  24. /// 只读 JSON 值。配置文件都很小,拷贝成本可忽略,因此直接用值语义(不搞 share_ptr)。
  25. struct Value {
  26. Type type = Type::Null;
  27. bool boolean = false; ///< type == Bool 时有意义
  28. double number = 0.0; ///< type == Number 时有意义
  29. /// type == Number 时的**原始字面量**(如 "180" / "180.0" / "1e+16")。
  30. /// 为什么保留:Python `json` 区分 int/float(`dumps(180)`→"180",`dumps(180.0)`→"180.0"),
  31. /// 只留 double 就把这个区别丢了 —— 标定记录的 sha256 会因此与 Python 不一致。
  32. std::string raw;
  33. std::string text; ///< type == String 时有意义
  34. std::vector<Value> items; ///< type == Array
  35. std::vector<std::pair<std::string, Value>> members; ///< type == Object(保留书写顺序)
  36. bool is_null() const { return type == Type::Null; }
  37. bool is_bool() const { return type == Type::Bool; }
  38. bool is_number() const { return type == Type::Number; }
  39. bool is_string() const { return type == Type::String; }
  40. bool is_array() const { return type == Type::Array; }
  41. bool is_object() const { return type == Type::Object; }
  42. /// 数组元素数 / 对象成员数;其它类型返回 0(字符串长度请直接看 `text.size()`)。
  43. std::size_t size() const;
  44. /// 取对象成员;不存在返回 Null 静态空值(可直接 `.is_number()` 判空)。
  45. const Value& member(const std::string& key) const;
  46. /// 取数组元素;越界返回 Null。
  47. const Value& item(std::size_t index) const;
  48. /// 是否含某成员。
  49. bool has(const std::string& key) const;
  50. /// 对象成员名(按书写顺序)。
  51. std::vector<std::string> keys() const;
  52. /// 类型名("null"/"bool"/"number"/"string"/"array"/"object",错误信息与日志用)。
  53. const char* type_name() const;
  54. };
  55. /// 解析 JSON 文本;失败返回 Null 并填写 `error`(含 1 基行列号)。
  56. Value parse(const std::string& text, std::string* error);
  57. /// 读文件并解析(UTF-8;容忍开头 BOM —— 现场用记事本另存常带 BOM,
  58. /// 而 BOM 不是内容,丢掉不影响语义)。失败时填写 `error`。
  59. Value parse_file(const std::string& path, std::string* error);
  60. /// 读整个文本文件(二进制读,不做换行转换;UTF-8 BOM 会被去掉)。
  61. bool read_file(const std::string& path, std::string* out, std::string* error);
  62. } // namespace json
  63. } // namespace portcv