This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 
<?php
/**
 * @package Helpers
 * @category Concrete
 * @author Andrew Embler <[email protected]>
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 */

/**
 * Functions for working with JSON (JavaScript Object Notation)
 * @package Helpers
 * @category Concrete
 * @author Andrew Embler <[email protected]>
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 */
defined('C5_EXECUTE') or die("Access Denied.");
class Concrete5_Helper_Json {

    
    /** 
     * Decodes a JSON string into a php variable
     * @param string $string
     * @param bool $assoc [default: false] When true, returned objects will be converted into associative arrays, when false they'll be converted into stdClass instances. 
     * @return mixed
     */
    public function decode($string, $assoc = false) {
        if (function_exists('json_decode')) {
            return json_decode($string, $assoc);
        } else {
            Loader::library('3rdparty/JSON/JSON');
            $sjs = new Services_JSON($assoc ? SERVICES_JSON_LOOSE_TYPE : 0);
            return $sjs->decode($string);
        }
    }
    
    
    /** 
     * Encodes a data structure into a JSON string
     * @param mixed $mixed
     * @return string
     */
    public function encode($mixed) {
        if (function_exists('json_encode')) {
            return json_encode($mixed);
        } else {
            Loader::library('3rdparty/JSON/JSON');
            $sjs = new Services_JSON();
            return $sjs->encode($mixed);
        }
    }
    


}