Skip to content

Commit 480d706

Browse files
committed
use QJsonDocument
1 parent ab61f21 commit 480d706

File tree

1 file changed

+17
-40
lines changed

1 file changed

+17
-40
lines changed

src/clientcommands.cpp

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <QEventLoop>
1212
#include <QTimer>
1313
#include <QTextStream>
14+
#include <QJsonDocument>
15+
#include <QJsonObject>
1416

1517
#include "merginuserauth.h"
1618

@@ -108,34 +110,6 @@ void ClientCommands::sync()
108110
download( lp.projectNamespace, lp.projectName );
109111
}
110112

111-
static void printVal( bool isJsonFormat,
112-
QTextStream &out,
113-
const QString &key,
114-
const QString &val,
115-
bool isLast )
116-
{
117-
118-
QChar separator;
119-
QChar quote;
120-
QString space;
121-
if ( isJsonFormat )
122-
{
123-
separator = ',';
124-
quote = '"';
125-
}
126-
else
127-
{
128-
separator = '\n';
129-
space = " ";
130-
}
131-
132-
out << quote << key << quote << ":" << space << quote << val << quote
133-
;
134-
135-
if ( !isLast )
136-
out << separator;
137-
}
138-
139113
void ClientCommands::info( bool isJsonFormat )
140114
{
141115
Q_ASSERT( isAuthorized() );
@@ -145,24 +119,27 @@ void ClientCommands::info( bool isJsonFormat )
145119

146120
QTextStream out( stdout );
147121

148-
QChar prefix;
149-
QChar suffix;
122+
150123
if ( isJsonFormat )
151124
{
152-
suffix = '}';
153-
prefix = '{';
125+
QJsonObject jsonData
126+
{
127+
{"name", QJsonValue( lp.projectName )},
128+
{"namespace", QJsonValue( lp.projectNamespace )},
129+
{"id", QJsonValue( lp.id() )},
130+
{"localVersion", QJsonValue( lp.localVersion )}
131+
};
132+
133+
QJsonDocument doc( jsonData );
134+
out << doc.toJson( QJsonDocument::Compact );
154135
}
155136
else
156137
{
157-
suffix = '\n';
138+
out << "name: " << lp.projectName << endl;
139+
out << "namespace: " << lp.projectNamespace << endl;
140+
out << "id: " << lp.id() << endl;
141+
out << "localVersion: " << lp.localVersion << endl;
158142
}
159-
160-
out << prefix;
161-
printVal( isJsonFormat, out, "name", lp.projectName, false );
162-
printVal( isJsonFormat, out, "namespace", lp.projectNamespace, false );
163-
printVal( isJsonFormat, out, "id", lp.id(), false );
164-
printVal( isJsonFormat, out, "localVersion", QString::number( lp.localVersion ), true );
165-
out << suffix;
166143
}
167144

168145

0 commit comments

Comments
 (0)