Plasma Engine  2.0
Loading...
Searching...
No Matches
JSONReader.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/IO/JSONParser.h>
5#include <Foundation/Types/Variant.h>
6
13class PL_FOUNDATION_DLL plJSONReader : public plJSONParser
14{
15public:
16 enum class ElementType : plInt8
17 {
18 None,
19 Dictionary,
20 Array,
21 };
22
24
27 plResult Parse(plStreamReader& ref_input, plUInt32 uiFirstLineOffset = 0);
28
30 const plVariantDictionary& GetTopLevelObject() const { return m_Stack.PeekBack().m_Dictionary; }
31
33 const plVariantArray& GetTopLevelArray() const { return m_Stack.PeekBack().m_Array; }
34
36 ElementType GetTopLevelElementType() const { return m_Stack.PeekBack().m_Mode; }
37
38private:
40 virtual bool OnVariable(plStringView sVarName) override;
41
43 virtual void OnReadValue(plStringView sValue) override;
44
46 virtual void OnReadValue(double fValue) override;
47
49 virtual void OnReadValue(bool bValue) override;
50
52 virtual void OnReadValueNULL() override;
53
55 virtual void OnBeginObject() override;
56
58 virtual void OnEndObject() override;
59
61 virtual void OnBeginArray() override;
62
64 virtual void OnEndArray() override;
65
66 virtual void OnParsingError(plStringView sMessage, bool bFatal, plUInt32 uiLine, plUInt32 uiColumn) override;
67
68protected:
69 struct Element
70 {
71 plString m_sName;
72 ElementType m_Mode = ElementType::None;
73 plVariantArray m_Array;
74 plVariantDictionary m_Dictionary;
75 };
76
78
79 bool m_bParsingError = false;
80 plString m_sLastName;
81};
T & PeekBack()
Returns the last element of the array.
Definition ArrayBase_inl.h:388
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
A low level JSON parser that can incrementally parse the structure of a JSON document.
Definition JSONParser.h:13
virtual void OnEndArray()=0
Called when the end of an array is encountered.
virtual void OnBeginArray()=0
Called when a new array is encountered.
virtual void OnParsingError(plStringView sMessage, bool bFatal, plUInt32 uiLine, plUInt32 uiColumn)
Called when something unexpected is encountered in the JSON document.
Definition JSONParser.h:98
virtual bool OnVariable(plStringView sVarName)=0
Called whenever a new variable is encountered. The variable name is passed along. At this point the t...
virtual void OnBeginObject()=0
Called when a new object is encountered.
virtual void OnEndObject()=0
Called when the end of an object is encountered.
virtual void OnReadValueNULL()=0
Called whenever a new value is read.
virtual void OnReadValue(plStringView sValue)=0
Called whenever a new value is read.
This JSON reader will read an entire JSON document into a hierarchical structure of plVariants.
Definition JSONReader.h:14
ElementType GetTopLevelElementType() const
Returns whether the top level element is an array or an object.
Definition JSONReader.h:36
const plVariantArray & GetTopLevelArray() const
Returns the top-level array of the JSON document.
Definition JSONReader.h:33
ElementType
Definition JSONReader.h:17
const plVariantDictionary & GetTopLevelObject() const
Returns the top-level object of the JSON document.
Definition JSONReader.h:30
Interface for binary in (read) streams.
Definition Stream.h:22
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
Definition JSONReader.h:70
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54