-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJPOPHP-min.php
12 lines (12 loc) · 10.9 KB
/
JPOPHP-min.php
1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
*
* @Name : JPOPHP (Json Parser Object PHP)
* @Version : 2.2.1
* @Programmer : Max
* @Date : 2018-06-26, 2018-06-27, 2019-03-23, 2019-03-24, 2019-03-26, 2019-03-27, 2019-04-04
* @Released under : https://github.com/BaseMax/JPOPHP/blob/master/LICENSE
* @Repository : https://github.com/BaseMax/JPOPHP
*
**/
abstract class jsontype{const jsonarray=0;const jsonobject=1;}abstract class tokentype{const tokeneof=-1;const tokenarrayopen=0;const tokenarrayclose=1;const tokenobjectopen=2;const tokenobjectclose=3;const tokenstring=4;const tokennumber=5;const tokensplit=6;const tokenpair=7;const tokennull=8;const tokenbool=9;}class json{public $token=null;public $length=0;public $input="";public $index=0;function typetoken($token){switch($token[0]){case tokentype::tokeneof:return "EOF";break;case tokentype::tokenarrayopen:return "ArrayOpen";break;case tokentype::tokenarrayclose:return "ArrayClose";break;case tokentype::tokenobjectopen:return "ObjectOpen";break;case tokentype::tokenobjectclose:return "ObjectClose";break;case tokentype::tokenstring:return "String";break;case tokentype::tokennumber:return "Number";break;case tokentype::tokensplit:return "Split";break;case tokentype::tokenpair:return "Pair";break;default:return "None";break;}}function isassociative(array $array){return array_keys($array)!==range(0,count($array)-1);}function encode($array){$response="";if(array()!==$array){$array_type=$this->isassociative($array)?"associative":"sequential";if($array_type=="associative"){$response.="{";}else{$response.="[";}$count=count($array);$index=0;foreach($array as $key=>$value){if($array_type=="associative"){$response.="\"";$response.=$key;$response.="\"";$response.=":";}if($value===true||$value===false){$response.=$value;}else if($value===null){$response.=$value;}else if(is_numeric($value)===true){$response.=$value;}else if(is_string($value)===true){$response.="\"".$value."\"";}else if(is_array($value)===true){$response.=$this->encode($value);}else{print "Error: Unknowm type!\n";break;}if(++$index!==$count){$response.=",";}}if($array_type=="associative"){$response.="}";}else{$response.="]";}}return $response;}function nextsif($characterif){$character=$this->input[$this->index];if(is_array($characterif)){while(in_array($character,$characterif)){if($this->index+1===$this->length){break;}$this->index++;$character=$this->input[$this->index];}}else{while($character==$characterif){if($this->index+1===$this->length){break;}$this->index++;$character=$this->input[$this->index];}}}function nextif($characterif){$character=$this->input[$this->index];if(is_array($characterif)){if(in_array($character,$characterif)){if($this->index+1===$this->length){return;}$this->index++;}}else{if($character==$characterif){if($this->index+1===$this->length){return;}$this->index++;}}}function skip($token,$tok){if($token[0]===$tok){$token=$this->nexttoken();}return $token;}function skips($token,$tok){while($token[0]===$tok){$token=$this->nexttoken();}return $token;}function nexttoken(){if($this->index+1>$this->length){return[tokentype::tokeneof,null];}$character=$this->input[$this->index];while($character==' '||$character==' '||$character=="\n"){if($this->index+1===$this->length){break;}$this->index++;$character=$this->input[$this->index];}if($character=='{'){$this->index++;return[tokentype::tokenobjectopen,null];}else if($character=='}'){$this->index++;return[tokentype::tokenobjectclose,null];}else if($character=='['){$this->index++;return[tokentype::tokenarrayopen,null];}else if($character==']'){$this->index++;return[tokentype::tokenarrayclose,null];}else if($character==','){$this->index++;return[tokentype::tokensplit,null];}else if($character==':'){$this->index++;return[tokentype::tokenpair,null];}else if($character==='n'||$character==='N'){$i=0;$i++;$character=$this->input[$this->index+$i];if($character==='u'||$character==='U'){$i++;$character=$this->input[$this->index+$i];if($character==='l'||$character==='L'){$i++;$character=$this->input[$this->index+$i];if($character==='l'||$character==='L'){$this->index++;$this->index++;$this->index++;$this->index++;return[tokentype::tokennull,null];}}}}else if($character==='t'||$character==='T'){$i=0;$i++;$character=$this->input[$this->index+$i];if($character==='r'||$character==='R'){$i++;$character=$this->input[$this->index+$i];if($character==='u'||$character==='U'){$i++;$character=$this->input[$this->index+$i];if($character==='e'||$character==='E'){$this->index++;$this->index++;$this->index++;$this->index++;return[tokentype::tokenbool,true];}}}}else if($character==='f'||$character==='F'){$i=0;$i++;$character=$this->input[$this->index+$i];if($character==='a'||$character==='A'){$i++;$character=$this->input[$this->index+$i];if($character==='l'||$character==='L'){$i++;$character=$this->input[$this->index+$i];if($character==='s'||$character==='S'){$i++;$character=$this->input[$this->index+$i];if($character==='e'||$character==='E'){$this->index++;$this->index++;$this->index++;$this->index++;$this->index++;return[tokentype::tokenbool,false];}}}}}else if($character==='"'||$character==='\''){$stype=null;if($character==='"'){$stype=1;}else if($character==='\''){$stype=2;}$result="";$characterprev="";$this->index++;$character=$this->input[$this->index];$characternext=null;while(($stype===1&&$characternext!=='"')||($stype===2&&$characternext!=='\'')){if($this->index==$this->length){break;}$character=$this->input[$this->index];if($this->index+1<$this->length){$characternext=$this->input[$this->index+1];}else{$characternext=null;}if($character==='\\'&&$characternext==='\''){$this->index++;$character=$characternext;if($this->index+1<$this->length){$characternext=$this->input[$this->index+1];}else{$characternext=null;}}else if($character==='\\'&&$characternext==='u'){$unicode='';$this->index++;$this->index++;$perform=true;$i=1;for(;$i<=4;$i++){if($this->index+1<$this->length){$characternext=$this->input[$this->index];$perform=true;if($characternext>='0'&&$characternext<='9'||$characternext>='A'&&$characternext<='F'){$character=$characternext;$unicode.=$character;$this->index++;}else{$perform=false;break;}}else{$perform=false;break;}}if($perform===true){$this->index--;$unicode="%u".$unicode;$unicode=preg_replace('/%u([0-9A-F]+)/','&#x$1;',$unicode);$character=html_entity_decode($unicode,ent_compat,'UTF-8');}else{$this->index--;$character="\\u".$unicode;}if($this->index+1<$this->length){$characternext=$this->input[$this->index+1];}else{$characternext=null;}}else if($character==='\\'&&$characternext==='n'){$this->index++;$character="\n";if($this->index+1<$this->length){$characternext=$this->input[$this->index+1];}else{$characternext=null;}}else if($character==='\\'&&$characternext==='\\'){$this->index++;$character="\\";if($this->index+1<$this->length){$characternext=$this->input[$this->index+1];}else{$characternext=null;}}else if($character==='\\'&&$characternext==='/'){$this->index++;$character="/";if($this->index+1<$this->length){$characternext=$this->input[$this->index+1];}else{$characternext=null;}}else if($character==='\\'&&$characternext==='t'){$this->index++;$character="\t";if($this->index+1<$this->length){$characternext=$this->input[$this->index+1];}else{$characternext=null;}}else if($character==='\\'&&$characternext==='r'){$this->index++;$character="\r";if($this->index+1<$this->length){$characternext=$this->input[$this->index+1];}else{$characternext=null;}}else if($character==='\\'&&$characternext==='b'){$this->index++;$character="\b";if($this->index+1<$this->length){$characternext=$this->input[$this->index+1];}else{$characternext=null;}}else if($character==='\\'&&$characternext==='"'){$this->index++;$character=$characternext;if($this->index+1<$this->length){$characternext=$this->input[$this->index+1];}else{$characternext=null;}}$result.=$character;$this->index++;}$this->index++;return[tokentype::tokenstring,$result];}else if(($character>='0'&&$character<='9')||$character=='-'||$character=='.'){$result=0;$bitflag=false;$bitfloat=false;$bitfloatindex=0;while(($character>='0'&&$character<='9')||$character=='-'||$character=='.'){if($this->index==$this->length){break;}if($bitflag===false&&$character==='-'){$bitflag=true;}else if($bitflag===true&&$character==='-'){exit("Aleady expression has a minus!\n");}else{if($bitfloat===false&&$character=='.'){$bitfloat=true;}else if($bitflag===true&&$character=='.'){exit("Aleady expression was a float type!\n");}else if($character=='e'||$character=='E'){exit("Soon, E+5 likely expression will develope....!\n");}else if($bitfloat===true){$bitfloatindex++;$floatcurrent=pow(10,$bitfloatindex);$result=$result+((int)$character/$floatcurrent);}else{$result=$result*10;$result=$result+(int)$character;}}$this->index++;if($this->index+1<$this->length){$character=$this->input[$this->index];}else{$character=null;}}if($bitflag===true){$result*=-1;}return[tokentype::tokennumber,$result];}else{$this->index++;}return[tokentype::tokeneof,null];}function isvalue($token){if($token[0]===tokentype::tokennumber){return[true,$token[1]];}else if($token[0]===tokentype::tokenstring){return[true,$token[1]];}else if($token[0]===tokentype::tokenbool){return[true,$token[1]];}else if($token[0]===tokentype::tokennull){return[true,null];}else if($token[0]===tokentype::tokenobjectopen){$this->index--;$result=$this->decode(null,false);return[true,$result];}else if($token[0]===tokentype::tokenarrayopen){$this->index--;$result=$this->decode(null,false);return[true,$result];}return[false,null];}function decode($input,$init=true){if($init===true){$this->index=0;$this->input=$input;$this->length=mb_strlen($input);}else{}$result=[];$this->token=$this->nexttoken();if($this->token[0]===tokentype::tokenarrayopen||$this->token[0]===tokentype::tokenobjectopen){$type=null;if($this->token[0]===tokentype::tokenarrayopen){$type=jsontype::jsonarray;}else if($this->token[0]===tokentype::tokenobjectopen){$type=jsontype::jsonobject;}$this->token=$this->nexttoken();$this->token=$this->skips($this->token,tokentype::tokensplit);while(($type===jsontype::jsonarray&&$this->token[0]!==tokentype::tokenarrayclose)||($type===jsontype::jsonobject&&$this->token[0]!==tokentype::tokenobjectclose)){if($this->token[0]===tokentype::tokeneof){exit("Command is finish, but arrayClose not found!\n");}$first=null;$second=null;$first=$this->isvalue($this->token);if($first[0]===true){$this->token=$this->nexttoken();if($type===jsontype::jsonobject){if($this->token[0]===tokentype::tokenpair){if(is_string($first[1])===true){$this->token=$this->nexttoken();$second=$this->isvalue($this->token);if($second[0]===true){$this->token=$this->nexttoken();}else{exit("Unknowm token, pair value is not a value!\n");}}else{exit("Unknowm token, key of pair value is not a string!\n");}}else{exit("Unknowm token, all item of object should was a pair value!\n");}}if($second===null){$result[]=$first[1];}else{$result[$first[1]]=$second[1];}$this->token=$this->skips($this->token,tokentype::tokensplit);}else{$this->token=$this->nexttoken();}}}else if($this->token[0]===tokentype::tokeneof){}else{exit("Unknowm token at the begin of command!\n");}return $result;}}$json=new json;