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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Grails2.1.0/Java7在Windows8.1上从何处获取当前用户名?   JavaSpringJMSActiveMQ   java异常处理,捕获导致while循环停止   sql server 2008将日期时间解析为JAVA日期   java是第一个servlet,但无法打开它   如何在Java中使用XML bean创建典型的XML头?   java将iOs应用程序转换为Android   java将jsp页面内容读取到其他jsp页面中的html   客户端计算机中小型数据库应用程序的java实现   java同步和ServletContextListener   安卓 Java将所有转义字符替换为双转义   当我在布局单元中实现ScrollView时,java GridView的setOnItemLongClickListener不起作用(使用适配器)   禁用Java web服务端点Wsdl   java如何编写一个程序来反转用户输入的数字