beequeue/tweaky

用于转换JSON数据的库

dev-master 2016-03-30 23:52 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:31:04 UTC


README

Build Status Coverage Status

Tweaky是一个库和基于JSON格式的领域特定语言,允许对JSON有效负载进行自定义转换。它更关注于更改值而不是更改形式。主要用例是在模拟代理中指定对API响应的修改。

用法

通过composer包含

composer require beequeue/tweaky

示例用法

use Beequeue\Tweaky\Spec;
use Beequeue\Tweaky\Tweaky;

$inputJson =<<< END
{
  "a": 1,
  "b": "original",
  "c": [
    {"key": "first"},
    {"key": "second"},
    {"key": "last"}
  ],
  "d": "leave alone"
}
END;

$specJson =<<< END
{
  "transforms": [{
    "a": "{+10}",
    "b": "new value",
    "c": {
      "{[1]}": {
        "key": "middle"
      }
    }
  }]
}
END;

$spec = new Spec($specJson);
$tweaky = new Tweaky($spec);
$output = $tweaky->process($inputJson);

echo json_encode($output, JSON_PRETTY_PRINT);

将输出

{
    "a": 11,
    "b": "new value",
    "c": [
        {
            "key": "first"
        },
        {
            "key": "middle"
        },
        {
            "key": "last"
        }
    ],
    "d": "leave alone"
}