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 : json_object *m_poRootObj = nullptr; 39 : size_t m_nRootObjMemEstimate = 0; 40 : json_object *m_poCurObj = nullptr; 41 : size_t m_nCurObjMemEstimate = 0; 42 : GUIntBig m_nTotalOGRFeatureMemEstimate = 0; 43 : bool m_bKeySet = false; 44 : std::string m_osCurKey{}; 45 : std::vector<json_object *> m_apoCurObj{}; 46 : std::vector<bool> m_abFirstMember{}; 47 : bool m_bStoreNativeData = false; 48 : std::string m_osJson{}; 49 : size_t m_nMaxObjectSize = 0; 50 : 51 : bool m_bStartFeature = false; 52 : bool m_bEndFeature = false; 53 : 54 : void AppendObject(json_object *poNewObj); 55 : 56 : CPL_DISALLOW_COPY_ASSIGN(OGRJSONCollectionStreamingParser) 57 : 58 : protected: 59 103 : inline bool IsFirstPass() const 60 : { 61 103 : return m_bFirstPass; 62 : } 63 : 64 : virtual void GotFeature(json_object *poObj, bool bFirstPass, 65 : const std::string &osJson) = 0; 66 : virtual void TooComplex() = 0; 67 : 68 : public: 69 : OGRJSONCollectionStreamingParser(bool bFirstPass, bool bStoreNativeData, 70 : size_t nMaxObjectSize); 71 : ~OGRJSONCollectionStreamingParser(); 72 : 73 : virtual void String(const char * /*pszValue*/, size_t) override; 74 : virtual void Number(const char * /*pszValue*/, size_t) override; 75 : virtual void Boolean(bool b) override; 76 : virtual void Null() override; 77 : 78 : virtual void StartObject() override; 79 : virtual void EndObject() override; 80 : virtual void StartObjectMember(const char * /*pszKey*/, size_t) override; 81 : 82 : virtual void StartArray() override; 83 : virtual void EndArray() override; 84 : virtual void StartArrayMember() override; 85 : 86 : virtual void Exception(const char * /*pszMessage*/) override; 87 : 88 : json_object *StealRootObject(); 89 : 90 861 : inline bool IsTypeKnown() const 91 : { 92 861 : return m_bIsTypeKnown; 93 : } 94 : 95 859 : inline bool IsFeatureCollection() const 96 : { 97 859 : return m_bIsFeatureCollection; 98 : } 99 : 100 265 : inline GUIntBig GetTotalOGRFeatureMemEstimate() const 101 : { 102 265 : return m_nTotalOGRFeatureMemEstimate; 103 : } 104 : 105 265 : inline bool CanEasilyAppend() const 106 : { 107 265 : return m_bCanEasilyAppend; 108 : } 109 : 110 1668 : inline void ResetFeatureDetectionState() 111 : { 112 1668 : m_bStartFeature = false; 113 1668 : m_bEndFeature = false; 114 1668 : } 115 : 116 1668 : inline bool IsStartFeature() const 117 : { 118 1668 : return m_bStartFeature; 119 : } 120 : 121 1650 : inline bool IsEndFeature() const 122 : { 123 1650 : return m_bEndFeature; 124 : } 125 : }; 126 : 127 : #endif // OGRJSONCOLLECTIONSTREAMING_PARSER_H_INCLUDED