laragear/json

轻松检索和操作应用程序中的 `Json`。

v2.0.0 2024-03-06 22:59 UTC

This package is auto-updated.

Last update: 2024-08-31 00:08:37 UTC


README

Latest Version on Packagist Latest stable test run Codecov coverage CodeClimate Maintainability Sonarcloud Status Laravel Octane Compatibility

轻松检索和操作应用程序中的 Json

use Laragear\Json\Json;

$json = Json::fromJson('{"foo":"bar"}');

$json->set('bar.quz', 'quz');

echo $json->foo; // "quz"

成为赞助商

您的支持使我能够保持这个包免费、更新和可维护。或者,您可以 传播这个信息!

要求

  • Laravel 10 或更高版本

安装

启动 Composer 并在项目中要求此包。

composer require laragear/json

就这样。

这是为什么?

如果您觉得构建复杂的 JSON 响应或处理 JSON 树值往返比较繁琐,这个包就是为您准备的。它提供了很多功能,不仅可以构建和操作 JSON 树。

// Before
$name = $json['users'][1]['name'] ?? null;

// After
$name = $json->get('users.1.name');

从 HTTP 请求

只需使用 getJson() 便可以自动检索请求中的所有 JSON,或者指定的键值。

use Illuminate\Http\Request;
use Laragear\Json\Json;

public function data(Request $request)
{
    // Get the request JSON.
    $json = $request->getJson();
    
    // Return a key value from the JSON.
    $value = $json->get('this.is');
    
    // Set a value and return it.
    return $json->set('this.is', 'awesome');
}

提示

您仍然可以使用 json() 方法以 ParameterBag 或键值的形式检索 JSON 数据。

作为 HTTP 响应

您可以使用 make() 方法构建一个 Json 实例,可选地使用自己的 array,但您也可以使用任何实现 Arrayable 接口或是 iterable 的东西。由于 Json 实例实现了 Responsable 特性,您可以直接返回它,它将被自动转换为 JSON 响应

use Laragear\Json\Json;
use Illuminate\Support\Facades\Route;

Route::get('send', fn() => Json::make(['this_is' => 'cool']));

您也可以将其转换为响应并修改输出的响应参数,如头部或状态。

use Laragear\Json\Json;
use Illuminate\Support\Facades\Route;

Route::get('send', function() {
    return Json::make(['this_is' => 'cool'])
        ->toResponse()
        ->header('Content-Version', '1.0');
});

创建实例

如果您已经有了想要转换为 JSON 的 array,请使用 make() 方法或手动实例化它。两种方式都可以。您还可以使用 Arrayable 对象和任何 iterable 的东西,比如 Collections

use Laragear\Json\Json;

$json = new Json([
    'users' => [
        'id' => 1,
        'name' => 'John',
    ]   
]);

如果值已经是 JSON 字符串,请使用 fromJson()

use Laragear\Json\Json;

$json = Json::fromJson('{"users":{"id":1,"name":"John"}}');

可用方法

Json 实例包含多个有助于处理 JSON 数据的方法

Eloquent JSON 映射

在处理模型中的 JSON 属性时,您会注意到这真的非常繁琐。您不必使用数组或 Collections,而是可以使用 AsJsonAsEncryptedJson 映射,它们将从模型属性直接提供 Json 实例或将其加密到数据库中。

只需将其中之一添加到 您的模型映射

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Laragear\Json\Casts\AsJson; 
use Laragear\Json\Casts\AsEncryptedJson; 
 
class User extends Model
{
    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'options' => AsJson::class,
        'secure_options' => AsEncryptedJson::class,
    ];
}

完成之后,您就可以像平常一样轻松地将 JSON 填充到模型中。

use App\Models\User;
use Laragear\Json\Json;

$user = User::find();

// Set a Json instance, like from a string. 
$user->options = Json::fromJson('{"apples":"tasty"}')

// Or just directly use an array tree.
$user->secure_options = [
    'visa' => [
        ['last_4' => '1234', 'preferred' => true] 
    ]
];

// You can use the Json instance like a normal monday.
$user->secure_options->get('visa.last_4'); // "1234" 

PhpStorm 模板

对于 PhpStorm 的用户,有一个模板文件可以帮助此包的宏自动补全。您可以使用 phpstorm 标签发布它们。

php artisan vendor:publish --provider="Laragear\Json\JsonServiceProvider" --tag="phpstorm"

该文件将被发布到您项目的 .stubs 文件夹中。您应该将 PhpStorm 指向这些模板

Laravel Octane 兼容性

  • 没有使用过时应用程序实例的单例。
  • 没有使用过时配置实例的单例。
  • 没有使用过时请求实例的单例。
  • 没有写入静态属性。

使用此软件包与 Laravel Octane 一起时不应有问题。

安全

如果您发现任何与安全相关的问题,请通过电子邮件发送至 darkghosthunter@gmail.com,而不是使用问题跟踪器。

许可证

本软件包特定版本在发布时遵循 MIT 许可协议

LaravelTaylor Otwell 的商标。版权所有 © 2011-2023 Laravel LLC。