<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */
 
  uses(
    'lang.types.String',
    'lang.types.Character',
    'io.streams.MemoryOutputStream',
    'text.StringTokenizer',
    'webservices.json.JsonException',
    'webservices.json.IJsonDecoder',
    'webservices.json.JsonParser',
    'webservices.json.JsonLexer'
  );

  /**
   * JSON decoder and encoder
   *
   * Used for converting JSON into PHP and PHP into JSON.
   *
   * Usage
   * =====
   * <code>
   *   uses('webservices.json.JsonDecoder');
   *
   *   $decoder= new JsonDecoder();
   *
   *   $phpData= $decoder->deocode($jsonString);
   *   $jsonString= $decoder->encode($phpData);
   *
   * </code>
   *
   * @test      xp://net.xp_framework.unittest.json.JsonDecoderTestDecoder
   * @test      xp://net.xp_framewort.unittest.json.JsonDecoderTestEncoder
   * @see       http://json.org
   * @purpose   JSON en- and decoder
   */
  class JsonDecoder extends Object implements IJsonDecoder {
  
    /**
     * Encode PHP data into JSON
     *
     * encode() gets PHP data and converts this into a JSON string.
     *
     *   <ul>
     *     <li>
     *       If you put in a boolean or NULL, you will retrieve a string within the
     *       name of this boolean.
     *       <code>
     *         $decoder->encode(TRUE); // Will return 'true'
     *       </code>
     *     </li>
     *     <li>
     *       If you put in an integer or a float, you will retrieve a string representation
     *       of this number.
     *       <code>
     *         $decoder->encode(10); // Will return '10'
     *         $decoder->encode(0.1); // Will return '0.1'
     *         $decoder->encode(0.000001); // Will return '1.0E-6', also valid JSON
     *       </code>
     *     </li>
     *     <li>
     *       If you put in a string, you will retrieve this string.
     *     </li>
     *     <li>
     *       If you put in an array, the value you return depends on the array. If
     *       it has only numeric keys (from 0 up to n-1), you will retrieve a JSON
     *       array.
     *       <code>
     *         $decode->encode(array('foo', 'bar')); // Will return '[ "foo" , "bar" ]'
     *       </code>
     *       If the array has string keys or disordered numeric keys, you will retrieve
     *       a JSON object.
     *       <code>
     *         $decode->encode(array('foo1' => 'bar1', 'foo2' => 'bar2'));
     *         // Will return '{ "foo1" : "bar1" , "foo2" : "bar2" }'
     *       <code>
     *     </li>
     *     <li>
     *       If you put in a PHP object, it will be serialized and you will retrieve
     *       it as a JSON object
     *     </li>
     *   </ul>
     *
     * @param var data
     * @return string
     * @throws webservices.json.JsonException
     */
    public function encode($data) {
      $stream= new MemoryOutputStream();
      $this->streamEncode($data, $stream);
      return $stream->getBytes();
    }

    /**
     * Encode PHP data into JSON via stream
     *
     * Gets the PHP data and a stream.<br/>
     * It converts the data to JSON and will put every atom into the stream as soon
     * as it is available.<br/>
     * The usage is similar to encode() except the second argument.
     *
     * @param   var data
     * @param   var stream
     * @return  boolean
     * @throws  webservices.json.JsonException if the data could not be serialized
     */
    public function streamEncode($data, $stream) {
      static $controlChars= array(
        '"'   => '\\"', 
        '\\'  => '\\\\', 
        '/'   => '\\/', 
        "\b"  => '\\b',
        "\f"  => '\\f', <commit_msg>- rename streamEncode to encodeTo<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */
 
  uses(
    'lang.types.String',
    'lang.types.Character',
    'io.streams.MemoryOutputStream',
    'text.StringTokenizer',
    'webservices.json.JsonException',
    'webservices.json.IJsonDecoder',
    'webservices.json.JsonParser',
    'webservices.json.JsonLexer'
  );

  /**
   * JSON decoder and encoder
   *
   * Used for converting JSON into PHP and PHP into JSON.
   *
   * Usage
   * =====
   * <code>
   *   uses('webservices.json.JsonDecoder');
   *
   *   $decoder= new JsonDecoder();
   *
   *   $phpData= $decoder->deocode($jsonString);
   *   $jsonString= $decoder->encode($phpData);
   *
   * </code>
   *
   * @test      xp://net.xp_framework.unittest.json.JsonDecoderTestDecoder
   * @test      xp://net.xp_framewort.unittest.json.JsonDecoderTestEncoder
   * @see       http://json.org
   * @purpose   JSON en- and decoder
   */
  class JsonDecoder extends Object implements IJsonDecoder {
  
    /**
     * Encode PHP data into JSON
     *
     * encode() gets PHP data and converts this into a JSON string.
     *
     *   <ul>
     *     <li>
     *       If you put in a boolean or NULL, you will retrieve a string within the
     *       name of this boolean.
     *       <code>
     *         $decoder->encode(TRUE); // Will return 'true'
     *       </code>
     *     </li>
     *     <li>
     *       If you put in an integer or a float, you will retrieve a string representation
     *       of this number.
     *       <code>
     *         $decoder->encode(10); // Will return '10'
     *         $decoder->encode(0.1); // Will return '0.1'
     *         $decoder->encode(0.000001); // Will return '1.0E-6', also valid JSON
     *       </code>
     *     </li>
     *     <li>
     *       If you put in a string, you will retrieve this string.
     *     </li>
     *     <li>
     *       If you put in an array, the value you return depends on the array. If
     *       it has only numeric keys (from 0 up to n-1), you will retrieve a JSON
     *       array.
     *       <code>
     *         $decode->encode(array('foo', 'bar')); // Will return '[ "foo" , "bar" ]'
     *       </code>
     *       If the array has string keys or disordered numeric keys, you will retrieve
     *       a JSON object.
     *       <code>
     *         $decode->encode(array('foo1' => 'bar1', 'foo2' => 'bar2'));
     *         // Will return '{ "foo1" : "bar1" , "foo2" : "bar2" }'
     *       <code>
     *     </li>
     *     <li>
     *       If you put in a PHP object, it will be serialized and you will retrieve
     *       it as a JSON object
     *     </li>
     *   </ul>
     *
     * @param var data
     * @return string
     * @throws webservices.json.JsonException
     */
    public function encode($data) {
      $stream= new MemoryOutputStream();
      $this->encodeTo($data, $stream);
      return $stream->getBytes();
    }

    /**
     * Encode PHP data into JSON via stream
     *
     * Gets the PHP data and a stream.<br/>
     * It converts the data to JSON and will put every atom into the stream as soon
     * as it is available.<br/>
     * The usage is similar to encode() except the second argument.
     *
     * @param   var data
     * @param   var stream
     * @return  boolean
     * @throws  webservices.json.JsonException if the data could not be serialized
     */
    public function encodeTo($data, $stream) {
      static $controlChars= array(
        '"'   => '\\"', 
        '\\'  => '\\\\', 
        '/'   => '\\/', 
        "\b"  => '\\b',
        "\f"  => '\\f', 
{'fraction_non_alphanumeric': 0.13677437020810515, 'fraction_numerical': 0.007667031763417305, 'mean_word_length': 1.7380059970014992, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5997184', 'n_tokens_mistral': 2327, 'n_tokens_neox': 2246, 'n_words': 676}