github.com/artyom/thrift@v0.0.0-20130902103359-388840a05deb/simple_json_protocol.go (about)

     1  /*
     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  
    20  package thrift
    21  
    22  import (
    23  	"bufio"
    24  	"bytes"
    25  	"encoding/base64"
    26  	"encoding/json"
    27  	"fmt"
    28  	"io"
    29  	"math"
    30  	"strconv"
    31  )
    32  
    33  type _ParseContext int
    34  
    35  const (
    36  	_CONTEXT_IN_TOPLEVEL          _ParseContext = 1
    37  	_CONTEXT_IN_LIST_FIRST        _ParseContext = 2
    38  	_CONTEXT_IN_LIST              _ParseContext = 3
    39  	_CONTEXT_IN_OBJECT_FIRST      _ParseContext = 4
    40  	_CONTEXT_IN_OBJECT_NEXT_KEY   _ParseContext = 5
    41  	_CONTEXT_IN_OBJECT_NEXT_VALUE _ParseContext = 6
    42  )
    43  
    44  func (p _ParseContext) String() string {
    45  	switch p {
    46  	case _CONTEXT_IN_TOPLEVEL:
    47  		return "TOPLEVEL"
    48  	case _CONTEXT_IN_LIST_FIRST:
    49  		return "LIST-FIRST"
    50  	case _CONTEXT_IN_LIST:
    51  		return "LIST"
    52  	case _CONTEXT_IN_OBJECT_FIRST:
    53  		return "OBJECT-FIRST"
    54  	case _CONTEXT_IN_OBJECT_NEXT_KEY:
    55  		return "OBJECT-NEXT-KEY"
    56  	case _CONTEXT_IN_OBJECT_NEXT_VALUE:
    57  		return "OBJECT-NEXT-VALUE"
    58  	}
    59  	return "UNKNOWN-PARSE-CONTEXT"
    60  }
    61  
    62  // JSON protocol implementation for thrift.
    63  //
    64  // This protocol produces/consumes a simple output format
    65  // suitable for parsing by scripting languages.  It should not be
    66  // confused with the full-featured TJSONProtocol.
    67  //
    68  type TSimpleJSONProtocol struct {
    69  	trans TTransport
    70  
    71  	parseContextStack []int
    72  	dumpContext []int
    73  
    74  	writer *bufio.Writer
    75  	reader *bufio.Reader
    76  }
    77  
    78  // Constructor
    79  func NewTSimpleJSONProtocol(t TTransport) *TSimpleJSONProtocol {
    80  	v := &TSimpleJSONProtocol{trans: t,
    81  		writer: bufio.NewWriter(t),
    82  		reader: bufio.NewReader(t),
    83  	}
    84  	v.parseContextStack = append(v.parseContextStack, int(_CONTEXT_IN_TOPLEVEL))
    85  	v.dumpContext = append(v.dumpContext, int(_CONTEXT_IN_TOPLEVEL))
    86  	return v
    87  }
    88  
    89  // Factory
    90  type TSimpleJSONProtocolFactory struct{}
    91  
    92  func (p *TSimpleJSONProtocolFactory) GetProtocol(trans TTransport) TProtocol {
    93  	return NewTSimpleJSONProtocol(trans)
    94  }
    95  
    96  func NewTSimpleJSONProtocolFactory() *TSimpleJSONProtocolFactory {
    97  	return &TSimpleJSONProtocolFactory{}
    98  }
    99  
   100  var (
   101  	JSON_COMMA                   []byte
   102  	JSON_COLON                   []byte
   103  	JSON_LBRACE                  []byte
   104  	JSON_RBRACE                  []byte
   105  	JSON_LBRACKET                []byte
   106  	JSON_RBRACKET                []byte
   107  	JSON_QUOTE                   byte
   108  	JSON_QUOTE_BYTES             []byte
   109  	JSON_NULL                    []byte
   110  	JSON_TRUE                    []byte
   111  	JSON_FALSE                   []byte
   112  	JSON_INFINITY                string
   113  	JSON_NEGATIVE_INFINITY       string
   114  	JSON_NAN                     string
   115  	JSON_INFINITY_BYTES          []byte
   116  	JSON_NEGATIVE_INFINITY_BYTES []byte
   117  	JSON_NAN_BYTES               []byte
   118  	json_nonbase_map_elem_bytes  []byte
   119  )
   120  
   121  func init() {
   122  	JSON_COMMA = []byte{','}
   123  	JSON_COLON = []byte{':'}
   124  	JSON_LBRACE = []byte{'{'}
   125  	JSON_RBRACE = []byte{'}'}
   126  	JSON_LBRACKET = []byte{'['}
   127  	JSON_RBRACKET = []byte{']'}
   128  	JSON_QUOTE = '"'
   129  	JSON_QUOTE_BYTES = []byte{'"'}
   130  	JSON_NULL = []byte{'n', 'u', 'l', 'l'}
   131  	JSON_TRUE = []byte{'t', 'r', 'u', 'e'}
   132  	JSON_FALSE = []byte{'f', 'a', 'l', 's', 'e'}
   133  	JSON_INFINITY = "Infinity"
   134  	JSON_NEGATIVE_INFINITY = "-Infinity"
   135  	JSON_NAN = "NaN"
   136  	JSON_INFINITY_BYTES = []byte{'I', 'n', 'f', 'i', 'n', 'i', 't', 'y'}
   137  	JSON_NEGATIVE_INFINITY_BYTES = []byte{'-', 'I', 'n', 'f', 'i', 'n', 'i', 't', 'y'}
   138  	JSON_NAN_BYTES = []byte{'N', 'a', 'N'}
   139  	json_nonbase_map_elem_bytes = []byte{']', ',', '['}
   140  }
   141  
   142  func jsonQuote(s string) string {
   143  	b, _ := json.Marshal(s)
   144  	s1 := string(b)
   145  	return s1
   146  }
   147  
   148  func jsonUnquote(s string) (string, bool) {
   149  	s1 := new(string)
   150  	err := json.Unmarshal([]byte(s), s1)
   151  	return *s1, err == nil
   152  }
   153  
   154  func mismatch(expected, actual string) error {
   155  	return fmt.Errorf("Expected '%s' but found '%s' while parsing JSON.", expected, actual)
   156  }
   157  
   158  func (p *TSimpleJSONProtocol) WriteMessageBegin(name string, typeId TMessageType, seqId int32) error {
   159  	if e := p.OutputListBegin(); e != nil {
   160  		return e
   161  	}
   162  	if e := p.WriteString(name); e != nil {
   163  		return e
   164  	}
   165  	if e := p.WriteByte(byte(typeId)); e != nil {
   166  		return e
   167  	}
   168  	if e := p.WriteI32(seqId); e != nil {
   169  		return e
   170  	}
   171  	return nil
   172  }
   173  
   174  func (p *TSimpleJSONProtocol) WriteMessageEnd() error {
   175  	return p.OutputListEnd()
   176  }
   177  
   178  func (p *TSimpleJSONProtocol) WriteStructBegin(name string) error {
   179  	if e := p.OutputObjectBegin(); e != nil {
   180  		return e
   181  	}
   182  	return nil
   183  }
   184  
   185  func (p *TSimpleJSONProtocol) WriteStructEnd() error {
   186  	return p.OutputObjectEnd()
   187  }
   188  
   189  func (p *TSimpleJSONProtocol) WriteFieldBegin(name string, typeId TType, id int16) error {
   190  	if e := p.WriteString(name); e != nil {
   191  		return e
   192  	}
   193  	return nil
   194  }
   195  
   196  func (p *TSimpleJSONProtocol) WriteFieldEnd() error {
   197  	//return p.OutputListEnd()
   198  	return nil
   199  }
   200  
   201  func (p *TSimpleJSONProtocol) WriteFieldStop() error { return nil }
   202  
   203  func (p *TSimpleJSONProtocol) WriteMapBegin(keyType TType, valueType TType, size int) error {
   204  	if e := p.OutputListBegin(); e != nil {
   205  		return e
   206  	}
   207  	if e := p.WriteByte(byte(keyType)); e != nil {
   208  		return e
   209  	}
   210  	if e := p.WriteByte(byte(valueType)); e != nil {
   211  		return e
   212  	}
   213  	return p.WriteI32(int32(size))
   214  }
   215  
   216  func (p *TSimpleJSONProtocol) WriteMapEnd() error {
   217  	return p.OutputListEnd()
   218  }
   219  
   220  func (p *TSimpleJSONProtocol) WriteListBegin(elemType TType, size int) error {
   221  	return p.OutputElemListBegin(elemType, size)
   222  }
   223  
   224  func (p *TSimpleJSONProtocol) WriteListEnd() error {
   225  	return p.OutputListEnd()
   226  }
   227  
   228  func (p *TSimpleJSONProtocol) WriteSetBegin(elemType TType, size int) error {
   229  	return p.OutputElemListBegin(elemType, size)
   230  }
   231  
   232  func (p *TSimpleJSONProtocol) WriteSetEnd() error {
   233  	return p.OutputListEnd()
   234  }
   235  
   236  func (p *TSimpleJSONProtocol) WriteBool(b bool) error {
   237  	return p.OutputBool(b)
   238  }
   239  
   240  func (p *TSimpleJSONProtocol) WriteByte(b byte) error {
   241  	return p.WriteI32(int32(b))
   242  }
   243  
   244  func (p *TSimpleJSONProtocol) WriteI16(v int16) error {
   245  	return p.WriteI32(int32(v))
   246  }
   247  
   248  func (p *TSimpleJSONProtocol) WriteI32(v int32) error {
   249  	return p.OutputI64(int64(v))
   250  }
   251  
   252  func (p *TSimpleJSONProtocol) WriteI64(v int64) error {
   253  	return p.OutputI64(int64(v))
   254  }
   255  
   256  func (p *TSimpleJSONProtocol) WriteDouble(v float64) error {
   257  	return p.OutputF64(v)
   258  }
   259  
   260  func (p *TSimpleJSONProtocol) WriteString(v string) error {
   261  	return p.OutputString(v)
   262  }
   263  
   264  func (p *TSimpleJSONProtocol) WriteBinary(v []byte) error {
   265  	// JSON library only takes in a string,
   266  	// not an arbitrary byte array, to ensure bytes are transmitted
   267  	// efficiently we must convert this into a valid JSON string
   268  	// therefore we use base64 encoding to avoid excessive escaping/quoting
   269  	if e := p.OutputPreValue(); e != nil {
   270  		return e
   271  	}
   272  	if _, e := p.writer.Write(JSON_QUOTE_BYTES); e != nil {
   273  		return NewTProtocolException(e)
   274  	}
   275  	writer := base64.NewEncoder(base64.StdEncoding, p.writer)
   276  	if _, e := writer.Write(v); e != nil {
   277  		return NewTProtocolException(e)
   278  	}
   279  	if e := writer.Close(); e != nil {
   280  		return NewTProtocolException(e)
   281  	}
   282  	if _, e := p.writer.Write(JSON_QUOTE_BYTES); e != nil {
   283  		return NewTProtocolException(e)
   284  	}
   285  	return p.OutputPostValue()
   286  }
   287  
   288  // Reading methods.
   289  
   290  func (p *TSimpleJSONProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqId int32, err error) {
   291  	if isNull, err := p.ParseListBegin(); isNull || err != nil {
   292  		return name, typeId, seqId, err
   293  	}
   294  	if name, err = p.ReadString(); err != nil {
   295  		return name, typeId, seqId, err
   296  	}
   297  	bTypeId, err := p.ReadByte()
   298  	typeId = TMessageType(bTypeId)
   299  	if err != nil {
   300  		return name, typeId, seqId, err
   301  	}
   302  	if seqId, err = p.ReadI32(); err != nil {
   303  		return name, typeId, seqId, err
   304  	}
   305  	return name, typeId, seqId, nil
   306  }
   307  
   308  func (p *TSimpleJSONProtocol) ReadMessageEnd() error {
   309  	return p.ParseListEnd()
   310  }
   311  
   312  func (p *TSimpleJSONProtocol) ReadStructBegin() (name string, err error) {
   313  	_, err = p.ParseObjectStart()
   314  	return "", err
   315  }
   316  
   317  func (p *TSimpleJSONProtocol) ReadStructEnd() error {
   318  	return p.ParseObjectEnd()
   319  }
   320  
   321  func (p *TSimpleJSONProtocol) ReadFieldBegin() (string, TType, int16, error) {
   322  	if err := p.ParsePreValue(); err != nil {
   323  		return "", STOP, 0, err
   324  	}
   325  	if p.reader.Buffered() < 1 {
   326  		return "", STOP, 0, nil
   327  	}
   328  	b, _ := p.reader.Peek(1)
   329  	if len(b) > 0 {
   330  		switch b[0] {
   331  		case JSON_RBRACE[0]:
   332  			return "", STOP, 0, nil
   333  		case JSON_QUOTE:
   334  			p.reader.ReadByte()
   335  			name, err := p.ParseStringBody()
   336  			if err != nil {
   337  				return name, STOP, 0, err
   338  			}
   339  			return name, STOP, -1, p.ParsePostValue()
   340  			/*
   341  			   if err = p.ParsePostValue(); err != nil {
   342  			     return name, STOP, 0, err
   343  			   }
   344  			   if isNull, err := p.ParseListBegin(); isNull || err != nil {
   345  			     return name, STOP, 0, err
   346  			   }
   347  			   bType, err := p.ReadByte()
   348  			   thetype := TType(bType)
   349  			   if err != nil {
   350  			     return name, thetype, 0, err
   351  			   }
   352  			   id, err := p.ReadI16()
   353  			   return name, thetype, id, err
   354  			*/
   355  		}
   356  		e := fmt.Errorf("Expected \"}\" or '\"', but found: '%s'", string(b))
   357  		return "", STOP, 0, NewTProtocolExceptionWithType(INVALID_DATA, e)
   358  	}
   359  	return "", STOP, 0, NewTProtocolException(io.EOF)
   360  }
   361  
   362  func (p *TSimpleJSONProtocol) ReadFieldEnd() error {
   363  	return nil
   364  	//return p.ParseListEnd()
   365  }
   366  
   367  func (p *TSimpleJSONProtocol) ReadMapBegin() (keyType TType, valueType TType, size int, e error) {
   368  	if isNull, e := p.ParseListBegin(); isNull || e != nil {
   369  		return VOID, VOID, 0, e
   370  	}
   371  
   372  	// read keyType
   373  	bKeyType, e := p.ReadByte()
   374  	keyType = TType(bKeyType)
   375  	if e != nil {
   376  		return keyType, valueType, size, e
   377  	}
   378  
   379  	// read valueType
   380  	bValueType, e := p.ReadByte()
   381  	valueType = TType(bValueType)
   382  	if e != nil {
   383  		return keyType, valueType, size, e
   384  	}
   385  
   386  	// read size
   387  	iSize, err := p.ReadI64()
   388  	size = int(iSize)
   389  	return keyType, valueType, size, err
   390  }
   391  
   392  func (p *TSimpleJSONProtocol) ReadMapEnd() error {
   393  	return p.ParseListEnd()
   394  }
   395  
   396  func (p *TSimpleJSONProtocol) ReadListBegin() (elemType TType, size int, e error) {
   397  	return p.ParseElemListBegin()
   398  }
   399  
   400  func (p *TSimpleJSONProtocol) ReadListEnd() error {
   401  	return p.ParseListEnd()
   402  }
   403  
   404  func (p *TSimpleJSONProtocol) ReadSetBegin() (elemType TType, size int, e error) {
   405  	return p.ParseElemListBegin()
   406  }
   407  
   408  func (p *TSimpleJSONProtocol) ReadSetEnd() error {
   409  	return p.ParseListEnd()
   410  }
   411  
   412  func (p *TSimpleJSONProtocol) ReadBool() (bool, error) {
   413  	var value bool
   414  	if err := p.ParsePreValue(); err != nil {
   415  		return value, err
   416  	}
   417  	b, _ := p.reader.Peek(len(JSON_TRUE))
   418  	if len(b) > 0 {
   419  		switch b[0] {
   420  		case JSON_TRUE[0]:
   421  			if string(b) == string(JSON_TRUE) {
   422  				p.reader.Read(b[0:len(JSON_TRUE)])
   423  				value = true
   424  			} else {
   425  				e := fmt.Errorf("Expected \"true\" but found: %s", string(b))
   426  				return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
   427  			}
   428  			break
   429  		case JSON_FALSE[0]:
   430  			if string(b) == string(JSON_FALSE[:len(b)]) {
   431  				p.reader.Read(b[0:len(JSON_FALSE)])
   432  				value = false
   433  			} else {
   434  				e := fmt.Errorf("Expected \"false\" but found: %s", string(b))
   435  				return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
   436  			}
   437  			break
   438  		case JSON_NULL[0]:
   439  			if string(b) == string(JSON_NULL) {
   440  				p.reader.Read(b[0:len(JSON_NULL)])
   441  				value = false
   442  			} else {
   443  				e := fmt.Errorf("Expected \"null\" but found: %s", string(b))
   444  				return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
   445  			}
   446  		default:
   447  			e := fmt.Errorf("Expected \"true\", \"false\", or \"null\" but found: %s", string(b))
   448  			return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
   449  		}
   450  	}
   451  	return value, p.ParsePostValue()
   452  }
   453  
   454  func (p *TSimpleJSONProtocol) ReadByte() (byte, error) {
   455  	v, err := p.ReadI64()
   456  	return byte(v), err
   457  }
   458  
   459  func (p *TSimpleJSONProtocol) ReadI16() (int16, error) {
   460  	v, err := p.ReadI64()
   461  	return int16(v), err
   462  }
   463  
   464  func (p *TSimpleJSONProtocol) ReadI32() (int32, error) {
   465  	v, err := p.ReadI64()
   466  	return int32(v), err
   467  }
   468  
   469  func (p *TSimpleJSONProtocol) ReadI64() (int64, error) {
   470  	v, _, err := p.ParseI64()
   471  	return v, err
   472  }
   473  
   474  func (p *TSimpleJSONProtocol) ReadDouble() (float64, error) {
   475  	v, _, err := p.ParseF64()
   476  	return v, err
   477  }
   478  
   479  func (p *TSimpleJSONProtocol) ReadString() (string, error) {
   480  	var v string
   481  	if err := p.ParsePreValue(); err != nil {
   482  		return v, err
   483  	}
   484  	var b []byte
   485  	if p.reader.Buffered() >= len(JSON_NULL) {
   486  		b, _ = p.reader.Peek(len(JSON_NULL))
   487  	} else {
   488  		b, _ = p.reader.Peek(1)
   489  	}
   490  	if len(b) > 0 && b[0] == JSON_QUOTE {
   491  		p.reader.ReadByte()
   492  		value, err := p.ParseStringBody()
   493  		v = value
   494  		if err != nil {
   495  			return v, err
   496  		}
   497  	} else if len(b) >= len(JSON_NULL) && string(b[0:len(JSON_NULL)]) == string(JSON_NULL) {
   498  		_, err := p.reader.Read(b[0:len(JSON_NULL)])
   499  		if err != nil {
   500  			return v, NewTProtocolException(err)
   501  		}
   502  	} else {
   503  		e := fmt.Errorf("Expected a JSON string, found %s", string(b))
   504  		return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
   505  	}
   506  	return v, p.ParsePostValue()
   507  }
   508  
   509  func (p *TSimpleJSONProtocol) ReadBinary() ([]byte, error) {
   510  	var v []byte
   511  	if err := p.ParsePreValue(); err != nil {
   512  		return nil, err
   513  	}
   514  	b, _ := p.reader.Peek(len(JSON_NULL))
   515  	if len(b) > 0 && b[0] == JSON_QUOTE {
   516  		p.reader.ReadByte()
   517  		value, err := p.ParseBase64EncodedBody()
   518  		v = value
   519  		if err != nil {
   520  			return v, err
   521  		}
   522  	} else if len(b) >= len(JSON_NULL) && string(b[0:len(JSON_NULL)]) == string(JSON_NULL) {
   523  		_, err := p.reader.Read(b[0:len(JSON_NULL)])
   524  		if err != nil {
   525  			return v, NewTProtocolException(err)
   526  		}
   527  	} else {
   528  		e := fmt.Errorf("Expected a JSON string, found %s", string(b))
   529  		return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
   530  	}
   531  	return v, p.ParsePostValue()
   532  }
   533  
   534  func (p *TSimpleJSONProtocol) Flush() (err error) {
   535  	return NewTProtocolException(p.writer.Flush())
   536  }
   537  
   538  func (p *TSimpleJSONProtocol) Skip(fieldType TType) (err error) {
   539  	return SkipDefaultDepth(p, fieldType)
   540  }
   541  
   542  func (p *TSimpleJSONProtocol) Transport() TTransport {
   543  	return p.trans
   544  }
   545  
   546  func (p *TSimpleJSONProtocol) OutputPreValue() error {
   547  	cxt := _ParseContext(p.dumpContext[len(p.dumpContext)-1])
   548  	switch cxt {
   549  	case _CONTEXT_IN_LIST, _CONTEXT_IN_OBJECT_NEXT_KEY:
   550  		if _, e := p.writer.Write(JSON_COMMA); e != nil {
   551  			return NewTProtocolException(e)
   552  		}
   553  		break
   554  	case _CONTEXT_IN_OBJECT_NEXT_VALUE:
   555  		if _, e := p.writer.Write(JSON_COLON); e != nil {
   556  			return NewTProtocolException(e)
   557  		}
   558  		break
   559  	}
   560  	return nil
   561  }
   562  
   563  func (p *TSimpleJSONProtocol) OutputPostValue() error {
   564  	cxt := _ParseContext(p.dumpContext[len(p.dumpContext)-1])
   565  	switch cxt {
   566  	case _CONTEXT_IN_LIST_FIRST:
   567  		p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
   568  		p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_LIST))
   569  		break
   570  	case _CONTEXT_IN_OBJECT_FIRST:
   571  		p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
   572  		p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_OBJECT_NEXT_VALUE))
   573  		break
   574  	case _CONTEXT_IN_OBJECT_NEXT_KEY:
   575  		p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
   576  		p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_OBJECT_NEXT_VALUE))
   577  		break
   578  	case _CONTEXT_IN_OBJECT_NEXT_VALUE:
   579  		p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
   580  		p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_OBJECT_NEXT_KEY))
   581  		break
   582  	}
   583  	return nil
   584  }
   585  
   586  func (p *TSimpleJSONProtocol) OutputBool(value bool) error {
   587  	if e := p.OutputPreValue(); e != nil {
   588  		return e
   589  	}
   590  	var v string
   591  	if value {
   592  		v = string(JSON_TRUE)
   593  	} else {
   594  		v = string(JSON_FALSE)
   595  	}
   596  	switch _ParseContext(p.dumpContext[len(p.dumpContext)-1]) {
   597  	case _CONTEXT_IN_OBJECT_FIRST, _CONTEXT_IN_OBJECT_NEXT_KEY:
   598  		v = jsonQuote(v)
   599  	default:
   600  	}
   601  	if e := p.OutputStringData(v); e != nil {
   602  		return e
   603  	}
   604  	return p.OutputPostValue()
   605  }
   606  
   607  func (p *TSimpleJSONProtocol) OutputNull() error {
   608  	if e := p.OutputPreValue(); e != nil {
   609  		return e
   610  	}
   611  	if _, e := p.writer.Write(JSON_NULL); e != nil {
   612  		return NewTProtocolException(e)
   613  	}
   614  	return p.OutputPostValue()
   615  }
   616  
   617  func (p *TSimpleJSONProtocol) OutputF64(value float64) error {
   618  	if e := p.OutputPreValue(); e != nil {
   619  		return e
   620  	}
   621  	var v string
   622  	if math.IsNaN(value) {
   623  		v = string(JSON_QUOTE) + JSON_NAN + string(JSON_QUOTE)
   624  	} else if math.IsInf(value, 1) {
   625  		v = string(JSON_QUOTE) + JSON_INFINITY + string(JSON_QUOTE)
   626  	} else if math.IsInf(value, -1) {
   627  		v = string(JSON_QUOTE) + JSON_NEGATIVE_INFINITY + string(JSON_QUOTE)
   628  	} else {
   629  		v = strconv.FormatFloat(value, 'g', -1, 64)
   630  		switch _ParseContext(p.dumpContext[len(p.dumpContext)-1]) {
   631  		case _CONTEXT_IN_OBJECT_FIRST, _CONTEXT_IN_OBJECT_NEXT_KEY:
   632  			v = string(JSON_QUOTE) + v + string(JSON_QUOTE)
   633  		default:
   634  		}
   635  	}
   636  	if e := p.OutputStringData(v); e != nil {
   637  		return e
   638  	}
   639  	return p.OutputPostValue()
   640  }
   641  
   642  func (p *TSimpleJSONProtocol) OutputI64(value int64) error {
   643  	if e := p.OutputPreValue(); e != nil {
   644  		return e
   645  	}
   646  	v := strconv.FormatInt(value, 10)
   647  	switch _ParseContext(p.dumpContext[len(p.dumpContext)-1]) {
   648  	case _CONTEXT_IN_OBJECT_FIRST, _CONTEXT_IN_OBJECT_NEXT_KEY:
   649  		v = jsonQuote(v)
   650  	default:
   651  	}
   652  	if e := p.OutputStringData(v); e != nil {
   653  		return e
   654  	}
   655  	return p.OutputPostValue()
   656  }
   657  
   658  func (p *TSimpleJSONProtocol) OutputString(s string) error {
   659  	if e := p.OutputPreValue(); e != nil {
   660  		return e
   661  	}
   662  	if e := p.OutputStringData(jsonQuote(s)); e != nil {
   663  		return e
   664  	}
   665  	return p.OutputPostValue()
   666  }
   667  
   668  func (p *TSimpleJSONProtocol) OutputStringData(s string) error {
   669  	_, e := p.writer.Write([]byte(s))
   670  	return NewTProtocolException(e)
   671  }
   672  
   673  func (p *TSimpleJSONProtocol) OutputObjectBegin() error {
   674  	if e := p.OutputPreValue(); e != nil {
   675  		return e
   676  	}
   677  	if _, e := p.writer.Write(JSON_LBRACE); e != nil {
   678  		return NewTProtocolException(e)
   679  	}
   680  	p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_OBJECT_FIRST))
   681  	return nil
   682  }
   683  
   684  func (p *TSimpleJSONProtocol) OutputObjectEnd() error {
   685  	if _, e := p.writer.Write(JSON_RBRACE); e != nil {
   686  		return NewTProtocolException(e)
   687  	}
   688  	p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
   689  	if e := p.OutputPostValue(); e != nil {
   690  		return e
   691  	}
   692  	return nil
   693  }
   694  
   695  func (p *TSimpleJSONProtocol) OutputListBegin() error {
   696  	if e := p.OutputPreValue(); e != nil {
   697  		return e
   698  	}
   699  	if _, e := p.writer.Write(JSON_LBRACKET); e != nil {
   700  		return NewTProtocolException(e)
   701  	}
   702  	p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_LIST_FIRST))
   703  	return nil
   704  }
   705  
   706  func (p *TSimpleJSONProtocol) OutputListEnd() error {
   707  	if _, e := p.writer.Write(JSON_RBRACKET); e != nil {
   708  		return NewTProtocolException(e)
   709  	}
   710  	p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
   711  	if e := p.OutputPostValue(); e != nil {
   712  		return e
   713  	}
   714  	return nil
   715  }
   716  
   717  func (p *TSimpleJSONProtocol) OutputElemListBegin(elemType TType, size int) error {
   718  	if e := p.OutputListBegin(); e != nil {
   719  		return e
   720  	}
   721  	if e := p.WriteByte(byte(elemType)); e != nil {
   722  		return e
   723  	}
   724  	if e := p.WriteI64(int64(size)); e != nil {
   725  		return e
   726  	}
   727  	return nil
   728  }
   729  
   730  func (p *TSimpleJSONProtocol) ParsePreValue() error {
   731  	if e := p.readNonSignificantWhitespace(); e != nil {
   732  		return NewTProtocolException(e)
   733  	}
   734  	cxt := _ParseContext(p.parseContextStack[len(p.parseContextStack)-1])
   735  	if p.reader.Buffered() < 1 {
   736  		return nil
   737  	}
   738  	b, _ := p.reader.Peek(1)
   739  	switch cxt {
   740  	case _CONTEXT_IN_LIST:
   741  		if len(b) > 0 {
   742  			switch b[0] {
   743  			case JSON_RBRACKET[0]:
   744  				return nil
   745  			case JSON_COMMA[0]:
   746  				p.reader.ReadByte()
   747  				if e := p.readNonSignificantWhitespace(); e != nil {
   748  					return NewTProtocolException(e)
   749  				}
   750  				return nil
   751  			default:
   752  				e := fmt.Errorf("Expected \"]\" or \",\" in list context, but found \"%s\"", string(b))
   753  				return NewTProtocolExceptionWithType(INVALID_DATA, e)
   754  			}
   755  		}
   756  		break
   757  	case _CONTEXT_IN_OBJECT_NEXT_KEY:
   758  		if len(b) > 0 {
   759  			switch b[0] {
   760  			case JSON_RBRACE[0]:
   761  				return nil
   762  			case JSON_COMMA[0]:
   763  				p.reader.ReadByte()
   764  				if e := p.readNonSignificantWhitespace(); e != nil {
   765  					return NewTProtocolException(e)
   766  				}
   767  				return nil
   768  			default:
   769  				e := fmt.Errorf("Expected \"}\" or \",\" in object context, but found \"%s\"", string(b))
   770  				return NewTProtocolExceptionWithType(INVALID_DATA, e)
   771  			}
   772  		}
   773  		break
   774  	case _CONTEXT_IN_OBJECT_NEXT_VALUE:
   775  		if len(b) > 0 {
   776  			switch b[0] {
   777  			case JSON_COLON[0]:
   778  				p.reader.ReadByte()
   779  				if e := p.readNonSignificantWhitespace(); e != nil {
   780  					return NewTProtocolException(e)
   781  				}
   782  				return nil
   783  			default:
   784  				e := fmt.Errorf("Expected \":\" in object context, but found \"%s\"", string(b))
   785  				return NewTProtocolExceptionWithType(INVALID_DATA, e)
   786  			}
   787  		}
   788  		break
   789  	}
   790  	return nil
   791  }
   792  
   793  func (p *TSimpleJSONProtocol) ParsePostValue() error {
   794  	if e := p.readNonSignificantWhitespace(); e != nil {
   795  		return NewTProtocolException(e)
   796  	}
   797  	cxt := _ParseContext(p.parseContextStack[len(p.parseContextStack)-1])
   798  	switch cxt {
   799  	case _CONTEXT_IN_LIST_FIRST:
   800  		p.parseContextStack = p.parseContextStack[:len(p.parseContextStack)-1]
   801  		p.parseContextStack = append(p.parseContextStack, int(_CONTEXT_IN_LIST))
   802  		break
   803  	case _CONTEXT_IN_OBJECT_FIRST, _CONTEXT_IN_OBJECT_NEXT_KEY:
   804  		p.parseContextStack = p.parseContextStack[:len(p.parseContextStack)-1]
   805  		p.parseContextStack = append(p.parseContextStack, int(_CONTEXT_IN_OBJECT_NEXT_VALUE))
   806  		break
   807  	case _CONTEXT_IN_OBJECT_NEXT_VALUE:
   808  		p.parseContextStack = p.parseContextStack[:len(p.parseContextStack)-1]
   809  		p.parseContextStack = append(p.parseContextStack, int(_CONTEXT_IN_OBJECT_NEXT_KEY))
   810  		break
   811  	}
   812  	return nil
   813  }
   814  
   815  func (p *TSimpleJSONProtocol) readNonSignificantWhitespace() error {
   816  	for p.reader.Buffered() > 0 {
   817  		b, _ := p.reader.Peek(1)
   818  		if len(b) < 1 {
   819  			return nil
   820  		}
   821  		switch b[0] {
   822  		case ' ', '\r', '\n', '\t':
   823  			p.reader.ReadByte()
   824  			continue
   825  		default:
   826  			break
   827  		}
   828  		break
   829  	}
   830  	return nil
   831  }
   832  
   833  func (p *TSimpleJSONProtocol) ParseStringBody() (string, error) {
   834  	line, err := p.reader.ReadString(JSON_QUOTE)
   835  	if err != nil {
   836  		return "", NewTProtocolException(err)
   837  	}
   838  	l := len(line)
   839  	// count number of escapes to see if we need to keep going
   840  	i := 1
   841  	for ; i < l; i++ {
   842  		if line[l-i-1] != '\\' {
   843  			break
   844  		}
   845  	}
   846  	if i&0x01 == 1 {
   847  		v, ok := jsonUnquote(string(JSON_QUOTE) + line)
   848  		if !ok {
   849  			return "", NewTProtocolException(err)
   850  		}
   851  		return v, nil
   852  	}
   853  	s, err := p.ParseQuotedStringBody()
   854  	if err != nil {
   855  		return "", NewTProtocolException(err)
   856  	}
   857  	str := string(JSON_QUOTE) + line + s
   858  	v, ok := jsonUnquote(str)
   859  	if !ok {
   860  		e := fmt.Errorf("Unable to parse as JSON string %s", str)
   861  		return "", NewTProtocolExceptionWithType(INVALID_DATA, e)
   862  	}
   863  	return v, nil
   864  }
   865  
   866  func (p *TSimpleJSONProtocol) ParseQuotedStringBody() (string, error) {
   867  	line, err := p.reader.ReadString(JSON_QUOTE)
   868  	if err != nil {
   869  		return "", NewTProtocolException(err)
   870  	}
   871  	l := len(line)
   872  	// count number of escapes to see if we need to keep going
   873  	i := 1
   874  	for ; i < l; i++ {
   875  		if line[l-i-1] != '\\' {
   876  			break
   877  		}
   878  	}
   879  	if i&0x01 == 1 {
   880  		return line, nil
   881  	}
   882  	s, err := p.ParseQuotedStringBody()
   883  	if err != nil {
   884  		return "", NewTProtocolException(err)
   885  	}
   886  	v := line + s
   887  	return v, nil
   888  }
   889  
   890  func (p *TSimpleJSONProtocol) ParseBase64EncodedBody() ([]byte, error) {
   891  	line, err := p.reader.ReadBytes(JSON_QUOTE)
   892  	if err != nil {
   893  		return line, NewTProtocolException(err)
   894  	}
   895  	line2 := line[0 : len(line)-1]
   896  	l := len(line2)
   897  	output := make([]byte, base64.StdEncoding.DecodedLen(l))
   898  	n, err := base64.StdEncoding.Decode(output, line2)
   899  	return output[0:n], NewTProtocolException(err)
   900  }
   901  
   902  func (p *TSimpleJSONProtocol) ParseI64() (int64, bool, error) {
   903  	if err := p.ParsePreValue(); err != nil {
   904  		return 0, false, err
   905  	}
   906  	var value int64
   907  	var isnull bool
   908  	b, _ := p.reader.Peek(len(JSON_NULL))
   909  	if len(b) >= len(JSON_NULL) && string(b) == string(JSON_NULL) {
   910  		p.reader.Read(b[0:len(JSON_NULL)])
   911  		isnull = true
   912  	} else {
   913  		num, err := p.readNumeric()
   914  		isnull = (num == nil)
   915  		if !isnull {
   916  			value = num.Int64()
   917  		}
   918  		if err != nil {
   919  			return value, isnull, err
   920  		}
   921  	}
   922  	return value, isnull, p.ParsePostValue()
   923  }
   924  
   925  func (p *TSimpleJSONProtocol) ParseF64() (float64, bool, error) {
   926  	if err := p.ParsePreValue(); err != nil {
   927  		return 0, false, err
   928  	}
   929  	var value float64
   930  	var isnull bool
   931  	b, _ := p.reader.Peek(len(JSON_NULL))
   932  	if len(b) >= len(JSON_NULL) && string(b) == string(JSON_NULL) {
   933  		p.reader.Read(b[0:len(JSON_NULL)])
   934  		isnull = true
   935  	} else {
   936  		num, err := p.readNumeric()
   937  		isnull = (num == nil)
   938  		if !isnull {
   939  			value = num.Float64()
   940  		}
   941  		if err != nil {
   942  			return value, isnull, err
   943  		}
   944  	}
   945  	return value, isnull, p.ParsePostValue()
   946  }
   947  
   948  func (p *TSimpleJSONProtocol) ParseObjectStart() (bool, error) {
   949  	if err := p.ParsePreValue(); err != nil {
   950  		return false, err
   951  	}
   952  	var b []byte
   953  	if p.reader.Buffered() >= len(JSON_NULL) {
   954  		b, _ = p.reader.Peek(len(JSON_NULL))
   955  	} else if p.reader.Buffered() >= 1 {
   956  		b, _ = p.reader.Peek(1)
   957  	}
   958  	if len(b) > 0 && b[0] == JSON_LBRACE[0] {
   959  		p.reader.ReadByte()
   960  		p.parseContextStack = append(p.parseContextStack, int(_CONTEXT_IN_OBJECT_FIRST))
   961  		return false, nil
   962  	} else if len(b) >= len(JSON_NULL) && string(b[0:len(JSON_NULL)]) == string(JSON_NULL) {
   963  		return true, nil
   964  	}
   965  	e := fmt.Errorf("Expected '{' or null, but found '%s'", string(b))
   966  	return false, NewTProtocolExceptionWithType(INVALID_DATA, e)
   967  }
   968  
   969  func (p *TSimpleJSONProtocol) ParseObjectEnd() error {
   970  	if isNull, err := p.readIfNull(); isNull || err != nil {
   971  		return err
   972  	}
   973  	cxt := _ParseContext(p.parseContextStack[len(p.parseContextStack)-1])
   974  	if cxt != _CONTEXT_IN_OBJECT_FIRST && cxt != _CONTEXT_IN_OBJECT_NEXT_KEY {
   975  		e := fmt.Errorf("Expected to be in the Object Context, but not in Object Context")
   976  		return NewTProtocolExceptionWithType(INVALID_DATA, e)
   977  	}
   978  	line, err := p.reader.ReadString(JSON_RBRACE[0])
   979  	if err != nil {
   980  		return NewTProtocolException(err)
   981  	}
   982  	for _, char := range line {
   983  		switch char {
   984  		default:
   985  			e := fmt.Errorf("Expecting end of object \"}\", but found: \"%s\"", line)
   986  			return NewTProtocolExceptionWithType(INVALID_DATA, e)
   987  		case ' ', '\n', '\r', '\t', '}':
   988  			break
   989  		}
   990  	}
   991  	p.parseContextStack = p.parseContextStack[:len(p.parseContextStack)-1]
   992  	return p.ParsePostValue()
   993  }
   994  
   995  func (p *TSimpleJSONProtocol) ParseListBegin() (isNull bool, err error) {
   996  	if e := p.ParsePreValue(); e != nil {
   997  		return false, e
   998  	}
   999  	var b []byte
  1000  	if p.reader.Buffered() >= len(JSON_NULL) {
  1001  		b, err = p.reader.Peek(len(JSON_NULL))
  1002  	} else {
  1003  		b, err = p.reader.Peek(1)
  1004  	}
  1005  	if err != nil {
  1006  		return false, err
  1007  	}
  1008  	if len(b) >= 1 && b[0] == JSON_LBRACKET[0] {
  1009  		p.parseContextStack = append(p.parseContextStack, int(_CONTEXT_IN_LIST_FIRST))
  1010  		p.reader.ReadByte()
  1011  		isNull = false
  1012  	} else if len(b) >= len(JSON_NULL) && string(b) == string(JSON_NULL) {
  1013  		isNull = true
  1014  	} else {
  1015  		err = fmt.Errorf("Expected \"null\" or \"[\", received %q", b)
  1016  	}
  1017  	return isNull, NewTProtocolExceptionWithType(INVALID_DATA, err)
  1018  }
  1019  
  1020  func (p *TSimpleJSONProtocol) ParseElemListBegin() (elemType TType, size int, e error) {
  1021  	if isNull, e := p.ParseListBegin(); isNull || e != nil {
  1022  		return VOID, 0, e
  1023  	}
  1024  	bElemType, err := p.ReadByte()
  1025  	elemType = TType(bElemType)
  1026  	if err != nil {
  1027  		return elemType, size, err
  1028  	}
  1029  	nSize, err2 := p.ReadI64()
  1030  	size = int(nSize)
  1031  	return elemType, size, err2
  1032  }
  1033  
  1034  func (p *TSimpleJSONProtocol) ParseListEnd() error {
  1035  	if isNull, err := p.readIfNull(); isNull || err != nil {
  1036  		return err
  1037  	}
  1038  	if _ParseContext(p.parseContextStack[len(p.parseContextStack)-1]) != _CONTEXT_IN_LIST {
  1039  		e := fmt.Errorf("Expected to be in the List Context, but not in List Context")
  1040  		return NewTProtocolExceptionWithType(INVALID_DATA, e)
  1041  	}
  1042  	line, err := p.reader.ReadString(JSON_RBRACKET[0])
  1043  	if err != nil {
  1044  		return NewTProtocolException(err)
  1045  	}
  1046  	for _, char := range line {
  1047  		switch char {
  1048  		default:
  1049  			e := fmt.Errorf("Expecting end of list \"]\", but found: \"", line, "\"")
  1050  			return NewTProtocolExceptionWithType(INVALID_DATA, e)
  1051  		case ' ', '\n', '\r', '\t', rune(JSON_RBRACKET[0]):
  1052  			break
  1053  		}
  1054  	}
  1055  	p.parseContextStack = p.parseContextStack[:len(p.parseContextStack)-1]
  1056  	return p.ParsePostValue()
  1057  }
  1058  
  1059  func (p *TSimpleJSONProtocol) readSingleValue() (interface{}, TType, error) {
  1060  	e := p.readNonSignificantWhitespace()
  1061  	if e != nil {
  1062  		return nil, VOID, NewTProtocolException(e)
  1063  	}
  1064  	b, e := p.reader.Peek(10)
  1065  	if len(b) > 0 {
  1066  		c := b[0]
  1067  		switch c {
  1068  		case JSON_NULL[0]:
  1069  			buf := make([]byte, len(JSON_NULL))
  1070  			_, e := p.reader.Read(buf)
  1071  			if e != nil {
  1072  				return nil, VOID, NewTProtocolException(e)
  1073  			}
  1074  			if string(JSON_NULL) != string(buf) {
  1075  				e = mismatch(string(JSON_NULL), string(buf))
  1076  				return nil, VOID, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1077  			}
  1078  			return nil, VOID, nil
  1079  		case JSON_QUOTE:
  1080  			p.reader.ReadByte()
  1081  			v, e := p.ParseStringBody()
  1082  			if e != nil {
  1083  				return v, UTF8, NewTProtocolException(e)
  1084  			}
  1085  			if v == JSON_INFINITY {
  1086  				return INFINITY, DOUBLE, nil
  1087  			} else if v == JSON_NEGATIVE_INFINITY {
  1088  				return NEGATIVE_INFINITY, DOUBLE, nil
  1089  			} else if v == JSON_NAN {
  1090  				return NAN, DOUBLE, nil
  1091  			}
  1092  			return v, UTF8, nil
  1093  		case JSON_TRUE[0]:
  1094  			buf := make([]byte, len(JSON_TRUE))
  1095  			_, e := p.reader.Read(buf)
  1096  			if e != nil {
  1097  				return true, BOOL, NewTProtocolException(e)
  1098  			}
  1099  			if string(JSON_TRUE) != string(buf) {
  1100  				e := mismatch(string(JSON_TRUE), string(buf))
  1101  				return true, BOOL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1102  			}
  1103  			return true, BOOL, nil
  1104  		case JSON_FALSE[0]:
  1105  			buf := make([]byte, len(JSON_FALSE))
  1106  			_, e := p.reader.Read(buf)
  1107  			if e != nil {
  1108  				return false, BOOL, NewTProtocolException(e)
  1109  			}
  1110  			if string(JSON_FALSE) != string(buf) {
  1111  				e := mismatch(string(JSON_FALSE), string(buf))
  1112  				return false, BOOL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1113  			}
  1114  			return false, BOOL, nil
  1115  		case JSON_LBRACKET[0]:
  1116  			_, e := p.reader.ReadByte()
  1117  			return make([]interface{}, 0), LIST, NewTProtocolException(e)
  1118  		case JSON_LBRACE[0]:
  1119  			_, e := p.reader.ReadByte()
  1120  			return make(map[string]interface{}), STRUCT, NewTProtocolException(e)
  1121  		case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'e', 'E', '.', '+', '-', JSON_INFINITY[0], JSON_NAN[0]:
  1122  			// assume numeric
  1123  			v, e := p.readNumeric()
  1124  			return v, DOUBLE, e
  1125  		default:
  1126  			e := fmt.Errorf("Expected element in list but found '%s' while parsing JSON.", string(c))
  1127  			return nil, VOID, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1128  		}
  1129  	}
  1130  	e = fmt.Errorf("Cannot read a single element while parsing JSON.")
  1131  	return nil, VOID, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1132  
  1133  }
  1134  
  1135  func (p *TSimpleJSONProtocol) readIfNull() (bool, error) {
  1136  	cont := true
  1137  	for p.reader.Buffered() > 0 && cont {
  1138  		b, _ := p.reader.Peek(1)
  1139  		if len(b) < 1 {
  1140  			return false, nil
  1141  		}
  1142  		switch b[0] {
  1143  		default:
  1144  			return false, nil
  1145  		case JSON_NULL[0]:
  1146  			cont = false
  1147  			break
  1148  		case ' ', '\n', '\r', '\t':
  1149  			p.reader.ReadByte()
  1150  			break
  1151  		}
  1152  	}
  1153  	if p.reader.Buffered() == 0 {
  1154  		return false, nil
  1155  	}
  1156  	b, _ := p.reader.Peek(len(JSON_NULL))
  1157  	if string(b) == string(JSON_NULL) {
  1158  		p.reader.Read(b[0:len(JSON_NULL)])
  1159  		return true, nil
  1160  	}
  1161  	return false, nil
  1162  }
  1163  
  1164  func (p *TSimpleJSONProtocol) readQuoteIfNext() {
  1165  	if p.reader.Buffered() < 1 {
  1166  		return
  1167  	}
  1168  	b, _ := p.reader.Peek(1)
  1169  	if len(b) > 0 && b[0] == JSON_QUOTE {
  1170  		p.reader.ReadByte()
  1171  	}
  1172  }
  1173  
  1174  func (p *TSimpleJSONProtocol) readNumeric() (Numeric, error) {
  1175  	isNull, err := p.readIfNull()
  1176  	if isNull || err != nil {
  1177  		return NUMERIC_NULL, err
  1178  	}
  1179  	hasDecimalPoint := false
  1180  	nextCanBeSign := true
  1181  	hasE := false
  1182  	MAX_LEN := 40
  1183  	buf := bytes.NewBuffer(make([]byte, 0, MAX_LEN))
  1184  	continueFor := true
  1185  	inQuotes := false
  1186  	for continueFor {
  1187  		c, err := p.reader.ReadByte()
  1188  		if err != nil {
  1189  			if err == io.EOF {
  1190  				break
  1191  			}
  1192  			return NUMERIC_NULL, NewTProtocolException(err)
  1193  		}
  1194  		switch c {
  1195  		case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  1196  			buf.WriteByte(c)
  1197  			nextCanBeSign = false
  1198  		case '.':
  1199  			if hasDecimalPoint {
  1200  				e := fmt.Errorf("Unable to parse number with multiple decimal points '%s.'", buf.String())
  1201  				return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1202  			}
  1203  			if hasE {
  1204  				e := fmt.Errorf("Unable to parse number with decimal points in the exponent '%s.'", buf.String())
  1205  				return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1206  			}
  1207  			buf.WriteByte(c)
  1208  			hasDecimalPoint, nextCanBeSign = true, false
  1209  		case 'e', 'E':
  1210  			if hasE {
  1211  				e := fmt.Errorf("Unable to parse number with multiple exponents '%s%c'", buf.String(), c)
  1212  				return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1213  			}
  1214  			buf.WriteByte(c)
  1215  			hasE, nextCanBeSign = true, true
  1216  		case '-', '+':
  1217  			if !nextCanBeSign {
  1218  				e := fmt.Errorf("Negative sign within number")
  1219  				return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1220  			}
  1221  			buf.WriteByte(c)
  1222  			nextCanBeSign = false
  1223  		case ' ', 0, '\t', '\n', '\r', JSON_RBRACE[0], JSON_RBRACKET[0], JSON_COMMA[0], JSON_COLON[0]:
  1224  			p.reader.UnreadByte()
  1225  			continueFor = false
  1226  		case JSON_NAN[0]:
  1227  			if buf.Len() == 0 {
  1228  				buffer := make([]byte, len(JSON_NAN))
  1229  				buffer[0] = c
  1230  				_, e := p.reader.Read(buffer[1:])
  1231  				if e != nil {
  1232  					return NUMERIC_NULL, NewTProtocolException(e)
  1233  				}
  1234  				if JSON_NAN != string(buffer) {
  1235  					e := mismatch(JSON_NAN, string(buffer))
  1236  					return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1237  				}
  1238  				if inQuotes {
  1239  					p.readQuoteIfNext()
  1240  				}
  1241  				return NAN, nil
  1242  			} else {
  1243  				e := fmt.Errorf("Unable to parse number starting with character '%c'", c)
  1244  				return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1245  			}
  1246  		case JSON_INFINITY[0]:
  1247  			if buf.Len() == 0 || (buf.Len() == 1 && buf.Bytes()[0] == '+') {
  1248  				buffer := make([]byte, len(JSON_INFINITY))
  1249  				buffer[0] = c
  1250  				_, e := p.reader.Read(buffer[1:])
  1251  				if e != nil {
  1252  					return NUMERIC_NULL, NewTProtocolException(e)
  1253  				}
  1254  				if JSON_INFINITY != string(buffer) {
  1255  					e := mismatch(JSON_INFINITY, string(buffer))
  1256  					return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1257  				}
  1258  				if inQuotes {
  1259  					p.readQuoteIfNext()
  1260  				}
  1261  				return INFINITY, nil
  1262  			} else if buf.Len() == 1 && buf.Bytes()[0] == JSON_NEGATIVE_INFINITY[0] {
  1263  				buffer := make([]byte, len(JSON_NEGATIVE_INFINITY))
  1264  				buffer[0] = JSON_NEGATIVE_INFINITY[0]
  1265  				buffer[1] = c
  1266  				_, e := p.reader.Read(buffer[2:])
  1267  				if e != nil {
  1268  					return NUMERIC_NULL, NewTProtocolException(e)
  1269  				}
  1270  				if JSON_NEGATIVE_INFINITY != string(buffer) {
  1271  					e := mismatch(JSON_NEGATIVE_INFINITY, string(buffer))
  1272  					return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1273  				}
  1274  				if inQuotes {
  1275  					p.readQuoteIfNext()
  1276  				}
  1277  				return NEGATIVE_INFINITY, nil
  1278  			} else {
  1279  				e := fmt.Errorf("Unable to parse number starting with character '%c' due to existing buffer %s", c, buf.String())
  1280  				return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1281  			}
  1282  		case JSON_QUOTE:
  1283  			if !inQuotes {
  1284  				inQuotes = true
  1285  			} else {
  1286  				break
  1287  			}
  1288  		default:
  1289  			e := fmt.Errorf("Unable to parse number starting with character '%c'", c)
  1290  			return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1291  		}
  1292  	}
  1293  	if buf.Len() == 0 {
  1294  		e := fmt.Errorf("Unable to parse number from empty string ''")
  1295  		return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1296  	}
  1297  	return NewNumericFromJSONString(buf.String(), false), nil
  1298  }