blob: e1d0e78832000b0c39d08e1d491e64067b2a01a2 [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{
30 /// <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 }
52
53 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
62 private byte[] ESCSEQ = new byte[] { (byte)'\\', (byte)'u', (byte)'0', (byte)'0' };
63
64 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,
69 };
70
Jens Geyer4d1b0ea2013-12-26 18:56:05 +010071 private char[] ESCAPE_CHARS = "\"\\/bfnrt".ToCharArray();
Bryan Duxburyfd32d792010-09-18 20:51:25 +000072
73 private byte[] ESCAPE_CHAR_VALS = {
Jens Geyer4d1b0ea2013-12-26 18:56:05 +010074 (byte)'"', (byte)'\\', (byte)'/', (byte)'\b', (byte)'\f', (byte)'\n', (byte)'\r', (byte)'\t',
Bryan Duxburyfd32d792010-09-18 20:51:25 +000075 };
76
77 private const int DEF_STRING_SIZE = 16;
78
79 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' };
90
91 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 }
122
123 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 }
181
182 ///<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;
190
191 public JSONBaseContext(TJSONProtocol proto)
192 {
193 this.proto = proto;
194 }
195
196 public virtual void Write() { }
197
198 public virtual void Read() { }
199
200 public virtual bool EscapeNumbers() { return false; }
201 }
202
203 ///<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 {
212
213 }
214
215 private bool first = true;
216
217 public override void Write()
218 {
219 if (first)
220 {
221 first = false;
222 }
223 else
224 {
225 proto.trans.Write(COMMA);
226 }
227 }
228
229 public override void Read()
230 {
231 if (first)
232 {
233 first = false;
234 }
235 else
236 {
237 proto.ReadJSONSyntaxChar(COMMA);
238 }
239 }
240 }
241
242 ///<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 {
253
254 }
255
256 private bool first = true;
257 private bool colon = true;
258
259 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 }
272
273 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 }
286
287 public override bool EscapeNumbers()
288 {
289 return colon;
290 }
291 }
292
293 ///<summary>
294 /// Holds up to one byte from the transport
295 ///</summary>
296 protected class LookaheadReader
297 {
298 protected TJSONProtocol proto;
299
300 public LookaheadReader(TJSONProtocol proto)
301 {
302 this.proto = proto;
303 }
304
305 private bool hasData;
306 private byte[] data = new byte[1];
307
308 ///<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 }
324
325 ///<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 }
339
340 // Default encoding
341 protected Encoding utf8Encoding = UTF8Encoding.UTF8;
342
343 // Stack of nested contexts that we may be in
344 protected Stack<JSONBaseContext> contextStack = new Stack<JSONBaseContext>();
345
346 // Current context that we are in
347 protected JSONBaseContext context;
348
349 // Reader that manages a 1-byte buffer
350 protected LookaheadReader reader;
351
352 ///<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 }
360
361 ///<summary>
362 /// Pop the last JSON context off the stack
363 ///</summary>
364 protected void PopContext()
365 {
366 context = contextStack.Pop();
367 }
368
369 ///<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 }
378
379 // Temporary buffer used by several methods
380 private byte[] tempBuffer = new byte[4];
381
382 ///<summary>
Roger Meiere0cac982010-12-16 13:15:49 +0000383 /// Read a byte that must match b[0]; otherwise an exception is thrown.
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000384 /// 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 }
396
397 ///<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 {
Jens Geyere5bfd4c2013-06-28 21:48:02 +0200409 ch += 10;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000410 return (byte)((char)ch - 'a');
411 }
412 else
413 {
414 throw new TProtocolException(TProtocolException.INVALID_DATA,
415 "Expected hex character");
416 }
417 }
418
419 ///<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 {
Jens Geyere5bfd4c2013-06-28 21:48:02 +0200431 val -= 10;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000432 return (byte)((char)val + 'a');
433 }
434 }
435
436 ///<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 }
481
482 ///<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();
490
491 bool escapeNum = context.EscapeNumbers();
492 if (escapeNum)
493 trans.Write(QUOTE);
494
495 trans.Write(utf8Encoding.GetBytes(str));
496
497 if (escapeNum)
498 trans.Write(QUOTE);
499 }
500
501 ///<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();
Roger Meier3da317b2011-07-28 18:35:51 +0000508 String str = num.ToString(CultureInfo.InvariantCulture);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000509 bool special = false;
510
511 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 }
524
525 bool escapeNum = special || context.EscapeNumbers();
526
527 if (escapeNum)
528 trans.Write(QUOTE);
529
530 trans.Write(utf8Encoding.GetBytes(str));
531
532 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);
543
544 int len = b.Length;
545 int off = 0;
546
547 while (len >= 3)
548 {
549 // Encode 3 bytes at a time
550 TBase64Utils.encode(b, off, 3, tempBuffer, 0);
551 trans.Write(tempBuffer, 0, 4);
552 off += 3;
553 len -= 3;
554 }
555 if (len > 0)
556 {
557 // Encode remainder
558 TBase64Utils.encode(b, off, len, tempBuffer, 0);
559 trans.Write(tempBuffer, 0, len + 1);
560 }
561
562 trans.Write(QUOTE);
563 }
564
565 private void WriteJSONObjectStart()
566 {
567 context.Write();
568 trans.Write(LBRACE);
569 PushContext(new JSONPairContext(this));
570 }
571
572 private void WriteJSONObjectEnd()
573 {
574 PopContext();
575 trans.Write(RBRACE);
576 }
577
578 private void WriteJSONArrayStart()
579 {
580 context.Write();
581 trans.Write(LBRACKET);
582 PushContext(new JSONListContext(this));
583 }
584
585 private void WriteJSONArrayEnd()
586 {
587 PopContext();
588 trans.Write(RBRACKET);
589 }
590
591 public override void WriteMessageBegin(TMessage message)
592 {
593 WriteJSONArrayStart();
594 WriteJSONInteger(VERSION);
595
596 byte[] b = utf8Encoding.GetBytes(message.Name);
597 WriteJSONString(b);
598
599 WriteJSONInteger((long)message.Type);
600 WriteJSONInteger(message.SeqID);
601 }
602
603 public override void WriteMessageEnd()
604 {
605 WriteJSONArrayEnd();
606 }
607
608 public override void WriteStructBegin(TStruct str)
609 {
610 WriteJSONObjectStart();
611 }
612
613 public override void WriteStructEnd()
614 {
615 WriteJSONObjectEnd();
616 }
617
618 public override void WriteFieldBegin(TField field)
619 {
620 WriteJSONInteger(field.ID);
621 WriteJSONObjectStart();
622 WriteJSONString(GetTypeNameForTypeID(field.Type));
623 }
624
625 public override void WriteFieldEnd()
626 {
627 WriteJSONObjectEnd();
628 }
629
630 public override void WriteFieldStop() { }
631
632 public override void WriteMapBegin(TMap map)
633 {
634 WriteJSONArrayStart();
635 WriteJSONString(GetTypeNameForTypeID(map.KeyType));
636 WriteJSONString(GetTypeNameForTypeID(map.ValueType));
637 WriteJSONInteger(map.Count);
638 WriteJSONObjectStart();
639 }
640
641 public override void WriteMapEnd()
642 {
643 WriteJSONObjectEnd();
644 WriteJSONArrayEnd();
645 }
646
647 public override void WriteListBegin(TList list)
648 {
649 WriteJSONArrayStart();
650 WriteJSONString(GetTypeNameForTypeID(list.ElementType));
651 WriteJSONInteger(list.Count);
652 }
653
654 public override void WriteListEnd()
655 {
656 WriteJSONArrayEnd();
657 }
658
659 public override void WriteSetBegin(TSet set)
660 {
661 WriteJSONArrayStart();
662 WriteJSONString(GetTypeNameForTypeID(set.ElementType));
663 WriteJSONInteger(set.Count);
664 }
665
666 public override void WriteSetEnd()
667 {
668 WriteJSONArrayEnd();
669 }
670
671 public override void WriteBool(bool b)
672 {
673 WriteJSONInteger(b ? (long)1 : (long)0);
674 }
675
Jens Geyerf509df92013-04-25 20:38:55 +0200676 public override void WriteByte(sbyte b)
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000677 {
678 WriteJSONInteger((long)b);
679 }
680
681 public override void WriteI16(short i16)
682 {
683 WriteJSONInteger((long)i16);
684 }
685
686 public override void WriteI32(int i32)
687 {
688 WriteJSONInteger((long)i32);
689 }
690
691 public override void WriteI64(long i64)
692 {
693 WriteJSONInteger(i64);
694 }
695
696 public override void WriteDouble(double dub)
697 {
698 WriteJSONDouble(dub);
699 }
700
701 public override void WriteString(String str)
702 {
703 byte[] b = utf8Encoding.GetBytes(str);
704 WriteJSONString(b);
705 }
706
707 public override void WriteBinary(byte[] bin)
708 {
709 WriteJSONBase64(bin);
710 }
711
712 /**
713 * Reading methods.
714 */
715
716 ///<summary>
717 /// Read in a JSON string, unescaping as appropriate.. Skip Reading from the
718 /// context if skipContext is true.
719 ///</summary>
720 private byte[] ReadJSONString(bool skipContext)
721 {
722 MemoryStream buffer = new MemoryStream();
723
724
725 if (!skipContext)
726 {
727 context.Read();
728 }
729 ReadJSONSyntaxChar(QUOTE);
730 while (true)
731 {
732 byte ch = reader.Read();
733 if (ch == QUOTE[0])
734 {
735 break;
736 }
Jens Geyer73938622014-02-07 22:22:36 +0100737
738 // escaped?
739 if (ch != ESCSEQ[0])
Jens Geyer06ad7212014-02-16 15:48:57 +0100740 {
Jens Geyer73938622014-02-07 22:22:36 +0100741 buffer.Write(new byte[] { (byte)ch }, 0, 1);
742 continue;
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000743 }
Jens Geyer73938622014-02-07 22:22:36 +0100744
745 // distinguish between \uXXXX and \?
746 ch = reader.Read();
747 if (ch != ESCSEQ[1]) // control chars like \n
748 {
749 int off = Array.IndexOf(ESCAPE_CHARS, (char)ch);
750 if (off == -1)
751 {
752 throw new TProtocolException(TProtocolException.INVALID_DATA,
753 "Expected control char");
754 }
Jens Geyer06ad7212014-02-16 15:48:57 +0100755 ch = ESCAPE_CHAR_VALS[off];
756 buffer.Write(new byte[] { (byte)ch }, 0, 1);
Jens Geyer73938622014-02-07 22:22:36 +0100757 continue;
Jens Geyer06ad7212014-02-16 15:48:57 +0100758 }
759
760
761 // it's \uXXXX
762 trans.ReadAll(tempBuffer, 0, 4);
763 var wch = (short)((HexVal((byte)tempBuffer[0]) << 12) +
764 (HexVal((byte)tempBuffer[1]) << 8) +
765 (HexVal((byte)tempBuffer[2]) << 4) +
766 HexVal(tempBuffer[3]));
767 var tmp = utf8Encoding.GetBytes(new char[] { (char)wch });
768 buffer.Write(tmp, 0, tmp.Length);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000769 }
770 return buffer.ToArray();
771 }
772
773 ///<summary>
774 /// Return true if the given byte could be a valid part of a JSON number.
775 ///</summary>
776 private bool IsJSONNumeric(byte b)
777 {
778 switch (b)
779 {
780 case (byte)'+':
781 case (byte)'-':
782 case (byte)'.':
783 case (byte)'0':
784 case (byte)'1':
785 case (byte)'2':
786 case (byte)'3':
787 case (byte)'4':
788 case (byte)'5':
789 case (byte)'6':
790 case (byte)'7':
791 case (byte)'8':
792 case (byte)'9':
793 case (byte)'E':
794 case (byte)'e':
795 return true;
796 }
797 return false;
798 }
799
800 ///<summary>
801 /// Read in a sequence of characters that are all valid in JSON numbers. Does
802 /// not do a complete regex check to validate that this is actually a number.
803 ////</summary>
804 private String ReadJSONNumericChars()
805 {
806 StringBuilder strbld = new StringBuilder();
807 while (true)
808 {
809 byte ch = reader.Peek();
810 if (!IsJSONNumeric(ch))
811 {
812 break;
813 }
814 strbld.Append((char)reader.Read());
815 }
816 return strbld.ToString();
817 }
818
819 ///<summary>
820 /// Read in a JSON number. If the context dictates, Read in enclosing quotes.
821 ///</summary>
822 private long ReadJSONInteger()
823 {
824 context.Read();
825 if (context.EscapeNumbers())
826 {
827 ReadJSONSyntaxChar(QUOTE);
828 }
829 String str = ReadJSONNumericChars();
830 if (context.EscapeNumbers())
831 {
832 ReadJSONSyntaxChar(QUOTE);
833 }
834 try
835 {
836 return Int64.Parse(str);
837 }
838 catch (FormatException)
839 {
840 throw new TProtocolException(TProtocolException.INVALID_DATA,
841 "Bad data encounted in numeric data");
842 }
843 }
844
845 ///<summary>
846 /// Read in a JSON double value. Throw if the value is not wrapped in quotes
847 /// when expected or if wrapped in quotes when not expected.
848 ///</summary>
849 private double ReadJSONDouble()
850 {
851 context.Read();
852 if (reader.Peek() == QUOTE[0])
853 {
854 byte[] arr = ReadJSONString(true);
Roger Meier284a9b52011-12-08 13:39:56 +0000855 double dub = Double.Parse(utf8Encoding.GetString(arr,0,arr.Length), CultureInfo.InvariantCulture);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000856
857 if (!context.EscapeNumbers() && !Double.IsNaN(dub) &&
858 !Double.IsInfinity(dub))
859 {
860 // Throw exception -- we should not be in a string in this case
861 throw new TProtocolException(TProtocolException.INVALID_DATA,
862 "Numeric data unexpectedly quoted");
863 }
864 return dub;
865 }
866 else
867 {
868 if (context.EscapeNumbers())
869 {
870 // This will throw - we should have had a quote if escapeNum == true
871 ReadJSONSyntaxChar(QUOTE);
872 }
873 try
874 {
Jens Geyerd73aa072013-09-18 23:30:42 +0200875 return Double.Parse(ReadJSONNumericChars(), CultureInfo.InvariantCulture);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000876 }
877 catch (FormatException)
878 {
879 throw new TProtocolException(TProtocolException.INVALID_DATA,
880 "Bad data encounted in numeric data");
881 }
882 }
883 }
884
885 //<summary>
886 /// Read in a JSON string containing base-64 encoded data and decode it.
887 ///</summary>
888 private byte[] ReadJSONBase64()
889 {
890 byte[] b = ReadJSONString(false);
891 int len = b.Length;
892 int off = 0;
893 int size = 0;
Jens Geyer06ad7212014-02-16 15:48:57 +0100894 // reduce len to ignore fill bytes
895 while ((len > 0) && (b[len - 1] == '='))
896 {
897 --len;
898 }
899 // read & decode full byte triplets = 4 source bytes
900 while (len > 4)
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000901 {
902 // Decode 4 bytes at a time
903 TBase64Utils.decode(b, off, 4, b, size); // NB: decoded in place
904 off += 4;
905 len -= 4;
906 size += 3;
907 }
908 // Don't decode if we hit the end or got a single leftover byte (invalid
909 // base64 but legal for skip of regular string type)
910 if (len > 1)
911 {
912 // Decode remainder
913 TBase64Utils.decode(b, off, len, b, size); // NB: decoded in place
914 size += len - 1;
915 }
916 // Sadly we must copy the byte[] (any way around this?)
917 byte[] result = new byte[size];
918 Array.Copy(b, 0, result, 0, size);
919 return result;
920 }
921
922 private void ReadJSONObjectStart()
923 {
924 context.Read();
925 ReadJSONSyntaxChar(LBRACE);
926 PushContext(new JSONPairContext(this));
927 }
928
929 private void ReadJSONObjectEnd()
930 {
931 ReadJSONSyntaxChar(RBRACE);
932 PopContext();
933 }
934
935 private void ReadJSONArrayStart()
936 {
937 context.Read();
938 ReadJSONSyntaxChar(LBRACKET);
939 PushContext(new JSONListContext(this));
940 }
941
942 private void ReadJSONArrayEnd()
943 {
944 ReadJSONSyntaxChar(RBRACKET);
945 PopContext();
946 }
947
948 public override TMessage ReadMessageBegin()
949 {
950 TMessage message = new TMessage();
951 ReadJSONArrayStart();
952 if (ReadJSONInteger() != VERSION)
953 {
954 throw new TProtocolException(TProtocolException.BAD_VERSION,
955 "Message contained bad version.");
956 }
957
Roger Meier284a9b52011-12-08 13:39:56 +0000958 var buf = ReadJSONString(false);
959 message.Name = utf8Encoding.GetString(buf,0,buf.Length);
Bryan Duxburyfd32d792010-09-18 20:51:25 +0000960 message.Type = (TMessageType)ReadJSONInteger();
961 message.SeqID = (int)ReadJSONInteger();
962 return message;
963 }
964
965 public override void ReadMessageEnd()
966 {
967 ReadJSONArrayEnd();
968 }
969
970 public override TStruct ReadStructBegin()
971 {
972 ReadJSONObjectStart();
973 return new TStruct();
974 }
975
976 public override void ReadStructEnd()
977 {
978 ReadJSONObjectEnd();
979 }
980
981 public override TField ReadFieldBegin()
982 {
983 TField field = new TField();
984 byte ch = reader.Peek();
985 if (ch == RBRACE[0])
986 {
987 field.Type = TType.Stop;
988 }
989 else
990 {
991 field.ID = (short)ReadJSONInteger();
992 ReadJSONObjectStart();
993 field.Type = GetTypeIDForTypeName(ReadJSONString(false));
994 }
995 return field;
996 }
997
998 public override void ReadFieldEnd()
999 {
1000 ReadJSONObjectEnd();
1001 }
1002
1003 public override TMap ReadMapBegin()
1004 {
1005 TMap map = new TMap();
1006 ReadJSONArrayStart();
1007 map.KeyType = GetTypeIDForTypeName(ReadJSONString(false));
1008 map.ValueType = GetTypeIDForTypeName(ReadJSONString(false));
1009 map.Count = (int)ReadJSONInteger();
1010 ReadJSONObjectStart();
1011 return map;
1012 }
1013
1014 public override void ReadMapEnd()
1015 {
1016 ReadJSONObjectEnd();
1017 ReadJSONArrayEnd();
1018 }
1019
1020 public override TList ReadListBegin()
1021 {
1022 TList list = new TList();
1023 ReadJSONArrayStart();
1024 list.ElementType = GetTypeIDForTypeName(ReadJSONString(false));
1025 list.Count = (int)ReadJSONInteger();
1026 return list;
1027 }
1028
1029 public override void ReadListEnd()
1030 {
1031 ReadJSONArrayEnd();
1032 }
1033
1034 public override TSet ReadSetBegin()
1035 {
1036 TSet set = new TSet();
1037 ReadJSONArrayStart();
1038 set.ElementType = GetTypeIDForTypeName(ReadJSONString(false));
1039 set.Count = (int)ReadJSONInteger();
1040 return set;
1041 }
1042
1043 public override void ReadSetEnd()
1044 {
1045 ReadJSONArrayEnd();
1046 }
1047
1048 public override bool ReadBool()
1049 {
1050 return (ReadJSONInteger() == 0 ? false : true);
1051 }
1052
Jens Geyerf509df92013-04-25 20:38:55 +02001053 public override sbyte ReadByte()
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001054 {
Jens Geyerf509df92013-04-25 20:38:55 +02001055 return (sbyte)ReadJSONInteger();
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001056 }
1057
1058 public override short ReadI16()
1059 {
1060 return (short)ReadJSONInteger();
1061 }
1062
1063 public override int ReadI32()
1064 {
1065 return (int)ReadJSONInteger();
1066 }
1067
1068 public override long ReadI64()
1069 {
1070 return (long)ReadJSONInteger();
1071 }
1072
1073 public override double ReadDouble()
1074 {
1075 return ReadJSONDouble();
1076 }
1077
1078 public override String ReadString()
1079 {
Roger Meier284a9b52011-12-08 13:39:56 +00001080 var buf = ReadJSONString(false);
1081 return utf8Encoding.GetString(buf,0,buf.Length);
Bryan Duxburyfd32d792010-09-18 20:51:25 +00001082 }
1083
1084 public override byte[] ReadBinary()
1085 {
1086 return ReadJSONBase64();
1087 }
1088
1089 }
1090}