Nebula
Loading...
Searching...
No Matches
jsonwriter.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
11#include "io/streamwriter.h"
12#include "util/string.h"
13#include "util/stack.h"
14#include "util/bitfield.h"
15#include "util/stringatom.h"
16#include "util/variant.h"
17#include "util/blob.h"
18#include "util/bitfield.h"
19#include <array>
20
21namespace pjson
22{
23 class value_variant;
24 class document;
25}
26
27//------------------------------------------------------------------------------
28namespace IO
29{
31{
33public:
35 JsonWriter();
37 virtual ~JsonWriter();
39 virtual bool Open();
41 virtual void Close();
42
44 void BeginArray(const char * nodeName = nullptr);
45
47 void BeginObject(const char * nodeName = nullptr);
49 void End();
50
52 template <typename T> void Add(const T & value, const Util::String & name = "");
54 template <typename T, size_t N> void Add(std::array<T, N> const& values, const Util::String& name = "");
56 template<unsigned int N> void Add(Util::BitField<N> const& value, const Util::String& name = "");
58 void Add(const char * value, const Util::String & name = "");
60// template <typename T> void Add(const Util::String & name, const T & value);
61
62 /*
64 void AddString(const Util::String & value);
66 void AddBool(bool value);
68 void AddInt(int value);
70 void AddFloat(float value);
72 void AddFloat2(const Math::vec2 & value);
74 void AddVec4(const Math::vec4 & value);
76 void AddMatrix44(const Math::mat4 & value);
78 void AddTransform44(const Math::transform44 & value);
79
81 void SetString(const Util::String& name, const Util::String& value);
83 void SetBool(const Util::String& name, bool value);
85 void SetInt(const Util::String& name, int value);
87 void SetFloat(const Util::String& name, float value);
89 void SetVec2(const Util::String& name, const Math::vec2& value);
91 void SetVec4(const Util::String& name, const Math::vec4& value);
93 void SetMat4(const Util::String& name, const Math::mat4& value);
95 void SetTransform44(const Util::String& name, const Math::transform44& value);
97 template<typename T> void Set(const Util::String& name, const T &value);
98 */
99
100private:
101
102 pjson::document * document;
105};
106
107template<> void JsonWriter::Add(const Util::String& value, const Util::String& name);
108template<> void JsonWriter::Add(const Util::StringAtom& value, const Util::String& name);
109template<> void JsonWriter::Add(const Util::FourCC& value, const Util::String& name);
110template<> void JsonWriter::Add(const bool& value, const Util::String& name);
111template<> void JsonWriter::Add(const char& value, const Util::String& name);
112template<> void JsonWriter::Add(const int8_t& value, const Util::String& name);
113template<> void JsonWriter::Add(const uint8_t& value, const Util::String& name);
114template<> void JsonWriter::Add(const int& value, const Util::String& name);
115template<> void JsonWriter::Add(const unsigned int& value, const Util::String& name);
116template<> void JsonWriter::Add(const uint64_t& value, const Util::String& name);
117template<> void JsonWriter::Add(const int16_t& value, const Util::String& name);
118template<> void JsonWriter::Add(const uint16_t& value, const Util::String& name);
119template<> void JsonWriter::Add(const float& value, const Util::String& name);
120template<> void JsonWriter::Add(const double& value, const Util::String& name);
121template<> void JsonWriter::Add(const Math::vec3& value, const Util::String& name);
122template<> void JsonWriter::Add(const Math::vec4& value, const Util::String& name);
123template<> void JsonWriter::Add(const Math::vec2& value, const Util::String& name);
124template<> void JsonWriter::Add(const Math::mat4& value, const Util::String& name);
125template<> void JsonWriter::Add(const Math::transform44& value, const Util::String& name);
126template<> void JsonWriter::Add(const Util::Variant& value, const Util::String& name);
127template<> void JsonWriter::Add(const Util::Guid& value, const Util::String& name);
128template<> void JsonWriter::Add(const Util::Array<int>& value, const Util::String& name);
129template<> void JsonWriter::Add(const Util::Array<Util::String>& value, const Util::String& name);
130
131//------------------------------------------------------------------------------
134template<typename T, size_t N>
135inline void
136JsonWriter::Add(std::array<T, N> const& values, const Util::String& name)
137{
138 this->BeginArray(name.AsCharPtr());
139 for (T const& value : values)
140 {
141 this->Add<T>(value);
142 }
143 this->End();
144}
145
146//------------------------------------------------------------------------------
149template<unsigned int N>
150inline void
152{
153 Util::Array<int> tempArr;
154 for (uint i = 0; i < N; i++)
155 {
156 if (value.IsSet(i))
157 {
158 tempArr.Append(i);
159 }
160 }
161 this->Add(tempArr, name);
162}
163
164
165} // namespace IO
166//------------------------------------------------------------------------------
JsonWriter()
constructor
Definition jsonwriter.cc:26
__DeclareClass(JsonWriter)
virtual bool Open()
begin writing the stream
Definition jsonwriter.cc:47
pjson::document * document
add a value to current object node
Definition jsonwriter.h:102
void BeginObject(const char *nodeName=nullptr)
begin a new node that can host key-value pairs
Definition jsonwriter.cc:100
virtual void Close()
end writing the stream
Definition jsonwriter.cc:66
void Add(const T &value, const Util::String &name="")
add a value to the current array node
virtual ~JsonWriter()
destructor
Definition jsonwriter.cc:35
void End()
end current object or array
Definition jsonwriter.cc:113
Util::Stack< pjson::value_variant * > hierarchy
Definition jsonwriter.h:103
void BeginArray(const char *nodeName=nullptr)
begin a new array node
Definition jsonwriter.cc:87
Util::Stack< Util::String > nameHierarchy
Definition jsonwriter.h:104
Stream writer classes provide a specialized write-interface for a stream.
Definition streamwriter.h:22
A 4x4 matrix which is described by translation, rotation and scale.
Definition transform44.h:20
Nebula's dynamic array class.
Definition array.h:61
void Append(const TYPE &first, const ELEM_TYPE &... elements)
Append multiple elements to the end of the array.
Definition array.h:1392
Implements large bit field.
Definition bitfield.h:24
constexpr bool IsSet(const uint64_t bitIndex) const
Check if single bit is set.
Definition bitfield.h:186
A four-character-code is a quasi-human-readable 32-bit-id.
Definition fourcc.h:19
Nebula's stack class (a FILO container).
Definition stack.h:19
A StringAtom.
Definition stringatom.h:22
An "any type" variable.
Definition variant.h:32
Instances of wrapped stream classes.
Definition multiplayerfeatureunit.cc:324
Definition jsonreader.h:26
A 4x4 single point precision float matrix.
Definition mat4.h:49
A 2-component float vector class.
Definition vec2.h:21
A 3D vector.
Definition vec3.h:37
A 4D vector.
Definition vec4.h:24
Nebula's universal string class.
Definition String.cs:8
const char * AsCharPtr() const
return contents as character pointer
Definition string.h:572
unsigned int uint
Definition types.h:33