dcarbone/json-writer-plus
最新稳定版本:0.3.0
Composer 安装命令:
composer require dcarbone/json-writer-plus
包简介
Simple JSON Writer library for PHP 5.3+
关键字:
README 文档
README
Simple JSON Writer library for PHP 5.3+
The goal for this library was to provide a way to construct a JSON object similar in syntax to the PHP XMLWriter class.
Inclusion in your Composer app
Add
"dcarbone/json-writer-plus" : "0.2.*"
To your application's composer.json file.
Learn more about Composer here: https://getcomposer.org/
Basic usage
To get started creating your own JSON object:
use \DCarbone\JsonWriterPlus; // Create instance $jsonWriter = new JsonWriterPlus(); // Start the writer object $jsonWriter->startJson(); // Open a new object for population $jsonWriter->writeStartObject(); // Directly write a property name and value to the opened object $jsonWriter->writeObjectProperty('PropertyKey', 'PropertyValue'); // Write a new property name for later population $jsonWriter->writeObjectPropertyName('ValueArray'); // Start an array. Since we wrote a new property name above, it is automatically appended // to the parent object at the previously specified key $jsonWriter->writeStartArray(); // Add two values to the array $jsonWriter->writeValue("Value1"); $jsonWriter->writeValue("Value2"); // Close the array $jsonWriter->writeEndArray(); // Close the parent object $jsonWriter->writeEndObject(); // Close the writer $jsonWriter->endJson(); // See the "jsonized" version of the above actions echo $jsonWriter->getEncoded()."\n"; // See the internal representation of the above actions as PHP sees it echo '<pre>'; var_dump($jsonWriter->getUnencoded()); echo '</pre>';
The above code block will result in the following being output:
{"PropertyKey":"PropertyValue","ValueArray":["Value1","Value2"]}
object(stdClass)#4 (2) {
["PropertyKey"]=>
string(13) "PropertyValue"
["ValueArray"]=>
array(2) {
[0]=>
string(6) "Value1"
[1]=>
string(6) "Value2"
}
}
Starting out
The above example demonstrates a Json output with the primary element being an Object, but you may also start things out with an array:
// Initialize writer $jsonWriter = new JsonWriterPlus(); // Start writer $jsonWriter->startJson(); // Open root array $jsonWriter->writeStartArray(); // Open object as first item of root array $jsonWriter->writeStartObject(); $jsonWriter->writeObjectProperty('Property1', 'This object is inside an array!'); $jsonWriter->writeEndObject(); // Open new array as 2nd item of root array $jsonWriter->writeStartArray(); $jsonWriter->writeValue('Nested array value 1'); $jsonWriter->writeValue('Nested array value 2'); $jsonWriter->writeEndArray(); // Write a string value directly to root array as 3rd item $jsonWriter->writeValue('Root array value'); $jsonWriter->writeEndArray(); $jsonWriter->endJson(); echo $jsonWriter->getEncoded()."\n"; echo '<pre>'; var_dump($jsonWriter->getUnencoded()); echo '</pre>';
The above will output:
[{"Property1":"This object is inside an array!"},["Nested array value 1","Nested array value 2"],"Root array value"]
array(3) {
[0]=>
object(stdClass)#4 (1) {
["Property1"]=>
string(31) "This object is inside an array!"
}
[1]=>
array(2) {
[0]=>
string(20) "Nested array value 1"
[1]=>
string(20) "Nested array value 2"
}
[2]=>
string(16) "Root array value"
}
Fun stuff
Lets say you have a JsonWriter instance already open and an array already constructed, and you wish to just append the entire thing to the Json output without looping through and manually performing actions. Well, good sir/ma'am/fish, you can!
$array = array( 'Look at all my cool information', 'My information is the coolest' ); $jsonWriter = new JsonWriterPlus(); $jsonWriter->startJson(); $jsonWriter->appendArray($array); $jsonWriter->endJson(); echo $jsonWriter->getEncoded()."\n"; echo '<pre>'; var_dump($jsonWriter->getUnencoded()); echo '</pre>';
The above will output:
["Look at all my cool information","My information is the coolest"]
array(2) { [0]=> string(31) "Look at all my cool information" [1]=> string(29) "My information is the coolest" }
$array = array( 'Look at all my cool information', 'property1' => 'property 1 is the coolest property', 'property2' => 'property2 is the next coolest property', 'this is also cool information' ); $jsonWriter = new JsonWriterPlus(); $jsonWriter->startJson(); $jsonWriter->appendArray($array); $jsonWriter->endJson(); echo $jsonWriter->getEncoded()."\n"; echo '<pre>'; var_dump($jsonWriter->getUnencoded()); echo '</pre>';
Will result in:
["Look at all my cool information",{"property1":"property 1 is the coolest property"},{"property2":"property2 is the next coolest property"},"this is also cool information"]
array(4) { [0]=> string(31) "Look at all my cool information" [1]=> object(stdClass)#4 (1) { ["property1"]=> string(34) "property 1 is the coolest property" } [2]=> object(stdClass)#5 (1) { ["property2"]=> string(38) "property2 is the next coolest property" } [3]=> string(29) "this is also cool information" }
You may also perform similar actions with an object via appendObject($object).
dcarbone/json-writer-plus 适用场景与选型建议
dcarbone/json-writer-plus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.24k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2014 年 06 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「json」 「jsonwriter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dcarbone/json-writer-plus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dcarbone/json-writer-plus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dcarbone/json-writer-plus 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Get wordpress nav menus and share the main site menus with the child sites.
JPOPHP (JSON Parser Object PHP) is a library for parsing the data in JSON format.
统计信息
- 总下载量: 1.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MPL-2.0
- 更新时间: 2014-06-22