<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\Validate\Helper;

use Exception;

/**
 * 验证是否为正常的 JSON 数据.
 *
 * @param mixed $value
 */
function json($value): bool
{
    if (!is_scalar($value) && !method_exists($value, '__toString')) {
        return false;
    }

    try {
        json_decode((string) $value, true, 512, JSON_THROW_ON_ERROR);

        return true;
    } catch (Exception $e) {
        return false;
    }
}

class json
{
}<commit_msg>fix(validate): fix function \Leevel\Validate\Helper\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\Validate\Helper;

use Exception;

/**
 * 验证是否为正常的 JSON 数据.
 *
 * @param mixed $value
 */
function json($value): bool
{
    if (is_object($value) && !method_exists($value, '__toString')) {
        return false;
    }

    if (is_string($value) && class_exists($value) &&
        !method_exists($value, '__toString')) {
        return false;
    }

    try {
        json_decode((string) $value, true, 512, JSON_THROW_ON_ERROR);

        return true;
    } catch (Exception $e) {
        return false;
    }
}

class json
{
}
{'fraction_non_alphanumeric': 0.16557377049180327, 'fraction_numerical': 0.009836065573770493, 'mean_word_length': 1.844988344988345, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '7539689', 'n_tokens_mistral': 850, 'n_tokens_neox': 812, 'n_words': 172}