enhanced AttributedItem and corrected number detection in appendJson
This commit is contained in:
+57
-45
@@ -100,6 +100,31 @@ void AttributedItem::dumpString(ostream& output, const string str, const bool pr
|
||||
}
|
||||
}
|
||||
|
||||
void AttributedItem::appendJson(ostream& output, const string name, const string value,
|
||||
const bool prependFieldSeparator, bool asString) {
|
||||
bool plain;
|
||||
if (asString) {
|
||||
plain = false;
|
||||
} else {
|
||||
plain = value == "false" || value == "true";
|
||||
if (!plain) {
|
||||
const char* str = value.c_str();
|
||||
char* strEnd = NULL;
|
||||
strtod(str, &strEnd);
|
||||
plain = strEnd && !*strEnd;
|
||||
}
|
||||
}
|
||||
if (prependFieldSeparator) {
|
||||
output << FIELD_SEPARATOR;
|
||||
}
|
||||
output << "\"" << name << "\": ";
|
||||
if (plain) {
|
||||
output << value;
|
||||
} else {
|
||||
output << '"' << value << '"';
|
||||
}
|
||||
}
|
||||
|
||||
void AttributedItem::mergeAttributes(map<string, string>& attributes) const {
|
||||
for (auto& entry : m_attributes) {
|
||||
auto it = attributes.find(entry.first);
|
||||
@@ -118,40 +143,43 @@ string AttributedItem::getAttribute(const string name) const {
|
||||
return it == m_attributes.end() ? "" : it->second;
|
||||
}
|
||||
|
||||
void appendJson(ostringstream& output, const string name, const string value, bool asString = false) {
|
||||
bool plain;
|
||||
if (asString) {
|
||||
plain = false;
|
||||
} else {
|
||||
plain = value == "false" || value == "true";
|
||||
if (!plain) {
|
||||
const char* str = value.c_str();
|
||||
char* strEnd = NULL;
|
||||
strtod(str, &strEnd);
|
||||
plain = strEnd && *strEnd;
|
||||
}
|
||||
}
|
||||
output << ", \"" << name << "\": ";
|
||||
if (plain) {
|
||||
output << value;
|
||||
} else {
|
||||
output << '"' << value << '"';
|
||||
}
|
||||
}
|
||||
|
||||
void AttributedItem::appendAttribute(ostringstream& output, OutputFormat outputFormat, const string name,
|
||||
bool AttributedItem::appendAttribute(ostringstream& output, OutputFormat outputFormat, const string name,
|
||||
const bool onlyIfNonEmpty, const string prefix, const string suffix) const {
|
||||
auto it = m_attributes.find(name);
|
||||
string value = it == m_attributes.end() ? "" : it->second;
|
||||
if (!onlyIfNonEmpty || !value.empty()) {
|
||||
if (outputFormat & OF_JSON) {
|
||||
appendJson(output, name, value, true);
|
||||
} else {
|
||||
output << " " << prefix << value << suffix;
|
||||
}
|
||||
if (onlyIfNonEmpty && value.empty()) {
|
||||
return false;
|
||||
}
|
||||
if (outputFormat & OF_JSON) {
|
||||
appendJson(output, name, value, true);
|
||||
} else {
|
||||
output << " " << prefix << value << suffix;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AttributedItem::appendAttributes(ostringstream& output, OutputFormat outputFormat) const {
|
||||
bool ret = false;
|
||||
if ((outputFormat & OF_UNITS)) {
|
||||
ret = appendAttribute(output, outputFormat, "unit") || ret;
|
||||
}
|
||||
if ((outputFormat & OF_COMMENTS)) {
|
||||
ret = appendAttribute(output, outputFormat, "comment", true, "[", "]") || ret;
|
||||
}
|
||||
if (outputFormat & OF_ALL_ATTRS) {
|
||||
for (auto& entry : m_attributes) {
|
||||
ret = true;
|
||||
if (!entry.second.empty() && entry.first != "unit" && entry.first != "comment") {
|
||||
if (outputFormat & OF_JSON) {
|
||||
appendJson(output, entry.first, entry.second);
|
||||
} else {
|
||||
output << " " << entry.first << "=" << entry.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
string formatInt(size_t value) {
|
||||
@@ -519,23 +547,7 @@ result_t SingleDataField::read(const SymbolString& data, size_t offset,
|
||||
return result;
|
||||
}
|
||||
if (!shortFormat) {
|
||||
if ((outputFormat & OF_UNITS)) {
|
||||
appendAttribute(output, outputFormat, "unit");
|
||||
}
|
||||
if ((outputFormat & OF_COMMENTS)) {
|
||||
appendAttribute(output, outputFormat, "comment", true, "[", "]");
|
||||
}
|
||||
if (outputFormat & OF_ALL_ATTRS) {
|
||||
for (auto& entry : m_attributes) {
|
||||
if (!entry.second.empty() && entry.first != "unit" && entry.first != "comment") {
|
||||
if (outputFormat & OF_JSON) {
|
||||
appendJson(output, entry.first, entry.second);
|
||||
} else {
|
||||
output << " " << entry.first << "=" << entry.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
appendAttributes(output, outputFormat);
|
||||
}
|
||||
if (!shortFormat && (outputFormat & OF_JSON)) {
|
||||
output << "}";
|
||||
|
||||
+22
-3
@@ -95,7 +95,7 @@ class SingleDataField;
|
||||
* Base class for named items with optional named attributes.
|
||||
*/
|
||||
class AttributedItem {
|
||||
protected:
|
||||
public:
|
||||
/**
|
||||
* Constructs a new instance.
|
||||
* @param name the item name.
|
||||
@@ -117,7 +117,6 @@ class AttributedItem {
|
||||
virtual ~AttributedItem() {}
|
||||
|
||||
|
||||
public:
|
||||
/**
|
||||
* Remove and return a certain value from a map.
|
||||
* @param row the map to remove the value from.
|
||||
@@ -134,6 +133,17 @@ class AttributedItem {
|
||||
*/
|
||||
static void dumpString(ostream& output, const string str, const bool prependFieldSeparator = true);
|
||||
|
||||
/**
|
||||
* Append a named attribute as JSON to the output.
|
||||
* @param output the @a ostream to append to.
|
||||
* @param name the name of the attribute.
|
||||
* @param value the value of the attribute.
|
||||
* @param prependFieldSeparator whether to start with a @a FIELD_SEPARATOR.
|
||||
* @param asString true to force writing as string, false to detect the type from the value.
|
||||
*/
|
||||
static void appendJson(ostream& output, const string name, const string value,
|
||||
const bool prependFieldSeparator = true, bool asString = false);
|
||||
|
||||
/**
|
||||
* Merge this instance's additional named attributes into the specified attributes.
|
||||
* @param attributes the additional named attributes to merge in this instance's additional named attributes.
|
||||
@@ -156,10 +166,19 @@ class AttributedItem {
|
||||
* @param onlyIfNonEmpty true to append only if the value is not empty.
|
||||
* @param prefix optional prefix to use (only for non-JSON output).
|
||||
* @param suffix optional suffix to use (only for non-JSON output).
|
||||
* @return true if data was added, false otherwise.
|
||||
*/
|
||||
void appendAttribute(ostringstream& output, OutputFormat outputFormat, const string name,
|
||||
bool appendAttribute(ostringstream& output, OutputFormat outputFormat, const string name,
|
||||
const bool onlyIfNonEmpty = true, const string prefix = "", const string suffix = "") const;
|
||||
|
||||
/**
|
||||
* Append the attributes to the output.
|
||||
* @param output the @a ostringstream to append the formatted values to.
|
||||
* @param outputFormat the @a OutputFormat options to use.
|
||||
* @return true if data was added, false otherwise.
|
||||
*/
|
||||
bool appendAttributes(ostringstream& output, OutputFormat outputFormat) const;
|
||||
|
||||
/**
|
||||
* Get the item name.
|
||||
* @return the item name.
|
||||
|
||||
Reference in New Issue
Block a user