<?
class TJSON extends TObject
{
    function __construct()
    {
        $this->getHeadHtml();
    }
    
    function getHeadHtml($params=array())
    {
        //parent::getHeadHtml($params);
        addHeadHtml("<script type='text/javascript' src='".getUrlFromPath(__FILE__, true)."JSON.class.js'></script>",true);
        addExtendingHeadHtml($params, __CLASS__);
    }
    
    public static function encode($value=array())
    {
        //$value = str_replace(ZWSP, "", $value);
        //$value = str_replace(ZWNL, "\n", $value);
        
        //var_dump($value);
        
        $value = @json_encode($value);
        
        //var_dump($value);
        
        // revert back \uXXXX utf8 characters
        $value = unicode_unescape($value);
        //var_dump($value);

        //var_dump($value);
        //exit;        
        return($value);
    }

    public static function isJSON($value="")
    {
        if (is_string($value))
        {
            $value = str_replace(ZWSP, "", $value);
            $value = str_replace(ZWNL, "", $value);
            $value = trim($value);
            
            $ret = ( 
                    /*
                    strpos($value, "{php:") !== 0 &&
                    strpos($value, "{#php:") !== 0 && 
                    strpos($value, "{##php:") !== 0 && 
                    strpos($value, "{#") !== 0 && 
                    */
                    //($value[0] == "{" || $value[0] == "[") && ($value[strlen($value)-1] == "}" || $value[strlen($value)-1] == "]") && 
                    (strpos($value, "{\"") === 0 || strpos($value, "[\"") === 0 || strpos($value, "[{\"") === 0)
            );
            
            //dump($value."!".toString($ret)."!");
            //dump();
            return($ret);
        }
        else
            return(false);
    }
    
    public static function decode($value="{}")
    {
        /*if (is_array($value))
        {
            var_dump($value);
            return($value);
        }
        */
        if (strpos($value, "{php:") === 0 || strpos($value, "{#php:") === 0 || strpos($value, "{##php:") === 0) 
        {
            echo "TJSON::decode() - BAD DATA - PHP INSIDE!";
            exit;
            
        }
        
        /*
        if (is_array($value)) 
        {
            echo "TJSON::decode() - BAD DATA - ARRAY!";
            echo getFunctionCall();
            var_dump($value);
            exit;
        }
        */
        echo $value."<br />";
        /*
        $value = str_replace("['", "[\"", $value);
        $value = str_replace("{'", "{\"", $value);
        $value = str_replace("']", "\"]", $value);
        $value = str_replace("'}", "\"}", $value);
        $value = str_replace("':", "\":", $value);
        $value = str_replace(":'", ":\"", $value);
        */
        
        //$value = str_replace("'", "\"", $value);
        //var_dump($value);
        
        
        while(strpos($value, ",,") !== FALSE)
            $value = str_replace(",,", ",null,", $value);
        while(strpos($value, "[,") !== FALSE)
            $value = str_replace("[,", "[null,", $value);
        while(strpos($value, ",]") !== FALSE)
            $value = str_replace(",]", ",null]", $value);
            
        /*
        $value = str_replace(ZWSP, "", $value);
        $value = str_replace(ZWNL, "", $value);
        $value = str_replace("\n", "", $value);
        */

        //file_append_contents(PROJECTPATH."out.txt", $value."\r\n");
        $value = json_decode($value, true);
        //file_append_contents(PROJECTPATH."out.txt", "========================\r\n");
        
        //var_dump($value);
        
        //$value = substr($value, strpos($value,'{')+1, strlen($value));
        //$value = substr($value, 0, strrpos($value,'}'));
        //$value = preg_replace('/(^|,)([\\s\\t]*)([^:]*) (([\\s\\t]*)):(([\\s\\t]*))/s', '$1"$3"$4:', trim($value));

        //return json_decode('{'.$value.'}', true);


        return($value);
    }
}

function isJSON($str)
{
    return(TJSON::isJSON($str));
}


?>
