1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

QQtDictionary初步添加toCbor和fromCbor

This commit is contained in:
tianduanrui 2020-04-19 19:51:21 +08:00
parent 5b215aa47a
commit 5006b17c70
4 changed files with 424 additions and 0 deletions

View File

@ -31,6 +31,10 @@
#include <QBuffer> #include <QBuffer>
#if QT_VERSION >= QT_VERSION_CHECK(5,12,0)
#include <QCborValue>
#endif
#include <iostream> #include <iostream>
using namespace std; using namespace std;
@ -59,6 +63,13 @@ void fromCSV ( const QByteArray& csv, QQtDictionary& dict,
const QString& textEncoding ); const QString& textEncoding );
#endif #endif
#if QT_VERSION >= QT_VERSION_CHECK(5,12,0)
QByteArray toCbor ( const QQtDictionary& dict );
void fromCbor ( const QByteArray& cbor, QQtDictionary& dict );
void parseCborNodeToDictionary ( const QCborValue& node, QQtDictionary& object );
void packDictionaryToCborNode ( const QQtDictionary& node, QCborValue& object );
#endif
QByteArray toJson ( const QQtDictionary& dict, int indent = 0 ); QByteArray toJson ( const QQtDictionary& dict, int indent = 0 );
void fromJson ( const QByteArray& json, QQtDictionary& dict ); void fromJson ( const QByteArray& json, QQtDictionary& dict );
void parseJsonValue ( const QJsonValue& value, QQtDictionary& parent ); void parseJsonValue ( const QJsonValue& value, QQtDictionary& parent );
@ -610,6 +621,18 @@ void QQtDictionary::fromCSV ( const QByteArray& csv,
} }
#endif #endif
#if QT_VERSION >= QT_VERSION_CHECK(5,12,0)
QByteArray QQtDictionary::toCbor() const
{
return ::toCbor ( *this );
}
void QQtDictionary::fromCbor ( const QByteArray& cbor )
{
::fromCbor ( cbor, *this );
}
#endif
bool QQtDictionary::operator == ( const QQtDictionary& other ) const bool QQtDictionary::operator == ( const QQtDictionary& other ) const
{ {
if ( m_type == other.getType() && if ( m_type == other.getType() &&
@ -1918,3 +1941,188 @@ void parseOrderedDictionary ( QQtDictionary& node, const QQtOrderedDictionary& o
} }
} }
#if QT_VERSION >= QT_VERSION_CHECK(5,12,0)
QByteArray toCbor ( const QQtDictionary& dict )
{
QCborValue root;
packDictionaryToCborNode ( dict, root );
return root.toCbor();
}
void fromCbor ( const QByteArray& cbor, QQtDictionary& dict )
{
QCborValue root;
QCborParserError error;
root.fromCbor ( cbor, &error );
if ( error.error != QCborError::NoError )
{
pline() << ( int ) ( error.error ) << error.errorString() << error.offset;
return;
}
parseCborNodeToDictionary ( root, dict );
}
void parseCborNodeToDictionary ( const QCborValue& node, QQtDictionary& object )
{
switch ( node.type() )
{
case QCborValue::Integer:
{
object = node.toInteger();
}
break;
case QCborValue::SimpleType:
case QCborValue::False:
case QCborValue::True:
case QCborValue::Undefined:
{
if ( node.type() == QCborValue::False || node.type() == QCborValue::True )
{
object = node.toBool();
break;
}
object = node.toInteger();
}
break;
case QCborValue::Double:
object = node.toDouble();
break;
case QCborValue::ByteArray:
object = node.toByteArray();
break;
case QCborValue::String:
object = node.toString();
break;
case QCborValue::DateTime:
object = node.toDateTime();
break;
case QCborValue::Url:
object = node.toUrl();
break;
case QCborValue::RegularExpression:
object = node.toRegularExpression();
break;
case QCborValue::Uuid:
object = node.toUuid();
break;
case QCborValue::Array:
{
QCborArray array = node.toArray();
if ( array.size() <= 0 )
{
object = QQtDictionary ( QQtDictionary::DictList );
break;
}
for ( int i = 0; i < array.size(); i++ )
{
parseCborNodeToDictionary ( array[i], object[i] );
}
}
break;
case QCborValue::Map:
{
QCborMap map = node.toMap();
if ( map.size() <= 0 )
{
object = QQtDictionary ( QQtDictionary::DictMap );
break;
}
for ( QCborMap::Iterator itor = map.begin();
itor != map.end(); itor++ )
{
const QCborValue& qkey = itor.key();
QCborValueRef qvalue = itor.value();
//强制转换为String如果不是String如此强制转换可能出现特别的数据。
QString key = qkey.toString();
parseCborNodeToDictionary ( qvalue, object[key] );
}
}
break;
case QCborValue::Tag:
{
//此处没有判定tag而是全部转换为String。
node.tag();
pline() << ( quint64 ) node.tag();
//其实可以按照tag类型划分成已知的Value类型但是我看基本上全都是String所以此处全都转换成String。
QString value = node.taggedValue().toString();
object = value;
}
break;
case QCborValue::Invalid:
pline() << "invalid Cbor.";
default:
break;
}
}
void packDictionaryToCborNode ( const QQtDictionary& node, QCborValue& result )
{
switch ( node.getType() )
{
case QQtDictionary::DictValue:
{
//null, bool, double, string
p3line() << node.getValue().type();
if ( node.getValue() == QVariant ( QJsonValue() ) )
{
result = QCborValue();
}
else if ( node.getValue().type() == QVariant::Bool )
{
result = QCborValue ( node.getValue().toBool() );
}
else if ( node.getValue().type() == QVariant::Double )
{
result = QCborValue ( node.getValue().toDouble() );
}
else if ( node.getValue().type() == QVariant::String )
{
result = QCborValue ( node.getValue().toString() );
}
else
{
result = QCborValue::fromVariant ( node.getValue() );
}
break;
}
case QQtDictionary::DictList:
{
//"name":[a, b, ...]
QCborArray array;
for ( int i = 0; i < node.getList().size(); i++ )
{
QList<QQtDictionary>& l = node.getList();
QCborValue value;
packDictionaryToCborNode ( l[i], value );
//array.append ( value );
array.push_back ( value );
}
result = array;
break;
}
case QQtDictionary::DictMap:
{
//"name": {"a":"b", "a2":"b2", "a3":["b31", "b32"], "a4":{"a41":"b41", "a42":"b42"}, ...}
QCborMap object;
for ( QMap<QString, QQtDictionary>::Iterator itor = node.getMap().begin(); itor != node.getMap().end(); itor++ )
{
//QMap<QString, QQtDictionary>& m = node.getMap();
const QString& key = itor.key();
const QQtDictionary& srcvalue = itor.value();
QCborValue value;
packDictionaryToCborNode ( srcvalue, value );
object.insert ( key, value );
}
result = object;
break;
}
case QQtDictionary::DictMax:
default:
break;
}
}
#endif

View File

@ -43,6 +43,7 @@ typedef QMutableListIterator<QQtDictionary> QQtDictionaryMutableListIterator;
* fromINI toINI ini * fromINI toINI ini
* fromProperties toProperties Properties Java配置文件的格式 * fromProperties toProperties Properties Java配置文件的格式
* fromCSV toCSV csv * fromCSV toCSV csv
* fromCbor toCbor Cbor
* from函数默认行为为合并clear(); * from函数默认行为为合并clear();
* *
* QVariant 使便 * QVariant 使便
@ -223,6 +224,9 @@ public:
const QString& textEncoding = QString ( "UTF-8" ) const QString& textEncoding = QString ( "UTF-8" )
); );
QByteArray toCbor() const;
void fromCbor ( const QByteArray& cbor );
private: private:
/*节点类型,指示性变量*/ /*节点类型,指示性变量*/
EDictType m_type; EDictType m_type;

