3 class haxe_Serializer {
4 public function __construct() {
5 if(!php_Boot::$skip_constructor) {
6 $this->buf = new StringBuf();
7 $this->cache = new _hx_array(array());
8 $this->useCache = haxe_Serializer::$USE_CACHE;
9 $this->useEnumIndex = haxe_Serializer::$USE_ENUM_INDEX;
10 $this->shash = new Hash();
13 public function serializeException($e) {
17 public function serialize($v) {
18 $»t = (Type::typeof($v));
26 if(_hx_equal($v, 0)) {
38 if(!Math::isFinite($v)) {
39 $this->buf->add((($v < 0) ? "m" : "p"));
48 $this->buf->add((($v) ? "t" : "f"));
53 if($c == _hx_qtype("String")) {
54 $this->serializeString($v);
57 if($this->useCache && $this->serializeRef($v)) {
61 case _hx_qtype("Array"):{
77 $this->buf->add($ucount);
81 $this->serialize($v[$i]);
91 $this->buf->add($ucount);
96 case _hx_qtype("List"):{
99 if(null == $v1) throw new HException('null iterable');
100 $»it = $v1->iterator();
101 while($»it->hasNext()) {
103 $this->serialize($i);
105 $this->buf->add("h");
107 case _hx_qtype("Date"):{
109 $this->buf->add("v");
110 $this->buf->add($d->toString());
112 case _hx_qtype("Hash"):{
113 $this->buf->add("b");
115 if(null == $v1) throw new HException('null iterable');
117 while($»it->hasNext()) {
119 $this->serializeString($k);
120 $this->serialize($v1->get($k));
122 $this->buf->add("h");
124 case _hx_qtype("IntHash"):{
125 $this->buf->add("q");
127 if(null == $v1) throw new HException('null iterable');
129 while($»it->hasNext()) {
131 $this->buf->add(":");
133 $this->serialize($v1->get($k));
135 $this->buf->add("h");
137 case _hx_qtype("haxe.io.Bytes"):{
140 $max = $v1->length - 2;
141 $charsBuf = new StringBuf();
142 $b64 = haxe_Serializer::$BASE64;
144 $b1 = ord($v1->b[$i++]);
145 $b2 = ord($v1->b[$i++]);
146 $b3 = ord($v1->b[$i++]);
147 $charsBuf->add(_hx_char_at($b64, $b1 >> 2));
148 $charsBuf->add(_hx_char_at($b64, ($b1 << 4 | $b2 >> 4) & 63));
149 $charsBuf->add(_hx_char_at($b64, ($b2 << 2 | $b3 >> 6) & 63));
150 $charsBuf->add(_hx_char_at($b64, $b3 & 63));
154 $b1 = ord($v1->b[$i++]);
155 $b2 = ord($v1->b[$i++]);
156 $charsBuf->add(_hx_char_at($b64, $b1 >> 2));
157 $charsBuf->add(_hx_char_at($b64, ($b1 << 4 | $b2 >> 4) & 63));
158 $charsBuf->add(_hx_char_at($b64, $b2 << 2 & 63));
160 if($i === $max + 1) {
161 $b1 = ord($v1->b[$i++]);
162 $charsBuf->add(_hx_char_at($b64, $b1 >> 2));
163 $charsBuf->add(_hx_char_at($b64, $b1 << 4 & 63));
166 $chars = $charsBuf->b;
167 $this->buf->add("s");
168 $this->buf->add(strlen($chars));
169 $this->buf->add(":");
170 $this->buf->add($chars);
174 if(_hx_field($v, "hxSerialize") !== null) {
175 $this->buf->add("C");
176 $this->serializeString(Type::getClassName($c));
177 $this->cache->push($v);
178 $v->hxSerialize($this);
179 $this->buf->add("g");
181 $this->buf->add("c");
182 $this->serializeString(Type::getClassName($c));
183 $this->cache->push($v);
184 $this->serializeFields($v);
191 if($this->useCache && $this->serializeRef($v)) {
194 $this->buf->add("o");
195 $this->serializeFields($v);
200 if($this->useCache && $this->serializeRef($v)) {
204 $this->buf->add((($this->useEnumIndex) ? "j" : "w"));
205 $this->serializeString(Type::getEnumName($e));
206 if($this->useEnumIndex) {
207 $this->buf->add(":");
208 $this->buf->add($v->index);
210 $this->serializeString($v->tag);
212 $this->buf->add(":");
213 $l = count($v->params);
214 if($l === 0 || _hx_field($v, "params") === null) {
222 $this->serialize($v->params[$i]);
227 $this->cache->push($v);
231 throw new HException("Cannot serialize function");
234 throw new HException("Cannot serialize " . Std::string($v));
238 public function serializeFields($v) {
240 $_g = 0; $_g1 = Reflect::fields($v);
241 while($_g < $_g1->length) {
244 $this->serializeString($f);
245 $this->serialize(Reflect::field($v, $f));
249 $this->buf->add("g");
251 public function serializeRef($v) {
253 $_g1 = 0; $_g = $this->cache->length;
256 if(_hx_equal($this->cache[$i], $v)) {
257 $this->buf->add("r");
264 $this->cache->push($v);
267 public function serializeString($s) {
268 $x = $this->shash->get($s);
270 $this->buf->add("R");
274 $this->shash->set($s, $this->scount++);
275 $this->buf->add("y");
276 $s = rawurlencode($s);
277 $this->buf->add(strlen($s));
278 $this->buf->add(":");
281 public function toString() {
282 return $this->buf->b;
284 public $useEnumIndex;
290 public function __call($m, $a) {
291 if(isset($this->$m) && is_callable($this->$m))
292 return call_user_func_array($this->$m, $a);
293 else if(isset($this->»dynamics[$m]) && is_callable($this->»dynamics[$m]))
294 return call_user_func_array($this->»dynamics[$m], $a);
295 else if('toString' == $m)
296 return $this->__toString();
298 throw new HException('Unable to call «'.$m.'»');
300 static $USE_CACHE = false;
301 static $USE_ENUM_INDEX = false;
302 static $BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%:";
303 static function run($v) {
304 $s = new haxe_Serializer();
306 return $s->toString();
308 function __toString() { return $this->toString(); }