static int64_t GetJsonInt(KonohaContext *kctx, struct JsonBuf *jsonbuf, const char *key, size_t keylen_or_zero, int64_t defval) { JSON json = AsJSON(jsonbuf); if(key == NULL) { if(JSON_TYPE_CHECK(Int32, json)) { return JSONInt_get(json); } else if(JSON_TYPE_CHECK(Int64, json)) { return JSONInt64_get(json); } else { return 0; } } return JSON_getInt(json, key, KeyLen(key, keylen_or_zero)); }
static size_t DoJsonEach(KonohaContext *kctx, struct JsonBuf *jsonbuf, void *thunk, void (*doEach)(KonohaContext *, const char *, size_t, struct JsonBuf *, void *)) { size_t count = 0; struct JsonBuf eachbuf; JSON Key, Val; JSONObject_iterator Itr; JSON obj = AsJSON(jsonbuf); JSON_OBJECT_EACH(obj, Itr, Key, Val) { const char *key = JSONString_get(Key); size_t len = JSONString_length(Key); eachbuf.json_i = Val.bits; doEach(kctx, key, len, &eachbuf, thunk); count++; } return count; }
static kbool_t SetJsonValue(KonohaContext *kctx, struct JsonBuf *jsonbuf, const char *key, size_t keylen_or_zero, KJSONTYPE type, ...) { JSON val; kbool_t ret = true; va_list ap; va_start(ap, type); val = toJSON(ValueP(NewJsonI((JSONMemoryPool *)(JSONAPI JsonHandler), type, ap))); if(key != NULL) { size_t keylen = KeyLen(key, keylen_or_zero); JSONObject_set((JSONMemoryPool *)(JSONAPI JsonHandler), AsJSON(jsonbuf), key, keylen, val); } else { jsonbuf->json_i = val.bits; } va_end(ap); return ret; }
String AsJSON(const Value& v, const String& sep, bool pretty) { String r; if(v.GetType() == VALUEMAP_V) { r << "{"; String sep1; if(pretty) { r << "\r\n"; sep1 = sep + '\t'; } ValueMap m = v; ValueArray va = m.GetValues(); for(int i = 0; i < m.GetCount(); i++) { if(i) { r << ","; if(pretty) r << "\r\n"; } if(pretty) r << sep1; r << AsJSON((String)m.GetKey(i)) << (pretty ? ": " : ":") << AsJSON(va[i], sep1, pretty); } if(pretty) r << "\r\n" << sep; r << "}"; return r; } if(v.GetType() == VALUEARRAY_V) { r << "["; String sep1; if(pretty) { r << "\r\n"; sep1 = sep + '\t'; } ValueArray va = v; for(int i = 0; i < va.GetCount(); i++) { if(i) { r << ","; if(pretty) r << "\r\n"; } if(pretty) r << sep1; r << AsJSON(va[i], sep1, pretty); } if(pretty) r << "\r\n" << sep; r << "]"; return r; } if(IsNumber(v) && IsNull(v)) return "null"; if(v.GetType() == INT_V) return Format("%d", (int)v); if(v.GetType() == BOOL_V) return (bool)v ? "true" : "false"; if(IsNumber(v)) return Format("%.16g", (double)v); if(IsString(v)) return AsCString((String)v, INT_MAX, NULL, ASCSTRING_JSON); if(IsDateTime(v)) return AsJSON((Time)v); if(IsNull(v)) return "null"; NEVER_("Non-JSON value in AsJSON: " + v.GetTypeName()); return "null"; }
String AsJSON(Date dt) { return AsJSON(ToTime(dt)); }
String AsJSON(const Value& v, bool pretty) { return AsJSON(v, String(), pretty); }
static size_t GetJsonSize(KonohaContext *kctx, struct JsonBuf *jsonbuf) { return JSON_length(AsJSON(jsonbuf)); }
static const char *JsonToNewText(KonohaContext *kctx, struct JsonBuf *jsonbuf) { size_t length; const char *text = JSON_toStringWithLength(AsJSON(jsonbuf), &length); return text; /* need free */ }
static void FreeJson(KonohaContext *kctx, struct JsonBuf *jsonbuf) { JSON_free(AsJSON(jsonbuf)); }