<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2020 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Support\Arr;

use InvalidArgumentException;
use JsonSerializable;
use Leevel\Support\IArray;
use Leevel\Support\IJson;

/**
 * 转换 JSON 数据.
 *
 * @param mixed $data
 *
 * @throws \InvalidArgumentException
 */
function convert_json($data = [], ?int $encodingOptions = null, bool $verifyIsJson = true): string
{
    if ($verifyIsJson && !should_json($data)) {
        return $data;
    }

    if (null === $encodingOptions) {
        $encodingOptions = JSON_UNESCAPED_UNICODE;
    }

    if ($data instanceof IArray) {
        $data = json_encode($data->toArray(), $encodingOptions);
    } elseif (is_object($data) && $data instanceof IJson) {
        $data = $data->toJson($encodingOptions);
    } elseif (is_object($data) && $data instanceof JsonSerializable) {
        $data = json_encode($data->jsonSerialize(), $encodingOptions);
    } else {
        $data = json_encode($data, $encodingOptions);
    }
<commit_msg>refactor(support): use JSON_THROW_ON_ERROR to refact convert_json<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2020 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Support\Arr;

use Exception;
use InvalidArgumentException;
use JsonSerializable;
use Leevel\Support\IArray;
use Leevel\Support\IJson;

/**
 * 转换 JSON 数据.
 *
 * @param mixed $data
 *
 * @throws \InvalidArgumentException
 */
function convert_json($data = [], ?int $encodingOptions = null, bool $verifyIsJson = true): string
{
    if ($verifyIsJson && !should_json($data)) {
        return $data;
    }

    if (null === $encodingOptions) {
        $encodingOptions = JSON_UNESCAPED_UNICODE;
    }

    try {
        if ($data instanceof IArray) {
            $data = json_encode($data->toArray(), $encodingOptions);
        } elseif (is_object($data) && $data instanceof IJson) {
            $data = $data->toJson($encodingOptions);
        } elseif (is_object($data) && $data instanceof JsonSerializable) {
            $data = json_encode($data->jsonSerialize(), $encodingOptions);
        } else {
            $data = json_encode($data, $encodingOptions);
        }
    } catch (Exception $e) {
        if ('Exception' === get_class($e) &&
            0 === strpos($e->getMessage(), 'Failed calling ')) {
            throw $e->getPrevious() ?: $e;
        }

        throw $e;
    }

    if (JSON_THROW_ON_ERROR & $encodingOptions) {
        return $data;
    }

{'fraction_non_alphanumeric': 0.15706947823736145, 'fraction_numerical': 0.005136523384698567, 'mean_word_length': 2.2201914708442123, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1996976', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1154, 'n_words': 248}