~saiarcot895/libopenshot/qt-json

« back to all changes in this revision

Viewing changes to src/ReaderBase.cpp

  • Committer: Saikrishna Arcot
  • Date: 2015-10-04 15:18:06 UTC
  • Revision ID: saiarcot895@gmail.com-20151004151806-ffpsybuaqc2yz09l
Switch from JsonCPP to Qt JSON.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
string ReaderBase::OutputDebugJSON()
68
68
{
69
69
        // Return formatted string
70
 
        return debug_root.toStyledString();
 
70
        QJsonDocument document;
 
71
        document.setArray(debug_root);
 
72
        return document.toJson().constData();
71
73
}
72
74
 
73
75
 
74
76
// Append debug information as JSON
75
 
void ReaderBase::AppendDebugItem(Json::Value debug_item)
 
77
void ReaderBase::AppendDebugItem(QJsonValue debug_item)
76
78
{
77
79
        // Append item to root array
78
80
        debug_root.append(debug_item);
90
92
                // Don't do anything
91
93
                return;
92
94
 
93
 
        Json::Value debug_item;
94
 
        debug_item["method"] = method_name;
 
95
        QJsonObject debug_item;
 
96
        debug_item["method"] = method_name.c_str();
95
97
 
96
98
        // Output to standard output
97
99
        #pragma omp critical (debug_output)
101
103
 
102
104
                // Add attributes to method JSON
103
105
                if (arg1_name.length() > 0) {
104
 
                        debug_item[arg1_name] = arg1_value;
 
106
                    debug_item[arg1_name.c_str()] = arg1_value;
105
107
                        cout << arg1_name << "=" << arg1_value;
106
108
                }
107
109
                if (arg2_name.length() > 0) {
108
 
                        debug_item[arg2_name] = arg2_value;
 
110
                    debug_item[arg2_name.c_str()] = arg2_value;
109
111
                        cout << ", " << arg2_name << "=" << arg2_value;
110
112
                }
111
113
                if (arg3_name.length() > 0) {
112
 
                        debug_item[arg3_name] = arg3_value;
 
114
                    debug_item[arg3_name.c_str()] = arg3_value;
113
115
                        cout << ", " << arg3_name << "=" << arg3_value;
114
116
                }
115
117
                if (arg4_name.length() > 0) {
116
 
                        debug_item[arg4_name] = arg4_value;
 
118
                    debug_item[arg4_name.c_str()] = arg4_value;
117
119
                        cout << ", " << arg4_name << "=" << arg4_value;
118
120
                }
119
121
                if (arg5_name.length() > 0) {
120
 
                        debug_item[arg5_name] = arg5_value;
 
122
                    debug_item[arg5_name.c_str()] = arg5_value;
121
123
                        cout << ", " << arg5_name << "=" << arg5_value;
122
124
                }
123
125
                if (arg6_name.length() > 0) {
124
 
                        debug_item[arg6_name] = arg6_value;
 
126
                    debug_item[arg6_name.c_str()] = arg6_value;
125
127
                        cout << ", " << arg6_name << "=" << arg6_value;
126
128
                }
127
129
 
129
131
                cout << ")" << endl;
130
132
 
131
133
                // Append method to root array
132
 
                debug_root.append(debug_item);
 
134
                debug_root.append(QJsonValue(debug_item));
133
135
        }
134
136
}
135
137
 
174
176
}
175
177
 
176
178
// Generate Json::JsonValue for this object
177
 
