… | |
… | |
683 | =back |
683 | =back |
684 | |
684 | |
685 | |
685 | |
686 | =head1 INCREMENTAL PARSING |
686 | =head1 INCREMENTAL PARSING |
687 | |
687 | |
688 | [This section is still EXPERIMENTAL] |
688 | [This section and the API it details is still EXPERIMENTAL] |
689 | |
689 | |
690 | In some cases, there is the need for incremental parsing of JSON |
690 | In some cases, there is the need for incremental parsing of JSON |
691 | texts. While this module always has to keep both JSON text and resulting |
691 | texts. While this module always has to keep both JSON text and resulting |
692 | Perl data structure in memory at one time, it does allow you to parse a |
692 | Perl data structure in memory at one time, it does allow you to parse a |
693 | JSON stream incrementally. It does so by accumulating text until it has |
693 | JSON stream incrementally. It does so by accumulating text until it has |
… | |
… | |
714 | return without doing anything further. This can be used to add more text |
714 | return without doing anything further. This can be used to add more text |
715 | in as many chunks as you want. |
715 | in as many chunks as you want. |
716 | |
716 | |
717 | If the method is called in scalar context, then it will try to extract |
717 | If the method is called in scalar context, then it will try to extract |
718 | exactly I<one> JSON object. If that is successful, it will return this |
718 | exactly I<one> JSON object. If that is successful, it will return this |
719 | object, otherwise it will return C<undef>. This is the most common way of |
719 | object, otherwise it will return C<undef>. If there is a parse error, |
|
|
720 | this method will croak just as C<decode> would do (one can then use |
|
|
721 | C<incr_skip> to skip the errornous part). This is the most common way of |
720 | using the method. |
722 | using the method. |
721 | |
723 | |
722 | And finally, in list context, it will try to extract as many objects |
724 | And finally, in list context, it will try to extract as many objects |
723 | from the stream as it can find and return them, or the empty list |
725 | from the stream as it can find and return them, or the empty list |
724 | otherwise. For this to work, there must be no separators between the JSON |
726 | otherwise. For this to work, there must be no separators between the JSON |
725 | objects or arrays, instead they must be concatenated back-to-back. |
727 | objects or arrays, instead they must be concatenated back-to-back. If |
|
|
728 | an error occurs, an exception will be raised as in the scalar context |
|
|
729 | case. Note that in this case, any previously-parsed JSON texts will be |
|
|
730 | lost. |
726 | |
731 | |
727 | =item $lvalue_string = $json->incr_text |
732 | =item $lvalue_string = $json->incr_text |
728 | |
733 | |
729 | This method returns the currently stored JSON fragment as an lvalue, that |
734 | This method returns the currently stored JSON fragment as an lvalue, that |
730 | is, you can manipulate it. This I<only> works when a preceding call to |
735 | is, you can manipulate it. This I<only> works when a preceding call to |
… | |
… | |
735 | method before having parsed anything. |
740 | method before having parsed anything. |
736 | |
741 | |
737 | This function is useful in two cases: a) finding the trailing text after a |
742 | This function is useful in two cases: a) finding the trailing text after a |
738 | JSON object or b) parsing multiple JSON objects separated by non-JSON text |
743 | JSON object or b) parsing multiple JSON objects separated by non-JSON text |
739 | (such as commas). |
744 | (such as commas). |
|
|
745 | |
|
|
746 | =item $json->incr_skip |
|
|
747 | |
|
|
748 | This will reset the state of the incremental parser and will remove the |
|
|
749 | parsed text from the input buffer. This is useful after C<incr_parse> |
|
|
750 | died, in which case the input buffer and incremental parser state is left |
|
|
751 | unchanged, to skip the text parsed so far and to reset the parse state. |
740 | |
752 | |
741 | =back |
753 | =back |
742 | |
754 | |
743 | =head2 LIMITATIONS |
755 | =head2 LIMITATIONS |
744 | |
756 | |