4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
21 * @package thrift.protocol
24 namespace Thrift\Protocol
;
26 use Thrift\Exception\TException
;
27 use Thrift\Exception\TProtocolException
;
28 use Thrift\Protocol\SimpleJSON\Context
;
29 use Thrift\Protocol\SimpleJSON\ListContext
;
30 use Thrift\Protocol\SimpleJSON\StructContext
;
31 use Thrift\Protocol\SimpleJSON\MapContext
;
32 use Thrift\Protocol\SimpleJSON\CollectionMapKeyException
;
35 * SimpleJSON implementation of thrift protocol, ported from Java.
37 class TSimpleJSONProtocol
extends TProtocol
47 const NAME_MAP
= "map";
48 const NAME_LIST
= "lst";
49 const NAME_SET
= "set";
51 protected $writeContext_ = null;
52 protected $writeContextStack_ = [];
55 * Push a new write context onto the stack.
57 protected function pushWriteContext(Context
$c)
59 $this->writeContextStack_
[] = $this->writeContext_
;
60 $this->writeContext_
= $c;
64 * Pop the last write context off the stack
66 protected function popWriteContext()
68 $this->writeContext_
= array_pop($this->writeContextStack_
);
72 * Used to make sure that we are not encountering a map whose keys are containers
74 protected function assertContextIsNotMapKey($invalidKeyType)
76 if ($this->writeContext_
->isMapKey()) {
77 throw new CollectionMapKeyException(
78 "Cannot serialize a map with keys that are of type " .
84 private function writeJSONString($b)
86 $this->writeContext_
->write();
88 $this->trans_
->write(json_encode((string)$b));
91 private function writeJSONInteger($num)
93 $isMapKey = $this->writeContext_
->isMapKey();
95 $this->writeContext_
->write();
98 $this->trans_
->write(self
::QUOTE
);
101 $this->trans_
->write((int)$num);
104 $this->trans_
->write(self
::QUOTE
);
108 private function writeJSONDouble($num)
110 $isMapKey = $this->writeContext_
->isMapKey();
112 $this->writeContext_
->write();
115 $this->trans_
->write(self
::QUOTE
);
118 $this->trans_
->write(json_encode((float)$num));
121 $this->trans_
->write(self
::QUOTE
);
128 public function __construct($trans)
130 parent
::__construct($trans);
131 $this->writeContext_
= new Context();
135 * Writes the message header
137 * @param string $name Function name
138 * @param int $type message type TMessageType::CALL or TMessageType::REPLY
139 * @param int $seqid The sequence id of this message
141 public function writeMessageBegin($name, $type, $seqid)
143 $this->trans_
->write(self
::LBRACKET
);
144 $this->pushWriteContext(new ListContext($this));
145 $this->writeJSONString($name);
146 $this->writeJSONInteger($type);
147 $this->writeJSONInteger($seqid);
153 public function writeMessageEnd()
155 $this->popWriteContext();
156 $this->trans_
->write(self
::RBRACKET
);
160 * Writes a struct header.
162 * @param string $name Struct name
164 public function writeStructBegin($name)
166 $this->writeContext_
->write();
167 $this->trans_
->write(self
::LBRACE
);
168 $this->pushWriteContext(new StructContext($this));
174 public function writeStructEnd()
176 $this->popWriteContext();
177 $this->trans_
->write(self
::RBRACE
);
180 public function writeFieldBegin($fieldName, $fieldType, $fieldId)
182 $this->writeJSONString($fieldName);
185 public function writeFieldEnd()
189 public function writeFieldStop()
193 public function writeMapBegin($keyType, $valType, $size)
195 $this->assertContextIsNotMapKey(self
::NAME_MAP
);
196 $this->writeContext_
->write();
197 $this->trans_
->write(self
::LBRACE
);
198 $this->pushWriteContext(new MapContext($this));
201 public function writeMapEnd()
203 $this->popWriteContext();
204 $this->trans_
->write(self
::RBRACE
);
207 public function writeListBegin($elemType, $size)
209 $this->assertContextIsNotMapKey(self
::NAME_LIST
);
210 $this->writeContext_
->write();
211 $this->trans_
->write(self
::LBRACKET
);
212 $this->pushWriteContext(new ListContext($this));
216 public function writeListEnd()
218 $this->popWriteContext();
219 $this->trans_
->write(self
::RBRACKET
);
222 public function writeSetBegin($elemType, $size)
224 $this->assertContextIsNotMapKey(self
::NAME_SET
);
225 $this->writeContext_
->write();
226 $this->trans_
->write(self
::LBRACKET
);
227 $this->pushWriteContext(new ListContext($this));
231 public function writeSetEnd()
233 $this->popWriteContext();
234 $this->trans_
->write(self
::RBRACKET
);
237 public function writeBool($bool)
239 $this->writeJSONInteger($bool ?
1 : 0);
242 public function writeByte($byte)
244 $this->writeJSONInteger($byte);
247 public function writeI16($i16)
249 $this->writeJSONInteger($i16);
252 public function writeI32($i32)
254 $this->writeJSONInteger($i32);
257 public function writeI64($i64)
259 $this->writeJSONInteger($i64);
262 public function writeDouble($dub)
264 $this->writeJSONDouble($dub);
267 public function writeString($str)
269 $this->writeJSONString($str);
275 * simplejson is not meant to be read back into thrift
276 * - see http://wiki.apache.org/thrift/ThriftUsageJava
280 public function readMessageBegin(&$name, &$type, &$seqid)
282 throw new TException("Not implemented");
285 public function readMessageEnd()
287 throw new TException("Not implemented");
290 public function readStructBegin(&$name)
292 throw new TException("Not implemented");
295 public function readStructEnd()
297 throw new TException("Not implemented");
300 public function readFieldBegin(&$name, &$fieldType, &$fieldId)
302 throw new TException("Not implemented");
305 public function readFieldEnd()
307 throw new TException("Not implemented");
310 public function readMapBegin(&$keyType, &$valType, &$size)
312 throw new TException("Not implemented");
315 public function readMapEnd()
317 throw new TException("Not implemented");
320 public function readListBegin(&$elemType, &$size)
322 throw new TException("Not implemented");
325 public function readListEnd()
327 throw new TException("Not implemented");
330 public function readSetBegin(&$elemType, &$size)
332 throw new TException("Not implemented");
335 public function readSetEnd()
337 throw new TException("Not implemented");
340 public function readBool(&$bool)
342 throw new TException("Not implemented");
345 public function readByte(&$byte)
347 throw new TException("Not implemented");
350 public function readI16(&$i16)
352 throw new TException("Not implemented");
355 public function readI32(&$i32)
357 throw new TException("Not implemented");
360 public function readI64(&$i64)
362 throw new TException("Not implemented");
365 public function readDouble(&$dub)
367 throw new TException("Not implemented");
370 public function readString(&$str)
372 throw new TException("Not implemented");