added DataField::getCount(), exclude name wrap when fieldIndex is used in SingleDataField::read(), corrected json indent

This commit is contained in:
john30
2017-07-02 10:09:47 +02:00
parent 11832ca126
commit b8a4e5e032
5 changed files with 40 additions and 18 deletions
+5 -1
View File
@@ -532,16 +532,20 @@ result_t SingleDataField::read(const SymbolString& data, size_t offset,
if (leadingSeparator) { if (leadingSeparator) {
*output << ","; *output << ",";
} }
if (!shortFormat) { if (fieldIndex < 0 && !shortFormat) {
*output << "\n "; *output << "\n ";
} }
if (outputIndex >= 0 || m_name.empty() || !(outputFormat & OF_NAMES)) { if (outputIndex >= 0 || m_name.empty() || !(outputFormat & OF_NAMES)) {
if (fieldIndex < 0) {
*output << "\"" << static_cast<signed int>(outputIndex < 0 ? 0 : outputIndex) << "\":"; *output << "\"" << static_cast<signed int>(outputIndex < 0 ? 0 : outputIndex) << "\":";
}
if (!shortFormat) { if (!shortFormat) {
*output << " {\"name\": \"" << m_name << "\"" << ", \"value\": "; *output << " {\"name\": \"" << m_name << "\"" << ", \"value\": ";
} }
} else { } else {
if (fieldIndex < 0) {
*output << "\"" << m_name << "\":"; *output << "\"" << m_name << "\":";
}
if (!shortFormat) { if (!shortFormat) {
*output << " {\"value\": "; *output << " {\"value\": ";
} }
+12
View File
@@ -249,6 +249,12 @@ class DataField : public AttributedItem {
const map<unsigned int, string>& values, map<string, string>* attributes, const map<unsigned int, string>& values, map<string, string>* attributes,
vector<const SingleDataField*>* fields) const = 0; vector<const SingleDataField*>* fields) const = 0;
/**
* Get the field count (excluding ignored fields).
* @return the field count (excluding ignored fields).
*/
virtual size_t getCount() const = 0;
/** /**
* Get the specified field name. * Get the specified field name.
* @param fieldIndex the index of the field (excluding ignored fields), or -1 for this. * @param fieldIndex the index of the field (excluding ignored fields), or -1 for this.
@@ -392,6 +398,9 @@ class SingleDataField : public DataField {
*/ */
bool hasFullByteOffset(bool after) const; bool hasFullByteOffset(bool after) const;
// @copydoc
size_t getCount() const override { return isIgnored() ? 0 : 1; }
// @copydoc // @copydoc
virtual string getName(ssize_t fieldIndex) const { virtual string getName(ssize_t fieldIndex) const {
return isIgnored() || fieldIndex > 0 ? "" : m_name; return isIgnored() || fieldIndex > 0 ? "" : m_name;
@@ -628,6 +637,9 @@ class DataFieldSet : public DataField {
// @copydoc // @copydoc
size_t getLength(PartType partType, size_t maxLength) const override; size_t getLength(PartType partType, size_t maxLength) const override;
// @copydoc
size_t getCount() const override { return m_fields.size() - m_ignoredCount; }
// @copydoc // @copydoc
string getName(ssize_t fieldIndex) const override; string getName(ssize_t fieldIndex) const override;
+8 -2
View File
@@ -267,12 +267,18 @@ class Message : public AttributedItem {
*/ */
static bool checkLevel(const string& level, const string& checkLevels); static bool checkLevel(const string& level, const string& checkLevels);
/**
* Get the number of non-ignored fields.
* @return the number of non-ignored fields.
*/
size_t getFieldCount() const { return m_data->getCount(); }
/** /**
* Get the specified field name. * Get the specified field name.
* @param fieldIndex the index of the field (excluding ignored fields). * @param fieldIndex the index of the field (excluding ignored fields).
* @return the field name, or the index as string if not unique or not available. * @return the field name, or the index as string if not unique, or empty not available.
*/ */
virtual string getFieldName(ssize_t fieldIndex) const { return m_data->getName(fieldIndex); } string getFieldName(ssize_t fieldIndex) const { return m_data->getName(fieldIndex); }
/** /**
* Get whether this is a write message. * Get whether this is a write message.