#include <string>
#include <sstream>
#include <exception>
-#include <stdexcept>
#include <new>
#define JSON_INIT(obj) do { \
if ((obj) == NULL) \
- throw new std::bad_alloc; \
+ throw std::bad_alloc(); \
m_obj = (obj); \
} while (0)
#define JSON_CHECK_CONTAINER() do { \
if (!json_is_object(m_obj) && !json_is_array(m_obj)) { \
std::string s("JSONObject is not a container"); \
- throw new std::runtime_error(s); \
+ throw JSONException(m_obj); \
} \
} while (0)
#define JSON_CHECK_OBJECT() do { \
if (!json_is_object(m_obj)) { \
std::string s("JSONObject is not a dictionary"); \
- throw new std::runtime_error(s); \
+ throw JSONException(m_obj, JSON_OBJECT); \
} \
} while (0)
#define JSON_CHECK_ARRAY() do { \
if (!json_is_array(m_obj)) { \
- std::string s("JSONObject is not an array"); \
- throw new std::runtime_error(s); \
+ throw JSONException(m_obj, JSON_ARRAY); \
} \
} while (0)
-#define JSON_CHECK(s) do { \
- if ((s) != 0) \
- throw new std::bad_alloc; \
+#define JSON_CHECK(s) do { \
+ if ((s) != 0) \
+ throw JSONException(); \
} while (0)
JSONObject
char *s = json_dumps(m_obj, flags);
if (s == NULL)
- throw new std::bad_alloc;
+ throw std::bad_alloc();
return s;
}
int r = json_dumpf(m_obj, fp, flags);
if (r != 0)
- throw new std::bad_alloc;
+ throw std::bad_alloc();
}
size_t
return json_number_value(m_obj);
}
-bool
-JSONObject::isnull(void) const
-{
- return json_is_null(m_obj);
-}
-
-JSONObject::JSONObject(DDF &ddf)
+JSONObject
+JSONObject::ddf(DDF &ddf)
{
if (ddf.isstruct()) {
DDF elem = ddf.first();
- JSONObject jobj = JSONObject::array();
+ JSONObject jobj = JSONObject::object();
while (!elem.isnull()) {
- JSONObject jtmp(elem);
- jobj.append(jtmp);
+ JSONObject jtmp = JSONObject::ddf(elem);
+ jobj.set(elem.name(), jtmp);
elem = ddf.next();
}
+
+ return jobj;
} else if (ddf.islist()) {
DDF elem = ddf.first();
- JSONObject jobj = JSONObject::object();
+ JSONObject jobj = JSONObject::array();
while (!elem.isnull()) {
- JSONObject jtmp(elem);
- jobj.set(elem.name(), jtmp);
+ JSONObject jtmp = JSONObject::ddf(elem);
+ jobj.append(jtmp);
elem = ddf.next();
}
+
+ return jobj;
} else if (ddf.isstring()) {
- JSONObject(ddf.string());
+ return JSONObject(ddf.string());
} else if (ddf.isint()) {
- JSONObject((json_int_t)ddf.integer());
+ return JSONObject((json_int_t)ddf.integer());
} else if (ddf.isfloat()) {
- JSONObject(ddf.floating());
+ return JSONObject(ddf.floating());
} else if (ddf.isempty() || ddf.ispointer()) {
- JSONObject::object();
+ return JSONObject::object();
} else if (ddf.isnull()) {
- JSONObject::null();
+ return JSONObject::null();
}
std::string s("Unbridgeable DDF object");
- throw new std::runtime_error(s);
+ throw JSONException();
}
DDF
do {
const char *key = iter.key();
DDF value = iter.value().ddf();
- ddf.add(value.name(key));
+ ddf.addmember(key).swap(value);
} while (iter.next());
break;
}
break;
}
- return DDF(NULL);
+ return ddf;
+}
+
+bool JSONObject::isObject(void) const
+{
+ return json_is_object(m_obj);
+}
+
+bool JSONObject::isArray(void) const
+{
+ return json_is_array(m_obj);
+}
+
+bool JSONObject::isString(void) const
+{
+ return json_is_string(m_obj);
+}
+
+bool JSONObject::isInteger(void) const
+{
+ return json_is_integer(m_obj);
+}
+
+bool JSONObject::isNumber(void) const
+{
+ return json_is_number(m_obj);
+}
+
+bool JSONObject::isBoolean(void) const
+{
+ return json_is_boolean(m_obj);
+}
+
+bool JSONObject::isNull(void) const
+{
+ return json_is_null(m_obj);
}
JSONIterator::JSONIterator(const JSONObject &obj)
m_iter = json_object_iter_next(m_obj, m_iter);
return m_iter != NULL;
}
+
+JSONException::JSONException(json_t *obj, json_type type)
+{
+ char *s = NULL;
+ const char *t;
+
+ m_obj = json_incref(obj);
+ m_type = type;
+
+ if (obj != NULL)
+ s = json_dumps(m_obj, 0);
+
+ switch (type) {
+ case JSON_OBJECT: t = "OBJECT"; break;
+ case JSON_ARRAY: t = "ARRAY"; break;
+ case JSON_STRING: t = "STRING"; break;
+ case JSON_INTEGER: t = "INTEGER"; break;
+ case JSON_REAL: t = "REAL"; break;
+ case JSON_TRUE: t = "TRUE"; break;
+ case JSON_FALSE: t = "FALSE"; break;
+ case JSON_NULL: t = "NULL"; break;
+ default: t = "UNKNOWN"; break;
+ }
+
+ if (obj != NULL) {
+ m_reason = "Invalid JSON object: " + std::string(s);
+ if (type != JSON_NULL)
+ m_reason += " (excepted type " + std::string(t) + ")";
+ } else {
+ m_reason = "Internal JSON error";
+ }
+
+ if (s != NULL)
+ GSSEAP_FREE(s);
+}