laktak/hjson
Composer 安装命令:
composer require laktak/hjson
包简介
JSON for Humans. A configuration file format with relaxed syntax, fewer mistakes and more comments.
README 文档
README
Hjson, the Human JSON. A configuration file format for humans. Relaxed syntax, fewer mistakes, more comments.
{
# specify rate in requests/second (because comments are helpful!)
rate: 1000
// prefer c-style comments?
/* feeling old fashioned? */
# did you notice that rate doesn't need quotes?
hey: look ma, no quotes for strings either!
# best of all
notice: []
anything: ?
# yes, commas are optional!
}
The PHP implementation of Hjson is based on hjson-js. For other platforms see hjson.github.io.
Install from composer
composer require laktak/hjson
Usage
use HJSON\HJSONParser;
use HJSON\HJSONStringifier;
$parser = new HJSONParser();
$obj = $parser->parse(hjsonText);
$stringifier = new HJSONStringifier();
$text = $stringifier->stringify($obj);
API
HJSONParser: parse($text, $options)
This method parses JSON or Hjson text to produce an object or array.
- text: the string to parse as JSON or Hjson
- options: array
- keepWsc: boolean, keep white space and comments. This is useful if you want to edit an hjson file and save it while preserving comments (default false)
- assoc: boolean, return associative array instead of object (default false)
HJSONStringifier: stringify($value, $options)
This method produces Hjson text from a value.
- value: any value, usually an object or array.
- options: array
- keepWsc: boolean, keep white space. See parse.
- bracesSameLine: boolean, makes braces appear on the same line as the key name. Default false.
- quotes: string, controls how strings are displayed.
- "min": no quotes whenever possible (default)
- "always": always use quotes
- space: specifies the indentation of nested structures. If it is a number, it will specify the number of spaces to indent at each level. If it is a string (such as '\t' or ' '), it contains the characters used to indent at each level.
- eol: specifies the EOL sequence
modify & keep comments
You can modify a Hjson file and keep the whitespace & comments intact. This is useful if an app updates its config file.
$parser = new HJSONParser();
$stringifier = new HJSONStringifier();
$text = "{
# specify rate in requests/second (because comments are helpful!)
rate: 1000
// prefer c-style comments?
/* feeling old fashioned? */
# did you notice that rate doesn't need quotes?
hey: look ma, no quotes for strings either!
# best of all
notice: []
anything: ?
# yes, commas are optional!
array: [
// hello
0
1
2
]
}";
// Parse, keep whitespace and comments
$data = $parser->parseWsc($text);
// Modify like you normally would
$data->rate = 500;
// You can also edit comments by accessing __WSC__
$wsc1 = &$data->__WSC__; // for objects
$wsc2 = &$data->array['__WSC__']; // for arrays
// __WSC__ for objects contains { c: {}, o: [] }
// - c with the actual comment and, firts comment is key ' '
// - o (array) with the order of the members
$emptyKey = " ";
$wsc1->c->$emptyKey = "\n # This is the first comment";
$wsc1->c->rate = "\n # This is the comment after rate";
// Sort comments order just because we can
sort($wsc1->o);
// Edit array comments
$wsc2[0] .= ' world';
// convert back to Hjson
$text2 = $stringifier->stringifyWsc($data);
History
laktak/hjson 适用场景与选型建议
laktak/hjson 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 243.95k 次下载、GitHub Stars 达 86, 最近一次更新时间为 2016 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「parser」 「config」 「comments」 「human」 「serializer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laktak/hjson 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laktak/hjson 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laktak/hjson 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Kinikit - PHP Application development framework MVC component
A Zend Framework module to quickly and easily set PHP settings.
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
An MT940 bank statement parser for PHP
统计信息
- 总下载量: 243.95k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 87
- 点击次数: 20
- 依赖项目数: 10
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-16