<commit_before><?php
/*
 * psx
 * A object oriented and modular based PHP framework for developing
 * dynamic web applications. For the current version and informations
 * visit <http://phpsx.org>
 *
 * Copyright (c) 2010-2013 <NAME> <<EMAIL>>
 *
 * This file is part of psx. psx is free software: you can
 * redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * psx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with psx. If not, see <http://www.gnu.org/licenses/>.
 */

namespace PSX;

/**
 * This class is a wrapper to the json_encode / json_decode functions. Here an
 * simple example howto use it.
 * <code>
 * $json = Json::encode(array('foo' => 'bar'));
 * $php  = Json::decode($json);
 * </code>
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/gpl.html GPLv3
 * @link    http://phpsx.org
 */
class Json
{
	/**
	 * Returns the json encoded value as string of $value
	 *
	 * @param mixed $value
	 * @return string
	 */
	public static function encode($value)
	{
		return json_encode($value);
	}

	/**
	 * Returns a php variable from the json decoded value
	 *
	 * @param string $value
	 * @return mixed
	 */
	public static function decode($value)
	{
		return json_decode($value, true);
	}
}<commit_msg>throw an exception if json decode fails<commit_after><?php
/*
 * psx
 * A object oriented and modular based PHP framework for developing
 * dynamic web applications. For the current version and informations
 * visit <http://phpsx.org>
 *
 * Copyright (c) 2010-2013 <NAME> <<EMAIL>>
 *
 * This file is part of psx. psx is free software: you can
 * redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * psx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with psx. If not, see <http://www.gnu.org/licenses/>.
 */

namespace PSX;

/**
 * This class is a wrapper to the json_encode / json_decode functions. Here an
 * simple example howto use it.
 * <code>
 * $json = Json::encode(array('foo' => 'bar'));
 * $php  = Json::decode($json);
 * </code>
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/gpl.html GPLv3
 * @link    http://phpsx.org
 */
class Json
{
	/**
	 * Returns the json encoded value as string of $value
	 *
	 * @param mixed $value
	 * @return string
	 */
	public static function encode($value)
	{
		return json_encode($value);
	}

	/**
	 * Returns a php variable from the json decoded value. Throws an exception 
	 * if decoding the data is not possible
	 *
	 * @param string $value
	 * @return mixed
	 */
	public static function decode($value)
	{
		$data = json_decode($value, true);

		switch(json_last_error())
		{
			case JSON_ERROR_NONE:
				return $data;
				break;

			case JSON_ERROR_DEPTH:
				throw new Exception('Maximum stack depth exceeded');
				break;

			case JSON_ERROR_STATE_MISMATCH:
				throw new Exception('Underflow or the modes mismatch');
				break;

			case JSON_ERROR_CTRL_CHAR:
				throw new Exception('Unexpected control character found');
				break;

			case JSON_ERROR_SYNTAX:
				throw new Exception('Syntax error, malformed JSON');
				break;

			case JSON_ERROR_UTF8:
				throw new Exception('Malformed UTF-8 characters, possibly incorrectly encoded');
				break;

			default:
				throw new Exception('Unknown error');
				break;
		}
	}
}
{'fraction_non_alphanumeric': 0.10355907068709837, 'fraction_numerical': 0.005437469105289174, 'mean_word_length': 3.427789934354486, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2056541', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1151, 'n_words': 484}