2015-06-21 09:25:45 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <rapidjson/filewritestream.h>
|
2015-07-14 05:25:49 +02:00
|
|
|
#include <rapidjson/filereadstream.h>
|
2016-04-28 04:33:06 +02:00
|
|
|
#include <rapidjson/prettywriter.h>
|
2015-07-14 05:25:49 +02:00
|
|
|
#include <rapidjson/document.h>
|
2015-06-21 09:25:45 +02:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstddef>
|
2015-07-14 03:09:07 +02:00
|
|
|
#include <string>
|
2015-06-21 09:25:45 +02:00
|
|
|
|
|
|
|
namespace base
|
|
|
|
{
|
|
|
|
class JsonWriterFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JsonWriterFile(FILE* fp, char* buffer, std::size_t length);
|
2015-06-26 07:10:46 +02:00
|
|
|
~JsonWriterFile();
|
2015-06-21 09:25:45 +02:00
|
|
|
|
|
|
|
FILE* GetFp() const;
|
|
|
|
void Flush();
|
2015-07-06 05:42:08 +02:00
|
|
|
void Reset();
|
2015-06-21 09:25:45 +02:00
|
|
|
bool IsComplete() const;
|
|
|
|
|
|
|
|
void StartObject(const char* name = nullptr);
|
|
|
|
void EndObject();
|
|
|
|
|
|
|
|
void StartArray(const char* name = nullptr);
|
|
|
|
void EndArray();
|
|
|
|
|
|
|
|
void WriteNull(const char* name);
|
|
|
|
void WriteBool(const char* name, bool value);
|
2015-07-13 02:51:25 +02:00
|
|
|
void WriteChar(const char* name, char value);
|
2015-06-21 09:25:45 +02:00
|
|
|
void WriteInt32(const char* name, std::int32_t value);
|
2015-07-06 05:42:08 +02:00
|
|
|
void WriteInt32(const char* name, std::int32_t value, bool writeCondition);
|
2015-06-21 09:25:45 +02:00
|
|
|
void WriteInt64(const char* name, std::int64_t value);
|
|
|
|
void WriteUInt32(const char* name, std::uint32_t value);
|
2015-07-06 05:42:08 +02:00
|
|
|
void WriteUInt32(const char* name, std::uint32_t value, bool writeCondition);
|
2015-06-21 09:25:45 +02:00
|
|
|
void WriteUint64(const char* name, std::uint64_t value);
|
|
|
|
void WriteString(const char* name, const char* value);
|
2015-09-01 05:07:51 +02:00
|
|
|
void WriteString(const char* name, const char* value, std::uint32_t length);
|
2015-07-14 03:09:07 +02:00
|
|
|
void WriteString(const char* name, const std::string& value);
|
2015-06-21 09:25:45 +02:00
|
|
|
void WriteFloat(const char* name, const double value);
|
|
|
|
|
|
|
|
void WriteBits(const char* name, const unsigned char* data, std::size_t numBits);
|
|
|
|
void WriteBytes(const char* name, const unsigned char* data, std::size_t numBytes);
|
|
|
|
|
|
|
|
private:
|
|
|
|
rapidjson::FileWriteStream m_fileStream;
|
2016-04-28 04:33:06 +02:00
|
|
|
rapidjson::PrettyWriter<rapidjson::FileWriteStream, rapidjson::ASCII<>> m_writer;
|
2015-06-21 09:25:45 +02:00
|
|
|
FILE* m_fp;
|
|
|
|
};
|
2015-07-14 05:25:49 +02:00
|
|
|
|
2015-09-01 04:16:06 +02:00
|
|
|
class JsonReaderObject
|
2015-07-14 05:25:49 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using JsonValue = rapidjson::GenericValue<rapidjson::ASCII<>>;
|
|
|
|
|
2015-09-01 04:16:06 +02:00
|
|
|
class JsonReaderIterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit JsonReaderIterator(JsonValue* value, bool& hasReadError);
|
2015-07-14 05:25:49 +02:00
|
|
|
|
2015-09-01 04:16:06 +02:00
|
|
|
JsonReaderObject operator*() const;
|
|
|
|
JsonReaderIterator& operator++();
|
|
|
|
bool operator==(const JsonReaderIterator& other) const;
|
|
|
|
bool operator!=(const JsonReaderIterator& other) const;
|
2015-07-14 05:25:49 +02:00
|
|
|
|
2015-09-01 04:16:06 +02:00
|
|
|
private:
|
|
|
|
JsonValue* m_value;
|
|
|
|
bool& m_hasReadError;
|
|
|
|
};
|
2015-07-14 05:25:49 +02:00
|
|
|
|
2015-09-01 04:16:06 +02:00
|
|
|
class JsonReaderArray
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit JsonReaderArray(JsonValue& value, bool& parseError);
|
2015-07-14 05:25:49 +02:00
|
|
|
|
2015-09-01 04:16:06 +02:00
|
|
|
bool HasReadError() const;
|
2015-07-14 05:25:49 +02:00
|
|
|
|
2015-09-01 04:16:06 +02:00
|
|
|
std::size_t size() const;
|
|
|
|
JsonReaderIterator begin();
|
|
|
|
JsonReaderIterator end();
|
2015-07-14 05:25:49 +02:00
|
|
|
|
2015-09-01 04:16:06 +02:00
|
|
|
template<typename Container, typename Fn>
|
|
|
|
void TransformTo(Container& c, Fn fn)
|
2015-07-14 05:25:49 +02:00
|
|
|
{
|
2015-09-01 04:16:06 +02:00
|
|
|
c.resize(m_value.Size());
|
|
|
|
std::size_t index = 0;
|
|
|
|
for (base::JsonReaderObject obj : *this)
|
|
|
|
{
|
|
|
|
fn(obj, c[index++]);
|
|
|
|
}
|
2015-07-14 05:25:49 +02:00
|
|
|
}
|
|
|
|
|
2015-09-01 04:16:06 +02:00
|
|
|
private:
|
|
|
|
JsonValue& m_value;
|
|
|
|
bool& m_hasReadError;
|
|
|
|
};
|
2015-07-14 05:25:49 +02:00
|
|
|
|
|
|
|
public:
|
2015-07-16 05:25:22 +02:00
|
|
|
explicit JsonReaderObject(JsonValue& value, bool& parseError);
|
2015-07-14 05:25:49 +02:00
|
|
|
|
|
|
|
bool HasReadError() const;
|
|
|
|
|
|
|
|
JsonReaderObject ReadObject(const char* name) const;
|
|
|
|
JsonReaderArray ReadArray(const char* name) const;
|
|
|
|
|
|
|
|
bool ReadBool(const char* name);
|
|
|
|
char ReadChar(const char* name);
|
|
|
|
std::int32_t ReadInt32(const char* name);
|
|
|
|
std::int64_t ReadInt64(const char* name);
|
|
|
|
std::uint32_t ReadUInt32(const char* name);
|
|
|
|
std::uint64_t ReadUint64(const char* name);
|
2015-09-01 05:07:51 +02:00
|
|
|
std::uint32_t ReadString(const char* name, char* dest, std::uint32_t maxLength);
|
|
|
|
//std::string ReadString(const char* name, std::uint32_t maxLength);
|
2015-07-14 05:25:49 +02:00
|
|
|
float ReadFloat(const char* name);
|
|
|
|
|
|
|
|
std::size_t ReadBits(const char* name, unsigned char* dest, std::size_t numBits);
|
|
|
|
std::size_t ReadBytes(const char* name, unsigned char* dest, std::size_t numBytes);
|
|
|
|
|
|
|
|
private:
|
|
|
|
JsonValue& m_value;
|
2015-07-16 05:25:22 +02:00
|
|
|
bool& m_hasReadError;
|
2015-07-14 05:25:49 +02:00
|
|
|
};
|
|
|
|
|
2015-09-01 04:16:06 +02:00
|
|
|
using JsonReaderIterator = JsonReaderObject::JsonReaderIterator;
|
|
|
|
using JsonReaderArray = JsonReaderObject::JsonReaderArray;
|
|
|
|
|
2015-07-14 05:25:49 +02:00
|
|
|
class JsonReaderFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JsonReaderFile(FILE* fp, char* buffer, std::size_t length);
|
|
|
|
|
|
|
|
JsonReaderObject ParseObject();
|
|
|
|
|
2015-07-16 05:25:22 +02:00
|
|
|
bool HasParseError() const;
|
|
|
|
bool HasReadError() const;
|
|
|
|
|
2015-07-14 05:25:49 +02:00
|
|
|
private:
|
|
|
|
rapidjson::FileReadStream m_fileStream;
|
|
|
|
rapidjson::GenericDocument<rapidjson::ASCII<>> m_document;
|
2015-07-16 05:25:22 +02:00
|
|
|
bool m_hasParseError;
|
|
|
|
bool m_hasReadError;
|
2015-07-14 05:25:49 +02:00
|
|
|
};
|
2015-06-21 09:25:45 +02:00
|
|
|
}
|