mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-07-25 12:04:23 +02:00
formatting
This commit is contained in:
parent
746033f3bd
commit
717c8ed3ad
1 changed files with 46 additions and 45 deletions
|
@ -1,5 +1,5 @@
|
|||
//
|
||||
// JSONBreakableMarshal.cpp
|
||||
// JSONBreakableMarshal.cpp
|
||||
// libraries/networking/src
|
||||
//
|
||||
// Created by Stephen Birarda on 04/28/15.
|
||||
|
@ -25,26 +25,26 @@ QStringList JSONBreakableMarshal::toStringList(const QJsonValue& jsonValue, cons
|
|||
if (jsonValue.isObject()) {
|
||||
|
||||
QJsonObject jsonObject = jsonValue.toObject();
|
||||
|
||||
|
||||
// enumerate the keys of the QJsonObject
|
||||
foreach(const QString& key, jsonObject.keys()) {
|
||||
QJsonValue childValue = jsonObject[key];
|
||||
|
||||
// setup the keypath for this key
|
||||
QString valueKeypath = (keypath.isEmpty() ? "" : keypath + ".") + key;
|
||||
|
||||
|
||||
if (childValue.isObject() || childValue.isArray()) {
|
||||
// recursion is required since the value is a QJsonObject or QJsonArray
|
||||
result << toStringList(childValue, valueKeypath);
|
||||
} else {
|
||||
// no recursion required, call our toString method to get the string representation
|
||||
// append the QStringList resulting from that to our QStringList
|
||||
result << toString(childValue, valueKeypath);
|
||||
result << toString(childValue, valueKeypath);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (jsonValue.isArray()) {
|
||||
QJsonArray jsonArray = jsonValue.toArray();
|
||||
|
||||
|
||||
// enumerate the elements in this QJsonArray
|
||||
for (int i = 0; i < jsonArray.size(); 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) {
|
||||
// default the value as a string to unknown in case conversion fails
|
||||
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()) {
|
||||
valueAsString = JSON_NULL_AS_STRING;
|
||||
} else if (jsonValue.isBool()) {
|
||||
|
@ -90,7 +90,7 @@ QString JSONBreakableMarshal::toString(const QJsonValue& jsonValue, const QStrin
|
|||
} else if (jsonValue.isUndefined()) {
|
||||
valueAsString = JSON_UNDEFINED_AS_STRING;
|
||||
} 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.";
|
||||
} else {
|
||||
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) {
|
||||
// default the value to null
|
||||
QVariant result;
|
||||
|
||||
|
||||
// attempt to match the value with our expected strings
|
||||
if (marshalValue == JSON_NULL_AS_STRING) {
|
||||
// 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) {
|
||||
result = QVariant(marshalValue == JSON_TRUE_AS_STRING ? true : false);
|
||||
} else if (marshalValue == JSON_UNDEFINED_AS_STRING) {
|
||||
result = JSON_UNDEFINED_AS_STRING;
|
||||
result = JSON_UNDEFINED_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
|
||||
result = JSON_UNKNOWN_AS_STRING;
|
||||
|
@ -125,14 +125,15 @@ QVariant JSONBreakableMarshal::fromString(const QString& marshalValue) {
|
|||
// use a regex to look for surrounding quotes first
|
||||
const QString JSON_STRING_REGEX = "^\"([\\s\\S]*)\"$";
|
||||
QRegExp stringRegex(JSON_STRING_REGEX);
|
||||
|
||||
|
||||
if (stringRegex.indexIn(marshalValue) != -1) {
|
||||
// set the result to the string value
|
||||
result = stringRegex.cap(1);
|
||||
} else {
|
||||
// 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.";
|
||||
result = JSON_UNKNOWN_AS_STRING;
|
||||
// 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.";
|
||||
result = JSON_UNKNOWN_AS_STRING;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -144,14 +145,14 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
|
|||
QVariant result = QVariantMap();
|
||||
|
||||
foreach(const QString& marshalString, stringList) {
|
||||
|
||||
|
||||
// find the equality operator
|
||||
int equalityIndex = marshalString.indexOf('=');
|
||||
|
||||
|
||||
// bail on parsing if we didn't find the equality operator
|
||||
if (equalityIndex != -1) {
|
||||
|
||||
QVariant* currentValue = &result;
|
||||
|
||||
QVariant* currentValue = &result;
|
||||
|
||||
// pull the key (everything left of the equality sign)
|
||||
QString parentKeypath;
|
||||
|
@ -160,7 +161,7 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
|
|||
// setup for array index checking
|
||||
const QString ARRAY_INDEX_REGEX_STRING = "\\[(\\d+)\\]";
|
||||
QRegExp arrayRegex(ARRAY_INDEX_REGEX_STRING);
|
||||
|
||||
|
||||
// as long as we have a keypath we need to recurse downwards
|
||||
while (!keypath.isEmpty()) {
|
||||
parentKeypath = keypath;
|
||||
|
@ -176,7 +177,7 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
|
|||
}
|
||||
|
||||
QVariantList& currentList = *static_cast<QVariantList*>(currentValue->data());
|
||||
|
||||
|
||||
// figure out what index we want to get the QJsonValue& for
|
||||
bool didConvert = false;
|
||||
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++) {
|
||||
// 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
|
||||
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
|
||||
if (keypath.startsWith(".")) {
|
||||
keypath = keypath.mid(1);
|
||||
|
@ -209,7 +210,7 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
|
|||
|
||||
} else {
|
||||
int keySeparatorIndex = keypath.indexOf('.');
|
||||
|
||||
|
||||
// we need to figure out what the key to look at is
|
||||
QString subKey = keypath;
|
||||
|
||||
|
@ -219,20 +220,20 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
|
|||
|
||||
if (arrayBracketIndex == -1 || (keySeparatorIndex != -1 && keySeparatorIndex < arrayBracketIndex)) {
|
||||
nextBreakIndex = keySeparatorIndex;
|
||||
nextKeypathStartIndex = keySeparatorIndex + 1;
|
||||
} else if (keySeparatorIndex == -1 || (arrayBracketIndex != -1
|
||||
nextKeypathStartIndex = keySeparatorIndex + 1;
|
||||
} else if (keySeparatorIndex == -1 || (arrayBracketIndex != -1
|
||||
&& arrayBracketIndex < keySeparatorIndex)) {
|
||||
nextBreakIndex = arrayBracketIndex;
|
||||
nextKeypathStartIndex = arrayBracketIndex;
|
||||
} 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.";
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// set the current key from the determined index
|
||||
subKey = keypath.left(nextBreakIndex);
|
||||
|
||||
|
||||
// update the keypath being processed
|
||||
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
|
||||
// if it isn't then make it one
|
||||
|
||||
|
||||
if (!currentValue->canConvert(QMetaType::QVariantMap) || currentValue->isNull()) {
|
||||
*currentValue = QVariantMap();
|
||||
}
|
||||
|
||||
|
||||
QVariantMap& currentMap = *static_cast<QVariantMap*>(currentValue->data());
|
||||
|
||||
// is there a QJsonObject for this key yet?
|
||||
|
@ -256,19 +257,19 @@ QVariantMap JSONBreakableMarshal::fromStringList(const QStringList& stringList)
|
|||
if (!currentMap.contains(subKey)) {
|
||||
currentMap[subKey] = QVariant();
|
||||
}
|
||||
|
||||
|
||||
// change the currentValue to the QJsonValue for this key
|
||||
currentValue = ¤tMap[subKey];
|
||||
}
|
||||
}
|
||||
|
||||
*currentValue = fromString(marshalString.mid(equalityIndex + 1));
|
||||
|
||||
|
||||
if (_interpolationMap.contains(parentKeypath)) {
|
||||
// we expect the currentValue here to be a string, that's the key we use for interpolation
|
||||
// bail if it isn't
|
||||
if (currentValue->canConvert(QMetaType::QString)
|
||||
&& _interpolationMap[parentKeypath].canConvert(QMetaType::QVariantMap)) {
|
||||
if (currentValue->canConvert(QMetaType::QString)
|
||||
&& _interpolationMap[parentKeypath].canConvert(QMetaType::QVariantMap)) {
|
||||
*currentValue = _interpolationMap[parentKeypath].toMap()[currentValue->toString()];
|
||||
}
|
||||
}
|
||||
|
@ -283,7 +284,7 @@ QVariantMap JSONBreakableMarshal::fromStringBuffer(const QByteArray& buffer) {
|
|||
QStringList packetList;
|
||||
int currentIndex = 0;
|
||||
int currentSeparator = buffer.indexOf('\0');
|
||||
|
||||
|
||||
while (currentIndex < buffer.size() - 1) {
|
||||
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
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// bump the currentIndex up to the last found separator
|
||||
currentIndex = currentSeparator + 1;
|
||||
|
||||
// find the index of the next separator, assuming this one wasn't the last one in the packet
|
||||
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);
|
||||
}
|
||||
|
||||
void JSONBreakableMarshal::addInterpolationForKey(const QString& rootKey, const QString& interpolationKey,
|
||||
void JSONBreakableMarshal::addInterpolationForKey(const QString& rootKey, const QString& interpolationKey,
|
||||
const QJsonValue& interpolationValue) {
|
||||
// if there is no map already beneath this key in our _interpolationMap create a QVariantMap there now
|
||||
|
||||
|
||||
if (!_interpolationMap.contains(rootKey)) {
|
||||
_interpolationMap.insert(rootKey, QVariantMap());
|
||||
}
|
||||
|
||||
|
||||
if (_interpolationMap[rootKey].canConvert(QMetaType::QVariantMap)) {
|
||||
QVariantMap& mapForRootKey = *static_cast<QVariantMap*>(_interpolationMap[rootKey].data());
|
||||
|
||||
|
||||
mapForRootKey.insert(interpolationKey, QVariant(interpolationValue));
|
||||
} 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.";
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
|
||||
if (_interpolationMap.contains(rootKey)) {
|
||||
QVariant& rootValue = _interpolationMap[rootKey];
|
||||
|
||||
|
||||
if (!rootValue.isNull() && rootValue.canConvert(QMetaType::QVariantMap)) {
|
||||
// remove the value at the interpolationKey
|
||||
static_cast<QVariantMap*>(rootValue.data())->remove(interpolationKey);
|
||||
|
|
Loading…
Reference in a new issue