16 #ifndef TIANCHI_JSON_H
17 #define TIANCHI_JSON_H
26 #include <QStringList>
28 TIANCHI_BEGIN_NAMESPACE
42 vtfBool = 4, vtfDateTime=5, vtfNull=6, vtfArray=99 };
47 static QString quote(
const QString& Text);
48 static QString dequote(
const QString& S);
54 QMap<QString, JSONObject*> m_list;
60 QString jsonKey()
const;
61 QString jsonValue()
const;
64 const wchar_t* fromObject(
const QString& Key,
const wchar_t* ptr);
65 const wchar_t* fromArray(
const wchar_t* ptr);
67 inline QString newKey(
const QString& key)
68 {
return key.trimmed().isEmpty() ? QString::number(m_index++) : key.trimmed(); }
70 const wchar_t* parseKey(QString& Value,
const wchar_t* ptr);
71 const wchar_t* parseValue(QString Key,
const wchar_t* ptr);
72 const wchar_t* parseNumber(QString& Value,
const wchar_t* ptr);
73 static inline const wchar_t* skip(
const wchar_t* in)
75 while(in && *in && *in<=32)
83 int type()
const {
return m_type; }
84 void setType(
int value) { m_type = (ValueTypeFlag) value; }
87 void setKey(
const QString& value);
88 QVariant value()
const;
89 inline QVariant value(
const QString& key)
const
90 { QVariant ret; value(key, ret);
return ret; }
91 bool value(
const QString& key, QVariant& v)
const;
92 inline bool exists(
const QString& key)
const
94 QMap<QString, JSONObject*>::const_iterator child = m_list.find(key);
95 return child != m_list.constEnd() && child.key() == key;
97 inline int count()
const {
return m_list.count(); }
98 inline JSONObject*
get(
const QString& key)
const
99 {
return m_list[key]; }
100 QList<JSONObject*> children()
const;
106 inline JSONObject* add(
const QString& key,
const char* value) {
return add(key, QString(value)); }
107 JSONObject* add(
const QString& key,
const QString& value);
108 JSONObject* add(
const QString& key,
int value);
109 JSONObject* add(
const QString& key,
double value);
110 JSONObject* add(
const QString& key,
bool value);
111 JSONObject* add(
const QString& key, QDateTime value);
114 inline JSONObject* add(
const char* value) {
return add(QString(value)); }
115 inline JSONObject* add(
const QString& value) {
return add(QString::number(m_index++), value); }
116 inline JSONObject* add(
int value) {
return add(QString::number(m_index++), value); }
117 inline JSONObject* add(
double value) {
return add(QString::number(m_index++), value); }
118 inline JSONObject* add(
bool value) {
return add(QString::number(m_index++), value); }
119 inline JSONObject* add(QDateTime value) {
return add(QString::number(m_index++), value); }
121 void deleteChildren(
const QString& key);
122 void clearChildren();
125 virtual QString toString()
const;
127 bool fromString(QString jsonText);
131 TIANCHI_END_NAMESPACE
133 #endif // TIANCHI_JSON_H