Changeset 3997

Show
Ignore:
Timestamp:
01/04/09 21:09:13 (4 years ago)
Author:
Cyrus
Message:

Introduce active (pulling) JSON-Decoder

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • luci/branches/luci-0.8/libs/json/luasrc/json.lua

    r3001 r3997  
    500500    ['{'] = Decoder.parse_object 
    501501} 
     502 
     503 
     504--- Create a new Active JSON-Decoder. 
     505-- @class   function 
     506-- @name    ActiveDecoder 
     507-- @param   customnull  Use luci.json.null instead of nil for decoding null 
     508-- @return  Active JSON-Decoder 
     509ActiveDecoder = util.class(Decoder) 
     510 
     511function ActiveDecoder.__init__(self, source, customnull) 
     512    Decoder.__init__(self, customnull) 
     513    self.source = source 
     514    self.chunk = nil 
     515    getmetatable(self).__call = self.get 
     516end 
     517 
     518 
     519--- Fetches one JSON-object from given source 
     520-- @return Decoded object 
     521function ActiveDecoder.get(self) 
     522    local chunk, src_err, object 
     523    if not self.chunk then 
     524        chunk, src_err = self.source() 
     525    else 
     526        chunk = self.chunk 
     527    end 
     528 
     529    self.chunk, object = self:dispatch(chunk, src_err, true) 
     530    return object 
     531end 
     532 
     533 
     534function ActiveDecoder.fetch(self) 
     535    local chunk, src_err = self.source() 
     536    assert(chunk or not src_err, src_err) 
     537    return chunk 
     538end