diff --git a/openbr/plugins/gallery/binary.cpp b/openbr/plugins/gallery/binary.cpp index 49a982e..bce186c 100644 --- a/openbr/plugins/gallery/binary.cpp +++ b/openbr/plugins/gallery/binary.cpp @@ -261,7 +261,7 @@ BR_REGISTER(Gallery, urlGallery) * \brief Newline-separated JSON objects. * \author Josh Klontz \cite jklontz */ -class jsonGallery : public BinaryGallery +class jsonObjectGallery : public BinaryGallery { Q_OBJECT @@ -289,7 +289,7 @@ class jsonGallery : public BinaryGallery } }; -BR_REGISTER(Gallery, jsonGallery) +BR_REGISTER(Gallery, jsonObjectGallery) } // namespace br diff --git a/openbr/plugins/gallery/json.cpp b/openbr/plugins/gallery/json.cpp new file mode 100644 index 0000000..0dbf1e2 --- /dev/null +++ b/openbr/plugins/gallery/json.cpp @@ -0,0 +1,72 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Copyright 2017 Rank One Computing Corporation * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include +#include +#include +#include + +namespace br +{ + +/*! + * \ingroup galleries + * \brief Treats the file as a JSON document. + * \author Josh Klontz \cite jklontz + */ +class jsonGallery : public FileGallery +{ + Q_OBJECT + Q_PROPERTY(QString label READ get_label WRITE set_label RESET reset_label STORED false) + BR_PROPERTY(QString, label, "PersonID") + + TemplateList readBlock(bool *done) + { + *done = true; + + if (!readOpen()) + qFatal("Failed to open JSON file!"); + + QJsonParseError jsonParseError; + const QJsonDocument jsonDocument = QJsonDocument::fromJson(f.readAll(), &jsonParseError); + if (jsonParseError.error != QJsonParseError::NoError) + qFatal("%s", qPrintable(jsonParseError.error)); + + if (!jsonDocument.isArray()) + qFatal("Expected JSON document to be an array!"); + + TemplateList result; + foreach (const QJsonValue &value, jsonDocument.array()) { + File file(value.toObject().toVariantMap()); + if (!label.isEmpty() && file.contains(label)) + file.set("Label", file.get(label)); + result.append(file); + } + + return result; + } + + void write(const Template &) + { + qFatal("Not implemented!"); + } +}; + +BR_REGISTER(Gallery, jsonGallery) + +} // namespace br + +#include "gallery/json.moc"