json解析器的包装器,允许注释、多行字符串和尾随逗号
jsoncomment的Python项目详细描述
json注释
json解析器的包装器,允许注释、多行字符串和尾随逗号
依赖关系
- Python3.3+
- TOdo新增功能
可选
- ujson 1.30+
说明
json注释允许解析json文件或字符串:
- 单行和多行注释
- 多行数据字符串
- 对象和数组中最后一项之后的尾随逗号
此包与任何支持以下功能的json解析器一起工作:
load(fp, ...)
分析文件loads(s, ...)
解析字符串
通过向这些调用添加预处理器。
评论
#
、;
和//
用于单行注释/*
和*/
包含多行注释
不支持内联注释。
多行字符串
任何字符串都可以是多行,甚至是对象键。
- 多行字符串以
"""
开头和结尾,类似于python中的 - 预处理器将所有行合并为一个json标准字符串
- 每行保留一个尾随空格(如果存在)
- 新线没有保留。要硬编码字符串中的新行,请使用
\\n
自定义方法
文件加载
loadf(path, *args, default = None, **kwargs)
打开带有注释的json文件。如果加载或分析失败,则允许使用默认值
文件保存
dumpf(json_obj, path, *args, indent=4, **kwargs)
用缩进保存json文件
安装
pip install jsoncomment
或
- 下载源代码
python setup.py install
用法
from jsoncomment import JsonComment
string = "[]"
json = JsonComment()
json_obj = json.loads(string)
示例
添加到/examples目录中
限制
#
、;
、//
和/*
前面只能有同一行的空格或制表符*/
后面只能跟同一行的空白或制表符- 尾随逗号必须是同一行的最后一个字符
来源
Source Bitbucket上的MIT许可证提供的代码。
API
添加到顶层__init__.py
如何读取api
API分为:
* `User Interface` for common use
* `Developer Interface` exposing some internals that could be useful
对于每个项(函数或类),有两个注释块,分别位于项定义的上方和下方:
* The top describes the return values
* The bottom describes the item and call variables
如果调用变量具有默认值或使用duck类型,则说明每个允许的值
示例:
# return_value
# description
from .some_module import SomeClass
# SomeClass description
# (
# variable_1,
# description
# variable_2 = something,
# description
# = Default
# description of default value ( something )
# = something_2
# description of alternate form ( duck typing )
# )
描述return_value = SomeClass(variable_1, variable_2 = current_value)
联系人
加斯帕雷·伊恩戈@email