|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/* <editor-fold desc="MIT License"> |
| 4 | +
|
| 5 | +Copyright(c) 2025 Robert Osfield |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8 | +this software and associated documentation files (the "Software"), to deal in |
| 9 | +the Software without restriction, including without limitation the rights to |
| 10 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 11 | +the Software, and to permit persons to whom the Software is furnished to do so, |
| 12 | +subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shimages be included in images |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 19 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 20 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 21 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | +
|
| 24 | +</editor-fold> */ |
| 25 | + |
| 26 | +#include <vsg/io/ReaderWriter.h> |
| 27 | +#include <vsg/io/mem_stream.h> |
| 28 | + |
| 29 | +namespace vsg |
| 30 | +{ |
| 31 | + |
| 32 | + /// JSON parser based on spec: https://www.json.org/json-en.html |
| 33 | + struct JSONParser |
| 34 | + { |
| 35 | + std::string buffer; |
| 36 | + std::size_t pos = 0; |
| 37 | + vsg::mem_stream mstr; |
| 38 | + vsg::indentation indent; |
| 39 | + |
| 40 | + JSONParser(); |
| 41 | + |
| 42 | + inline bool white_space(char c) const |
| 43 | + { |
| 44 | + return (c == ' ' || c == '\t' || c == '\r' || c == '\n'); |
| 45 | + } |
| 46 | + |
| 47 | + bool read_string(std::string& value); |
| 48 | + |
| 49 | + vsg::ref_ptr<vsg::Object> read_array(); |
| 50 | + vsg::ref_ptr<vsg::Object> read_object(); |
| 51 | + }; |
| 52 | + VSG_type_name(vsg::JSONParser); |
| 53 | + |
| 54 | + /// json ReaderWriter |
| 55 | + class json : public vsg::Inherit<vsg::ReaderWriter, json> |
| 56 | + { |
| 57 | + public: |
| 58 | + json(); |
| 59 | + |
| 60 | + vsg::ref_ptr<vsg::Object> read(const vsg::Path&, vsg::ref_ptr<const vsg::Options>) const override; |
| 61 | + vsg::ref_ptr<vsg::Object> read(std::istream&, vsg::ref_ptr<const vsg::Options>) const override; |
| 62 | + vsg::ref_ptr<vsg::Object> read(const uint8_t* ptr, size_t size, vsg::ref_ptr<const vsg::Options> options = {}) const override; |
| 63 | + |
| 64 | + vsg::ref_ptr<vsg::Object> _read(std::istream&, vsg::ref_ptr<const vsg::Options>) const; |
| 65 | + |
| 66 | + bool supportedExtension(const vsg::Path& ext) const; |
| 67 | + |
| 68 | + bool getFeatures(Features& features) const override; |
| 69 | + }; |
| 70 | + VSG_type_name(vsg::json); |
| 71 | + |
| 72 | +} // namespace vsg |
0 commit comments