void testListsMaps() { QCOMPARE(writeJson(QVariantList()), QByteArray("[]")); { QVariantList list; list << 42; QCOMPARE(writeJson(list), QByteArray("[42]")); } QCOMPARE(writeJson(QVariantMap()), QByteArray("{}")); { QVariantMap map; map["def"] = 42; map["abc"] = true; QCOMPARE(writeJson(map), QByteArray("{\"abc\":true,\"def\":42}")); } { QMap<QString,QVariant> map; map["x"] = true; map["y"] = "789"; map["z"] = 1234; QList<QVariant> list; list << 1 << QString::fromLatin1("foo bar' <- quote baz \xe9 <- unicode \t<- tab \" <- dquote") << 1.3 << map << 0 << QVariant(); QCOMPARE(writeJson(list), QByteArray("[1,\"foo bar' <- quote baz \\u00e9 <- unicode \\t<- tab \\\" <- dquote\",1.3," "{\"x\":true,\"y\":\"789\",\"z\":1234},0,null]")); } }
Bytes write(const data::Value &data, EncodeFormat fmt) { if (fmt.isRaw()) { switch (fmt.format) { case EncodeFormat::Json: { String s = writeJson(data, false); Bytes ret; ret.reserve(s.length()); ret.assign(s.begin(), s.end()); return ret; } break; case EncodeFormat::Pretty: { String s = writeJson(data, true); Bytes ret; ret.reserve(s.length()); ret.assign(s.begin(), s.end()); return ret; } break; case EncodeFormat::Cbor: case EncodeFormat::DefaultFormat: return writeCbor(data); break; } } return Bytes(); }
void testStrings() { QCOMPARE(writeJson(QVariant("")), QByteArray("\"\"")); QCOMPARE(writeJson(QVariant("x")), QByteArray("\"x\"")); QCOMPARE(writeJson(QVariant("foo\\bar\"baz")), QByteArray("\"foo\\\\bar\\\"baz\"")); QCOMPARE(writeJson(QVariant("foo'bar")), QByteArray("\"foo'bar\"")); QCOMPARE(writeJson(QString::fromLatin1("-\xe9\t-")), QByteArray("\"-\\u00e9\\t-\"")); }
bool write(std::ostream &stream, const data::Value &data, EncodeFormat fmt) { if (fmt.isRaw()) { switch (fmt.format) { case EncodeFormat::Json: return writeJson(stream, data, false); break; case EncodeFormat::Pretty: return writeJson(stream, data, true); break; case EncodeFormat::Cbor: case EncodeFormat::DefaultFormat: return writeCbor(stream, data); break; } } return false; }
// on "init" you need to initialize your instance bool HelloWorld::init() { if (!Layer::init()) { return false; } writeJson(); return true; }
void jsonWriterFree(void* ptrFileWriter){ FileWriter *ID = (FileWriter*)ptrFileWriter; int i; /* If this FileWriter writes output at terminal(), dump data upon destruction. */ if (ID->dumpAtDestruction){ writeJson(ptrFileWriter, ID->varVals, ID->numKeys); } free(ID->varVals); for (i = 0; i < ID->numKeys; ++i) { free(ID->varKeys[i]); } free(ID->varKeys); freeBase(ptrFileWriter); return; }
void testConsts() { QCOMPARE(writeJson(QVariant()), QByteArray("null")); QCOMPARE(writeJson(QVariant(true)), QByteArray("true")); QCOMPARE(writeJson(QVariant(false)), QByteArray("false")); }
int main(int argc, char *argv[]) { CURL *ch; /* curl handle */ CURLcode rcode; /* curl result code */ struct curl_fetch_st curl_fetch; /* curl fetch struct */ struct curl_fetch_st *cf = &curl_fetch; /* pointer to fetch struct */ struct curl_slist *headers = NULL; /* http headers to send with request */ /* url to test site */ std::string url = "localhost/wec/sample.php"; /* init curl handle */ if ((ch = curl_easy_init()) == NULL) { /* log error */ fprintf(stderr, "ERROR: Failed to create curl handle in fetch_session"); /* return error */ return 1; } /* set content type */ headers = curl_slist_append(headers, "Accept: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json"); std::string json; writeJson(json); /* set curl options */ curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(ch, CURLOPT_POSTFIELDS, json.c_str()); /* fetch page and capture return code */ rcode = curl_fetch_url(ch, url.c_str(), cf); /* cleanup curl handle */ curl_easy_cleanup(ch); /* free headers */ curl_slist_free_all(headers); /* check return code */ if (rcode != CURLE_OK || cf->size < 1) { /* log error */ fprintf(stderr, "ERROR: Failed to fetch url (%s) - curl said: %s", url, curl_easy_strerror(rcode)); /* return error */ return 2; } /* check payload */ if (cf->payload != NULL) { /* print result */ printf("CURL Returned: \n%s\n", cf->payload); /* parse return */ free(cf->payload); } else { /* error */ fprintf(stderr, "ERROR: Failed to populate payload"); /* free payload */ free(cf->payload); /* return */ return 3; } /* exit */ return 0; }
String toString(const data::Value &data, bool pretty) { return writeJson(data, pretty); }