Electroneum
GenericReader< SourceEncoding, TargetEncoding, StackAllocator > Class Template Reference

SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator. More...

#include <fwd.h>

Public Types

typedef SourceEncoding::Ch Ch
 SourceEncoding character type. More...
 

Public Member Functions

 GenericReader (StackAllocator *stackAllocator=0, size_t stackCapacity=kDefaultStackCapacity)
 Constructor. More...
 
template<unsigned parseFlags, typename InputStream , typename Handler >
ParseResult Parse (InputStream &is, Handler &handler)
 Parse JSON text. More...
 
template<typename InputStream , typename Handler >
ParseResult Parse (InputStream &is, Handler &handler)
 Parse JSON text (with kParseDefaultFlags) More...
 
void IterativeParseInit ()
 Initialize JSON text token-by-token parsing. More...
 
template<unsigned parseFlags, typename InputStream , typename Handler >
bool IterativeParseNext (InputStream &is, Handler &handler)
 Parse one token from JSON text. More...
 
RAPIDJSON_FORCEINLINE bool IterativeParseComplete () const
 Check if token-by-token parsing JSON text is complete. More...
 
bool HasParseError () const
 Whether a parse error has occurred in the last parsing. More...
 
ParseErrorCode GetParseErrorCode () const
 Get the ParseErrorCode of last parsing. More...
 
size_t GetErrorOffset () const
 Get the position of last parsing error in input, 0 otherwise. More...
 

Protected Member Functions

void SetParseError (ParseErrorCode code, size_t offset)
 

Detailed Description

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
class GenericReader< SourceEncoding, TargetEncoding, StackAllocator >

SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.

GenericReader parses JSON text from a stream, and send events synchronously to an object implementing Handler concept.

It needs to allocate a stack for storing a single decoded string during non-destructive parsing.

For in-situ parsing, the decoded string is directly written to the source text string, no temporary buffer is required.

A GenericReader object can be reused for parsing multiple JSON text.

Template Parameters
SourceEncodingEncoding of the input stream.
TargetEncodingEncoding of the parse output.
StackAllocatorAllocator type for stack.

Definition at line 88 of file fwd.h.

Member Typedef Documentation

◆ Ch

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
typedef SourceEncoding::Ch GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::Ch

SourceEncoding character type.

Definition at line 539 of file reader.h.

Constructor & Destructor Documentation

◆ GenericReader()

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::GenericReader ( StackAllocator *  stackAllocator = 0,
size_t  stackCapacity = kDefaultStackCapacity 
)
inline

Constructor.

Parameters
stackAllocatorOptional allocator for allocating stack memory. (Only use for non-destructive parsing)
stackCapacitystack capacity in bytes for storing a single decoded string. (Only use for non-destructive parsing)

Definition at line 545 of file reader.h.

545  :
546  stack_(stackAllocator, stackCapacity), parseResult_(), state_(IterativeParsingStartState) {}

Member Function Documentation

◆ GetErrorOffset()

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
size_t GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::GetErrorOffset ( ) const
inline

Get the position of last parsing error in input, 0 otherwise.

Definition at line 686 of file reader.h.

686 { return parseResult_.Offset(); }
size_t Offset() const
Get the error offset, if IsError(), 0 otherwise.
Definition: error.h:118
Here is the caller graph for this function:

◆ GetParseErrorCode()

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
ParseErrorCode GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::GetParseErrorCode ( ) const
inline

Get the ParseErrorCode of last parsing.

Definition at line 683 of file reader.h.

683 { return parseResult_.Code(); }
ParseErrorCode Code() const
Get the error code.
Definition: error.h:116
Here is the caller graph for this function:

◆ HasParseError()

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
bool GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::HasParseError ( ) const
inline

Whether a parse error has occurred in the last parsing.

Definition at line 680 of file reader.h.

680 { return parseResult_.IsError(); }
bool IsError() const
Whether the result is an error.
Definition: error.h:123
Here is the caller graph for this function:

◆ IterativeParseComplete()

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
RAPIDJSON_FORCEINLINE bool GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::IterativeParseComplete ( ) const
inline

Check if token-by-token parsing JSON text is complete.

Returns
Whether the JSON has been fully decoded.

Definition at line 675 of file reader.h.

675  {
676  return IsIterativeParsingCompleteState(state_);
677  }
Here is the caller graph for this function:

◆ IterativeParseInit()

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
void GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::IterativeParseInit ( )
inline

Initialize JSON text token-by-token parsing.

Definition at line 605 of file reader.h.

605  {
606  parseResult_.Clear();
607  state_ = IterativeParsingStartState;
608  }
void Clear()
Reset error code.
Definition: error.h:134
Here is the caller graph for this function:

◆ IterativeParseNext()

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
template<unsigned parseFlags, typename InputStream , typename Handler >
bool GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::IterativeParseNext ( InputStream &  is,
Handler &  handler 
)
inline

Parse one token from JSON text.

Template Parameters
InputStreamType of input stream, implementing Stream concept
HandlerType of handler, implementing Handler concept.
Parameters
isInput stream to be parsed.
handlerThe handler to receive events.
Returns
Whether the parsing is successful.

Definition at line 618 of file reader.h.

