Skip to content

Commit 276868b

Browse files
Merge pull request #1441 from vsg-dev/json
Initial JSON parser and ReaderWriter.
2 parents 74cb748 + f20536d commit 276868b

5 files changed

Lines changed: 430 additions & 1 deletion

File tree

include/vsg/all.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
4545
#include <vsg/maths/clamp.h>
4646
#include <vsg/maths/color.h>
4747
#include <vsg/maths/common.h>
48+
#include <vsg/maths/mat2.h>
4849
#include <vsg/maths/mat3.h>
4950
#include <vsg/maths/mat4.h>
5051
#include <vsg/maths/plane.h>
@@ -250,6 +251,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
250251
#include <vsg/vk/RenderPass.h>
251252
#include <vsg/vk/ResourceRequirements.h>
252253
#include <vsg/vk/Semaphore.h>
254+
#include <vsg/vk/Slots.h>
253255
#include <vsg/vk/State.h>
254256
#include <vsg/vk/SubmitCommands.h>
255257
#include <vsg/vk/Surface.h>
@@ -274,6 +276,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
274276
#include <vsg/io/VSG.h>
275277
#include <vsg/io/convert_utf.h>
276278
#include <vsg/io/glsl.h>
279+
#include <vsg/io/json.h>
277280
#include <vsg/io/mem_stream.h>
278281
#include <vsg/io/read.h>
279282
#include <vsg/io/read_line.h>

include/vsg/io/json.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

src/vsg/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ set(SOURCES
141141
io/Path.cpp
142142
io/ReaderWriter.cpp
143143
io/VSG.cpp
144+
io/glsl.cpp
145+
io/json.cpp
144146
io/spirv.cpp
145147
io/tile.cpp
146-
io/glsl.cpp
147148
io/txt.cpp
148149
io/read.cpp
149150
io/write.cpp

0 commit comments

Comments
 (0)