ecshop类的解析2 json_encode和json_decode的具体实现
在看ecshop源码时,看到json这个类,研究了一下,它是为了兼容低版本php而做出的类,是对php的数据和json转换时使用的。
encode和decode函数是对json的操作,对应json_encode和json_decode函数。
<?php
/**
* ECSHOP JSON
* $Author: liubo $
* $Id: cls_json.php 17217 2011-01-19 06:29:08Z liubo $
*/
class JSON
{
var $at = 0;
var $ch = '';
var $text = '';
function encode($arg, $force = true)
{
static $_force;
if (is_null($_force))
{
$_force = $force;
}
if ($_force && EC_CHARSET == 'utf-8' && function_exists('json_encode'))
{
return json_encode($arg);
}
$returnValue = '';
$c = '';
$i = '';
$l = '';
$s = '';
$v = '';
$numeric = true;
switch (gettype($arg))
{
case 'array':
foreach ($arg AS $i => $v)
{
if (!is_numeric($i))
{
$numeric = false;
break;
}
}
if ($numeric)
{
foreach ($arg AS $i => $v)
{
if (strlen($s) > 0)
{
$s .= ',';
}
$s .= $this->encode($arg[$i]);
}
$returnValue = '[' . $s . ']';
}
else
{
foreach ($arg AS $i => $v)
{
if (strlen($s) > 0)
{
$s .= ',';
}
$s .= $this->encode($i) . ':' . $this->encode($arg[$i]);
}
$returnValue = '{' . $s . '}';
}
break;
case 'object':
foreach (get_object_vars($arg) AS $i => $v)
{
$v = $this->encode($v);
if (strlen($s) > 0)
{
$s .= ',';
}
$s .= $this->encode($i) . ':' . $v;
}
$returnValue = '{' . $s . '}';
break;
case 'integer':
case 'double':
$returnValue = is_numeric($arg) ? (string) $arg : 'null';
break;
case 'string':
$returnValue = '"' . strtr($arg, array(
"\r" => '\\r', "\n" => '\\n', "\t" => '\\t', "\b" => '\\b',
"\f" => '\\f', '\\' => '\\\\', '"' => '\"',
"\x00" => '\u0000', "\x01" => '\u0001', "\x02" => '\u0002', "\x03" => '\u0003',
"\x04" => '\u0004', "\x05" => '\u0005', "\x06" => '\u0006', "\x07" => '\u0007',
"\x08" => '\b', "\x0b" => '\u000b', "\x0c" => '\f', "\x0e" => '\u000e',
"\x0f" => '\u000f', "\x10" => '\u0010', "\x11" => '\u0011', "\x12" => '\u0012',
"\x13" => '\u0013', "\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016',
"\x17" => '\u0017', "\x18" => '\u0018', "\x19" => '\u0019', "\x1a" => '\u001a',
"\x1b" => '\u001b', "\x1c" => '\u001c', "\x1d" => '\u001d', "\x1e" => '\u001e',
"\x1f" => '\u001f'
)) . '"';
break;
case 'boolean':
$returnValue = $arg?'true':'false';
break;
default:
$returnValue = 'null';
}
return $returnValue;
}
function decode($text,$type=0) // 榛樿?type=0杩斿洖obj,type=1杩斿洖array
{
if (empty($text))
{
return '';
}
elseif (!is_string($text))
{
return false;
}
if (EC_CHARSET === 'utf-8' && function_exists('json_decode'))
{
return addslashes_deep_obj(json_decode(stripslashes($text),$type));
}
$this->at = 0;
$this->ch = '';
$this->text = strtr(stripslashes($text), array(
"\r" => '', "\n" => '', "\t" => '', "\b" => '',
"\x00" => '', "\x01" => '', "\x02" => '', "\x03" => '',
"\x04" => '', "\x05" => '', "\x06" => '', "\x07" => '',
"\x08" => '', "\x0b" => '', "\x0c" => '', "\x0e" => '',
"\x0f" => '', "\x10" => '', "\x11" => '', "\x12" => '',
"\x13" => '', "\x14" => '', "\x15" => '', "\x16" => '',
"\x17" => '', "\x18" => '', "\x19" => '', "\x1a" => '',
"\x1b" => '', "\x1c" => '', "\x1d" => '', "\x1e" => '',
"\x1f" => ''
));
$this->next();
$return = $this->val();
$result = empty($type) ? $return : $this->object_to_array($return);
return addslashes_deep_obj($result);
}
/**
* triggers a PHP_ERROR
*
* @access private
* @param string $m error message
*
* @return void
*/
function error($m)
{
echo($m . ' at offset ' . $this->at . ': ' . $this->text);
}
/**
* returns the next character of a JSON string
*
* @access private
*
* @return string
*/
function next()
{
$this->ch = !isset($this->text{$this->at}) ? '' : $this->text{$this->at};
$this->at++;
return $this->ch;
}
/**
* handles strings
*
* @access private
*
* @return void
*/
function str()
{
$i = '';
$s = '';
$t = '';
$u = '';
if ($this->ch == '"')
{
while ($this->next() !== null)
{
if ($this->ch == '"')
{
$this->next();
return $s;
}
elseif ($this->ch == '\\')
{
switch ($this->next())
{
case 'b':
$s .= '\b';
break;
case 'f':
$s .= '\f';
break;
case 'n':
$s .= '\n';
break;
case 'r':
$s .= '\r';
break;
case 't':
$s .= '\t';
break;
case 'u':
$u = 0;
for ($i = 0; $i < 4; $i++)
{
$t = (integer) sprintf('%01c', hexdec($this->next()));
if (!is_numeric($t))
{
break 2;
}
$u = $u * 16 + $t;
}
$s .= chr($u);
break;
case '\'':
$s .= '\'';
break;
default:
$s .= $this->ch;
}
}
else
{
$s .= $this->ch;
}
}
}
$this->error('Bad string');
}
/**
* handless arrays
*
* @access private
*
* @return void
*/
function arr()
{
$a = array();
if ($this->ch == '[')
{
$this->next();
if ($this->ch == ']')
{
$this->next();
return $a;
}
while (isset($this->ch))
{
array_push($a, $this->val());
if ($this->ch == ']')
{
$this->next();
return $a;
}
elseif ($this->ch != ',')
{
break;
}
$this->next();
}
$this->error('Bad array');
}
}
/**
* handles objects
*
* @access public
*
* @return void
*/
function obj()
{
$k = '';
$o = new StdClass();
if ($this->ch == '{')
{
$this->next();
if ($this->ch == '}')
{
$this->next();
return $o;
}
while ($this->ch)
{
$k = $this->str();
if ($this->ch != ':')
{
break;
}
$this->next();
$o->$k = $this->val();
if ($this->ch == '}')
{
$this->next();
return $o;
}
elseif ($this->ch != ',')
{
break;
}
$this->next();
}
}
$this->error('Bad object');
}
/**
* handles objects
*
* @access public
*
* @return void
*/
function assoc()
{
$k = '';
$a = array();
if ($this->ch == '<')
{
$this->next();
if ($this->ch == '>')
{
$this->next();
return $a;
}
while ($this->ch)
{
$k = $this->str();
if ($this->ch != ':')
{
break;
}
$this->next();
$a[$k] = $this->val();
if ($this->ch == '>')
{
$this->next();
return $a;
}
elseif ($this->ch != ',')
{
break;
}
$this->next();
}
}
$this->error('Bad associative array');
}
/**
* handles numbers
*
* @access private
*
* @return void
*/
function num()
{
$n = '';
$v = '';
if ($this->ch == '-')
{
$n = '-';
$this->next();
}
while ($this->ch >= '0' && $this->ch <= '9')
{
$n .= $this->ch;
$this->next();
}
if ($this->ch == '.')
{
$n .= '.';
while ($this->next() && $this->ch >= '0' && $this->ch <= '9')
{
$n .= $this->ch;
}
}
if ($this->ch == 'e' || $this->ch == 'E')
{
$n .= 'e';
$this->next();
if ($this->ch == '-' || $this->ch == '+')
{
$n .= $this->ch;
$this->next();
}
while ($this->ch >= '0' && $this->ch <= '9')
{
$n .= $this->ch;
$this->next();
}
}
$v += $n;
if (!is_numeric($v))
{
$this->error('Bad number');
}
else
{
return $v;
}
}
/**
* handles words
*
* @access private
*
* @return mixed
*/
function word()
{
switch ($this->ch)
{
case 't':
if ($this->next() == 'r' && $this->next() == 'u' && $this->next() == 'e')
{
$this->next();
return true;
}
break;
case 'f':
if ($this->next() == 'a' && $this->next() == 'l' && $this->next() == 's' && $this->next() == 'e')
{
$this->next();
return false;
}
break;
case 'n':
if ($this->next() == 'u' && $this->next() == 'l' && $this->next() == 'l')
{
$this->next();
return null;
}
break;
}
$this->error('Syntax error');
}
/**
* generic value handler
*
* @access private
*
* @return mixed
*/
function val()
{
switch ($this->ch)
{
case '{':
return $this->obj();
case '[':
return $this->arr();
case '<':
return $this->assoc();
case '"':
return $this->str();
case '-':
return $this->num();
default:
return ($this->ch >= '0' && $this->ch <= '9') ? $this->num() : $this->word();
}
}
/**
* Gets the properties of the given object recursion
*
* @access private
*
* @return array
*/
function object_to_array($obj)
{
$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
foreach ($_arr as $key => $val)
{
$val = (is_array($val) || is_object($val)) ? $this->object_to_array($val) : $val;
$arr[$key] = $val;
}
return $arr;
}
}
?>
ecshop类的解析2 json_encode和json_decode的具体实现的更多相关文章
- ecshop类的解析1
前面写了一下我理解的ecshop数据库表,现在看一下我理解的ecshop的类. ecshop类,ECS是一个基础类,它的取得域名的函数我感觉是比较不错的. function get_domain() ...
- php中的json_encode()和json_decode()函数的一些说明
一,json语法( php中的json_decode($json)中的$json要符合json语法格式 ) ① JSON可以表示三种类型的值 1,简单值.包括整型,字符串型,布尔值和null.例如:5 ...
- php json_encode()和json_decode()
json_encode()和json_decode()分别是编译和反编译过程 注意json只接受utf-8编码的字符,所以json_encode()的参数必须是utf-8编码,否则会得到空字符或者nu ...
- PHP数组和Json之间的互相转换 json_encode() 和 json_decode()
之所以要用到Json,很多时候是因为使用ajax对象时,程序与JS函数之间的数据交互.因为JS不认识PHP中的数组,PHP也不认识JS中的数组或对象.Json很好的解决了这个问题. Json简介 JS ...
- 如何利用.Net内置类,解析未知复杂Json对象
如何利用.Net内置类,解析未知复杂Json对象 如果你乐意,当然可以使用强大的第三方类库Json.Net中的JObject类解析复杂Json字串 . 我不太希望引入第三方类库,所以在.Net内置类J ...
- Java 8 Optional 类深度解析
Java 8 Optional 类深度解析 身为一名Java程序员,大家可能都有这样的经历:调用一个方法得到了返回值却不能直接将返回值作为参数去调用别的方法.我们首先要判断这个返回值是否为null,只 ...
- javap -- Java 类文件解析器
参考文档 http://blog.chinaunix.net/uid-692788-id-2681132.html http://docs.oracle.com/javase/7/docs/techn ...
- json_encode和json_decode和isset和array_key_exists
1.json_decode() json_decode — 对 JSON 格式的字符串进行编码 说明 mixed json_decode ( string $json [, bool ...
- 解析HTML文件 - 运用SgmlReader类来解析HTML文件
运用.NET Framework类来解析HTML文件.读取数据并不是最容易的.虽然你可以用.NET Framework中的许多类(如StreamReader)来逐行解析文件,但XmlReader提供的 ...
随机推荐
- css的基本单词
<border>边框 border边框 <text>文本 text文本 <indent>缩进 indent缩进 <align>对齐方式 align对齐方 ...
- C# 定时无操作则退出登陆,回到登陆界面。
有时候根据需求需要为程序添加在规定的时间内无操作则退出当前的登陆程序的功能,如下代码模拟描述的需求功能. using System; using System.Collections.Generic; ...
- 打造最强NGINX HTTPS
SSL LABS 检测 完整配置如下 server { listen 443 ssl; server_name xxx.xxxke.com; ssl on; ssl_certificate /xxx/ ...
- 一个ROS的服务,使机器人向前移动指定距离
源代码有点长,放文末链接里了. 服务描述及代码现在的服务是:请求时携带要前进的距离,然后底盘前进相应距离.代码如下,改动很小: #!/usr/bin/env python import rospyfr ...
- [API 开发管理] EOLINKER 升级为多产品架构, AMS V4.5 版本常见问题汇总
自AMS4.5开始,eoLinker 全面升级为多产品架构,部分操作方式较以前有较大改变,本文针对改进部分做重点说明. 在说明之前,我们先通过以下的图文看看AMSV4.5更新了哪些内容: Q:我可以创 ...
- pandas操作,按序号取列,按条件筛选,df格式转换等
这几天遇到比较多的dataframe操作,频繁使用,在此整理记录下,方便查找. 1.num为列的数字序号,name=df.columns[num],返回的是column的字符串名字,df[name]= ...
- SQL上门2
SQL高级教程学习 MySQL的字符匹配和其他数据库不同,一下语句查找(第一个字符不是h,第三个字符是m)不能用“!” select * from country where countryname ...
- C#关键字详解第三节
byte:字节 字节是计算机信息技术用于计量存储容量的一种计量单位,通常情况下一字节等于八位,也在一些计算机编程 语言中表示数据类型和语言字符.这是百度百科给出的解释,在C#语言中byte也可以是一种 ...
- 【codeforces 701C】They Are Everywhere
[题目链接]:http://codeforces.com/contest/701/problem/C [题意] 让你选择一段最短的区间; 使得这段区间里面包含所有种类的字符; [题解] 之前都是用二分 ...
- CSVHelper在Asp.Net MVC中的使用
1,从数据库读取数据,然后导出CSV文件 [HttpPost] public FileResult ExportCSV() { var apps =....//linq以及EF从数据库查询数据 Mem ...