blob: 5e6589e0b0bb2f25055b2219c19aed3e54405ca7 [file] [log] [blame]
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20using System;
21using System.IO;
22using System.Text;
23using System.Collections.Generic;
24
25using Thrift.Transport;
Roger Meier3da317b2011-07-28 18:35:51 +000026using System.Globalization;
Bryan Duxburyfd32d792010-09-18 20:51:25 +000027
28namespace Thrift.Protocol
29{
Jens Geyerd5436f52014-10-03 19:50:38 +020030 /// <summary>
31 /// JSON protocol implementation for thrift.
32 ///
33 /// This is a full-featured protocol supporting Write and Read.
34 ///
35 /// Please see the C++ class header for a detailed description of the
36 /// protocol's wire format.
37 ///
38 /// Adapted from the Java version.
39 /// </summary>
40 public class TJSONProtocol : TProtocol
41 {
42 /// <summary>
43 /// Factory for JSON protocol objects
44 /// </summary>
45 public class Factory : TProtocolFactory
46 {
47 public TProtocol GetProtocol(TTransport trans)
48 {
49 return new TJSONProtocol(trans);
50 }
51 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +000052
Jens Geyerd5436f52014-10-03 19:50:38 +020053 private static byte[] COMMA = new byte[] { (byte)',' };
54 private static byte[] COLON = new byte[] { (byte)':' };
55 private static byte[] LBRACE = new byte[] { (byte)'{' };
56 private static byte[] RBRACE = new byte[] { (byte)'}' };
57 private static byte[] LBRACKET = new byte[] { (byte)'[' };
58 private static byte[] RBRACKET = new byte[] { (byte)']' };
59 private static byte[] QUOTE = new byte[] { (byte)'"' };
60 private static byte[] BACKSLASH = new byte[] { (byte)'\\' };
Bryan Duxburyfd32d792010-09-18 20:51:25 +000061
Jens Geyerd5436f52014-10-03 19:50:38 +020062 private byte[] ESCSEQ = new byte[] { (byte)'\\', (byte)'u', (byte)'0', (byte)'0' };
Bryan Duxburyfd32d792010-09-18 20:51:25 +000063
Jens Geyerd5436f52014-10-03 19:50:38 +020064 private const long VERSION = 1;
65 private byte[] JSON_CHAR_TABLE = {
66 0, 0, 0, 0, 0, 0, 0, 0,(byte)'b',(byte)'t',(byte)'n', 0,(byte)'f',(byte)'r', 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 1, 1,(byte)'"', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Bryan Duxburyfd32d792010-09-18 20:51:25 +000069 };
70
Jens Geyerd5436f52014-10-03 19:50:38 +020071 private char[] ESCAPE_CHARS = "\"\\/bfnrt".ToCharArray();
Bryan Duxburyfd32d792010-09-18 20:51:25 +000072
Jens Geyerd5436f52014-10-03 19:50:38 +020073 private byte[] ESCAPE_CHAR_VALS = {
74 (byte)'"', (byte)'\\', (byte)'/', (byte)'\b', (byte)'\f', (byte)'\n', (byte)'\r', (byte)'\t',
Bryan Duxburyfd32d792010-09-18 20:51:25 +000075 };
76
Jens Geyerd5436f52014-10-03 19:50:38 +020077 private const int DEF_STRING_SIZE = 16;
Bryan Duxburyfd32d792010-09-18 20:51:25 +000078
Jens Geyerd5436f52014-10-03 19:50:38 +020079 private static byte[] NAME_BOOL = new byte[] { (byte)'t', (byte)'f' };
80 private static byte[] NAME_BYTE = new byte[] { (byte)'i', (byte)'8' };
81 private static byte[] NAME_I16 = new byte[] { (byte)'i', (byte)'1', (byte)'6' };
82 private static byte[] NAME_I32 = new byte[] { (byte)'i', (byte)'3', (byte)'2' };
83 private static byte[] NAME_I64 = new byte[] { (byte)'i', (byte)'6', (byte)'4' };
84 private static byte[] NAME_DOUBLE = new byte[] { (byte)'d', (byte)'b', (byte)'l' };
85 private static byte[] NAME_STRUCT = new byte[] { (byte)'r', (byte)'e', (byte)'c' };
86 private static byte[] NAME_STRING = new byte[] { (byte)'s', (byte)'t', (byte)'r' };
87 private static byte[] NAME_MAP = new byte[] { (byte)'m', (byte)'a', (byte)'p' };
88 private static byte[] NAME_LIST = new byte[] { (byte)'l', (byte)'s', (byte)'t' };
89 private static byte[] NAME_SET = new byte[] { (byte)'s', (byte)'e', (byte)'t' };
Bryan Duxburyfd32d792010-09-18 20:51:25 +000090
Jens Geyerd5436f52014-10-03 19:50:38 +020091 private static byte[] GetTypeNameForTypeID(TType typeID)
92 {
93 switch (typeID)
94 {
95 case TType.Bool:
96 return NAME_BOOL;
97 case TType.Byte:
98 return NAME_BYTE;
99 case TType.I16:
100 return NAME_I16;
101 case TType.I32:
102 return NAME_I32;
103 case TType.I64:
104 return NAME_I64;
105 case TType.Double:
106 return NAME_DOUBLE;
107 case TType.String:
108 return NAME_STRING;
109 case TType.Struct:
110 return NAME_STRUCT;
111 case TType.Map:
112 return NAME_MAP;
113 case TType.Set:
114 return NAME_SET;
115 case TType.List:
116 return NAME_LIST;
117 default:
118 throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED,
119 "Unrecognized type");
120 }
121 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000122
Jens Geyerd5436f52014-10-03 19:50:38 +0200123 private static TType GetTypeIDForTypeName(byte[] name)
124 {
125 TType result = TType.Stop;
126 if (name.Length > 1)
127 {
128 switch (name[0])
129 {
130 case (byte)'d':
131 result = TType.Double;
132 break;
133 case (byte)'i':
134 switch (name[1])
135 {
136 case (byte)'8':
137 result = TType.Byte;
138 break;
139 case (byte)'1':
140 result = TType.I16;
141 break;
142 case (byte)'3':
143 result = TType.I32;
144 break;
145 case (byte)'6':
146 result = TType.I64;
147 break;
148 }
149 break;
150 case (byte)'l':
151 result = TType.List;
152 break;
153 case (byte)'m':
154 result = TType.Map;
155 break;
156 case (byte)'r':
157 result = TType.Struct;
158 break;
159 case (byte)'s':
160 if (name[1] == (byte)'t')
161 {
162 result = TType.String;
163 }
164 else if (name[1] == (byte)'e')
165 {
166 result = TType.Set;
167 }
168 break;
169 case (byte)'t':
170 result = TType.Bool;
171 break;
172 }
173 }
174 if (result == TType.Stop)
175 {
176 throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED,
177 "Unrecognized type");
178 }
179 return result;
180 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000181
Jens Geyerd5436f52014-10-03 19:50:38 +0200182 ///<summary>
183 /// Base class for tracking JSON contexts that may require
184 /// inserting/Reading additional JSON syntax characters
185 /// This base context does nothing.
186 ///</summary>
187 protected class JSONBaseContext
188 {
189 protected TJSONProtocol proto;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000190
Jens Geyerd5436f52014-10-03 19:50:38 +0200191 public JSONBaseContext(TJSONProtocol proto)
192 {
193 this.proto = proto;
194 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000195
Jens Geyerd5436f52014-10-03 19:50:38 +0200196 public virtual void Write() { }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000197
Jens Geyerd5436f52014-10-03 19:50:38 +0200198 public virtual void Read() { }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000199
Jens Geyerd5436f52014-10-03 19:50:38 +0200200 public virtual bool EscapeNumbers() { return false; }
201 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000202
Jens Geyerd5436f52014-10-03 19:50:38 +0200203 ///<summary>
204 /// Context for JSON lists. Will insert/Read commas before each item except
205 /// for the first one
206 ///</summary>
207 protected class JSONListContext : JSONBaseContext
208 {
209 public JSONListContext(TJSONProtocol protocol)
210 : base(protocol)
211 {
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000212
Jens Geyerd5436f52014-10-03 19:50:38 +0200213 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000214
Jens Geyerd5436f52014-10-03 19:50:38 +0200215 private bool first = true;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000216
Jens Geyerd5436f52014-10-03 19:50:38 +0200217 public override void Write()
218 {
219 if (first)
220 {
221 first = false;
222 }
223 else
224 {
225 proto.trans.Write(COMMA);
226 }
227 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000228
Jens Geyerd5436f52014-10-03 19:50:38 +0200229 public override void Read()
230 {
231 if (first)
232 {
233 first = false;
234 }
235 else
236 {
237 proto.ReadJSONSyntaxChar(COMMA);
238 }
239 }
240 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000241
Jens Geyerd5436f52014-10-03 19:50:38 +0200242 ///<summary>
243 /// Context for JSON records. Will insert/Read colons before the value portion
244 /// of each record pair, and commas before each key except the first. In
245 /// addition, will indicate that numbers in the key position need to be
246 /// escaped in quotes (since JSON keys must be strings).
247 ///</summary>
248 protected class JSONPairContext : JSONBaseContext
249 {
250 public JSONPairContext(TJSONProtocol proto)
251 : base(proto)
252 {
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000253
Jens Geyerd5436f52014-10-03 19:50:38 +0200254 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000255
Jens Geyerd5436f52014-10-03 19:50:38 +0200256 private bool first = true;
257 private bool colon = true;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000258
Jens Geyerd5436f52014-10-03 19:50:38 +0200259 public override void Write()
260 {
261 if (first)
262 {
263 first = false;
264 colon = true;
265 }
266 else
267 {
268 proto.trans.Write(colon ? COLON : COMMA);
269 colon = !colon;
270 }
271 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000272
Jens Geyerd5436f52014-10-03 19:50:38 +0200273 public override void Read()
274 {
275 if (first)
276 {
277 first = false;
278 colon = true;
279 }
280 else
281 {
282 proto.ReadJSONSyntaxChar(colon ? COLON : COMMA);
283 colon = !colon;
284 }
285 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000286
Jens Geyerd5436f52014-10-03 19:50:38 +0200287 public override bool EscapeNumbers()
288 {
289 return colon;
290 }
291 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000292
Jens Geyerd5436f52014-10-03 19:50:38 +0200293 ///<summary>
294 /// Holds up to one byte from the transport
295 ///</summary>
296 protected class LookaheadReader
297 {
298 protected TJSONProtocol proto;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000299
Jens Geyerd5436f52014-10-03 19:50:38 +0200300 public LookaheadReader(TJSONProtocol proto)
301 {
302 this.proto = proto;
303 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000304
Jens Geyerd5436f52014-10-03 19:50:38 +0200305 private bool hasData;
306 private byte[] data = new byte[1];
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000307
Jens Geyerd5436f52014-10-03 19:50:38 +0200308 ///<summary>
309 /// Return and consume the next byte to be Read, either taking it from the
310 /// data buffer if present or getting it from the transport otherwise.
311 ///</summary>
312 public byte Read()
313 {
314 if (hasData)
315 {
316 hasData = false;
317 }
318 else
319 {
320 proto.trans.ReadAll(data, 0, 1);
321 }
322 return data[0];
323 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000324
Jens Geyerd5436f52014-10-03 19:50:38 +0200325 ///<summary>
326 /// Return the next byte to be Read without consuming, filling the data
327 /// buffer if it has not been filled alReady.
328 ///</summary>
329 public byte Peek()
330 {
331 if (!hasData)
332 {
333 proto.trans.ReadAll(data, 0, 1);
334 }
335 hasData = true;
336 return data[0];
337 }
338 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000339
Jens Geyerd5436f52014-10-03 19:50:38 +0200340 // Default encoding
341 protected Encoding utf8Encoding = UTF8Encoding.UTF8;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000342
Jens Geyerd5436f52014-10-03 19:50:38 +0200343 // Stack of nested contexts that we may be in
344 protected Stack<JSONBaseContext> contextStack = new Stack<JSONBaseContext>();
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000345
Jens Geyerd5436f52014-10-03 19:50:38 +0200346 // Current context that we are in
347 protected JSONBaseContext context;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000348
Jens Geyerd5436f52014-10-03 19:50:38 +0200349 // Reader that manages a 1-byte buffer
350 protected LookaheadReader reader;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000351
Jens Geyerd5436f52014-10-03 19:50:38 +0200352 ///<summary>
353 /// Push a new JSON context onto the stack.
354 ///</summary>
355 protected void PushContext(JSONBaseContext c)
356 {
357 contextStack.Push(context);
358 context = c;
359 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000360
Jens Geyerd5436f52014-10-03 19:50:38 +0200361 ///<summary>
362 /// Pop the last JSON context off the stack
363 ///</summary>
364 protected void PopContext()
365 {
366 context = contextStack.Pop();
367 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000368
Jens Geyerd5436f52014-10-03 19:50:38 +0200369 ///<summary>
370 /// TJSONProtocol Constructor
371 ///</summary>
372 public TJSONProtocol(TTransport trans)
373 : base(trans)
374 {
375 context = new JSONBaseContext(this);
376 reader = new LookaheadReader(this);
377 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000378
Jens Geyerd5436f52014-10-03 19:50:38 +0200379 // Temporary buffer used by several methods
380 private byte[] tempBuffer = new byte[4];
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000381
Jens Geyerd5436f52014-10-03 19:50:38 +0200382 ///<summary>
383 /// Read a byte that must match b[0]; otherwise an exception is thrown.
384 /// Marked protected to avoid synthetic accessor in JSONListContext.Read
385 /// and JSONPairContext.Read
386 ///</summary>
387 protected void ReadJSONSyntaxChar(byte[] b)
388 {
389 byte ch = reader.Read();
390 if (ch != b[0])
391 {
392 throw new TProtocolException(TProtocolException.INVALID_DATA,
393 "Unexpected character:" + (char)ch);
394 }
395 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000396
Jens Geyerd5436f52014-10-03 19:50:38 +0200397 ///<summary>
398 /// Convert a byte containing a hex char ('0'-'9' or 'a'-'f') into its
399 /// corresponding hex value
400 ///</summary>
401 private static byte HexVal(byte ch)
402 {
403 if ((ch >= '0') && (ch <= '9'))
404 {
405 return (byte)((char)ch - '0');
406 }
407 else if ((ch >= 'a') && (ch <= 'f'))
408 {
409 ch += 10;
410 return (byte)((char)ch - 'a');
411 }
412 else
413 {
414 throw new TProtocolException(TProtocolException.INVALID_DATA,
415 "Expected hex character");
416 }
417 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000418
Jens Geyerd5436f52014-10-03 19:50:38 +0200419 ///<summary>
420 /// Convert a byte containing a hex value to its corresponding hex character
421 ///</summary>
422 private static byte HexChar(byte val)
423 {
424 val &= 0x0F;
425 if (val < 10)
426 {
427 return (byte)((char)val + '0');
428 }
429 else
430 {
431 val -= 10;
432 return (byte)((char)val + 'a');
433 }
434 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000435
Jens Geyerd5436f52014-10-03 19:50:38 +0200436 ///<summary>
437 /// Write the bytes in array buf as a JSON characters, escaping as needed
438 ///</summary>
439 private void WriteJSONString(byte[] b)
440 {
441 context.Write();
442 trans.Write(QUOTE);
443 int len = b.Length;
444 for (int i = 0; i < len; i++)
445 {
446 if ((b[i] & 0x00FF) >= 0x30)
447 {
448 if (b[i] == BACKSLASH[0])
449 {
450 trans.Write(BACKSLASH);
451 trans.Write(BACKSLASH);
452 }
453 else
454 {
455 trans.Write(b, i, 1);
456 }
457 }
458 else
459 {
460 tempBuffer[0] = JSON_CHAR_TABLE[b[i]];
461 if (tempBuffer[0] == 1)
462 {
463 trans.Write(b, i, 1);
464 }
465 else if (tempBuffer[0] > 1)
466 {
467 trans.Write(BACKSLASH);
468 trans.Write(tempBuffer, 0, 1);
469 }
470 else
471 {
472 trans.Write(ESCSEQ);
473 tempBuffer[0] = HexChar((byte)(b[i] >> 4));
474 tempBuffer[1] = HexChar(b[i]);
475 trans.Write(tempBuffer, 0, 2);
476 }
477 }
478 }
479 trans.Write(QUOTE);
480 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000481
Jens Geyerd5436f52014-10-03 19:50:38 +0200482 ///<summary>
483 /// Write out number as a JSON value. If the context dictates so, it will be
484 /// wrapped in quotes to output as a JSON string.
485 ///</summary>
486 private void WriteJSONInteger(long num)
487 {
488 context.Write();
489 String str = num.ToString();
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000490
Jens Geyerd5436f52014-10-03 19:50:38 +0200491 bool escapeNum = context.EscapeNumbers();
492 if (escapeNum)
493 trans.Write(QUOTE);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000494
Jens Geyerd5436f52014-10-03 19:50:38 +0200495 trans.Write(utf8Encoding.GetBytes(str));
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000496
Jens Geyerd5436f52014-10-03 19:50:38 +0200497 if (escapeNum)
498 trans.Write(QUOTE);
499 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000500
Jens Geyerd5436f52014-10-03 19:50:38 +0200501 ///<summary>
502 /// Write out a double as a JSON value. If it is NaN or infinity or if the
503 /// context dictates escaping, Write out as JSON string.
504 ///</summary>
505 private void WriteJSONDouble(double num)
506 {
507 context.Write();
Nobuaki Sukegawa4eb24f82015-10-25 11:28:54 +0900508 String str = num.ToString("G17", CultureInfo.InvariantCulture);
Jens Geyerd5436f52014-10-03 19:50:38 +0200509 bool special = false;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000510
Jens Geyerd5436f52014-10-03 19:50:38 +0200511 switch (str[0])
512 {
513 case 'N': // NaN
514 case 'I': // Infinity
515 special = true;
516 break;
517 case '-':
518 if (str[1] == 'I')
519 { // -Infinity
520 special = true;
521 }
522 break;
523 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000524
Jens Geyerd5436f52014-10-03 19:50:38 +0200525 bool escapeNum = special || context.EscapeNumbers();
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000526
Jens Geyerd5436f52014-10-03 19:50:38 +0200527 if (escapeNum)
528 trans.Write(QUOTE);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000529
Jens Geyerd5436f52014-10-03 19:50:38 +0200530 trans.Write(utf8Encoding.GetBytes(str));
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000531
Jens Geyerd5436f52014-10-03 19:50:38 +0200532 if (escapeNum)
533 trans.Write(QUOTE);
534 }
535 ///<summary>
536 /// Write out contents of byte array b as a JSON string with base-64 encoded
537 /// data
538 ///</summary>
539 private void WriteJSONBase64(byte[] b)
540 {
541 context.Write();
542 trans.Write(QUOTE);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000543
Jens Geyerd5436f52014-10-03 19:50:38 +0200544 int len = b.Length;
545 int off = 0;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000546
Nobuaki Sukegawaa1754372015-10-10 10:44:07 +0900547 // Ignore padding
548 int bound = len >= 2 ? len - 2 : 0;
549 for (int i = len - 1; i >= bound && b[i] == '='; --i) {
550 --len;
551 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200552 while (len >= 3)
553 {
554 // Encode 3 bytes at a time
555 TBase64Utils.encode(b, off, 3, tempBuffer, 0);
556 trans.Write(tempBuffer, 0, 4);
557 off += 3;
558 len -= 3;
559 }
560 if (len > 0)
561 {
562 // Encode remainder
563 TBase64Utils.encode(b, off, len, tempBuffer, 0);
564 trans.Write(tempBuffer, 0, len + 1);
565 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000566
Jens Geyerd5436f52014-10-03 19:50:38 +0200567 trans.Write(QUOTE);
568 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000569
Jens Geyerd5436f52014-10-03 19:50:38 +0200570 private void WriteJSONObjectStart()
571 {
572 context.Write();
573 trans.Write(LBRACE);
574 PushContext(new JSONPairContext(this));
575 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000576
Jens Geyerd5436f52014-10-03 19:50:38 +0200577 private void WriteJSONObjectEnd()
578 {
579 PopContext();
580 trans.Write(RBRACE);
581 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000582
Jens Geyerd5436f52014-10-03 19:50:38 +0200583 private void WriteJSONArrayStart()
584 {
585 context.Write();
586 trans.Write(LBRACKET);
587 PushContext(new JSONListContext(this));
588 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000589
Jens Geyerd5436f52014-10-03 19:50:38 +0200590 private void WriteJSONArrayEnd()
591 {
592 PopContext();
593 trans.Write(RBRACKET);
594 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000595
Jens Geyerd5436f52014-10-03 19:50:38 +0200596 public override void WriteMessageBegin(TMessage message)
597 {
598 WriteJSONArrayStart();
599 WriteJSONInteger(VERSION);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000600
Jens Geyerd5436f52014-10-03 19:50:38 +0200601 byte[] b = utf8Encoding.GetBytes(message.Name);
602 WriteJSONString(b);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000603
Jens Geyerd5436f52014-10-03 19:50:38 +0200604 WriteJSONInteger((long)message.Type);
605 WriteJSONInteger(message.SeqID);
606 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000607
Jens Geyerd5436f52014-10-03 19:50:38 +0200608 public override void WriteMessageEnd()
609 {
610 WriteJSONArrayEnd();
611 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000612
Jens Geyerd5436f52014-10-03 19:50:38 +0200613 public override void WriteStructBegin(TStruct str)
614 {
615 WriteJSONObjectStart();
616 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000617
Jens Geyerd5436f52014-10-03 19:50:38 +0200618 public override void WriteStructEnd()
619 {
620 WriteJSONObjectEnd();
621 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000622
Jens Geyerd5436f52014-10-03 19:50:38 +0200623 public override void WriteFieldBegin(TField field)
624 {
625 WriteJSONInteger(field.ID);
626 WriteJSONObjectStart();
627 WriteJSONString(GetTypeNameForTypeID(field.Type));
628 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000629
Jens Geyerd5436f52014-10-03 19:50:38 +0200630 public override void WriteFieldEnd()
631 {
632 WriteJSONObjectEnd();
633 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000634
Jens Geyerd5436f52014-10-03 19:50:38 +0200635 public override void WriteFieldStop() { }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000636
Jens Geyerd5436f52014-10-03 19:50:38 +0200637 public override void WriteMapBegin(TMap map)
638 {
639 WriteJSONArrayStart();
640 WriteJSONString(GetTypeNameForTypeID(map.KeyType));
641 WriteJSONString(GetTypeNameForTypeID(map.ValueType));
642 WriteJSONInteger(map.Count);
643 WriteJSONObjectStart();
644 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000645
Jens Geyerd5436f52014-10-03 19:50:38 +0200646 public override void WriteMapEnd()
647 {
648 WriteJSONObjectEnd();
649 WriteJSONArrayEnd();
650 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000651
Jens Geyerd5436f52014-10-03 19:50:38 +0200652 public override void WriteListBegin(TList list)
653 {
654 WriteJSONArrayStart();
655 WriteJSONString(GetTypeNameForTypeID(list.ElementType));
656 WriteJSONInteger(list.Count);
657 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000658
Jens Geyerd5436f52014-10-03 19:50:38 +0200659 public override void WriteListEnd()
660 {
661 WriteJSONArrayEnd();
662 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000663
Jens Geyerd5436f52014-10-03 19:50:38 +0200664 public override void WriteSetBegin(TSet set)
665 {
666 WriteJSONArrayStart();
667 WriteJSONString(GetTypeNameForTypeID(set.ElementType));
668 WriteJSONInteger(set.Count);
669 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000670
Jens Geyerd5436f52014-10-03 19:50:38 +0200671 public override void WriteSetEnd()
672 {
673 WriteJSONArrayEnd();
674 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000675
Jens Geyerd5436f52014-10-03 19:50:38 +0200676 public override void WriteBool(bool b)
677 {
678 WriteJSONInteger(b ? (long)1 : (long)0);
679 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000680
Jens Geyerd5436f52014-10-03 19:50:38 +0200681 public override void WriteByte(sbyte b)
682 {
683 WriteJSONInteger((long)b);
684 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000685
Jens Geyerd5436f52014-10-03 19:50:38 +0200686 public override void WriteI16(short i16)
687 {
688 WriteJSONInteger((long)i16);
689 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000690
Jens Geyerd5436f52014-10-03 19:50:38 +0200691 public override void WriteI32(int i32)
692 {
693 WriteJSONInteger((long)i32);
694 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000695
Jens Geyerd5436f52014-10-03 19:50:38 +0200696 public override void WriteI64(long i64)
697 {
698 WriteJSONInteger(i64);
699 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000700
Jens Geyerd5436f52014-10-03 19:50:38 +0200701 public override void WriteDouble(double dub)
702 {
703 WriteJSONDouble(dub);
704 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000705
Jens Geyerd5436f52014-10-03 19:50:38 +0200706 public override void WriteString(String str)
707 {
708 byte[] b = utf8Encoding.GetBytes(str);
709 WriteJSONString(b);
710 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000711
Jens Geyerd5436f52014-10-03 19:50:38 +0200712 public override void WriteBinary(byte[] bin)
713 {
714 WriteJSONBase64(bin);
715 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000716
Jens Geyerd5436f52014-10-03 19:50:38 +0200717 /**
718 * Reading methods.
719 */
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000720
Jens Geyerd5436f52014-10-03 19:50:38 +0200721 ///<summary>
722 /// Read in a JSON string, unescaping as appropriate.. Skip Reading from the
723 /// context if skipContext is true.
724 ///</summary>
725 private byte[] ReadJSONString(bool skipContext)
726 {
727 MemoryStream buffer = new MemoryStream();
Phongphan Phuttha11b515c2015-10-30 01:31:44 +0700728 List<char> codeunits = new List<char>();
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000729
730
Jens Geyerd5436f52014-10-03 19:50:38 +0200731 if (!skipContext)
732 {
733 context.Read();
734 }
735 ReadJSONSyntaxChar(QUOTE);
736 while (true)
737 {
738 byte ch = reader.Read();
739 if (ch == QUOTE[0])
740 {
741 break;
742 }
Jens Geyer73938622014-02-07 22:22:36 +0100743
Jens Geyerd5436f52014-10-03 19:50:38 +0200744 // escaped?
745 if (ch != ESCSEQ[0])
746 {
747 buffer.Write(new byte[] { (byte)ch }, 0, 1);
748 continue;
749 }
Jens Geyer73938622014-02-07 22:22:36 +0100750
Jens Geyerd5436f52014-10-03 19:50:38 +0200751 // distinguish between \uXXXX and \?
752 ch = reader.Read();
753 if (ch != ESCSEQ[1]) // control chars like \n
754 {
755 int off = Array.IndexOf(ESCAPE_CHARS, (char)ch);
756 if (off == -1)
757 {
758 throw new TProtocolException(TProtocolException.INVALID_DATA,
759 "Expected control char");
760 }
761 ch = ESCAPE_CHAR_VALS[off];
762 buffer.Write(new byte[] { (byte)ch }, 0, 1);
763 continue;
764 }
Jens Geyer06ad7212014-02-16 15:48:57 +0100765
766
Jens Geyerd5436f52014-10-03 19:50:38 +0200767 // it's \uXXXX
768 trans.ReadAll(tempBuffer, 0, 4);
769 var wch = (short)((HexVal((byte)tempBuffer[0]) << 12) +
770 (HexVal((byte)tempBuffer[1]) << 8) +
771 (HexVal((byte)tempBuffer[2]) << 4) +
772 HexVal(tempBuffer[3]));
Phongphan Phuttha11b515c2015-10-30 01:31:44 +0700773 if (Char.IsHighSurrogate((char)wch))
774 {
775 if (codeunits.Count > 0)
776 {
777 throw new TProtocolException(TProtocolException.INVALID_DATA,
778 "Expected low surrogate char");
779 }
780 codeunits.Add((char)wch);
781 }
782 else if (Char.IsLowSurrogate((char)wch))
783 {
784 if (codeunits.Count == 0)
785 {
786 throw new TProtocolException(TProtocolException.INVALID_DATA,
787 "Expected high surrogate char");
788 }
789 codeunits.Add((char)wch);
790 var tmp = utf8Encoding.GetBytes(codeunits.ToArray());
791 buffer.Write(tmp, 0, tmp.Length);
792 codeunits.Clear();
793 }
794 else
795 {
796 var tmp = utf8Encoding.GetBytes(new char[] { (char)wch });
797 buffer.Write(tmp, 0, tmp.Length);
798 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200799 }
Phongphan Phuttha11b515c2015-10-30 01:31:44 +0700800
801
802 if (codeunits.Count > 0)
803 {
804 throw new TProtocolException(TProtocolException.INVALID_DATA,
805 "Expected low surrogate char");
806 }
807
Jens Geyerd5436f52014-10-03 19:50:38 +0200808 return buffer.ToArray();
809 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000810
Jens Geyerd5436f52014-10-03 19:50:38 +0200811 ///<summary>
812 /// Return true if the given byte could be a valid part of a JSON number.
813 ///</summary>
814 private bool IsJSONNumeric(byte b)
815 {
816 switch (b)
817 {
818 case (byte)'+':
819 case (byte)'-':
820 case (byte)'.':
821 case (byte)'0':
822 case (byte)'1':
823 case (byte)'2':
824 case (byte)'3':
825 case (byte)'4':
826 case (byte)'5':
827 case (byte)'6':
828 case (byte)'7':
829 case (byte)'8':
830 case (byte)'9':
831 case (byte)'E':
832 case (byte)'e':
833 return true;
834 }
835 return false;
836 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000837
Jens Geyerd5436f52014-10-03 19:50:38 +0200838 ///<summary>
839 /// Read in a sequence of characters that are all valid in JSON numbers. Does
840 /// not do a complete regex check to validate that this is actually a number.
841 ////</summary>
842 private String ReadJSONNumericChars()
843 {
844 StringBuilder strbld = new StringBuilder();
845 while (true)
846 {
847 byte ch = reader.Peek();
848 if (!IsJSONNumeric(ch))
849 {
850 break;
851 }
852 strbld.Append((char)reader.Read());
853 }
854 return strbld.ToString();
855 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000856
Jens Geyerd5436f52014-10-03 19:50:38 +0200857 ///<summary>
858 /// Read in a JSON number. If the context dictates, Read in enclosing quotes.
859 ///</summary>
860 private long ReadJSONInteger()
861 {
862 context.Read();
863 if (context.EscapeNumbers())
864 {
865 ReadJSONSyntaxChar(QUOTE);
866 }
867 String str = ReadJSONNumericChars();
868 if (context.EscapeNumbers())
869 {
870 ReadJSONSyntaxChar(QUOTE);
871 }
872 try
873 {
874 return Int64.Parse(str);
875 }
876 catch (FormatException)
877 {
878 throw new TProtocolException(TProtocolException.INVALID_DATA,
879 "Bad data encounted in numeric data");
880 }
881 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000882
Jens Geyerd5436f52014-10-03 19:50:38 +0200883 ///<summary>
884 /// Read in a JSON double value. Throw if the value is not wrapped in quotes
885 /// when expected or if wrapped in quotes when not expected.
886 ///</summary>
887 private double ReadJSONDouble()
888 {
889 context.Read();
890 if (reader.Peek() == QUOTE[0])
891 {
892 byte[] arr = ReadJSONString(true);
893 double dub = Double.Parse(utf8Encoding.GetString(arr,0,arr.Length), CultureInfo.InvariantCulture);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000894
Jens Geyerd5436f52014-10-03 19:50:38 +0200895 if (!context.EscapeNumbers() && !Double.IsNaN(dub) &&
896 !Double.IsInfinity(dub))
897 {
898 // Throw exception -- we should not be in a string in this case
899 throw new TProtocolException(TProtocolException.INVALID_DATA,
900 "Numeric data unexpectedly quoted");
901 }
902 return dub;
903 }
904 else
905 {
906 if (context.EscapeNumbers())
907 {
908 // This will throw - we should have had a quote if escapeNum == true
909 ReadJSONSyntaxChar(QUOTE);
910 }
911 try
912 {
913 return Double.Parse(ReadJSONNumericChars(), CultureInfo.InvariantCulture);
914 }
915 catch (FormatException)
916 {
917 throw new TProtocolException(TProtocolException.INVALID_DATA,
918 "Bad data encounted in numeric data");
919 }
920 }
921 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000922
Jens Geyerd5436f52014-10-03 19:50:38 +0200923 //<summary>
924 /// Read in a JSON string containing base-64 encoded data and decode it.
925 ///</summary>
926 private byte[] ReadJSONBase64()
927 {
928 byte[] b = ReadJSONString(false);
929 int len = b.Length;
930 int off = 0;
931 int size = 0;
932 // reduce len to ignore fill bytes
933 while ((len > 0) && (b[len - 1] == '='))
934 {
935 --len;
936 }
937 // read & decode full byte triplets = 4 source bytes
938 while (len > 4)
939 {
940 // Decode 4 bytes at a time
941 TBase64Utils.decode(b, off, 4, b, size); // NB: decoded in place
942 off += 4;
943 len -= 4;
944 size += 3;
945 }
946 // Don't decode if we hit the end or got a single leftover byte (invalid
947 // base64 but legal for skip of regular string type)
948 if (len > 1)
949 {
950 // Decode remainder
951 TBase64Utils.decode(b, off, len, b, size); // NB: decoded in place
952 size += len - 1;
953 }
954 // Sadly we must copy the byte[] (any way around this?)
955 byte[] result = new byte[size];
956 Array.Copy(b, 0, result, 0, size);
957 return result;
958 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000959
Jens Geyerd5436f52014-10-03 19:50:38 +0200960 private void ReadJSONObjectStart()
961 {
962 context.Read();
963 ReadJSONSyntaxChar(LBRACE);
964 PushContext(new JSONPairContext(this));
965 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000966
Jens Geyerd5436f52014-10-03 19:50:38 +0200967 private void ReadJSONObjectEnd()
968 {
969 ReadJSONSyntaxChar(RBRACE);
970 PopContext();
971 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000972
Jens Geyerd5436f52014-10-03 19:50:38 +0200973 private void ReadJSONArrayStart()
974 {
975 context.Read();
976 ReadJSONSyntaxChar(LBRACKET);
977 PushContext(new JSONListContext(this));
978 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000979
Jens Geyerd5436f52014-10-03 19:50:38 +0200980 private void ReadJSONArrayEnd()
981 {
982 ReadJSONSyntaxChar(RBRACKET);
983 PopContext();
984 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000985
Jens Geyerd5436f52014-10-03 19:50:38 +0200986 public override TMessage ReadMessageBegin()
987 {
988 TMessage message = new TMessage();
989 ReadJSONArrayStart();
990 if (ReadJSONInteger() != VERSION)
991 {
992 throw new TProtocolException(TProtocolException.BAD_VERSION,
993 "Message contained bad version.");
994 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000995
Roger Meier284a9b52011-12-08 13:39:56 +0000996 var buf = ReadJSONString(false);
Jens Geyerd5436f52014-10-03 19:50:38 +0200997 message.Name = utf8Encoding.GetString(buf,0,buf.Length);
998 message.Type = (TMessageType)ReadJSONInteger();
999 message.SeqID = (int)ReadJSONInteger();
1000 return message;
1001 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001002
Jens Geyerd5436f52014-10-03 19:50:38 +02001003 public override void ReadMessageEnd()
1004 {
1005 ReadJSONArrayEnd();
1006 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001007
Jens Geyerd5436f52014-10-03 19:50:38 +02001008 public override TStruct ReadStructBegin()
1009 {
1010 ReadJSONObjectStart();
1011 return new TStruct();
1012 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001013
Jens Geyerd5436f52014-10-03 19:50:38 +02001014 public override void ReadStructEnd()
1015 {
1016 ReadJSONObjectEnd();
1017 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001018
Jens Geyerd5436f52014-10-03 19:50:38 +02001019 public override TField ReadFieldBegin()
1020 {
1021 TField field = new TField();
1022 byte ch = reader.Peek();
1023 if (ch == RBRACE[0])
1024 {
1025 field.Type = TType.Stop;
1026 }
1027 else
1028 {
1029 field.ID = (short)ReadJSONInteger();
1030 ReadJSONObjectStart();
1031 field.Type = GetTypeIDForTypeName(ReadJSONString(false));
1032 }
1033 return field;
1034 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001035
Jens Geyerd5436f52014-10-03 19:50:38 +02001036 public override void ReadFieldEnd()
1037 {
1038 ReadJSONObjectEnd();
1039 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001040
Jens Geyerd5436f52014-10-03 19:50:38 +02001041 public override TMap ReadMapBegin()
1042 {
1043 TMap map = new TMap();
1044 ReadJSONArrayStart();
1045 map.KeyType = GetTypeIDForTypeName(ReadJSONString(false));
1046 map.ValueType = GetTypeIDForTypeName(ReadJSONString(false));
1047 map.Count = (int)ReadJSONInteger();
1048 ReadJSONObjectStart();
1049 return map;
1050 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001051
Jens Geyerd5436f52014-10-03 19:50:38 +02001052 public override void ReadMapEnd()
1053 {
1054 ReadJSONObjectEnd();
1055 ReadJSONArrayEnd();
1056 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001057
Jens Geyerd5436f52014-10-03 19:50:38 +02001058 public override TList ReadListBegin()
1059 {
1060 TList list = new TList();
1061 ReadJSONArrayStart();
1062 list.ElementType = GetTypeIDForTypeName(ReadJSONString(false));
1063 list.Count = (int)ReadJSONInteger();
1064 return list;
1065 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001066
Jens Geyerd5436f52014-10-03 19:50:38 +02001067 public override void ReadListEnd()
1068 {
1069 ReadJSONArrayEnd();
1070 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001071
Jens Geyerd5436f52014-10-03 19:50:38 +02001072 public override TSet ReadSetBegin()
1073 {
1074 TSet set = new TSet();
1075 ReadJSONArrayStart();
1076 set.ElementType = GetTypeIDForTypeName(ReadJSONString(false));
1077 set.Count = (int)ReadJSONInteger();
1078 return set;
1079 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001080
Jens Geyerd5436f52014-10-03 19:50:38 +02001081 public override void ReadSetEnd()
1082 {
1083 ReadJSONArrayEnd();
1084 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001085
Jens Geyerd5436f52014-10-03 19:50:38 +02001086 public override bool ReadBool()
1087 {
1088 return (ReadJSONInteger() == 0 ? false : true);
1089 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001090
Jens Geyerd5436f52014-10-03 19:50:38 +02001091 public override sbyte ReadByte()
1092 {
1093 return (sbyte)ReadJSONInteger();
1094 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001095
Jens Geyerd5436f52014-10-03 19:50:38 +02001096 public override short ReadI16()
1097 {
1098 return (short)ReadJSONInteger();
1099 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001100
Jens Geyerd5436f52014-10-03 19:50:38 +02001101 public override int ReadI32()
1102 {
1103 return (int)ReadJSONInteger();
1104 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001105
Jens Geyerd5436f52014-10-03 19:50:38 +02001106 public override long ReadI64()
1107 {
1108 return (long)ReadJSONInteger();
1109 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001110
Jens Geyerd5436f52014-10-03 19:50:38 +02001111 public override double ReadDouble()
1112 {
1113 return ReadJSONDouble();
1114 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001115
Jens Geyerd5436f52014-10-03 19:50:38 +02001116 public override String ReadString()
1117 {
Roger Meier284a9b52011-12-08 13:39:56 +00001118 var buf = ReadJSONString(false);
Jens Geyerd5436f52014-10-03 19:50:38 +02001119 return utf8Encoding.GetString(buf,0,buf.Length);
1120 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001121
Jens Geyerd5436f52014-10-03 19:50:38 +02001122 public override byte[] ReadBinary()
1123 {
1124 return ReadJSONBase64();
1125 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001126
Jens Geyerd5436f52014-10-03 19:50:38 +02001127 }
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001128}