| | 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 |
| | 509 | ActiveDecoder = util.class(Decoder) |
| | 510 | |
| | 511 | function ActiveDecoder.__init__(self, source, customnull) |
| | 512 | Decoder.__init__(self, customnull) |
| | 513 | self.source = source |
| | 514 | self.chunk = nil |
| | 515 | getmetatable(self).__call = self.get |
| | 516 | end |
| | 517 | |
| | 518 | |
| | 519 | --- Fetches one JSON-object from given source |
| | 520 | -- @return Decoded object |
| | 521 | function 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 |
| | 531 | end |
| | 532 | |
| | 533 | |
| | 534 | function ActiveDecoder.fetch(self) |
| | 535 | local chunk, src_err = self.source() |
| | 536 | assert(chunk or not src_err, src_err) |
| | 537 | return chunk |
| | 538 | end |