formatting

This commit is contained in:
Seth Alves 2015-06-21 18:12:24 -07:00
parent 746033f3bd
commit 717c8ed3ad

View file

@ -1,5 +1,5 @@
// //
// JSONBreakableMarshal.cpp // JSONBreakableMarshal.cpp
// libraries/networking/src // libraries/networking/src
// //
// Created by Stephen Birarda on 04/28/15. // Created by Stephen Birarda on 04/28/15.
@ -25,26 +25,26 @@ QStringList JSONBreakableMarshal::toStringList(const QJsonValue& jsonValue, cons
if (jsonValue.isObject()) { if (jsonValue.isObject()) {
QJsonObject jsonObject = jsonValue.toObject(); QJsonObject jsonObject = jsonValue.toObject();
// enumerate the keys of the QJsonObject // enumerate the keys of the QJsonObject
foreach(const QString& key, jsonObject.keys()) { foreach(const QString& key, jsonObject.keys()) {
QJsonValue childValue = jsonObject[key]; QJsonValue childValue = jsonObject[key];
// setup the keypath for this key // setup the keypath for this key
QString valueKeypath = (keypath.isEmpty() ? "" : keypath + ".") + key; QString valueKeypath = (keypath.isEmpty() ? "" : keypath + ".") + key;
if (childValue.isObject() || childValue.isArray()) { if (childValue.isObject() || childValue.isArray()) {
// recursion is required since the value is a QJsonObject or QJsonArray // recursion is required since the value is a QJsonObject or QJsonArray
result << toStringList(childValue, valueKeypath); result << toStringList(childValue, valueKeypath);
} else { } else {
// no recursion required, call our toString method to get the string representation // no recursion required, call our toString method to get the string representation
// append the QStringList resulting from that to our QStringList // append the QStringList resulting from that to our QStringList
result << toString(childValue, valueKeypath); result << toString(childValue, valueKeypath);
} }
} }
} else if (jsonValue.isArray()) { } else if (jsonValue.isArray()) {
QJsonArray jsonArray = jsonValue.toArray(); QJsonArray jsonArray = jsonValue.toArray();
// enumerate the elements in this QJsonArray // enumerate the elements in this QJsonArray
for (int i = 0; i < jsonArray.size(); i++) { for (int i = 0; i < jsonArray.size(); i++) {
QJsonValue arrayValue = jsonArray[i]; QJsonValue arrayValue = jsonArray[i];
@ -77,8 +77,8 @@ const QString JSON_UNKNOWN_AS_STRING = "unknown";
QString JSONBreakableMarshal::toString(const QJsonValue& jsonValue, const QString& keypath) { QString JSONBreakableMarshal::toString(const QJsonValue& jsonValue, const QString& keypath) {
// default the value as a string to unknown in case conversion fails // default the value as a string to unknown in case conversion fails
QString valueAsString = JSON_UNKNOWN_AS_STRING; QString valueAsString = JSON_UNKNOWN_AS_STRING;
// as the QJsonValue what type it is and format its value as a string accordingly // ask the QJsonValue what type it is and format its value as a string accordingly
if (jsonValue.isNull()) { if (jsonValue.isNull()) {
valueAsString = JSON_NULL_AS_STRING; valueAsString = JSON_NULL_AS_STRING;
} else if (jsonValue.isBool()) { } else if (jsonValue.isBool()) {
@ -90,7 +90,7 @@ QString JSONBreakableMarshal::toString(const QJsonValue& jsonValue, const QStrin
} else if (jsonValue.isUndefined()) { } else if (jsonValue.isUndefined()) {
valueAsString = JSON_UNDEFINED_AS_STRING; valueAsString = JSON_UNDEFINED_AS_STRING;
} else if (jsonValue.isArray() || jsonValue.isObject()) { } else if (jsonValue.isArray() || jsonValue.isObject()) {
qDebug() << "JSONBreakableMarshal::toString does not handle conversion of a QJsonObject or QJsonArray." qDebug() << "JSONBreakableMarshal::toString does not handle conversion of a QJsonObject or QJsonArray."
<< "You should call JSONBreakableMarshal::toStringList instead."; << "You should call JSONBreakableMarshal::toStringList instead.";
} else { } else {
qDebug() << "Unrecognized QJsonValue - JSONBreakableMarshal cannot convert to string."; qDebug() << "Unrecognized QJsonValue - JSONBreakableMarshal cannot convert to string.";
@ -102,14 +102,14 @@ QString JSONBreakableMarshal::toString(const QJsonValue& jsonValue, const QStrin
QVariant JSONBreakableMarshal::fromString(const QString& marshalValue) { QVariant JSONBreakableMarshal::fromString(const QString& marshalValue) {
// default the value to null // default the value to null
QVariant result; QVariant result;
// attempt to match the value with our expected strings // attempt to match the value with our expected strings
if (marshalValue == JSON_NULL_AS_STRING) { if (marshalValue == JSON_NULL_AS_STRING) {
// this is already our default, we don't need to do anything here // this is already our default, we don't need to do anything here
} else if (marshalValue == JSON_TRUE_AS_STRING || marshalValue == JSON_FALSE_AS_STRING) { } else if (marshalValue == JSON_TRUE_AS_STRING || marshalValue == JSON_FALSE_AS_STRING) {
result = QVariant(marshalValue == JSON_TRUE_AS_STRING ? true : false); result = QVariant(marshalValue == JSON_TRUE_AS_STRING ? true : false);
} else if (marshalValue == JSON_UNDEFINED_AS_STRING) { } else if (marshalValue == JSON_UNDEFINED_AS_STRING) {
result = JSON_UNDEFINED_AS_STRING; result = JSON_UNDEFINED_AS_STRING;
} else if (marshalValue == JSON_UNKNOWN_AS_STRING) { } else if (marshalValue == JSON_UNKNOWN_AS_STRING) {
// we weren't able to marshal this value at the other end, set it as our unknown string // we weren't able to marshal this value at the other end, set it as our unknown string
result = JSON_UNKNOWN_AS_STRING; result = JSON_UNKNOWN_AS_STRING;
@ -125,14 +125,15 @@ QVariant JSONBreakableMarshal::fromString(const QString& marshalValue) {
// use a regex to look for surrounding quotes first // use a regex to look for surrounding quotes first
const QString JSON_STRING_REGEX = "^\"([\\s\\S]*)\"$"; const QString JSON_STRING_REGEX = "^\"([\\s\\S]*)\"$";
QRegExp stringRegex(JSON_STRING_REGEX); QRegExp stringRegex(JSON_STRING_REGEX);
if (stringRegex.indexIn(marshalValue) != -1) { if (stringRegex.indexIn(marshalValue) != -1) {
// set the result to the string value // set the result to the string value
result = stringRegex.cap(1); result = stringRegex.cap(1);
} else { } else {
// we failed to convert the value to anything, set the result to our unknown value // we failed to convert the value to anything, set the result to our unknown value
qDebug() << "Unrecognized output from JSONBreakableMarshal - could not convert" << marshalValue << "to QVariant."; qDebug() << "Unrecognized output from JSONBreakableMarshal - could not convert"
result = JSON_UNKNOWN_AS_STRING; << marshalValue << "to QVariant.";
result = JSON_UNKNOWN_AS_STRING;
} }
} }
} }
@ -144,14 +145,14 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
QVariant result = QVariantMap(); QVariant result = QVariantMap();
foreach(const QString& marshalString, stringList) { foreach(const QString& marshalString, stringList) {
// find the equality operator // find the equality operator
int equalityIndex = marshalString.indexOf('='); int equalityIndex = marshalString.indexOf('=');
// bail on parsing if we didn't find the equality operator // bail on parsing if we didn't find the equality operator
if (equalityIndex != -1) { if (equalityIndex != -1) {
QVariant* currentValue = &result; QVariant* currentValue = &result;
// pull the key (everything left of the equality sign) // pull the key (everything left of the equality sign)
QString parentKeypath; QString parentKeypath;
@ -160,7 +161,7 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
// setup for array index checking // setup for array index checking
const QString ARRAY_INDEX_REGEX_STRING = "\\[(\\d+)\\]"; const QString ARRAY_INDEX_REGEX_STRING = "\\[(\\d+)\\]";
QRegExp arrayRegex(ARRAY_INDEX_REGEX_STRING); QRegExp arrayRegex(ARRAY_INDEX_REGEX_STRING);
// as long as we have a keypath we need to recurse downwards // as long as we have a keypath we need to recurse downwards
while (!keypath.isEmpty()) { while (!keypath.isEmpty()) {
parentKeypath = keypath; parentKeypath = keypath;
@ -176,7 +177,7 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
} }
QVariantList& currentList = *static_cast<QVariantList*>(currentValue->data()); QVariantList& currentList = *static_cast<QVariantList*>(currentValue->data());
// figure out what index we want to get the QJsonValue& for // figure out what index we want to get the QJsonValue& for
bool didConvert = false; bool didConvert = false;
int arrayIndex = arrayRegex.cap(1).toInt(&didConvert); int arrayIndex = arrayRegex.cap(1).toInt(&didConvert);
@ -187,7 +188,7 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
for (int i = currentList.size(); i < arrayIndex + 1; i++) { for (int i = currentList.size(); i < arrayIndex + 1; i++) {
// add the null QJsonValue at this array index to get the array to the right size // add the null QJsonValue at this array index to get the array to the right size
currentList.push_back(QJsonValue()); currentList.push_back(QJsonValue());
} }
} }
@ -196,7 +197,7 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
// update the keypath by bumping past the array index // update the keypath by bumping past the array index
keypath = keypath.mid(keypath.indexOf(']') + 1); keypath = keypath.mid(keypath.indexOf(']') + 1);
// check if there is a key after the array index - if so push the keypath forward by a char // check if there is a key after the array index - if so push the keypath forward by a char
if (keypath.startsWith(".")) { if (keypath.startsWith(".")) {
keypath = keypath.mid(1); keypath = keypath.mid(1);
@ -209,7 +210,7 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
} else { } else {
int keySeparatorIndex = keypath.indexOf('.'); int keySeparatorIndex = keypath.indexOf('.');
// we need to figure out what the key to look at is // we need to figure out what the key to look at is
QString subKey = keypath; QString subKey = keypath;
@ -219,20 +220,20 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
if (arrayBracketIndex == -1 || (keySeparatorIndex != -1 && keySeparatorIndex < arrayBracketIndex)) { if (arrayBracketIndex == -1 || (keySeparatorIndex != -1 && keySeparatorIndex < arrayBracketIndex)) {
nextBreakIndex = keySeparatorIndex; nextBreakIndex = keySeparatorIndex;
nextKeypathStartIndex = keySeparatorIndex + 1; nextKeypathStartIndex = keySeparatorIndex + 1;
} else if (keySeparatorIndex == -1 || (arrayBracketIndex != -1 } else if (keySeparatorIndex == -1 || (arrayBracketIndex != -1
&& arrayBracketIndex < keySeparatorIndex)) { && arrayBracketIndex < keySeparatorIndex)) {
nextBreakIndex = arrayBracketIndex; nextBreakIndex = arrayBracketIndex;
nextKeypathStartIndex = arrayBracketIndex; nextKeypathStartIndex = arrayBracketIndex;
} else { } else {
qDebug() << "Unrecognized key format while trying to parse " << keypath << " - will not add" qDebug() << "Unrecognized key format while trying to parse " << keypath << " - will not add"
<< "value to resulting QJsonObject."; << "value to resulting QJsonObject.";
break; break;
} }
// set the current key from the determined index // set the current key from the determined index
subKey = keypath.left(nextBreakIndex); subKey = keypath.left(nextBreakIndex);
// update the keypath being processed // update the keypath being processed
keypath = keypath.mid(nextKeypathStartIndex); keypath = keypath.mid(nextKeypathStartIndex);
@ -244,11 +245,11 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
// we're here becuase we know the current value should be an object // we're here becuase we know the current value should be an object
// if it isn't then make it one // if it isn't then make it one
if (!currentValue->canConvert(QMetaType::QVariantMap) || currentValue->isNull()) { if (!currentValue->canConvert(QMetaType::QVariantMap) || currentValue->isNull()) {
*currentValue = QVariantMap(); *currentValue = QVariantMap();
} }
QVariantMap& currentMap = *static_cast<QVariantMap*>(currentValue->data()); QVariantMap& currentMap = *static_cast<QVariantMap*>(currentValue->data());
// is there a QJsonObject for this key yet? // is there a QJsonObject for this key yet?
@ -256,19 +257,19 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
if (!currentMap.contains(subKey)) { if (!currentMap.contains(subKey)) {
currentMap[subKey] = QVariant(); currentMap[subKey] = QVariant();
} }
// change the currentValue to the QJsonValue for this key // change the currentValue to the QJsonValue for this key
currentValue = &currentMap[subKey]; currentValue = &currentMap[subKey];
} }
} }
*currentValue = fromString(marshalString.mid(equalityIndex + 1)); *currentValue = fromString(marshalString.mid(equalityIndex + 1));
if (_interpolationMap.contains(parentKeypath)) { if (_interpolationMap.contains(parentKeypath)) {
// we expect the currentValue here to be a string, that's the key we use for interpolation // we expect the currentValue here to be a string, that's the key we use for interpolation
// bail if it isn't // bail if it isn't
if (currentValue->canConvert(QMetaType::QString) if (currentValue->canConvert(QMetaType::QString)
&& _interpolationMap[parentKeypath].canConvert(QMetaType::QVariantMap)) { && _interpolationMap[parentKeypath].canConvert(QMetaType::QVariantMap)) {
*currentValue = _interpolationMap[parentKeypath].toMap()[currentValue->toString()]; *currentValue = _interpolationMap[parentKeypath].toMap()[currentValue->toString()];
} }
} }
@ -283,7 +284,7 @@ QVariantMap JSONBreakableMarshal::fromStringBuffer(const QByteArray& buffer) {
QStringList packetList; QStringList packetList;
int currentIndex = 0; int currentIndex = 0;
int currentSeparator = buffer.indexOf('\0'); int currentSeparator = buffer.indexOf('\0');
while (currentIndex < buffer.size() - 1) { while (currentIndex < buffer.size() - 1) {
packetList << QString::fromUtf8(buffer.mid(currentIndex, currentSeparator)); packetList << QString::fromUtf8(buffer.mid(currentIndex, currentSeparator));
@ -291,13 +292,13 @@ QVariantMap JSONBreakableMarshal::fromStringBuffer(const QByteArray& buffer) {
// no more separators to be found, break out of here so we're not looping for nothing // no more separators to be found, break out of here so we're not looping for nothing
break; break;
} }
// bump the currentIndex up to the last found separator // bump the currentIndex up to the last found separator
currentIndex = currentSeparator + 1; currentIndex = currentSeparator + 1;
// find the index of the next separator, assuming this one wasn't the last one in the packet // find the index of the next separator, assuming this one wasn't the last one in the packet
if (currentSeparator < buffer.size() - 1) { if (currentSeparator < buffer.size() - 1) {
currentSeparator = buffer.indexOf('\0', currentIndex); currentSeparator = buffer.indexOf('\0', currentIndex);
} }
} }
@ -305,30 +306,30 @@ QVariantMap JSONBreakableMarshal::fromStringBuffer(const QByteArray& buffer) {
return fromStringList(packetList); return fromStringList(packetList);
} }
void JSONBreakableMarshal::addInterpolationForKey(const QString& rootKey, const QString& interpolationKey, void JSONBreakableMarshal::addInterpolationForKey(const QString& rootKey, const QString& interpolationKey,
const QJsonValue& interpolationValue) { const QJsonValue& interpolationValue) {
// if there is no map already beneath this key in our _interpolationMap create a QVariantMap there now // if there is no map already beneath this key in our _interpolationMap create a QVariantMap there now
if (!_interpolationMap.contains(rootKey)) { if (!_interpolationMap.contains(rootKey)) {
_interpolationMap.insert(rootKey, QVariantMap()); _interpolationMap.insert(rootKey, QVariantMap());
} }
if (_interpolationMap[rootKey].canConvert(QMetaType::QVariantMap)) { if (_interpolationMap[rootKey].canConvert(QMetaType::QVariantMap)) {
QVariantMap& mapForRootKey = *static_cast<QVariantMap*>(_interpolationMap[rootKey].data()); QVariantMap& mapForRootKey = *static_cast<QVariantMap*>(_interpolationMap[rootKey].data());
mapForRootKey.insert(interpolationKey, QVariant(interpolationValue)); mapForRootKey.insert(interpolationKey, QVariant(interpolationValue));
} else { } else {
qDebug() << "JSONBreakableMarshal::addInterpolationForKey could not convert variant at key" << rootKey qDebug() << "JSONBreakableMarshal::addInterpolationForKey could not convert variant at key" << rootKey
<< "to a QVariantMap. Can not add interpolation."; << "to a QVariantMap. Can not add interpolation.";
} }
} }
void JSONBreakableMarshal::removeInterpolationForKey(const QString& rootKey, const QString& interpolationKey) { void JSONBreakableMarshal::removeInterpolationForKey(const QString& rootKey, const QString& interpolationKey) {
// make sure the interpolation map contains this root key and that the value is a map // make sure the interpolation map contains this root key and that the value is a map
if (_interpolationMap.contains(rootKey)) { if (_interpolationMap.contains(rootKey)) {
QVariant& rootValue = _interpolationMap[rootKey]; QVariant& rootValue = _interpolationMap[rootKey];
if (!rootValue.isNull() && rootValue.canConvert(QMetaType::QVariantMap)) { if (!rootValue.isNull() && rootValue.canConvert(QMetaType::QVariantMap)) {
// remove the value at the interpolationKey // remove the value at the interpolationKey
static_cast<QVariantMap*>(rootValue.data())->remove(interpolationKey); static_cast<QVariantMap*>(rootValue.data())->remove(interpolationKey);