2018-09-09 06:44:48 +08:00
|
|
|
#pragma once
|
2018-08-31 16:21:24 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2018-09-11 04:28:31 +08:00
|
|
|
#include <cmath>
|
2018-09-08 09:26:05 +08:00
|
|
|
#include "flow/flow.h"
|
|
|
|
#include "flow/Trace.h"
|
2018-10-27 04:49:42 +08:00
|
|
|
#include "fdbclient/JSONDoc.h"
|
2018-08-31 16:21:24 +08:00
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
class JsonBuilder;
|
|
|
|
class JsonBuilderObject;
|
|
|
|
class JsonBuilderArray;
|
|
|
|
typedef JsonBuilder JsonString;
|
|
|
|
template <typename T> class JsonBuilderObjectSetter;
|
2018-08-31 16:21:24 +08:00
|
|
|
|
2018-09-10 09:12:41 +08:00
|
|
|
// Class for building JSON string values.
|
2018-09-09 06:44:48 +08:00
|
|
|
// Default value is null, as in the JSON type
|
|
|
|
class JsonBuilder {
|
|
|
|
protected:
|
2018-09-09 09:27:02 +08:00
|
|
|
enum EType { NULLVALUE, OBJECT, ARRAY };
|
2018-08-31 16:21:24 +08:00
|
|
|
|
2018-09-10 13:39:51 +08:00
|
|
|
typedef VectorRef<char> VString;
|
2018-09-09 06:44:48 +08:00
|
|
|
public:
|
|
|
|
// Default value is null, which will be considered "empty"
|
2018-09-10 13:39:51 +08:00
|
|
|
JsonBuilder() : type(NULLVALUE), elements(0), bytes(0) {
|
|
|
|
jsonText.resize(arena, 1);
|
|
|
|
}
|
2018-09-08 22:15:28 +08:00
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
int getFinalLength() const {
|
2018-09-10 09:12:41 +08:00
|
|
|
return bytes + strlen(getEnd());
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
2018-08-31 16:21:24 +08:00
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
// TODO: Remove the need for this by changing usages to steal this's content
|
|
|
|
std::string getJson() const {
|
2018-09-10 09:12:41 +08:00
|
|
|
std::string result;
|
|
|
|
result.reserve(bytes + 1);
|
|
|
|
for(auto& it : jsonText) {
|
|
|
|
result.append(it.begin(), it.end());
|
|
|
|
}
|
|
|
|
result.append(getEnd());
|
|
|
|
return result;
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
2018-08-31 16:21:24 +08:00
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
int size() const {
|
|
|
|
return elements;
|
2018-09-06 03:22:04 +08:00
|
|
|
}
|
2018-08-31 16:21:24 +08:00
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
bool empty() const {
|
|
|
|
return elements == 0;
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
static JsonBuilderObject makeMessage(const char *name, const char *description);
|
2018-09-08 22:15:28 +08:00
|
|
|
|
2018-09-10 18:07:11 +08:00
|
|
|
static int coerceAsciiNumberToJSON(const char *s, int len, char *dst);
|
|
|
|
|
2018-09-08 22:15:28 +08:00
|
|
|
protected:
|
|
|
|
EType type;
|
2018-09-10 09:12:41 +08:00
|
|
|
Arena arena;
|
2018-09-10 13:39:51 +08:00
|
|
|
mutable VectorRef<VString> jsonText;
|
2018-09-09 06:44:48 +08:00
|
|
|
int elements;
|
2018-09-10 09:12:41 +08:00
|
|
|
int bytes;
|
|
|
|
|
2018-09-10 13:39:51 +08:00
|
|
|
// 'raw' write methods
|
2018-09-10 13:47:12 +08:00
|
|
|
inline void write(const char *s, int len) {
|
|
|
|
bytes += len;
|
|
|
|
jsonText.back().append(arena, s, len);
|
2018-09-10 09:12:41 +08:00
|
|
|
}
|
2018-09-08 22:15:28 +08:00
|
|
|
|
2018-09-10 09:12:41 +08:00
|
|
|
inline void write(const char* s) {
|
2018-09-10 13:47:12 +08:00
|
|
|
write(s, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void write(const StringRef &s) {
|
|
|
|
write((char *)s.begin(), s.size());
|
2018-09-10 09:12:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void write(char s) {
|
2018-09-10 13:39:51 +08:00
|
|
|
++bytes;
|
2018-09-10 09:12:41 +08:00
|
|
|
jsonText.back().push_back(arena, s);
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
|
|
|
|
2018-09-10 13:39:51 +08:00
|
|
|
// writeValue() methods write JSON form of the value
|
2018-09-09 06:44:48 +08:00
|
|
|
void writeValue(const json_spirit::mValue &val) {
|
2018-09-10 13:39:51 +08:00
|
|
|
switch(val.type()) {
|
|
|
|
case json_spirit::int_type:
|
|
|
|
return writeValue(val.get_int64());
|
|
|
|
case json_spirit::bool_type:
|
|
|
|
return writeValue(val.get_bool());
|
|
|
|
case json_spirit::real_type:
|
|
|
|
return writeValue(val.get_real());
|
|
|
|
case json_spirit::str_type:
|
|
|
|
return writeValue(val.get_str());
|
|
|
|
default:
|
|
|
|
// Catch-all for objects/arrays
|
|
|
|
return write(json_spirit::write_string(val));
|
|
|
|
};
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
2018-08-31 16:21:24 +08:00
|
|
|
|
2018-09-10 09:12:41 +08:00
|
|
|
void writeValue(const bool& val) {
|
|
|
|
write(val ? "true" : "false");
|
|
|
|
}
|
|
|
|
|
2018-09-10 15:44:09 +08:00
|
|
|
template<typename T> inline void writeFormat(const char *fmt, const T &val) {
|
|
|
|
VString &dst = jsonText.back();
|
|
|
|
const int limit = 30;
|
|
|
|
dst.reserve(arena, dst.size() + limit);
|
|
|
|
int len = snprintf(dst.end(), limit, fmt, val);
|
|
|
|
if(len > 0 && len < limit) {
|
|
|
|
dst.extendUnsafeNoReallocNoInit(len);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
write(format(fmt, val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 09:12:41 +08:00
|
|
|
void writeValue(const int64_t& val) {
|
2018-09-10 15:44:09 +08:00
|
|
|
writeFormat("%lld", val);
|
2018-09-10 09:12:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeValue(const uint64_t& val) {
|
2018-09-10 15:44:09 +08:00
|
|
|
writeFormat("%llu", val);
|
2018-09-10 09:12:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeValue(const int& val) {
|
2018-09-10 15:44:09 +08:00
|
|
|
writeFormat("%d", val);
|
2018-09-10 09:12:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeValue(const double& val) {
|
2018-09-11 04:28:31 +08:00
|
|
|
if(std::isfinite(val)) {
|
|
|
|
writeFormat("%g", val);
|
|
|
|
}
|
|
|
|
else if(std::isnan(val)) {
|
|
|
|
write("-999");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
write("1e99");
|
|
|
|
}
|
2018-09-10 09:12:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldEscape(char c) {
|
|
|
|
switch( c ) {
|
|
|
|
case '"':
|
|
|
|
case '\\':
|
|
|
|
case '\b':
|
|
|
|
case '\f':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\t':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 13:39:51 +08:00
|
|
|
void writeValue(const char *val, int len) {
|
2018-09-10 10:08:54 +08:00
|
|
|
write('"');
|
|
|
|
int beginCopy = 0;
|
2018-09-10 13:39:51 +08:00
|
|
|
VString &dst = jsonText.back();
|
2018-09-10 10:08:54 +08:00
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
if (shouldEscape(val[i])) {
|
2018-09-10 13:39:51 +08:00
|
|
|
dst.append(arena, val + beginCopy, i - beginCopy);
|
2018-09-10 10:08:54 +08:00
|
|
|
beginCopy = i + 1;
|
|
|
|
write('\\');
|
|
|
|
write(val[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(beginCopy < len) {
|
2018-09-10 13:39:51 +08:00
|
|
|
dst.append(arena, val + beginCopy, len - beginCopy);
|
2018-09-10 10:08:54 +08:00
|
|
|
}
|
|
|
|
write('"');
|
|
|
|
}
|
|
|
|
|
2018-09-10 13:39:51 +08:00
|
|
|
inline void writeValue(const std::string& val) {
|
|
|
|
writeValue(val.data(), val.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void writeValue(const char* val) {
|
|
|
|
writeValue(val, strlen(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void writeValue(const StringRef &s) {
|
|
|
|
writeValue((const char *)s.begin(), s.size());
|
|
|
|
}
|
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
// Write the finalized (closed) form of val
|
|
|
|
void writeValue(const JsonBuilder &val) {
|
2018-09-10 09:12:41 +08:00
|
|
|
bytes += val.bytes;
|
2018-09-10 13:39:51 +08:00
|
|
|
jsonText.append(arena, val.jsonText.begin(), val.jsonText.size());
|
|
|
|
val.jsonText.push_back(arena, VString());
|
2018-09-10 09:12:41 +08:00
|
|
|
arena.dependsOn(val.arena);
|
2018-09-09 06:44:48 +08:00
|
|
|
write(val.getEnd());
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
2018-09-09 06:44:48 +08:00
|
|
|
|
2018-09-10 18:07:11 +08:00
|
|
|
void writeCoercedAsciiNumber(const char *s, int len) {
|
|
|
|
VString &val = jsonText.back();
|
2018-09-11 06:20:41 +08:00
|
|
|
val.reserve(arena, val.size() + len + 3);
|
2018-09-10 18:07:11 +08:00
|
|
|
int written = coerceAsciiNumberToJSON(s, len, val.end());
|
|
|
|
if(written > 0) {
|
|
|
|
val.extendUnsafeNoReallocNoInit(written);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
write("-999");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void writeCoercedAsciiNumber(const StringRef &s) {
|
|
|
|
writeCoercedAsciiNumber((const char *)s.begin(), s.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void writeCoercedAsciiNumber(const std::string &s) {
|
|
|
|
writeCoercedAsciiNumber(s.data(), s.size());
|
|
|
|
}
|
|
|
|
|
2018-09-10 13:39:51 +08:00
|
|
|
// Helper function to add contents of another JsonBuilder to this one.
|
|
|
|
// This is only used by the subclasses to combine like-typed (at compile time) objects,
|
|
|
|
// so it can be assumed that the other object has been initialized with an opening character.
|
|
|
|
void _addContents(const JsonBuilder &other) {
|
|
|
|
if(other.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(elements > 0) {
|
|
|
|
write(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add everything but the first byte of the first string in arr
|
|
|
|
bytes += other.bytes - 1;
|
|
|
|
const VString &front = other.jsonText.front();
|
2018-09-10 13:53:12 +08:00
|
|
|
jsonText.push_back(arena, front.slice(1, front.size()));
|
2018-09-10 13:39:51 +08:00
|
|
|
jsonText.append(arena, other.jsonText.begin() + 1, other.jsonText.size() - 1);
|
|
|
|
|
|
|
|
// Both JsonBuilders would now want to write to the same additional VString capacity memory
|
|
|
|
// if they were modified, so force the other (likely to not be modified again) to start a new one.
|
|
|
|
other.jsonText.push_back(arena, VString());
|
|
|
|
|
|
|
|
arena.dependsOn(other.arena);
|
|
|
|
elements += other.elements;
|
|
|
|
}
|
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
// Get the text necessary to finish the JSON string
|
|
|
|
const char * getEnd() const {
|
|
|
|
switch(type) {
|
|
|
|
case NULLVALUE:
|
|
|
|
return "null";
|
|
|
|
case OBJECT:
|
|
|
|
return "}";
|
|
|
|
case ARRAY:
|
|
|
|
return "]";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
};
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
2018-09-09 06:44:48 +08:00
|
|
|
};
|
2018-09-08 22:15:28 +08:00
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
class JsonBuilderArray : public JsonBuilder {
|
|
|
|
public:
|
|
|
|
JsonBuilderArray() {
|
|
|
|
type = ARRAY;
|
2018-09-10 09:12:41 +08:00
|
|
|
write('[');
|
2018-09-09 06:44:48 +08:00
|
|
|
}
|
2018-09-06 17:28:23 +08:00
|
|
|
|
2018-09-11 09:48:30 +08:00
|
|
|
template<typename VT> inline JsonBuilderArray & push_back(const VT &val) {
|
2018-09-09 06:44:48 +08:00
|
|
|
if(elements++ > 0) {
|
2018-09-10 09:12:41 +08:00
|
|
|
write(',');
|
2018-09-09 06:44:48 +08:00
|
|
|
}
|
2018-09-11 09:48:30 +08:00
|
|
|
writeValue(val);
|
2018-09-09 06:44:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonBuilderArray & addContents(const json_spirit::mArray &arr) {
|
|
|
|
for(auto &v : arr) {
|
|
|
|
push_back(v);
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
2018-09-09 06:44:48 +08:00
|
|
|
return *this;
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
2018-09-09 06:44:48 +08:00
|
|
|
|
|
|
|
JsonBuilderArray & addContents(const JsonBuilderArray &arr) {
|
2018-09-10 13:39:51 +08:00
|
|
|
_addContents(arr);
|
2018-09-09 06:44:48 +08:00
|
|
|
return *this;
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
2018-09-09 06:44:48 +08:00
|
|
|
};
|
2018-09-08 22:15:28 +08:00
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
class JsonBuilderObject : public JsonBuilder {
|
2018-09-08 22:15:28 +08:00
|
|
|
public:
|
2018-09-09 06:44:48 +08:00
|
|
|
JsonBuilderObject() {
|
|
|
|
type = OBJECT;
|
2018-09-10 09:12:41 +08:00
|
|
|
write('{');
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
|
|
|
|
2018-09-11 09:48:30 +08:00
|
|
|
template<typename KT, typename VT> inline JsonBuilderObject & setKey(const KT &name, const VT &val) {
|
2018-09-09 06:44:48 +08:00
|
|
|
if(elements++ > 0) {
|
2018-09-10 09:12:41 +08:00
|
|
|
write(',');
|
2018-09-09 06:44:48 +08:00
|
|
|
}
|
2018-09-10 09:12:41 +08:00
|
|
|
write('"');
|
|
|
|
write(name);
|
2018-09-10 15:56:16 +08:00
|
|
|
write("\":");
|
2018-09-11 09:48:30 +08:00
|
|
|
writeValue(val);
|
2018-09-09 06:44:48 +08:00
|
|
|
return *this;
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
|
|
|
|
2018-09-11 09:48:30 +08:00
|
|
|
template<typename KT, typename VT> inline JsonBuilderObject & setKeyRawNumber(const KT &name, const VT &val) {
|
2018-09-10 14:19:00 +08:00
|
|
|
if(elements++ > 0) {
|
|
|
|
write(',');
|
|
|
|
}
|
|
|
|
write('"');
|
|
|
|
write(name);
|
2018-09-10 15:56:16 +08:00
|
|
|
write("\":");
|
2018-09-10 18:07:11 +08:00
|
|
|
writeCoercedAsciiNumber(val);
|
2018-09-10 14:19:00 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
template<typename T> inline JsonBuilderObjectSetter<T> operator[](T &&name);
|
|
|
|
|
|
|
|
JsonBuilderObject & addContents(const json_spirit::mObject &obj) {
|
|
|
|
for(auto &kv : obj) {
|
|
|
|
setKey(kv.first, kv.second);
|
|
|
|
}
|
|
|
|
return *this;
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
JsonBuilderObject & addContents(const JsonBuilderObject &obj) {
|
2018-09-10 13:39:51 +08:00
|
|
|
_addContents(obj);
|
2018-09-09 06:44:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2018-09-10 15:22:27 +08:00
|
|
|
|
2018-09-08 22:15:28 +08:00
|
|
|
};
|
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
// Template is the key name, accepted as an r-value if possible to avoid copying if it's a string
|
2018-09-08 22:15:28 +08:00
|
|
|
template<typename KT>
|
2018-09-09 06:44:48 +08:00
|
|
|
class JsonBuilderObjectSetter {
|
2018-09-08 22:15:28 +08:00
|
|
|
public:
|
2018-09-09 06:44:48 +08:00
|
|
|
JsonBuilderObjectSetter(JsonBuilderObject &dest, KT &&name) : dest(dest), name(std::forward<KT>(name)) {}
|
2018-09-08 22:15:28 +08:00
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
// Value is accepted as an rvalue if possible
|
2018-09-11 09:48:30 +08:00
|
|
|
template <class VT> inline void operator=(const VT &value) {
|
|
|
|
dest.setKey(name, value);
|
2018-09-08 22:15:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-09-09 06:44:48 +08:00
|
|
|
JsonBuilderObject& dest;
|
2018-09-08 22:15:28 +08:00
|
|
|
KT name;
|
|
|
|
};
|
|
|
|
|
2018-09-09 06:44:48 +08:00
|
|
|
template<typename T> inline JsonBuilderObjectSetter<T> JsonBuilderObject::operator[](T &&name) {
|
|
|
|
return JsonBuilderObjectSetter<T>(*this, std::forward<T>(name));
|
2018-09-06 17:28:23 +08:00
|
|
|
}
|
|
|
|
|