[ 
https://issues.apache.org/jira/browse/THRIFT-3631?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15151622#comment-15151622
 ] 

ASF GitHub Bot commented on THRIFT-3631:
----------------------------------------

Github user mygityf commented on a diff in the pull request:

    https://github.com/apache/thrift/pull/864#discussion_r53265228
  
    --- Diff: lib/lua/TJsonProtocol.lua ---
    @@ -0,0 +1,725 @@
    +--
    +-- Licensed to the Apache Software Foundation (ASF) under one
    +-- or more contributor license agreements. See the NOTICE file
    +-- distributed with this work for additional information
    +-- regarding copyright ownership. The ASF licenses this file
    +-- to you under the Apache License, Version 2.0 (the
    +-- "License"), you may not use this file except in compliance
    +-- with the License. You may obtain a copy of the License at
    +--
    +--   http://www.apache.org/licenses/LICENSE-2.0
    +--
    +-- Unless required by applicable law or agreed to in writing,
    +-- software distributed under the License is distributed on an
    +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +-- KIND, either express or implied. See the License for the
    +-- specific language governing permissions and limitations
    +-- under the License.
    +--
    +
    +require 'TProtocol'
    +require 'libluabpack'
    +require 'libluabitwise'
    +
    +TJSONProtocol = __TObject.new(TProtocolBase, {
    +  __type = 'TJSONProtocol',
    +  THRIFT_JSON_PROTOCOL_VERSION = 1,
    +  jsonContext = {},
    +  jsonContextVal = {first = true, colon = true, ttype = 2, null = true},
    +  jsonContextIndex = 1,
    +  hasReadByte = ""
    +})
    +
    +TTypeToString = {}
    +TTypeToString[TType.BOOL]   = "tf"
    +TTypeToString[TType.BYTE]   = "i8"
    +TTypeToString[TType.I16]    = "i16"
    +TTypeToString[TType.I32]    = "i32"
    +TTypeToString[TType.I64]    = "i64"
    +TTypeToString[TType.DOUBLE] = "dbl"
    +TTypeToString[TType.STRING] = "str"
    +TTypeToString[TType.STRUCT] = "rec"
    +TTypeToString[TType.LIST]   = "lst"
    +TTypeToString[TType.SET]    = "set"
    +TTypeToString[TType.MAP]    = "map"
    +
    +StringToTType = {
    +  tf  = TType.BOOL,
    +  i8  = TType.BYTE,
    +  i16 = TType.I16,
    +  i32 = TType.I32,
    +  i64 = TType.I64,
    +  dbl = TType.DOUBLE,
    +  str = TType.STRING,
    +  rec = TType.STRUCT,
    +  map = TType.MAP,
    +  set = TType.SET,
    +  lst = TType.LIST
    +}
    +
    +JSONNode = {
    +  ObjectBegin = '{',
    +  ObjectEnd = '}',
    +  ArrayBegin = '[',
    +  ArrayEnd = ']',
    +  PairSeparator = ':',
    +  ElemSeparator = ',',
    +  Backslash = '\\',
    +  StringDelimiter = '"',
    +  ZeroChar = '0',
    +  EscapeChar = 'u',
    +  Nan = 'NaN',
    +  Infinity = 'Infinity',
    +  NegativeInfinity = '-Infinity',
    +  EscapeChars = "\"\\bfnrt",
    +  EscapePrefix = "\\u00"
    +}
    +
    +EscapeCharVals = {
    +  '"', '\\', '\b', '\f', '\n', '\r', '\t'
    +}
    +
    +JSONCharTable = {
    +  --0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    +    0,  0,  0,  0,  0,  0,  0,  0, 98,116,110,  0,102,114,  0,  0,
    +    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    +    1,  1,34,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
    +}
    +
    +-- character table string
    +local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
    +
    +-- encoding
    +function base64_encode(data)
    +    return ((data:gsub('.', function(x) 
    +        local r,b='',x:byte()
    +        for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
    +        return r;
    +    end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
    +        if (#x < 6) then return '' end
    +        local c=0
    +        for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
    +        return b:sub(c+1,c+1)
    +    end)..({ '', '==', '=' })[#data%3+1])
    +end
    +
    +-- decoding
    +function base64_decode(data)
    +    data = string.gsub(data, '[^'..b..'=]', '')
    +    return (data:gsub('.', function(x)
    +        if (x == '=') then return '' end
    +        local r,f='',(b:find(x)-1)
    +        for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
    +        return r;
    +    end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
    +        if (#x ~= 8) then return '' end
    +        local c=0
    +        for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
    +        return string.char(c)
    +    end))
    +end
    +
    +function TJSONProtocol:resetContext()
    +  self.jsonContext = {}
    +  self.jsonContextVal = {first = true, colon = true, ttype = 2, null = 
true}
    +  self.jsonContextIndex = 1
    +end
    +
    +function TJSONProtocol:contextPush(context)
    +  self.jsonContextIndex = self.jsonContextIndex + 1
    +  self.jsonContext[self.jsonContextIndex] = self.jsonContextVal
    +  self.jsonContextVal = context
    +end
    +
    +function TJSONProtocol:contextPop()
    +  self.jsonContextVal = self.jsonContext[self.jsonContextIndex]
    +  self.jsonContextIndex = self.jsonContextIndex - 1
    +end
    +
    +function TJSONProtocol:escapeNum()
    +  if self.jsonContextVal.ttype == 1 then
    +    return self.jsonContextVal.colon
    +  else
    +    return false
    +  end
    +end
    +
    +function TJSONProtocol:writeElemSeparator()
    +  if self.jsonContextVal.null then
    +    return
    +  end
    +  if self.jsonContextVal.first then
    +    self.jsonContextVal.first = false
    +  else
    +    if self.jsonContextVal.ttype == 1 then
    +      if self.jsonContextVal.colon then
    +        self.trans:write(JSONNode.PairSeparator)
    +        self.jsonContextVal.colon = false
    +      else
    +        self.trans:write(JSONNode.ElemSeparator)
    +        self.jsonContextVal.colon = true
    +      end
    +    else
    +      self.trans:write(JSONNode.ElemSeparator)
    +    end
    +  end
    +end
    +
    +function TJSONProtocol:hexChar(val)
    +  val = libluabitwise.band(val, 0x0f)
    +  if val < 10 then
    +    return val + 48
    +  else
    +    return val + 87
    +  end
    +end
    +
    +function TJSONProtocol:writeJSONEscapeChar(ch)
    +  self.trans:write(JSONNode.EscapePrefix)
    +  local outCh = hexChar(libluabitwise.shiftr(ch, 4))
    +  local buff = libluabpack.bpack('c', outCh)
    +  self.trans:write(buff)
    +  outCh = hexChar(ch)
    +  buff = libluabpack.bpack('c', outCh)
    +  self.trans:write(buff)
    +end
    +
    +function TJSONProtocol:writeJSONChar(byte)
    +  ch = string.byte(byte)
    +  if ch >= 0x30 then
    +    if ch == JSONNode.Backslash then
    +      self.trans:write(JSONNode.Backslash)
    +      self.trans:write(JSONNode.Backslash)
    +    else
    +      self.trans:write(byte)
    +    end
    +  else
    +    local outCh = JSONCharTable[ch+1]
    +    if outCh == 1 then
    +      self.trans:write(byte)
    +    elseif outCh > 1 then
    +      self.trans:write(JSONNode.Backslash)
    +      local buff = libluabpack.bpack('c', outCh)
    +      self.trans:write(buff)
    +    else
    +      self:writeJSONEscapeChar(ch)
    +    end
    +  end
    +end
    +
    +function TJSONProtocol:writeJSONString(str)
    +  self:writeElemSeparator()
    +  self.trans:write(JSONNode.StringDelimiter)
    +  -- TODO 
    --- End diff --
    
    It can be escaped by being preceded with a backslash,
    like " to \", \t to \\t, \r to \\r, \f to \\f, \n to \\n , \b to \\b.
    Otherwise, character " will break JSON format.
    e.g:
    To be sent string is "Begin:This is Send\"String\b\t\n\r\f:End."



> JSON protocol implementation for Lua
> ------------------------------------
>
>                 Key: THRIFT-3631
>                 URL: https://issues.apache.org/jira/browse/THRIFT-3631
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Lua - Library
>    Affects Versions: 0.9.3
>         Environment: Linux Mac Windows
>            Reporter: WangYaofu
>            Assignee: WangYaofu
>             Fix For: 0.9.4
>
>
> This is JSON protocol implement for thrift-lua library.
> TJsonProtocol.lua is new source of lua for thrift JSON protocol.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to