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
10 -- http://www.apache.org/licenses/LICENSE-2.0
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
22 require 'libluabitwise'
24 TJSONProtocol = __TObject.new(TProtocolBase, {
25 __type = 'TJSONProtocol',
26 THRIFT_JSON_PROTOCOL_VERSION = 1,
28 jsonContextVal = {first = true, colon = true, ttype = 2, null = true},
34 TTypeToString[TType.BOOL] = "tf"
35 TTypeToString[TType.BYTE] = "i8"
36 TTypeToString[TType.I16] = "i16"
37 TTypeToString[TType.I32] = "i32"
38 TTypeToString[TType.I64] = "i64"
39 TTypeToString[TType.DOUBLE] = "dbl"
40 TTypeToString[TType.STRING] = "str"
41 TTypeToString[TType.STRUCT] = "rec"
42 TTypeToString[TType.LIST] = "lst"
43 TTypeToString[TType.SET] = "set"
44 TTypeToString[TType.MAP] = "map"
68 StringDelimiter = '"',
72 Infinity = 'Infinity',
73 NegativeInfinity = '-Infinity',
74 EscapeChars = "\"\\bfnrt",
75 EscapePrefix = "\\u00"
79 '"', '\\', '\b', '\f', '\n', '\r', '\t'
83 --0 1 2 3 4 5 6 7 8 9 A B C D E F
84 0, 0, 0, 0, 0, 0, 0, 0, 98,116,110, 0,102,114, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 1, 1,34, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
89 -- character table string
90 local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
93 function base64_encode(data)
94 return ((data:gsub('.', function(x)
96 for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
98 end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
99 if (#x < 6) then return '' end
101 for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
102 return b:sub(c+1,c+1)
103 end)..({ '', '==', '=' })[#data%3+1])
107 function base64_decode(data)
108 data = string.gsub(data, '[^'..b..'=]', '')
109 return (data:gsub('.', function(x)
110 if (x == '=') then return '' end
111 local r,f='',(b:find(x)-1)
112 for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
114 end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
115 if (#x ~= 8) then return '' end
117 for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
118 return string.char(c)
122 function TJSONProtocol:resetContext()
123 self.jsonContext = {}
124 self.jsonContextVal = {first = true, colon = true, ttype = 2, null = true}
125 self.jsonContextIndex = 1
128 function TJSONProtocol:contextPush(context)
129 self.jsonContextIndex = self.jsonContextIndex + 1
130 self.jsonContext[self.jsonContextIndex] = self.jsonContextVal
131 self.jsonContextVal = context
134 function TJSONProtocol:contextPop()
135 self.jsonContextVal = self.jsonContext[self.jsonContextIndex]
136 self.jsonContextIndex = self.jsonContextIndex - 1
139 function TJSONProtocol:escapeNum()
140 if self.jsonContextVal.ttype == 1 then
141 return self.jsonContextVal.colon
147 function TJSONProtocol:writeElemSeparator()
148 if self.jsonContextVal.null then
151 if self.jsonContextVal.first then
152 self.jsonContextVal.first = false
154 if self.jsonContextVal.ttype == 1 then
155 if self.jsonContextVal.colon then
156 self.trans:write(JSONNode.PairSeparator)
157 self.jsonContextVal.colon = false
159 self.trans:write(JSONNode.ElemSeparator)
160 self.jsonContextVal.colon = true
163 self.trans:write(JSONNode.ElemSeparator)
168 function TJSONProtocol:hexChar(val)
169 val = libluabitwise.band(val, 0x0f)
177 function TJSONProtocol:writeJSONEscapeChar(ch)
178 self.trans:write(JSONNode.EscapePrefix)
179 local outCh = hexChar(libluabitwise.shiftr(ch, 4))
180 local buff = libluabpack.bpack('c', outCh)
181 self.trans:write(buff)
183 buff = libluabpack.bpack('c', outCh)
184 self.trans:write(buff)
187 function TJSONProtocol:writeJSONChar(byte)
188 ch = string.byte(byte)
190 if ch == JSONNode.Backslash then
191 self.trans:write(JSONNode.Backslash)
192 self.trans:write(JSONNode.Backslash)
194 self.trans:write(byte)
197 local outCh = JSONCharTable[ch+1]
199 self.trans:write(byte)
200 elseif outCh > 1 then
201 self.trans:write(JSONNode.Backslash)
202 local buff = libluabpack.bpack('c', outCh)
203 self.trans:write(buff)
205 self:writeJSONEscapeChar(ch)
210 function TJSONProtocol:writeJSONString(str)
211 self:writeElemSeparator()
212 self.trans:write(JSONNode.StringDelimiter)
213 -- TODO escape special characters
214 local length = string.len(str)
216 while ii <= length do
217 self:writeJSONChar(string.sub(str, ii, ii))
220 self.trans:write(JSONNode.StringDelimiter)
223 function TJSONProtocol:writeJSONBase64(str)
224 self:writeElemSeparator()
225 self.trans:write(JSONNode.StringDelimiter)
226 local length = string.len(str)
229 -- Encode 3 bytes at a time
230 local bytes = base64_encode(string.sub(str, offset, offset+3))
231 self.trans:write(bytes)
236 local bytes = base64_encode(string.sub(str, offset, offset+length))
237 self.trans:write(bytes)
239 self.trans:write(JSONNode.StringDelimiter)
242 function TJSONProtocol:writeJSONInteger(num)
243 self:writeElemSeparator()
244 if self:escapeNum() then
245 self.trans:write(JSONNode.StringDelimiter)
247 local numstr = "" .. num
248 numstr = string.sub(numstr, string.find(numstr, "^[+-]?%d+"))
249 self.trans:write(numstr)
250 if self:escapeNum() then
251 self.trans:write(JSONNode.StringDelimiter)
255 function TJSONProtocol:writeJSONDouble(dub)
256 self:writeElemSeparator()
257 local val = "" .. dub
258 local prefix = string.sub(val, 1, 1)
259 local special = false
260 if prefix == 'N' or prefix == 'n' then
263 elseif prefix == 'I' or prefix == 'i' then
264 val = JSONNode.Infinity
266 elseif prefix == '-' then
267 local secondByte = string.sub(val, 2, 2)
268 if secondByte == 'I' or secondByte == 'i' then
269 val = JSONNode.NegativeInfinity
274 if special or self:escapeNum() then
275 self.trans:write(JSONNode.StringDelimiter)
277 self.trans:write(val)
278 if special or self:escapeNum() then
279 self.trans:write(JSONNode.StringDelimiter)
283 function TJSONProtocol:writeJSONObjectBegin()
284 self:writeElemSeparator()
285 self.trans:write(JSONNode.ObjectBegin)
286 self:contextPush({first = true, colon = true, ttype = 1, null = false})
289 function TJSONProtocol:writeJSONObjectEnd()
291 self.trans:write(JSONNode.ObjectEnd)
294 function TJSONProtocol:writeJSONArrayBegin()
295 self:writeElemSeparator()
296 self.trans:write(JSONNode.ArrayBegin)
297 self:contextPush({first = true, colon = true, ttype = 2, null = false})
300 function TJSONProtocol:writeJSONArrayEnd()
302 self.trans:write(JSONNode.ArrayEnd)
305 function TJSONProtocol:writeMessageBegin(name, ttype, seqid)
307 self:writeJSONArrayBegin()
308 self:writeJSONInteger(TJSONProtocol.THRIFT_JSON_PROTOCOL_VERSION)
309 self:writeJSONString(name)
310 self:writeJSONInteger(ttype)
311 self:writeJSONInteger(seqid)
314 function TJSONProtocol:writeMessageEnd()
315 self:writeJSONArrayEnd()
318 function TJSONProtocol:writeStructBegin(name)
319 self:writeJSONObjectBegin()
322 function TJSONProtocol:writeStructEnd()
323 self:writeJSONObjectEnd()
326 function TJSONProtocol:writeFieldBegin(name, ttype, id)
327 self:writeJSONInteger(id)
328 self:writeJSONObjectBegin()
329 self:writeJSONString(TTypeToString[ttype])
332 function TJSONProtocol:writeFieldEnd()
333 self:writeJSONObjectEnd()
336 function TJSONProtocol:writeFieldStop()
339 function TJSONProtocol:writeMapBegin(ktype, vtype, size)
340 self:writeJSONArrayBegin()
341 self:writeJSONString(TTypeToString[ktype])
342 self:writeJSONString(TTypeToString[vtype])
343 self:writeJSONInteger(size)
344 return self:writeJSONObjectBegin()
347 function TJSONProtocol:writeMapEnd()
348 self:writeJSONObjectEnd()
349 self:writeJSONArrayEnd()
352 function TJSONProtocol:writeListBegin(etype, size)
353 self:writeJSONArrayBegin()
354 self:writeJSONString(TTypeToString[etype])
355 self:writeJSONInteger(size)
358 function TJSONProtocol:writeListEnd()
359 self:writeJSONArrayEnd()
362 function TJSONProtocol:writeSetBegin(etype, size)
363 self:writeJSONArrayBegin()
364 self:writeJSONString(TTypeToString[etype])
365 self:writeJSONInteger(size)
368 function TJSONProtocol:writeSetEnd()
369 self:writeJSONArrayEnd()
372 function TJSONProtocol:writeBool(bool)
374 self:writeJSONInteger(1)
376 self:writeJSONInteger(0)
380 function TJSONProtocol:writeByte(byte)
381 local buff = libluabpack.bpack('c', byte)
382 local val = libluabpack.bunpack('c', buff)
383 self:writeJSONInteger(val)
386 function TJSONProtocol:writeI16(i16)
387 local buff = libluabpack.bpack('s', i16)
388 local val = libluabpack.bunpack('s', buff)
389 self:writeJSONInteger(val)
392 function TJSONProtocol:writeI32(i32)
393 local buff = libluabpack.bpack('i', i32)
394 local val = libluabpack.bunpack('i', buff)
395 self:writeJSONInteger(val)
398 function TJSONProtocol:writeI64(i64)
399 local buff = libluabpack.bpack('l', i64)
400 local val = libluabpack.bunpack('l', buff)
401 self:writeJSONInteger(tostring(val))
404 function TJSONProtocol:writeDouble(dub)
405 self:writeJSONDouble(string.format("%.16f", dub))
408 function TJSONProtocol:writeString(str)
409 self:writeJSONString(str)
412 function TJSONProtocol:writeBinary(str)
414 self:writeJSONBase64(str)
417 function TJSONProtocol:readJSONSyntaxChar(ch)
419 if self.hasReadByte ~= "" then
420 ch2 = self.hasReadByte
421 self.hasReadByte = ""
423 ch2 = self.trans:readAll(1)
426 terror(TProtocolException:new{message = "Expected ".. ch .. ", got " .. ch2})
430 function TJSONProtocol:readElemSeparator()
431 if self.jsonContextVal.null then
434 if self.jsonContextVal.first then
435 self.jsonContextVal.first = false
437 if self.jsonContextVal.ttype == 1 then
438 if self.jsonContextVal.colon then
439 self:readJSONSyntaxChar(JSONNode.PairSeparator)
440 self.jsonContextVal.colon = false
442 self:readJSONSyntaxChar(JSONNode.ElemSeparator)
443 self.jsonContextVal.colon = true
446 self:readJSONSyntaxChar(JSONNode.ElemSeparator)
451 function TJSONProtocol:hexVal(ch)
452 local val = string.byte(ch)
453 if val >= 48 and val <= 57 then
455 elseif val >= 97 and val <= 102 then
458 terror(TProtocolException:new{message = "Expected hex val ([0-9a-f]); got " .. ch})
462 function TJSONProtocol:readJSONEscapeChar(ch)
463 self:readJSONSyntaxChar(JSONNode.ZeroChar)
464 self:readJSONSyntaxChar(JSONNode.ZeroChar)
465 local b1 = self.trans:readAll(1)
466 local b2 = self.trans:readAll(1)
467 return libluabitwise.shiftl(self:hexVal(b1), 4) + self:hexVal(b2)
471 function TJSONProtocol:readJSONString()
472 self:readElemSeparator()
473 self:readJSONSyntaxChar(JSONNode.StringDelimiter)
476 local ch = self.trans:readAll(1)
477 if ch == JSONNode.StringDelimiter then
480 if ch == JSONNode.Backslash then
481 ch = self.trans:readAll(1)
482 if ch == JSONNode.EscapeChar then
483 self:readJSONEscapeChar(ch)
485 local pos, _ = string.find(JSONNode.EscapeChars, ch)
487 terror(TProtocolException:new{message = "Expected control char, got " .. ch})
489 ch = EscapeCharVals[pos]
492 result = result .. ch
497 function TJSONProtocol:readJSONBase64()
498 local result = self:readJSONString()
499 local length = string.len(result)
503 local bytes = string.sub(result, offset, offset+4)
504 str = str .. base64_decode(bytes)
509 str = str .. base64_decode(string.sub(result, offset, offset + length))
514 function TJSONProtocol:readJSONNumericChars()
517 local ch = self.trans:readAll(1)
518 if string.find(ch, '[-+0-9.Ee]') then
519 result = result .. ch
521 self.hasReadByte = ch
528 function TJSONProtocol:readJSONLongInteger()
529 self:readElemSeparator()
530 if self:escapeNum() then
531 self:readJSONSyntaxChar(JSONNode.StringDelimiter)
533 local result = self:readJSONNumericChars()
534 if self:escapeNum() then
535 self:readJSONSyntaxChar(JSONNode.StringDelimiter)
540 function TJSONProtocol:readJSONInteger()
541 return tonumber(self:readJSONLongInteger())
544 function TJSONProtocol:readJSONDouble()
545 self:readElemSeparator()
546 local delimiter = self.trans:readAll(1)
548 if delimiter == JSONNode.StringDelimiter then
549 local str = self:readJSONString()
550 if str == JSONNode.Nan then
552 elseif str == JSONNode.Infinity then
553 num = math.maxinteger
554 elseif str == JSONNode.NegativeInfinity then
555 num = math.mininteger
560 if self:escapeNum() then
561 self:readJSONSyntaxChar(JSONNode.StringDelimiter)
563 local result = self:readJSONNumericChars()
564 num = tonumber(delimiter.. result)
569 function TJSONProtocol:readJSONObjectBegin()
570 self:readElemSeparator()
571 self:readJSONSyntaxChar(JSONNode.ObjectBegin)
572 self:contextPush({first = true, colon = true, ttype = 1, null = false})
575 function TJSONProtocol:readJSONObjectEnd()
576 self:readJSONSyntaxChar(JSONNode.ObjectEnd)
580 function TJSONProtocol:readJSONArrayBegin()
581 self:readElemSeparator()
582 self:readJSONSyntaxChar(JSONNode.ArrayBegin)
583 self:contextPush({first = true, colon = true, ttype = 2, null = false})
586 function TJSONProtocol:readJSONArrayEnd()
587 self:readJSONSyntaxChar(JSONNode.ArrayEnd)
591 function TJSONProtocol:readMessageBegin()
593 self:readJSONArrayBegin()
594 local version = self:readJSONInteger()
595 if version ~= self.THRIFT_JSON_PROTOCOL_VERSION then
596 terror(TProtocolException:new{message = "Message contained bad version."})
598 local name = self:readJSONString()
599 local ttype = self:readJSONInteger()
600 local seqid = self:readJSONInteger()
601 return name, ttype, seqid
604 function TJSONProtocol:readMessageEnd()
605 self:readJSONArrayEnd()
608 function TJSONProtocol:readStructBegin()
609 self:readJSONObjectBegin()
613 function TJSONProtocol:readStructEnd()
614 self:readJSONObjectEnd()
617 function TJSONProtocol:readFieldBegin()
618 local ttype = TType.STOP
620 local ch = self.trans:readAll(1)
621 self.hasReadByte = ch
622 if ch ~= JSONNode.ObjectEnd then
623 id = self:readJSONInteger()
624 self:readJSONObjectBegin()
625 local typeName = self:readJSONString()
626 ttype = StringToTType[typeName]
628 return nil, ttype, id
631 function TJSONProtocol:readFieldEnd()
632 self:readJSONObjectEnd()
635 function TJSONProtocol:readMapBegin()
636 self:readJSONArrayBegin()
637 local typeName = self:readJSONString()
638 local ktype = StringToTType[typeName]
639 typeName = self:readJSONString()
640 local vtype = StringToTType[typeName]
641 local size = self:readJSONInteger()
642 self:readJSONObjectBegin()
643 return ktype, vtype, size
646 function TJSONProtocol:readMapEnd()
647 self:readJSONObjectEnd()
648 self:readJSONArrayEnd()
651 function TJSONProtocol:readListBegin()
652 self:readJSONArrayBegin()
653 local typeName = self:readJSONString()
654 local etype = StringToTType[typeName]
655 local size = self:readJSONInteger()
659 function TJSONProtocol:readListEnd()
660 return self:readJSONArrayEnd()
663 function TJSONProtocol:readSetBegin()
664 return self:readListBegin()
667 function TJSONProtocol:readSetEnd()
668 return self:readJSONArrayEnd()
671 function TJSONProtocol:readBool()
672 local result = self:readJSONInteger()
680 function TJSONProtocol:readByte()
681 local result = self:readJSONInteger()
682 if result >= 256 then
683 terror(TProtocolException:new{message = "UnExpected Byte " .. result})
688 function TJSONProtocol:readI16()
689 return self:readJSONInteger()
692 function TJSONProtocol:readI32()
693 return self:readJSONInteger()
696 function TJSONProtocol:readI64()
697 local long = liblualongnumber.new
698 return long(self:readJSONLongInteger())
701 function TJSONProtocol:readDouble()
702 return self:readJSONDouble()
705 function TJSONProtocol:readString()
706 return self:readJSONString()
709 function TJSONProtocol:readBinary()
710 return self:readJSONBase64()
713 TJSONProtocolFactory = TProtocolFactory:new{
714 __type = 'TJSONProtocolFactory',
717 function TJSONProtocolFactory:getProtocol(trans)
718 -- TODO Enforce that this must be a transport class (ie not a bool)
720 terror(TProtocolException:new{
721 message = 'Must supply a transport to ' .. ttype(self)
724 return TJSONProtocol:new{