TSimpleJSONProtocol.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. * @package thrift.protocol
  21. */
  22. namespace Thrift\Protocol;
  23. use Thrift\Exception\TException;
  24. use Thrift\Exception\TProtocolException;
  25. use Thrift\Protocol\SimpleJSON\Context;
  26. use Thrift\Protocol\SimpleJSON\ListContext;
  27. use Thrift\Protocol\SimpleJSON\StructContext;
  28. use Thrift\Protocol\SimpleJSON\MapContext;
  29. use Thrift\Protocol\SimpleJSON\CollectionMapKeyException;
  30. /**
  31. * SimpleJSON implementation of thrift protocol, ported from Java.
  32. */
  33. class TSimpleJSONProtocol extends TProtocol
  34. {
  35. const COMMA = ',';
  36. const COLON = ':';
  37. const LBRACE = '{';
  38. const RBRACE = '}';
  39. const LBRACKET = '[';
  40. const RBRACKET = ']';
  41. const QUOTE = '"';
  42. const NAME_MAP = "map";
  43. const NAME_LIST = "lst";
  44. const NAME_SET = "set";
  45. protected $writeContext_ = null;
  46. protected $writeContextStack_ = [];
  47. /**
  48. * Push a new write context onto the stack.
  49. */
  50. protected function pushWriteContext(Context $c) {
  51. $this->writeContextStack_[] = $this->writeContext_;
  52. $this->writeContext_ = $c;
  53. }
  54. /**
  55. * Pop the last write context off the stack
  56. */
  57. protected function popWriteContext() {
  58. $this->writeContext_ = array_pop($this->writeContextStack_);
  59. }
  60. /**
  61. * Used to make sure that we are not encountering a map whose keys are containers
  62. */
  63. protected function assertContextIsNotMapKey($invalidKeyType) {
  64. if ($this->writeContext_->isMapKey()) {
  65. throw new CollectionMapKeyException(
  66. "Cannot serialize a map with keys that are of type " .
  67. $invalidKeyType
  68. );
  69. }
  70. }
  71. private function writeJSONString($b)
  72. {
  73. $this->writeContext_->write();
  74. $this->trans_->write(json_encode((string)$b));
  75. }
  76. private function writeJSONInteger($num)
  77. {
  78. $isMapKey = $this->writeContext_->isMapKey();
  79. $this->writeContext_->write();
  80. if ($isMapKey) {
  81. $this->trans_->write(self::QUOTE);
  82. }
  83. $this->trans_->write((int)$num);
  84. if ($isMapKey) {
  85. $this->trans_->write(self::QUOTE);
  86. }
  87. }
  88. private function writeJSONDouble($num)
  89. {
  90. $isMapKey = $this->writeContext_->isMapKey();
  91. $this->writeContext_->write();
  92. if ($isMapKey) {
  93. $this->trans_->write(self::QUOTE);
  94. }
  95. $this->trans_->write(json_encode((float)$num));
  96. if ($isMapKey) {
  97. $this->trans_->write(self::QUOTE);
  98. }
  99. }
  100. /**
  101. * Constructor
  102. */
  103. public function __construct($trans)
  104. {
  105. parent::__construct($trans);
  106. $this->writeContext_ = new Context();
  107. }
  108. /**
  109. * Writes the message header
  110. *
  111. * @param string $name Function name
  112. * @param int $type message type TMessageType::CALL or TMessageType::REPLY
  113. * @param int $seqid The sequence id of this message
  114. */
  115. public function writeMessageBegin($name, $type, $seqid)
  116. {
  117. $this->trans_->write(self::LBRACKET);
  118. $this->pushWriteContext(new ListContext($this));
  119. $this->writeJSONString($name);
  120. $this->writeJSONInteger($type);
  121. $this->writeJSONInteger($seqid);
  122. }
  123. /**
  124. * Close the message
  125. */
  126. public function writeMessageEnd()
  127. {
  128. $this->popWriteContext();
  129. $this->trans_->write(self::RBRACKET);
  130. }
  131. /**
  132. * Writes a struct header.
  133. *
  134. * @param string $name Struct name
  135. */
  136. public function writeStructBegin($name)
  137. {
  138. $this->writeContext_->write();
  139. $this->trans_->write(self::LBRACE);
  140. $this->pushWriteContext(new StructContext($this));
  141. }
  142. /**
  143. * Close a struct.
  144. */
  145. public function writeStructEnd()
  146. {
  147. $this->popWriteContext();
  148. $this->trans_->write(self::RBRACE);
  149. }
  150. public function writeFieldBegin($fieldName, $fieldType, $fieldId)
  151. {
  152. $this->writeJSONString($fieldName);
  153. }
  154. public function writeFieldEnd()
  155. {
  156. }
  157. public function writeFieldStop()
  158. {
  159. }
  160. public function writeMapBegin($keyType, $valType, $size)
  161. {
  162. $this->assertContextIsNotMapKey(self::NAME_MAP);
  163. $this->writeContext_->write();
  164. $this->trans_->write(self::LBRACE);
  165. $this->pushWriteContext(new MapContext($this));
  166. }
  167. public function writeMapEnd()
  168. {
  169. $this->popWriteContext();
  170. $this->trans_->write(self::RBRACE);
  171. }
  172. public function writeListBegin($elemType, $size)
  173. {
  174. $this->assertContextIsNotMapKey(self::NAME_LIST);
  175. $this->writeContext_->write();
  176. $this->trans_->write(self::LBRACKET);
  177. $this->pushWriteContext(new ListContext($this));
  178. // No metadata!
  179. }
  180. public function writeListEnd()
  181. {
  182. $this->popWriteContext();
  183. $this->trans_->write(self::RBRACKET);
  184. }
  185. public function writeSetBegin($elemType, $size)
  186. {
  187. $this->assertContextIsNotMapKey(self::NAME_SET);
  188. $this->writeContext_->write();
  189. $this->trans_->write(self::LBRACKET);
  190. $this->pushWriteContext(new ListContext($this));
  191. // No metadata!
  192. }
  193. public function writeSetEnd()
  194. {
  195. $this->popWriteContext();
  196. $this->trans_->write(self::RBRACKET);
  197. }
  198. public function writeBool($bool)
  199. {
  200. $this->writeJSONInteger($bool ? 1 : 0);
  201. }
  202. public function writeByte($byte)
  203. {
  204. $this->writeJSONInteger($byte);
  205. }
  206. public function writeI16($i16)
  207. {
  208. $this->writeJSONInteger($i16);
  209. }
  210. public function writeI32($i32)
  211. {
  212. $this->writeJSONInteger($i32);
  213. }
  214. public function writeI64($i64)
  215. {
  216. $this->writeJSONInteger($i64);
  217. }
  218. public function writeDouble($dub)
  219. {
  220. $this->writeJSONDouble($dub);
  221. }
  222. public function writeString($str)
  223. {
  224. $this->writeJSONString($str);
  225. }
  226. /**
  227. * Reading methods.
  228. *
  229. * simplejson is not meant to be read back into thrift
  230. * - see http://wiki.apache.org/thrift/ThriftUsageJava
  231. * - use JSON instead
  232. */
  233. public function readMessageBegin(&$name, &$type, &$seqid)
  234. {
  235. throw new TException("Not implemented");
  236. }
  237. public function readMessageEnd()
  238. {
  239. throw new TException("Not implemented");
  240. }
  241. public function readStructBegin(&$name)
  242. {
  243. throw new TException("Not implemented");
  244. }
  245. public function readStructEnd()
  246. {
  247. throw new TException("Not implemented");
  248. }
  249. public function readFieldBegin(&$name, &$fieldType, &$fieldId)
  250. {
  251. throw new TException("Not implemented");
  252. }
  253. public function readFieldEnd()
  254. {
  255. throw new TException("Not implemented");
  256. }
  257. public function readMapBegin(&$keyType, &$valType, &$size)
  258. {
  259. throw new TException("Not implemented");
  260. }
  261. public function readMapEnd()
  262. {
  263. throw new TException("Not implemented");
  264. }
  265. public function readListBegin(&$elemType, &$size)
  266. {
  267. throw new TException("Not implemented");
  268. }
  269. public function readListEnd()
  270. {
  271. throw new TException("Not implemented");
  272. }
  273. public function readSetBegin(&$elemType, &$size)
  274. {
  275. throw new TException("Not implemented");
  276. }
  277. public function readSetEnd()
  278. {
  279. throw new TException("Not implemented");
  280. }
  281. public function readBool(&$bool)
  282. {
  283. throw new TException("Not implemented");
  284. }
  285. public function readByte(&$byte)
  286. {
  287. throw new TException("Not implemented");
  288. }
  289. public function readI16(&$i16)
  290. {
  291. throw new TException("Not implemented");
  292. }
  293. public function readI32(&$i32)
  294. {
  295. throw new TException("Not implemented");
  296. }
  297. public function readI64(&$i64)
  298. {
  299. throw new TException("Not implemented");
  300. }
  301. public function readDouble(&$dub)
  302. {
  303. throw new TException("Not implemented");
  304. }
  305. public function readString(&$str)
  306. {
  307. throw new TException("Not implemented");
  308. }
  309. }