Json::Value ReaderBase::JsonValue() {
 
179
QJsonObject ReaderBase::JsonValue() {
178
180
 
179
181
        // Create root json object
180
 
        Json::Value root;
 
182
    QJsonObject root;
181
183
        root["has_video"] = info.has_video;
182
184
        root["has_audio"] = info.has_audio;
183
185
        root["has_single_image"] = info.has_single_image;
184
 
        root["duration"] = info.duration;
185
 
        stringstream filesize_stream;
186
 
        filesize_stream << info.file_size;
187
 
        root["file_size"] = filesize_stream.str();
 
186
    root["duration"] = info.duration;
 
187
    root["file_size"] = info.file_size;
188
188
        root["height"] = info.height;
189
189
        root["width"] = info.width;
190
190
        root["pixel_format"] = info.pixel_format;
191
 
        root["fps"] = Json::Value(Json::objectValue);
192
 
        root["fps"]["num"] = info.fps.num;
193
 
        root["fps"]["den"] = info.fps.den;
 
191
    root["fps"] = QJsonObject();
 
192
    root["fps"].toObject()["num"] = info.fps.num;
 
193
    root["fps"].toObject()["den"] = info.fps.den;
194
194
        root["video_bit_rate"] = info.video_bit_rate;
195
 
        root["pixel_ratio"] = Json::Value(Json::objectValue);
196
 
        root["pixel_ratio"]["num"] = info.pixel_ratio.num;
197
 
        root["pixel_ratio"]["den"] = info.pixel_ratio.den;
198
 
        root["display_ratio"] = Json::Value(Json::objectValue);
199
 
        root["display_ratio"]["num"] = info.display_ratio.num;
200
 
        root["display_ratio"]["den"] = info.display_ratio.den;
201
 
        root["vcodec"] = info.vcodec;
202
 
        stringstream video_length_stream;
203
 
        video_length_stream << info.video_length;
204
 
        root["video_length"] = video_length_stream.str();
 
195
    root["pixel_ratio"] = QJsonObject();
 
196
    root["pixel_ratio"].toObject()["num"] = info.pixel_ratio.num;
 
197
    root["pixel_ratio"].toObject()["den"] = info.pixel_ratio.den;
 
198
    root["display_ratio"] = QJsonObject();
 
199
    root["display_ratio"].toObject()["num"] = info.display_ratio.num;
 
200
    root["display_ratio"].toObject()["den"] = info.display_ratio.den;
 
201
    root["vcodec"] = info.vcodec.c_str();
 
202
    root["video_length"] = (long long) info.video_length;
205
203
        root["video_stream_index"] = info.video_stream_index;
206
 
        root["video_timebase"] = Json::Value(Json::objectValue);
207
 
        root["video_timebase"]["num"] = info.video_timebase.num;
208
 
        root["video_timebase"]["den"] = info.video_timebase.den;
 
204
    root["video_timebase"] = QJsonObject();
 
205
    root["video_timebase"].toObject()["num"] = info.video_timebase.num;
 
206
    root["video_timebase"].toObject()["den"] = info.video_timebase.den;
209
207
        root["interlaced_frame"] = info.interlaced_frame;
210
208
        root["top_field_first"] = info.top_field_first;
211
 
        root["acodec"] = info.acodec;
 
209
    root["acodec"] = info.acodec.c_str();
212
210
        root["audio_bit_rate"] = info.audio_bit_rate;
213
211
        root["sample_rate"] = info.sample_rate;
214
212
        root["channels"] = info.channels;
215
213
        root["channel_layout"] = info.channel_layout;
216
214
        root["audio_stream_index"] = info.audio_stream_index;
217
 
        root["audio_timebase"] = Json::Value(Json::objectValue);
218
 
        root["audio_timebase"]["num"] = info.audio_timebase.num;
219
 
        root["audio_timebase"]["den"] = info.audio_timebase.den;
 
215
    root["audio_timebase"] = QJsonObject();
 
216
    root["audio_timebase"].toObject()["num"] = info.audio_timebase.num;
 
217
    root["audio_timebase"].toObject()["den"] = info.audio_timebase.den;
220
218
 
221
219
        // return JsonValue
222
220
        return root;
223
221
}
224
222
 
225
223
// Load Json::JsonValue into this object
226
 
void ReaderBase::SetJsonValue(Json::Value root) {
 
224
void ReaderBase::SetJsonValue(QJsonObject root) {
227
225
 
228
226
        // Set data from Json (if key is found)
229
227
        if (!root["has_video"].isNull())
230
 
                info.has_video = root["has_video"].asBool();
 
228
        info.has_video = root["has_video"].toBool();
231
229
        if (!root["has_audio"].isNull())
232
 
                info.has_audio = root["has_audio"].asBool();
 
230
        info.has_audio = root["has_audio"].toBool();
233
231
        if (!root["has_single_image"].isNull())
234
 
                info.has_single_image = root["has_single_image"].asBool();
 
232
        info.has_single_image = root["has_single_image"].toBool();
235
233
        if (!root["duration"].isNull())
236
 
                info.duration = root["duration"].asDouble();
 
234
        info.duration = root["duration"].toDouble();
237
235
        if (!root["file_size"].isNull())
238
 
                info.file_size = atoll(root["file_size"].asString().c_str());
 
236
        info.file_size = root.value("file_size").toVariant().toLongLong();
239
237
        if (!root["height"].isNull())
240
 
                info.height = root["height"].asInt();
 
238
        info.height = root["height"].toInt();
241
239
        if (!root["width"].isNull())
242
 
                info.width = root["width"].asInt();
 
240
        info.width = root["width"].toInt();
243
241
        if (!root["pixel_format"].isNull())
244
 
                info.pixel_format = root["pixel_format"].asInt();
 
242
        info.pixel_format = root["pixel_format"].toInt();
245
243
        if (!root["fps"].isNull() && root["fps"].isObject()) {
246
 
                if (!root["fps"]["num"].isNull())
247
 
                        info.fps.num = root["fps"]["num"].asInt();
248
 
                if (!root["fps"]["den"].isNull())
249
 
                info.fps.den = root["fps"]["den"].asInt();
 
244
        if (!root["fps"].toObject()["num"].isNull())
 
245
            info.fps.num = root["fps"].toObject()["num"].toInt();
 
246
        if (!root["fps"].toObject()["den"].isNull())
 
247
        info.fps.den = root["fps"].toObject()["den"].toInt();
250
248
        }
251
249
        if (!root["video_bit_rate"].isNull())
252
 
                info.video_bit_rate = root["video_bit_rate"].asInt();
 
250
        info.video_bit_rate = root["video_bit_rate"].toInt();
253
251
        if (!root["pixel_ratio"].isNull() && root["pixel_ratio"].isObject()) {
254
 
                if (!root["pixel_ratio"]["num"].isNull())
255
 
                        info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
256
 
                if (!root["pixel_ratio"]["den"].isNull())
257
 
                        info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
 
252
        if (!root["pixel_ratio"].toObject()["num"].isNull())
 
253
            info.pixel_ratio.num = root["pixel_ratio"].toObject()["num"].toInt();
 
254
        if (!root["pixel_ratio"].toObject()["den"].isNull())
 
255
            info.pixel_ratio.den = root["pixel_ratio"].toObject()["den"].toInt();
258
256
        }
259
257
        if (!root["display_ratio"].isNull() && root["display_ratio"].isObject()) {
260
 
                if (!root["display_ratio"]["num"].isNull())
261
 
                        info.display_ratio.num = root["display_ratio"]["num"].asInt();
262
 
                if (!root["display_ratio"]["den"].isNull())
263
 
                        info.display_ratio.den = root["display_ratio"]["den"].asInt();
 
258
        if (!root["display_ratio"].toObject()["num"].isNull())
 
259
            info.display_ratio.num = root["display_ratio"].toObject()["num"].toInt();
 
260
        if (!root["display_ratio"].toObject()["den"].isNull())
 
261
            info.display_ratio.den = root["display_ratio"].toObject()["den"].toInt();
264
262
        }
265
263
        if (!root["vcodec"].isNull())
266
 
                info.vcodec = root["vcodec"].asString();
 
264
        info.vcodec = root["vcodec"].toString().toLocal8Bit().constData();
267
265
        if (!root["video_length"].isNull())
268
 
                info.video_length = atoll(root["video_length"].asString().c_str());
 
266
        info.video_length = root.value("video_length").toVariant().toLongLong();
269
267
        if (!root["video_stream_index"].isNull())
270
 
                info.video_stream_index = root["video_stream_index"].asInt();
 
268
        info.video_stream_index = root["video_stream_index"].toInt();
271
269
        if (!root["video_timebase"].isNull() && root["video_timebase"].isObject()) {
272
 
                if (!root["video_timebase"]["num"].isNull())
273
 
                        info.video_timebase.num = root["video_timebase"]["num"].asInt();
274
 
                if (!root["video_timebase"]["den"].isNull())
275
 
                        info.video_timebase.den = root["video_timebase"]["den"].asInt();
 
270
        if (!root["video_timebase"].toObject()["num"].isNull())
 
271
            info.video_timebase.num = root["video_timebase"].toObject()["num"].toInt();
 
272
        if (!root["video_timebase"].toObject()["den"].isNull())
 
273
            info.video_timebase.den = root["video_timebase"].toObject()["den"].toInt();
276
274
        }
277
275
        if (!root["interlaced_frame"].isNull())
278
 
                info.interlaced_frame = root["interlaced_frame"].asBool();
 
276
        info.interlaced_frame = root["interlaced_frame"].toBool();
279
277
        if (!root["top_field_first"].isNull())
280
 
                info.top_field_first = root["top_field_first"].asBool();
 
278
        info.top_field_first = root["top_field_first"].toBool();
281
279
        if (!root["acodec"].isNull())
282
 
                info.acodec = root["acodec"].asString();
 
280
        info.acodec = root["acodec"].toString().toLocal8Bit().constData();
283
281
 
284
282
        if (!root["audio_bit_rate"].isNull())
285
 
                info.audio_bit_rate = root["audio_bit_rate"].asInt();
 
283
        info.audio_bit_rate = root["audio_bit_rate"].toInt();
286
284
        if (!root["sample_rate"].isNull())
287
 
                info.sample_rate = root["sample_rate"].asInt();
 
285
        info.sample_rate = root["sample_rate"].toInt();
288
286
        if (!root["channels"].isNull())
289
 
                info.channels = root["channels"].asInt();
 
287
        info.channels = root["channels"].toInt();
290
288
        if (!root["channel_layout"].isNull())
291
 
                info.channel_layout = (ChannelLayout) root["channel_layout"].asInt();
 
289
        info.channel_layout = (ChannelLayout) root["channel_layout"].toInt();
292
290
        if (!root["audio_stream_index"].isNull())
293
 
                info.audio_stream_index = root["audio_stream_index"].asInt();
 
291
        info.audio_stream_index = root["audio_stream_index"].toInt();
294
292
        if (!root["audio_timebase"].isNull() && root["audio_timebase"].isObject()) {
295
 
                if (!root["audio_timebase"]["num"].isNull())
296
 
                        info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
297
 
                if (!root["audio_timebase"]["den"].isNull())
298
 
                        info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
 
293
        if (!root["audio_timebase"].toObject()["num"].isNull())
 
294
            info.audio_timebase.num = root["audio_timebase"].toObject()["num"].toInt();
 
295
        if (!root["audio_timebase"].toObject()["den"].isNull())
 
296
            info.audio_timebase.den = root["audio_timebase"].toObject()["den"].toInt();
299
297
        }
300
298
}
301
299