コード例 #1
0
ファイル: test_load_callback.c プロジェクト: Mikhus/jansson
static void run_tests()
{
    struct my_source s;
    json_t *json;
    json_error_t error;

    s.off = 0;
    s.cap = strlen(my_str);
    s.buf = my_str;

    json = json_load_callback(greedy_reader, &s, 0, &error);

    if (!json)
        fail("json_load_callback failed on a valid callback");
    json_decref(json);

    s.off = 0;
    s.cap = strlen(my_str) - 1;
    s.buf = my_str;

    json = json_load_callback(greedy_reader, &s, 0, &error);
    if (json) {
        json_decref(json);
        fail("json_load_callback should have failed on an incomplete stream, but it didn't");
    }
    if (strcmp(error.source, "<callback>") != 0) {
        fail("json_load_callback returned an invalid error source");
    }
    if (strcmp(error.text, "']' expected near end of file") != 0) {
        fail("json_load_callback returned an invalid error message for an unclosed top-level array");
    }

    json = json_load_callback(NULL, NULL, 0, &error);
    if (json) {
        json_decref(json);
        fail("json_load_callback should have failed on NULL load callback, but it didn't");
    }
    if (strcmp(error.text, "wrong arguments") != 0) {
        fail("json_load_callback returned an invalid error message for a NULL load callback");
    }
}
コード例 #2
0
ファイル: ext-json.cpp プロジェクト: ufasoft/el
	pair<VarValue, Blob> ParseStream(Stream& stm, RCSpan preBuf) override {
#if JANSSON_VERSION_HEX >= 0x020400
		CallbackData cbd;
		cbd.LastChunk = preBuf;
		cbd.m_pStm = &stm;
		cbd.LastChunkPos = int(-(ssize_t)preBuf.size());

		json_error_t err;
		if (json_t *json = json_load_callback(&LoadCallback, &cbd, JSON_DISABLE_EOF_CHECK, &err)) {
			int off = err.position - std::max(cbd.LastChunkPos, 0);
			return make_pair(JsonVarValueObj::FromJsonT(json), Blob(cbd.LastChunk.constData() + off, cbd.LastChunk.Size - off));
		} else {
			if (!cbd.Eof)
				JanssonCheck(json, err);
			return make_pair(VarValue(), Blob(nullptr));
		}
#else
		Throw(E_NOTIMPL);
#endif
	}