├── LICENSE ├── README.md ├── composer.json └── src ├── Facade.php ├── ServiceProvider.php ├── YaJson.php └── config.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 刘勋 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-anothor-json 2 | 3 | Yet Another JSON Encoder For Laravel. 4 | 用于修复 PHP7.1、7.2beta 中 json_encode() 处理 float/double 型数值时溢出的问题. 5 | > 感谢博主提供的思路 [http://www.itread01.com/articles/1489774743.html](http://www.itread01.com/articles/1489774743.html) 6 | 7 | 8 | ## 示例 9 | 10 | ```shell 11 | >>> $a = 0.1 + 0.7 12 | => 0.8 13 | >>> printf('%.20f', $a) 14 | => 0.79999999999999993339 15 | >>> json_encode($a) 16 | => "0.7999999999999999" 17 | >>> \YaJson::encode($a) 18 | => "0.8" 19 | ``` 20 | 21 | ## 用法 22 | 1. 修复精度并进行 `json_encode` : 23 | ```php 24 | $data = [ 25 | 'a' => 0.1 + 0.7, 26 | 'b' => ['string1', 'string2'], 27 | ]; 28 | 29 | \YaJson::encode($data); //"{"a":0.8,"b":["string1","string2"]}" 30 | ``` 31 | 32 | 2. 只获取修复后的数据,不进行 `json_encode` : 33 | ```php 34 | $data = [ 35 | 'a' => 0.1 + 0.7, 36 | ]; 37 | 38 | \YaJson::prepare($data); //['a'=>0.8] 39 | ``` 40 | 41 | ## 安装 42 | 43 | 1. 安装包文件 44 | 45 | ```shell 46 | composer require "seekerliu/laravel-another-json:dev-master" 47 | ``` 48 | 49 | ## 配置 50 | 51 | ### Laravel 5.5 52 | 53 | > `Laravel 5.5` 安装新包后会默认执行 `@php artisan package:discover` 命令,所以可以不进行下面的操作。 54 | 55 | 1. 注册 `ServiceProvider` 及 `Facade`: 56 | 57 | ```shell 58 | php artisan package:discover 59 | ``` 60 | 61 | 2. 如需修改默认循环深度、精度位数,则创建配置文件: 62 | 63 | ```shell 64 | php artisan vendor:publish 65 | ``` 66 | 67 | ### Laravel 5.4 及以下 68 | 1. 注册 `ServiceProvider` 及 `Facade`: 69 | 70 | ```php 71 | 'providers' => [ 72 | //... 73 | 74 | Seekerliu\YaJson\ServiceProvider::class, 75 | ], 76 | 77 | 'aliases' => [ 78 | //... 79 | 80 | 'YaJson' => Seekerliu\YaJson\Facade::class, 81 | ], 82 | ``` 83 | 84 | 85 | 2. 如需修改默认循环深度、精度位数,则创建配置文件: 86 | 87 | ```shell 88 | php artisan vendor:publish --provider="Seekerliu\YaJson\ServiceProvider" 89 | ``` 90 | 91 | 92 | ## License 93 | 94 | MIT 95 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "seekerliu/laravel-another-json", 3 | "description": "Yet Another JSON encoder for Laravel.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "seekerliu", 8 | "email": "seekerliu@vip.qq.com" 9 | } 10 | ], 11 | "autoload": { 12 | "psr-4": { 13 | "Seekerliu\\YaJson\\": "src/" 14 | } 15 | }, 16 | "minimum-stability": "dev", 17 | "require": {}, 18 | "extra": { 19 | "laravel": { 20 | "providers": [ 21 | "Seekerliu\\YaJson\\ServiceProvider" 22 | ], 23 | "aliases": { 24 | "YaJson": "Seekerliu\\YaJson\\Facade" 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Facade.php: -------------------------------------------------------------------------------- 1 | publishes( 22 | [$configPath => config_path('yajson.php')], 23 | 'yajson' 24 | ); 25 | } 26 | 27 | /** 28 | * Register the provider. 29 | * 30 | * @return void 31 | */ 32 | public function register() 33 | { 34 | $this->app->singleton(YaJson::class, function ($app) { 35 | return new YaJson($app['config']['yajson']); 36 | }); 37 | 38 | $this->app->alias(YaJson::class, 'YaJson'); 39 | 40 | $this->mergeConfigFrom( 41 | realpath(__DIR__.'/config.php'), 'yajson' 42 | ); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/YaJson.php: -------------------------------------------------------------------------------- 1 | depth = $config['depth']; 22 | $this->decimals = $config['decimals']; 23 | } 24 | 25 | /** 26 | * 对数据进行 json_encode 27 | * @param mixed $data 28 | * @param int $depth 遍历的深度 29 | * @param int $decimals 给浮点数定精度,默认为 8 30 | * @return string 31 | */ 32 | public function encode($data = '', $decimals = 0, $depth = 0) 33 | { 34 | $data = $this->prepare($data, $decimals, $depth); 35 | return json_encode($data); 36 | } 37 | 38 | /** 39 | * 对数据进行遍历,修复浮点数的精度 40 | * @param string $data 41 | * @param int $decimals 42 | * @param int $depth 43 | * @param int $level 44 | * @return mixed $data 45 | */ 46 | public function prepare($data = '', $decimals = 0, $depth = 0, $level = 0) 47 | { 48 | $depth = $depth ?: $this->depth; 49 | 50 | if($level > $depth - 1) 51 | return $data; 52 | 53 | if(is_array($data)){ 54 | foreach ($data as $i => $v) { 55 | $data[$i] = $this->prepare($v, $decimals, $depth, $level + 1); 56 | } 57 | return $data; 58 | } 59 | 60 | if(is_float($data)){ 61 | return $this->fixNumberPrecision($data, $decimals); 62 | } 63 | 64 | return $data; 65 | } 66 | 67 | /** 68 | * 修复浮点数精度 69 | * @param float $number 70 | * @param int $decimals 71 | * @return float 72 | */ 73 | private function fixNumberPrecision($number, $decimals = 0) 74 | { 75 | $decimals = $decimals ?: $this->decimals; 76 | $formatted = number_format($number, $decimals, '.', ''); 77 | 78 | return floatval($formatted); 79 | } 80 | 81 | // /** 82 | // * 原博提供的方法,供参考: 83 | // * @param $d 84 | // * @param int $depth 85 | // * @param int $level 86 | // * @return array 87 | // */ 88 | // private function json_encode_pre($d, $depth=128, $level=0){ 89 | // if($level>$depth) return $d; 90 | // if(is_array($d)){ 91 | // foreach ($d as $i => $v) { $d[$i] = json_encode_pre($v, $depth, $level+1); } 92 | // return $d; 93 | // } 94 | // if(is_float($d)){ 95 | // # 測試發現number_format有效數字低於18(保守取16)時,不會溢出 96 | // $p = 16 - strlen(intval($d)); 97 | // $f = number_format($d, $p); 98 | // if($p>1){ $f = preg_replace('/0+$/','', $d); } 99 | // return $d; 100 | // } 101 | // return $d; 102 | // } 103 | } 104 | -------------------------------------------------------------------------------- /src/config.php: -------------------------------------------------------------------------------- 1 | 512, //默认处理数据时的循环深度 5 | 6 | /* 7 | * number_format 的默认参数 8 | * string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," ) 9 | */ 10 | 'decimals' => 8, //浮点数精度,默认为 8 位 11 | 12 | ]; 13 | --------------------------------------------------------------------------------