19 #include <nlohmann/json.hpp>
21 using namespace nlohmann;
23 static void jumpSpace(
const QString &txt,
int &i )
25 while ( i < txt.length() && txt.at( i ).isSpace() )
29 QString QgsPostgresStringUtils::getNextString(
const QString &txt,
int &i,
const QString &sep )
32 QString cur = txt.mid( i );
33 if ( cur.startsWith(
'"' ) )
35 QRegExp stringRe(
"^\"((?:\\\\.|[^\"\\\\])*)\".*" );
36 if ( !stringRe.exactMatch( cur ) )
38 QgsMessageLog::logMessage( QObject::tr(
"Cannot find end of double quoted string: %1" ).arg( txt ), QObject::tr(
"PostgresStringUtils" ) );
41 i += stringRe.cap( 1 ).length() + 2;
43 if ( !txt.midRef( i ).startsWith( sep ) && i < txt.length() )
45 QgsMessageLog::logMessage( QObject::tr(
"Cannot find separator: %1" ).arg( txt.mid( i ) ), QObject::tr(
"PostgresStringUtils" ) );
49 return stringRe.cap( 1 ).replace( QLatin1String(
"\\\"" ), QLatin1String(
"\"" ) ).replace( QLatin1String(
"\\\\" ), QLatin1String(
"\\" ) );
53 int sepPos = cur.indexOf( sep );
59 i += sepPos + sep.length();
60 return cur.left( sepPos ).trimmed();
66 QVariantList variantList;
69 QString newVal =
string.mid( 1,
string.length() - 2 );
71 if ( newVal.trimmed().startsWith(
'{' ) )
75 QString subarray = newVal;
76 while ( !subarray.isEmpty() )
79 int openedBrackets = 1;
81 while ( i < subarray.length() && openedBrackets > 0 )
85 if ( subarray.at( i ) ==
'}' && !escaped ) openedBrackets--;
86 else if ( subarray.at( i ) ==
'{' && !escaped ) openedBrackets++;
88 escaped = !escaped ? subarray.at( i ) ==
'\\' :
false;
91 variantList.append( subarray.left( ++i ) );
92 i = subarray.indexOf(
',', i );
93 i = i > 0 ? subarray.indexOf(
'{', i ) : -1;
97 subarray = subarray.mid( i );
103 while ( i < newVal.length() )
105 const QString value = getNextString( newVal, i, QStringLiteral(
"," ) );
106 if ( value.isNull() )
108 QgsMessageLog::logMessage( QObject::tr(
"Error parsing PG like array: %1" ).arg( newVal ), QObject::tr(
"PostgresStringUtils" ) );
111 variantList.append( value );
122 for (
const QVariant &v : qgis::as_const( list ) )
127 case QVariant::Type::Int:
128 case QVariant::Type::LongLong:
129 sl.push_back( v.toString() );
132 QString newS = v.toString();
133 if ( newS.startsWith(
'{' ) )
135 sl.push_back( newS );
139 newS.replace(
'\\', QStringLiteral( R
"(\\)" ) );
140 newS.replace( '\"', QStringLiteral( R
"(\")" ) );
141 sl.push_back( "\"" + newS +
"\"" );
147 QString s = sl.join(
',' ).prepend(
'{' ).append(
'}' );