618  {
619  while (RAPIDJSON_LIKELY(is.Peek() != '\0')) {
620  SkipWhitespaceAndComments<parseFlags>(is);
621 
622  Token t = Tokenize(is.Peek());
623  IterativeParsingState n = Predict(state_, t);
624  IterativeParsingState d = Transit<parseFlags>(state_, t, n, is, handler);
625 
626  // If we've finished or hit an error...
627  if (RAPIDJSON_UNLIKELY(IsIterativeParsingCompleteState(d))) {
628  // Report errors.
629  if (d == IterativeParsingErrorState) {
630  HandleError(state_, is);
631  return false;
632  }
633 
634  // Transition to the finish state.
635  RAPIDJSON_ASSERT(d == IterativeParsingFinishState);
636  state_ = d;
637 
638  // If StopWhenDone is not set...
639  if (!(parseFlags & kParseStopWhenDoneFlag)) {
640  // ... and extra non-whitespace data is found...
641  SkipWhitespaceAndComments<parseFlags>(is);
642  if (is.Peek() != '\0') {
643  // ... this is considered an error.
644  HandleError(state_, is);
645  return false;
646  }
647  }
648 
649  // Success! We are done!
650  return true;
651  }
652 
653  // Transition to the new state.
654  state_ = d;
655 
656  // If we parsed anything other than a delimiter, we invoked the handler, so we can return true now.
657  if (!IsIterativeParsingDelimiterState(n))
658  return true;
659  }
660 
661  // We reached the end of file.
662  stack_.Clear();
663 
664  if (state_ != IterativeParsingFinishState) {
665  HandleError(state_, is);
666  return false;
667  }
668 
669  return true;
670  }
#define RAPIDJSON_LIKELY(x)
Compiler branching hint for expression with high probability to be true.
Definition: rapidjson.h:468
void Clear()
Definition: stack.h:98
After parsing a complete JSON root from stream, stop further processing the rest of stream...
Definition: reader.h:150
#define RAPIDJSON_UNLIKELY(x)
Compiler branching hint for expression with low probability to be true.
Definition: rapidjson.h:481
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:411
Here is the caller graph for this function:

◆ Parse() [1/2]

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
template<unsigned parseFlags, typename InputStream , typename Handler >
ParseResult GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::Parse ( InputStream &  is,
Handler &  handler 
)
inline

Parse JSON text.

Template Parameters
parseFlagsCombination of ParseFlag.
InputStreamType of input stream, implementing Stream concept.
HandlerType of handler, implementing Handler concept.
Parameters
isInput stream to be parsed.
handlerThe handler to receive events.
Returns
Whether the parsing is successful.

Definition at line 557 of file reader.h.

557  {
558  if (parseFlags & kParseIterativeFlag)
559  return IterativeParse<parseFlags>(is, handler);
560 
561  parseResult_.Clear();
562 
563  ClearStackOnExit scope(*this);
564 
565  SkipWhitespaceAndComments<parseFlags>(is);
566  RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
567 
568  if (RAPIDJSON_UNLIKELY(is.Peek() == '\0')) {
570  RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
571  }
572  else {
573  ParseValue<parseFlags>(is, handler);
574  RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
575 
576  if (!(parseFlags & kParseStopWhenDoneFlag)) {
577  SkipWhitespaceAndComments<parseFlags>(is);
578  RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
579 
580  if (RAPIDJSON_UNLIKELY(is.Peek() != '\0')) {
582  RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_);
583  }
584  }
585  }
586 
587  return parseResult_;
588  }
The document is empty.
Definition: error.h:67
Iterative(constant complexity in terms of function call stack size) parsing.
Definition: reader.h:149
void Clear()
Reset error code.
Definition: error.h:134
#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset)
Macro to indicate a parse error.
Definition: reader.h:99
After parsing a complete JSON root from stream, stop further processing the rest of stream...
Definition: reader.h:150
#define RAPIDJSON_UNLIKELY(x)
Compiler branching hint for expression with low probability to be true.
Definition: rapidjson.h:481
The document root must not follow by other values.
Definition: error.h:68
Here is the caller graph for this function:

◆ Parse() [2/2]

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
template<typename InputStream , typename Handler >
ParseResult GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::Parse ( InputStream &  is,
Handler &  handler 
)
inline

Parse JSON text (with kParseDefaultFlags)

Template Parameters
InputStreamType of input stream, implementing Stream concept
HandlerType of handler, implementing Handler concept.
Parameters
isInput stream to be parsed.
handlerThe handler to receive events.
Returns
Whether the parsing is successful.

Definition at line 598 of file reader.h.

598  {
599  return Parse<kParseDefaultFlags>(is, handler);
600  }

◆ SetParseError()

template<typename SourceEncoding, typename TargetEncoding, typename StackAllocator = CrtAllocator>
void GenericReader< SourceEncoding, TargetEncoding, StackAllocator >::SetParseError ( ParseErrorCode  code,
size_t  offset 
)
inlineprotected

Definition at line 689 of file reader.h.

689 { parseResult_.Set(code, offset); }
void Set(ParseErrorCode code, size_t offset=0)
Update error code and offset.
Definition: error.h:136

The documentation for this class was generated from the following files: