FastJSON::convert PHP Method

convert() public method

FastJSON::convert(params:* [, result:Instance]):*
public convert ( $params, $result = null )
    function convert($params, $result = null)
    {
        switch (gettype($params)) {
            case 'array':
                $tmp = array();
                foreach ($params as $key => $value) {
                    if (($value = FastJSON::encode($value)) !== '') {
                        array_push($tmp, FastJSON::encode(strval($key)) . ':' . $value);
                    }
                }
                $result = '{' . implode(',', $tmp) . '}';
                break;
            case 'boolean':
                $result = $params ? 'true' : 'false';
                break;
            case 'double':
            case 'float':
            case 'integer':
                $result = $result !== null ? strftime('%Y-%m-%dT%H:%M:%S', $params) : strval($params);
                break;
            case 'NULL':
                $result = 'null';
                break;
            case 'string':
                $i = create_function('&$e, $p, $l', 'return intval(substr($e, $p, $l));');
                if (preg_match('/^[0-9]{4}\\-[0-9]{2}\\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $params)) {
                    $result = mktime($i($params, 11, 2), $i($params, 14, 2), $i($params, 17, 2), $i($params, 5, 2), $i($params, 9, 2), $i($params, 0, 4));
                }
                break;
            case 'object':
                $tmp = array();
                if (is_object($result)) {
                    foreach ($params as $key => $value) {
                        $result->{$key} = $value;
                    }
                } else {
                    $result = get_object_vars($params);
                    foreach ($result as $key => $value) {
                        if (($value = FastJSON::encode($value)) !== '') {
                            array_push($tmp, FastJSON::encode($key) . ':' . $value);
                        }
                    }
                    $result = '{' . implode(',', $tmp) . '}';
                }
                break;
        }
        return $result;
    }

Usage Example

Exemplo n.º 1
0
 static public function encode($decode) {
     $result = '';
     switch(gettype($decode)) {
         case	'array':
             if(!count($decode) ||array_keys($decode) === range(0,count($decode) -1)) {
                 $keys = array();
                 foreach($decode as $value) {
                     if(($value = FastJSON::encode($value)) !== '')
                         array_push($keys,$value);
                 }
                 $result = '['.implode(',',$keys).']';
             }
             else
                 $result = FastJSON::convert($decode);
             break;
         case	'string':
             $replacement = FastJSON::__getStaticReplacement();
             $result = '"'.str_replace($replacement['find'],$replacement['replace'],$decode).'"';
             break;
         default:
             if(!is_callable($decode))
                 $result = FastJSON::convert($decode);
             break;
     }
     return $result;
 }
All Usage Examples Of FastJSON::convert