protected function writeObjectBegin($obj) { $class = get_class($obj); $alias = HproseClassManager::getClassAlias($class); $fields = array_keys((array) $obj); if (array_key_exists($alias, $this->classref)) { $index = $this->classref[$alias]; } else { $index = $this->writeClass($alias, $fields); } return $index; }
public function writeObject(&$object, $checkRef = true) { if ($checkRef && ($ref = $this->ref_search($object)) !== false) { $this->writeRef($ref); } else { $classname = HproseClassManager::getClassAlias(get_class($object)); $array = (array) $object; $fields = array_keys($array); $class = array($classname, $fields); if (($classref = array_search($class, $this->classref, true)) === false) { $classref = $this->writeClass($class); } $this->ref[] =& $object; $count = count($fields); $this->stream->write(HproseTags::TagObject . $classref . HproseTags::TagOpenbrace); for ($i = 0; $i < $count; $i++) { $this->serialize($array[$fields[$i]]); } $this->stream->write(HproseTags::TagClosebrace); } }
protected function readClass() { $classname = HproseClassManager::getClass(self::readString()); $count = (int) $this->stream->readuntil(HproseTags::TagOpenbrace); $fields = array(); for ($i = 0; $i < $count; ++$i) { $fields[] = $this->readString(true); } $this->stream->skip(1); $this->classref[] = array($classname, $fields); }
function hprose_fast_read_class($o) { $classname = HproseClassManager::getClass(hprose_simple_read_string($o)); $c = (int) hprose_readuntil($o, '{'); $fields = array(); for ($i = 0; $i < $c; ++$i) { $fields[] = hprose_fast_unserialize_string($o); } ++$o->p; $o->cr[] = array($classname, $fields); }
public function writeObject($obj) { $class = get_class($obj); $alias = HproseClassManager::getClassAlias($class); $fields = array_keys((array) $obj); if (array_key_exists($alias, $this->classref)) { $index = $this->classref[$alias]; } else { $index = $this->writeClass($alias, $fields); } $this->refer->set($obj); $fields = $this->fieldsref[$index]; $count = count($fields); $this->stream->write(HproseTags::TagObject . $index . HproseTags::TagOpenbrace); $array = (array) $obj; for ($i = 0; $i < $count; ++$i) { $this->serialize($array[$fields[$i]]); } $this->stream->write(HproseTags::TagClosebrace); }
function hprose_fast_serialize(&$v, $ro) { if ($v === NULL) { return 'n'; } if (is_int($v)) { if ($v >= 0 && $v <= 9) { return (string) $v; } return 'i' . $v . ';'; } if (is_bool($v)) { return $v ? 't' : 'f'; } if (is_float($v)) { if (is_nan($v)) { return 'N'; } if (is_infinite($v)) { return $v > 0 ? 'I+' : 'I-'; } return 'd' . $v . ';'; } if (is_string($v)) { if ($v === '') { return 'e'; } if (is_utf8($v)) { $l = ustrlen($v); if ($l == 1) { return 'u' . $v; } $h = hprose_hash($v, $ro); if (array_key_exists($h, $ro->r)) { return 'r' . $ro->r[$h] . ';'; } $ro->r[$h] = $ro->length++; return 's' . $l . '"' . $v . '"'; } $h = hprose_hash($v, $ro); if (array_key_exists($h, $ro->r)) { return 'r' . $ro->r[$h] . ';'; } $ro->r[$h] = $ro->length++; return 'b' . strlen($v) . '"' . $v . '"'; } if (is_array($v)) { $c = count($v); if (is_list($v)) { $h = hprose_hash($v, $ro); if (array_key_exists($h, $ro->r)) { return 'r' . $ro->r[$h] . ';'; } $ro->r[$h] = $ro->length++; if ($c == 0) { return 'a{}'; } $s = 'a' . $c . '{'; foreach ($v as &$val) { $s .= hprose_fast_serialize($val, $ro); } return $s . '}'; } $h = hprose_hash(map($v), $ro); if (array_key_exists($h, $ro->r)) { return 'r' . $ro->r[$h] . ';'; } $ro->r[$h] = $ro->length++; $s = 'm' . $c . '{'; foreach ($v as $key => &$val) { $s .= hprose_fast_serialize($key, $ro) . hprose_fast_serialize($val, $ro); } return $s . '}'; } if (is_object($v)) { $h = hprose_hash($v, $ro); if (array_key_exists($h, $ro->r)) { return 'r' . $ro->r[$h] . ';'; } if ($v instanceof stdClass) { $ro->r[$h] = $ro->length++; $v = (array) $v; $c = count($v); if ($c == 0) { return 'a{}'; } $s = 'm' . $c . '{'; foreach ($v as $key => &$val) { $s .= hprose_fast_serialize($key, $ro) . hprose_fast_serialize($val, $ro); } return $s . '}'; } if ($v instanceof DateTime) { $ro->r[$h] = $ro->length++; if ($v->getOffset() == 0) { return $v->format("\\DYmd\\THis.u\\Z"); } return $v->format("\\DYmd\\THis.u;"); } if ($v instanceof HproseDate || $v instanceof HproseDateTime) { $ro->r[$h] = $ro->length++; if ($v->utc) { return 'D' . $v->toString(false); } return 'D' . $v->toString(false) . 'Z'; } if ($v instanceof HproseTime) { $ro->r[$h] = $ro->length++; if ($v->utc) { return 'T' . $v->toString(false); } return 'T' . $v->toString(false) . 'Z'; } if ($v instanceof HproseBytes) { $ro->r[$h] = $ro->length++; $c = strlen($v->value); if ($c == 0) { return 'b""'; } return 'b' . $c . '"' . $v->value . '"'; } if ($v instanceof HproseMap) { $ro->r[$h] = $ro->length++; $c = count($v->value); if ($c == 0) { return 'a{}'; } $s = 'm' . $c . '{'; foreach ($v->value as $key => &$val) { $s .= hprose_fast_serialize($key, $ro) . hprose_fast_serialize($val, $ro); } return $s . '}'; } $class = get_class($v); $alias = HproseClassManager::getClassAlias($class); $a = (array) $v; if (array_key_exists($alias, $ro->cr)) { $index = $ro->cr[$alias]; $fields = $ro->fr[$index]; $c = count($fields); $s = ''; } else { $l = ustrlen($alias); $s = 'c' . $l . '"' . $alias . '"'; $fields = array_keys($a); $c = count($fields); if ($c > 0) { $s .= $c . '{'; foreach ($fields as $field) { if ($field[0] === "") { $field = substr($field, strpos($field, "", 1) + 1); } $fh = hprose_hash($field, $ro); $ro->r[$fh] = $ro->length++; $s .= 's' . ustrlen($field) . '"' . $field . '"'; } $s .= '}'; } else { $s .= '{}'; } $index = count($ro->fr); $ro->cr[$alias] = $index; $ro->fr[$index] = $fields; } $ro->r[$h] = $ro->length++; $s .= 'o' . $index . '{'; for ($i = 0; $i < $c; ++$i) { $s .= hprose_fast_serialize($a[$fields[$i]], $ro); } return $s . '}'; } throw new Exception('Not support to serialize this data'); }