eugene-melbourne/json-encoder-decoder

编码和解码 JSON 字符串

1.0.6 2021-08-30 08:02 UTC

This package is auto-updated.

Last update: 2024-09-29 05:43:16 UTC


README

编码和解码 JSON 字符串

安装

$ composer require  eugene-melbourne/json-encoder-decoder

最重要的是它预先设置了默认参数

  • JSON_THROW_ON_ERROR
  • convertEmptyStringToNull
  • ReturnAsAssociative

不过,这可以更改。

此外,它可以将空字符串转换为 null,并包含单元测试以展示其功能。

示例

// Example 1
$val = (new JsonEncoderDecoder())->json_encode(null)
// $val = null   

// Example 2
$val  = [chr(160),];
$json = (new JsonEncoderDecoder())->json_encode($val);
// throws JsonException

// Example 3
$val  = [chr(160),];
$json = (new JsonEncoderDecoder())
        ->addOption(JSON_INVALID_UTF8_SUBSTITUTE)
        ->json_encode($val);
// $json = '["\ufffd"]';

在单元测试中查看更多示例。

备注:字符编码 160 将被转换为