View File

@ -46,6 +46,10 @@
#include <QBuffer> #include <QBuffer>
#if QT_VERSION >= QT_VERSION_CHECK(5,12,0)
#include <QCborValue>
#endif
#include <iostream> #include <iostream>
using namespace std; using namespace std;
@ -75,6 +79,13 @@ void fromCSV ( const QByteArray& csv, QQtOrderedDictionary& dict,
#endif #endif
#if QT_VERSION >= QT_VERSION_CHECK(5,12,0)
QByteArray toCbor ( const QQtOrderedDictionary& dict );
void fromCbor ( const QByteArray& cbor, QQtOrderedDictionary& dict );
void parseCborNodeToDictionary ( const QCborValue& node, QQtOrderedDictionary& object );
void packDictionaryToCborNode ( const QQtOrderedDictionary& node, QCborValue& object );
#endif
#ifdef USE_JSONCPP #ifdef USE_JSONCPP
QByteArray toJson ( const QQtOrderedDictionary& dict, int indent = 0 ); QByteArray toJson ( const QQtOrderedDictionary& dict, int indent = 0 );
void fromJson ( const QByteArray& json, QQtOrderedDictionary& dict ); void fromJson ( const QByteArray& json, QQtOrderedDictionary& dict );
@ -99,6 +110,7 @@ void packDictionaryToDomNode ( const QQtOrderedDictionary& node, QDomNode& resul
void parseDictionary ( QQtOrderedDictionary& node, const QQtDictionary& obj ); void parseDictionary ( QQtOrderedDictionary& node, const QQtDictionary& obj );
//#define LOCAL_DEBUG //#define LOCAL_DEBUG
#ifdef LOCAL_DEBUG #ifdef LOCAL_DEBUG
#define p3line() p2line() #define p3line() p2line()
@ -615,6 +627,18 @@ void QQtOrderedDictionary::fromCSV ( const QByteArray& csv,
} }
#endif #endif
#if QT_VERSION >= QT_VERSION_CHECK(5,12,0)
QByteArray QQtOrderedDictionary::toCbor() const
{
return ::toCbor ( *this );
}
void QQtOrderedDictionary::fromCbor ( const QByteArray& cbor )
{
::fromCbor ( cbor, *this );
}
#endif
bool QQtOrderedDictionary::operator == ( const QQtOrderedDictionary& other ) const bool QQtOrderedDictionary::operator == ( const QQtOrderedDictionary& other ) const
{ {
if ( m_type == other.getType() && if ( m_type == other.getType() &&
@ -2350,3 +2374,188 @@ void parseDictionary ( QQtOrderedDictionary& node, const QQtDictionary& obj )
break; break;
} }
} }
#if QT_VERSION >= QT_VERSION_CHECK(5,12,0)
QByteArray toCbor ( const QQtOrderedDictionary& dict )
{
QCborValue root;
packDictionaryToCborNode ( dict, root );
return root.toCbor();
}
void fromCbor ( const QByteArray& cbor, QQtOrderedDictionary& dict )
{
QCborValue root;
QCborParserError error;
root.fromCbor ( cbor, &error );
if ( error.error != QCborError::NoError )
{
pline() << ( int ) ( error.error ) << error.errorString() << error.offset;
return;
}
parseCborNodeToDictionary ( root, dict );
}
void parseCborNodeToDictionary ( const QCborValue& node, QQtOrderedDictionary& object )
{
switch ( node.type() )
{
case QCborValue::Integer:
{
object = node.toInteger();
}
break;
case QCborValue::SimpleType:
case QCborValue::False:
case QCborValue::True:
case QCborValue::Undefined:
{
if ( node.type() == QCborValue::False || node.type() == QCborValue::True )
{
object = node.toBool();
break;
}
object = node.toInteger();
}
break;
case QCborValue::Double:
object = node.toDouble();
break;
case QCborValue::ByteArray:
object = node.toByteArray();
break;
case QCborValue::String:
object = node.toString();
break;
case QCborValue::DateTime:
object = node.toDateTime();
break;
case QCborValue::Url:
object = node.toUrl();
break;
case QCborValue::RegularExpression:
object = node.toRegularExpression();
break;
case QCborValue::Uuid:
object = node.toUuid();
break;
case QCborValue::Array:
{
QCborArray array = node.toArray();
if ( array.size() <= 0 )
{
object = QQtOrderedDictionary ( QQtOrderedDictionary::DictList );
break;
}
for ( int i = 0; i < array.size(); i++ )
{
parseCborNodeToDictionary ( array[i], object[i] );
}
}
break;
case QCborValue::Map:
{
QCborMap map = node.toMap();
if ( map.size() <= 0 )
{
object = QQtOrderedDictionary ( QQtOrderedDictionary::DictMap );
break;
}
for ( QCborMap::Iterator itor = map.begin();
itor != map.end(); itor++ )
{
const QCborValue& qkey = itor.key();
QCborValueRef qvalue = itor.value();
//强制转换为String如果不是String如此强制转换可能出现特别的数据。
QString key = qkey.toString();
parseCborNodeToDictionary ( qvalue, object[key] );
}
}
break;
case QCborValue::Tag:
{
//此处没有判定tag而是全部转换为String。
node.tag();
pline() << ( quint64 ) node.tag();
//其实可以按照tag类型划分成已知的Value类型但是我看基本上全都是String所以此处全都转换成String。
QString value = node.taggedValue().toString();
object = value;
}
break;
case QCborValue::Invalid:
pline() << "invalid Cbor.";
default:
break;
}
}
void packDictionaryToCborNode ( const QQtOrderedDictionary& node, QCborValue& result )
{
switch ( node.getType() )
{
case QQtOrderedDictionary::DictValue:
{
//null, bool, double, string
p3line() << node.getValue().type();
if ( node.getValue() == QVariant ( QJsonValue() ) )
{
result = QCborValue();
}
else if ( node.getValue().type() == QVariant::Bool )
{
result = QCborValue ( node.getValue().toBool() );
}
else if ( node.getValue().type() == QVariant::Double )
{
result = QCborValue ( node.getValue().toDouble() );
}
else if ( node.getValue().type() == QVariant::String )
{
result = QCborValue ( node.getValue().toString() );
}
else
{
result = QCborValue::fromVariant ( node.getValue() );
}
break;
}
case QQtOrderedDictionary::DictList:
{
//"name":[a, b, ...]
QCborArray array;
for ( int i = 0; i < node.getList().size(); i++ )
{
QList<QQtOrderedDictionary>& l = node.getList();
QCborValue value;
packDictionaryToCborNode ( l[i], value );
//array.append ( value );
array.push_back ( value );
}
result = array;
break;
}
case QQtOrderedDictionary::DictMap:
{
//"name": {"a":"b", "a2":"b2", "a3":["b31", "b32"], "a4":{"a41":"b41", "a42":"b42"}, ...}
QCborMap object;
for ( QOrderedMap<QString, QQtOrderedDictionary>::Iterator itor = node.getMap().begin(); itor != node.getMap().end(); itor++ )
{
//QOrderedMap<QString, QQtOrderedDictionary>& m = node.getMap();
const QString& key = itor.key();
const QQtOrderedDictionary& srcvalue = itor.value();
QCborValue value;
packDictionaryToCborNode ( srcvalue, value );
object.insert ( key, value );
}
result = object;
break;
}
case QQtOrderedDictionary::DictMax:
default:
break;
}
}
#endif

View File

@ -204,6 +204,9 @@ public:
const QString& textEncoding = QString ( "UTF-8" ) const QString& textEncoding = QString ( "UTF-8" )
); );
QByteArray toCbor() const;
void fromCbor ( const QByteArray& cbor );
private: private:
/*节点类型,指示性变量*/ /*节点类型,指示性变量*/
EDictType m_type; EDictType m_type;