modify Json::dump to preserve our format
This commit is contained in:
parent
7bef1eb1e1
commit
12614a174a
2 changed files with 64 additions and 25 deletions
|
@ -56,11 +56,16 @@
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QTextStream>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
|
|
||||||
|
// temp
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#if _MSC_VER <= 1800 // VS 2013
|
#if _MSC_VER <= 1800 // VS 2013
|
||||||
#ifndef noexcept
|
#ifndef noexcept
|
||||||
|
@ -158,10 +163,14 @@ public:
|
||||||
const Json & operator[](const QString &key) const;
|
const Json & operator[](const QString &key) const;
|
||||||
|
|
||||||
// Serialize.
|
// Serialize.
|
||||||
void dump(QString &out) const;
|
void dump(QString &out, int *) const;
|
||||||
QString dump() const {
|
QString dump(int *indent = nullptr) const {
|
||||||
QString out;
|
QString out;
|
||||||
dump(out);
|
if (!indent) {
|
||||||
|
int temp = 0;
|
||||||
|
indent = &temp;
|
||||||
|
}
|
||||||
|
dump(out, indent);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,6 +208,24 @@ private:
|
||||||
std::shared_ptr<JsonValue> m_ptr;
|
std::shared_ptr<JsonValue> m_ptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class JsonDoc {
|
||||||
|
public:
|
||||||
|
JsonDoc(Json *object) {
|
||||||
|
this->m_obj = object;
|
||||||
|
this->m_indent = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
void dump(QFile *file) {
|
||||||
|
QTextStream fileStream(file);
|
||||||
|
fileStream << m_obj->dump(&m_indent);
|
||||||
|
fileStream << "\n"; // pad file with newline
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Json *m_obj;
|
||||||
|
int m_indent;
|
||||||
|
};
|
||||||
|
|
||||||
// Internal class hierarchy - JsonValue objects are not exposed to users of this API.
|
// Internal class hierarchy - JsonValue objects are not exposed to users of this API.
|
||||||
class JsonValue {
|
class JsonValue {
|
||||||
protected:
|
protected:
|
||||||
|
@ -208,7 +235,7 @@ protected:
|
||||||
virtual Json::Type type() const = 0;
|
virtual Json::Type type() const = 0;
|
||||||
virtual bool equals(const JsonValue * other) const = 0;
|
virtual bool equals(const JsonValue * other) const = 0;
|
||||||
virtual bool less(const JsonValue * other) const = 0;
|
virtual bool less(const JsonValue * other) const = 0;
|
||||||
virtual void dump(QString &out) const = 0;
|
virtual void dump(QString &out, int *indent) const = 0;
|
||||||
virtual double number_value() const;
|
virtual double number_value() const;
|
||||||
virtual int int_value() const;
|
virtual int int_value() const;
|
||||||
virtual bool bool_value() const;
|
virtual bool bool_value() const;
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
namespace poryjson {
|
namespace poryjson {
|
||||||
|
|
||||||
static const int max_depth = 200;
|
static const int max_depth = 200;
|
||||||
|
@ -48,11 +50,11 @@ struct NullStruct {
|
||||||
* Serialization
|
* Serialization
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void dump(NullStruct, QString &out) {
|
static void dump(NullStruct, QString &out, int *) {
|
||||||
out += "null";
|
out += "null";
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump(double value, QString &out) {
|
static void dump(double value, QString &out, int *) {
|
||||||
if (std::isfinite(value)) {
|
if (std::isfinite(value)) {
|
||||||
char buf[32];
|
char buf[32];
|
||||||
snprintf(buf, sizeof buf, "%.17g", value);
|
snprintf(buf, sizeof buf, "%.17g", value);
|
||||||
|
@ -62,17 +64,17 @@ static void dump(double value, QString &out) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump(int value, QString &out) {
|
static void dump(int value, QString &out, int *) {
|
||||||
char buf[32];
|
char buf[32];
|
||||||
snprintf(buf, sizeof buf, "%d", value);
|
snprintf(buf, sizeof buf, "%d", value);
|
||||||
out += buf;
|
out += buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump(bool value, QString &out) {
|
static void dump(bool value, QString &out, int *) {
|
||||||
out += value ? "true" : "false";
|
out += value ? "true" : "false";
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump(const QString &value, QString &out) {
|
static void dump(const QString &value, QString &out, int *) {
|
||||||
out += '"';
|
out += '"';
|
||||||
for (int i = 0; i < value.length(); i++) {
|
for (int i = 0; i < value.length(); i++) {
|
||||||
const char ch = value[i].unicode();
|
const char ch = value[i].unicode();
|
||||||
|
@ -109,34 +111,44 @@ static void dump(const QString &value, QString &out) {
|
||||||
out += '"';
|
out += '"';
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump(const Json::array &values, QString &out) {
|
static void dump(const Json::array &values, QString &out, int *indent) {
|
||||||
bool first = true;
|
bool first = true;
|
||||||
out += "[";
|
if (!out.endsWith(": ")) out += QString(*indent * 2, ' ');
|
||||||
|
out += "[\n";
|
||||||
|
*indent += 1;
|
||||||
for (const auto &value : values) {
|
for (const auto &value : values) {
|
||||||
if (!first)
|
if (!first) {
|
||||||
out += ", ";
|
out += ",\n";
|
||||||
value.dump(out);
|
}
|
||||||
|
value.dump(out, indent);
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
out += "]";
|
*indent -= 1;
|
||||||
|
out += "\n" + QString(*indent * 2, ' ') + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump(const Json::object &values, QString &out) {
|
static void dump(const Json::object &values, QString &out, int *indent) {
|
||||||
bool first = true;
|
bool first = true;
|
||||||
out += "{";
|
if (!out.endsWith(": ")) out += QString(*indent * 2, ' ');
|
||||||
|
out += "{\n";
|
||||||
|
*indent += 1;
|
||||||
for (const auto &kv : values) {
|
for (const auto &kv : values) {
|
||||||
if (!first)
|
if (!first) {
|
||||||
out += ", ";
|
out += ",\n";
|
||||||
dump(kv.first, out);
|
}
|
||||||
|
out += QString(*indent * 2, ' ');
|
||||||
|
dump(kv.first, out, indent);
|
||||||
out += ": ";
|
out += ": ";
|
||||||
kv.second.dump(out);
|
kv.second.dump(out, indent);
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
out += "}";
|
*indent -= 1;
|
||||||
|
out += "\n" + QString(*indent * 2, ' ') + "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Json::dump(QString &out) const {
|
|
||||||
m_ptr->dump(out);
|
void Json::dump(QString &out, int *indent) const {
|
||||||
|
m_ptr->dump(out, indent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* * * * * * * * * * * * * * * * * * * *
|
/* * * * * * * * * * * * * * * * * * * *
|
||||||
|
@ -165,7 +177,7 @@ protected:
|
||||||
}
|
}
|
||||||
|
|
||||||
const T m_value;
|
const T m_value;
|
||||||
void dump(QString &out) const override { poryjson::dump(m_value, out); }
|
void dump(QString &out, int *indent) const override { poryjson::dump(m_value, out, indent); }
|
||||||
};
|
};
|
||||||
|
|
||||||
class JsonDouble final : public Value<Json::NUMBER, double> {
|
class JsonDouble final : public Value<Json::NUMBER, double> {
|
||||||
|
|
Loading…
Reference in a new issue