added short output format, also use numeric fields for json when names are not requested
This commit is contained in:
+17
-9
@@ -382,16 +382,24 @@ result_t SingleDataField::read(SymbolString& data, size_t offset,
|
|||||||
if (isIgnored() || (fieldName != NULL && (m_name != fieldName || fieldIndex > 0))) {
|
if (isIgnored() || (fieldName != NULL && (m_name != fieldName || fieldIndex > 0))) {
|
||||||
return RESULT_EMPTY;
|
return RESULT_EMPTY;
|
||||||
}
|
}
|
||||||
|
bool shortFormat = outputFormat & OF_SHORT;
|
||||||
if (outputFormat & OF_JSON) {
|
if (outputFormat & OF_JSON) {
|
||||||
if (leadingSeparator) {
|
if (leadingSeparator) {
|
||||||
output << ",";
|
output << ",";
|
||||||
}
|
}
|
||||||
|
if (!shortFormat) {
|
||||||
|
output << "\n ";
|
||||||
|
}
|
||||||
if (outputIndex >= 0 || m_name.empty() || !(outputFormat & OF_NAMES)) {
|
if (outputIndex >= 0 || m_name.empty() || !(outputFormat & OF_NAMES)) {
|
||||||
output << "\n \"" << static_cast<signed int>(outputIndex < 0 ? 0 : outputIndex) << "\": {\"name\": \""
|
output << "\"" << static_cast<signed int>(outputIndex < 0 ? 0 : outputIndex) << "\":";
|
||||||
<< m_name << "\"" << ", \"value\": ";
|
if (!shortFormat) {
|
||||||
|
output << " {\"name\": \"" << m_name << "\"" << ", \"value\": ";
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
output << "\n \"" << m_name << "\": {\"value\": ";
|
output << "\"" << m_name << "\":";
|
||||||
|
if (!shortFormat) {
|
||||||
|
output << " {\"value\": ";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (leadingSeparator) {
|
if (leadingSeparator) {
|
||||||
@@ -406,21 +414,21 @@ result_t SingleDataField::read(SymbolString& data, size_t offset,
|
|||||||
if (result != RESULT_OK) {
|
if (result != RESULT_OK) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if ((outputFormat & OF_UNITS) && m_unit.length() > 0) {
|
if (!shortFormat && (outputFormat & OF_UNITS) && m_unit.length() > 0) {
|
||||||
if (outputFormat & OF_JSON) {
|
if (outputFormat & OF_JSON) {
|
||||||
output << ", \"unit\": \"" << m_unit << '"';
|
output << ", \"unit\": \"" << m_unit << '"';
|
||||||
} else {
|
} else {
|
||||||
output << " " << m_unit;
|
output << " " << m_unit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((outputFormat & OF_COMMENTS) && m_comment.length() > 0) {
|
if (!shortFormat && (outputFormat & OF_COMMENTS) && m_comment.length() > 0) {
|
||||||
if (outputFormat & OF_JSON) {
|
if (outputFormat & OF_JSON) {
|
||||||
output << ", \"comment\": \"" << m_comment << '"';
|
output << ", \"comment\": \"" << m_comment << '"';
|
||||||
} else {
|
} else {
|
||||||
output << " [" << m_comment << "]";
|
output << " [" << m_comment << "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (outputFormat & OF_JSON) {
|
if (!shortFormat && (outputFormat & OF_JSON)) {
|
||||||
output << "}";
|
output << "}";
|
||||||
}
|
}
|
||||||
return RESULT_OK;
|
return RESULT_OK;
|
||||||
@@ -895,7 +903,7 @@ result_t DataFieldSet::read(SymbolString& data, size_t offset,
|
|||||||
ostringstream& output, OutputFormat outputFormat, ssize_t outputIndex,
|
ostringstream& output, OutputFormat outputFormat, ssize_t outputIndex,
|
||||||
bool leadingSeparator, const char* fieldName, ssize_t fieldIndex) {
|
bool leadingSeparator, const char* fieldName, ssize_t fieldIndex) {
|
||||||
bool previousFullByteOffset = true, found = false, findFieldIndex = fieldName != NULL && fieldIndex >= 0;
|
bool previousFullByteOffset = true, found = false, findFieldIndex = fieldName != NULL && fieldIndex >= 0;
|
||||||
if (!m_uniqueNames && outputIndex < 0) {
|
if (outputIndex < 0 && (!m_uniqueNames || ((outputFormat & OF_JSON) && !(outputFormat & OF_NAMES)))) {
|
||||||
outputIndex = 0;
|
outputIndex = 0;
|
||||||
}
|
}
|
||||||
PartType partType = data.isMaster() ? pt_masterData : pt_slaveData;
|
PartType partType = data.isMaster() ? pt_masterData : pt_slaveData;
|
||||||
@@ -938,7 +946,7 @@ result_t DataFieldSet::read(SymbolString& data, size_t offset,
|
|||||||
if (!found) {
|
if (!found) {
|
||||||
return RESULT_EMPTY;
|
return RESULT_EMPTY;
|
||||||
}
|
}
|
||||||
if ((outputFormat & OF_COMMENTS) && m_comment.length() > 0) {
|
if (!(outputFormat & OF_SHORT) && (outputFormat & OF_COMMENTS) && m_comment.length() > 0) {
|
||||||
if (outputFormat & OF_JSON) {
|
if (outputFormat & OF_JSON) {
|
||||||
output << ",\"comment\": \"" << m_comment << '"';
|
output << ",\"comment\": \"" << m_comment << '"';
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -95,6 +95,9 @@ typedef int OutputFormat;
|
|||||||
/** bit flag for @a OutputFormat: JSON format. */
|
/** bit flag for @a OutputFormat: JSON format. */
|
||||||
#define OF_JSON 0x10
|
#define OF_JSON 0x10
|
||||||
|
|
||||||
|
/** bit flag for @a OutputFormat: short format (only name and value, no indentation). */
|
||||||
|
#define OF_SHORT 0x20
|
||||||
|
|
||||||
/** the message part in which a data field is stored. */
|
/** the message part in which a data field is stored. */
|
||||||
enum PartType {
|
enum PartType {
|
||||||
pt_any, //!< stored in any data (master or slave)
|
pt_any, //!< stored in any data (master or slave)
|
||||||
|
|||||||
Reference in New Issue
Block a user