承接 vanilla/garden-jsont 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vanilla/garden-jsont

Composer 安装命令:

composer require vanilla/garden-jsont

包简介

A little JSON transformation library.

README 文档

README

Packagist Version MIT License CI CLA

A little library that transforms data based on a specification that can be expressed in JSON.

Purpose

The purpose of this package is to provide simple data transformation that can be applied to arrays of data like those that come from JSON APIs. The transformations are specified in simple arrays so that an entire transformation specification can also be serialized to JSON, making this a completely data-driven library.

The Transformer Class

The heart of this library is the Transformer class that takes a transformation spec in its constructor and then uses that spec to tell it how to transform data.

$t = new Transformer($spec);
$target = $t->transform($source);

The Transformer class is also callable so it can be passed as an argument that requires a callback such as array_map.

Transformation Specs

A transformation spec is an associated array that contains all of the keys of the target array, but with values that are JSON references to keys in the source array.

{
    "targetKey1": "/sourceKey1",
    "targetKey2": "/sourceKey2/sourceKey2.1"
}

You can see from the above, you can reference a nested value using a forward slash to separate nested references.

Nested Data

For complex target arrays you nest keys by structuring the target like your desired data format:

{
    "user": {
        "username": "/username",
        "fullName": "/fullName"
    }
}

Default Values

If you reference a key that doesn't exist in the source data then it will be omitted in the transformed data. If you want to provide a default value you can use the following spec:

{
    "username": {
        "$ref": "/username",
        "$default": "anonymous"
    }
}

If you omit the $default value then the default value is assumed to be null.

Transforming arrays with $each

You can loop through an array and transform each item using $each and $item.

{
  "$each": "/",
  "$item": {
    "name": "username",
    "id": "userID"
  }
}

The above would transform an array like this:

[
    { "username":  "bot", "userID":  1 },
    { "username":  "dog", "userID":  2 }
]

Into this:

[
    { "name":  "bot", "id":  1 },
    { "name":  "dog", "id":  2 }
]

You can aso specify a transform for the keys in an array using the $key attribute. Here is an example spec:

{
  "$each": "/",
  "$item": "userID",
  "$key": "username"
}

This spec would transform the example from above into this:

{
  "bot": 1,
  "dog": 2
}

Absolute vs. Relative References

You may have noticed that the references most of the examples all start with a / character. This is because they are all absolute references.

If you don't use a / at the beginning of your reference then you are specifying a relative reference. You use relative references in the $item spec to refer to items within the loop.

Literal Values

Sometimes you might want to provide a specific literal value in the result. You can do this with the $literal key.

{
    "version": {
        "$literal": "1.1"
    }
}

Why doesn't this library do enough?

Some people think this library is flimsy, foolish, and not worth worrying about. But don't you believe it! The initial implementation of this package is intended to provide the minimum amount of functionality to be useful for 90% of problems.

Any sensible features will be added as requirements evolve. However, they must adhere to the core purpose of this package: transformations that can be expressed as plain JSON.

vanilla/garden-jsont 适用场景与选型建议

vanilla/garden-jsont 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62.2k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 vanilla/garden-jsont 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vanilla/garden-jsont 我们能提供哪些服务?
定制开发 / 二次开发

基于 vanilla/garden-jsont 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 62.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 10
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-02