Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: OpenGIS Simple Features Reference Implementation 4 : * Purpose: Streaming parser for GeoJSON-like FeatureCollection 5 : * Author: Even Rouault <even.rouault at spatialys.com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2017, Even Rouault <even.rouault at spatialys.com> 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #ifndef OGRJSONCOLLECTIONSTREAMING_PARSER_H_INCLUDED 14 : #define OGRJSONCOLLECTIONSTREAMING_PARSER_H_INCLUDED 15 : 16 : #include "cpl_json_streaming_parser.h" 17 : 18 : #include <json.h> // JSON-C 19 : 20 : /************************************************************************/ 21 : /* OGRJSONCollectionStreamingParser */ 22 : /************************************************************************/ 23 : 24 : /** Streaming parser for GeoJSON-like FeatureCollection */ 25 : class OGRJSONCollectionStreamingParser CPL_NON_FINAL 26 : : public CPLJSonStreamingParser 27 : { 28 : bool m_bFirstPass = false; 29 : 30 : int m_nDepth = 0; 31 : bool m_bInFeatures = false; 32 : bool m_bCanEasilyAppend = false; 33 : bool m_bInFeaturesArray = false; 34 : bool m_bInCoordinates = false; 35 : bool m_bInType = false; 36 : bool m_bIsTypeKnown = false; 37 : bool m_bIsFeatureCollection = false; 38 : bool m_bInMeasures = false; 39 : bool m_bInMeasuresEnabled = false; 40 : json_object *m_poRootObj = nullptr; 41 : size_t m_nRootObjMemEstimate = 0; 42 : json_object *m_poCurObj = nullptr; 43 : size_t m_nCurObjMemEstimate = 0; 44 : GUIntBig m_nTotalOGRFeatureMemEstimate = 0; 45 : bool m_bKeySet = false; 46 : std::string m_osCurKey{}; 47 : std::vector<json_object *> m_apoCurObj{}; 48 : std::vector<bool> m_abFirstMember{}; 49 : bool m_bStoreNativeData = false; 50 : std::string m_osJson{}; 51 : size_t m_nMaxObjectSize = 0; 52 : 53 : bool m_bStartFeature = false; 54 : bool m_bEndFeature = false; 55 : 56 : void AppendObject(json_object *poNewObj); 57 : 58 : CPL_DISALLOW_COPY_ASSIGN(OGRJSONCollectionStreamingParser) 59 : 60 : protected: 61 107 : inline bool IsFirstPass() const 62 : { 63 107 : return m_bFirstPass; 64 : } 65 : 66 : virtual void GotFeature(json_object *poObj, bool bFirstPass, 67 : const std::string &osJson) = 0; 68 : virtual void TooComplex() = 0; 69 : 70 : bool m_bHasTopLevelMeasures = false; 71 : 72 : public: 73 : OGRJSONCollectionStreamingParser(bool bFirstPass, bool bStoreNativeData, 74 : size_t nMaxObjectSize); 75 : ~OGRJSONCollectionStreamingParser() override; 76 : 77 : void String(std::string_view) override; 78 : void Number(std::string_view) override; 79 : void Boolean(bool b) override; 80 : void Null() override; 81 : 82 : void StartObject() override; 83 : void EndObject() override; 84 : void StartObjectMember(std::string_view) override; 85 : 86 : void StartArray() override; 87 : void EndArray() override; 88 : void StartArrayMember() override; 89 : 90 : void Exception(const char * /*pszMessage*/) override; 91 : 92 : json_object *StealRootObject(); 93 : 94 126 : inline bool HasTopLevelMeasures() const 95 : { 96 126 : return m_bHasTopLevelMeasures; 97 : } 98 : 99 965 : inline bool IsTypeKnown() const 100 : { 101 965 : return m_bIsTypeKnown; 102 : } 103 : 104 963 : inline bool IsFeatureCollection() const 105 : { 106 963 : return m_bIsFeatureCollection; 107 : } 108 : 109 283 : inline GUIntBig GetTotalOGRFeatureMemEstimate() const 110 : { 111 283 : return m_nTotalOGRFeatureMemEstimate; 112 : } 113 : 114 283 : inline bool CanEasilyAppend() const 115 : { 116 283 : return m_bCanEasilyAppend; 117 : } 118 : 119 1668 : inline void ResetFeatureDetectionState() 120 : { 121 1668 : m_bStartFeature = false; 122 1668 : m_bEndFeature = false; 123 1668 : } 124 : 125 1668 : inline bool IsStartFeature() const 126 : { 127 1668 : return m_bStartFeature; 128 : } 129 : 130 1650 : inline bool IsEndFeature() const 131 : { 132 1650 : return m_bEndFeature; 133 : } 134 : }; 135 : 136 : #endif // OGRJSONCOLLECTIONSTREAMING_PARSER_H_INCLUDED