diff options
| author | Steven Barth <steven@midlink.org> | 2009-01-04 20:09:13 +0000 |
|---|---|---|
| committer | Steven Barth <steven@midlink.org> | 2009-01-04 20:09:13 +0000 |
| commit | ea361d9d2c60c961c09332592bce8423b8c708f3 (patch) | |
| tree | 3034f168dcb5591e85309686d65b9a59162c88ed | |
| parent | e703d536cc0fdd153b259633d8fad78df496e7bb (diff) | |
Introduce active (pulling) JSON-Decoder
| -rw-r--r-- | libs/json/luasrc/json.lua | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/libs/json/luasrc/json.lua b/libs/json/luasrc/json.lua index 5d1abcf484..26b540428a 100644 --- a/libs/json/luasrc/json.lua +++ b/libs/json/luasrc/json.lua @@ -498,4 +498,41 @@ Decoder.parsers = { ['n'] = Decoder.parse_null, ['['] = Decoder.parse_array, ['{'] = Decoder.parse_object -}
\ No newline at end of file +} + + +--- Create a new Active JSON-Decoder. +-- @class function +-- @name ActiveDecoder +-- @param customnull Use luci.json.null instead of nil for decoding null +-- @return Active JSON-Decoder +ActiveDecoder = util.class(Decoder) + +function ActiveDecoder.__init__(self, source, customnull) + Decoder.__init__(self, customnull) + self.source = source + self.chunk = nil + getmetatable(self).__call = self.get +end + + +--- Fetches one JSON-object from given source +-- @return Decoded object +function ActiveDecoder.get(self) + local chunk, src_err, object + if not self.chunk then + chunk, src_err = self.source() + else + chunk = self.chunk + end + + self.chunk, object = self:dispatch(chunk, src_err, true) + return object +end + + +function ActiveDecoder.fetch(self) + local chunk, src_err = self.source() + assert(chunk or not src_err, src_err) + return chunk +end |
