File indexing completed on 2024-04-14 03:49:00

0001 /*************************************************************************
0002  *  Copyright (C) 2020 by Jean Lima Andrade <jeno.andrade@gmail.com>     *
0003  *                                                                       *
0004  *  This program is free software; you can redistribute it and/or        *
0005  *  modify it under the terms of the GNU General Public License as       *
0006  *  published by the Free Software Foundation; either version 3 of       *
0007  *  the License, or (at your option) any later version.                  *
0008  *                                                                       *
0009  *  This program is distributed in the hope that it will be useful,      *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of       *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
0012  *  GNU General Public License for more details.                         *
0013  *                                                                       *
0014  *  You should have received a copy of the GNU General Public License    *
0015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.*
0016  *************************************************************************/
0017 
0018 #ifndef SERIALIZER_H
0019 #define SERIALIZER_H
0020 
0021 #include "core/markedobject.h"
0022 
0023 #include <QVector>
0024 #include <QString>
0025 
0026 /** Class responsible of writing and reading annotation data from files. */
0027 class Serializer
0028 {
0029 public:
0030     /** Output format of annotation data. */
0031     enum class OutputType {
0032         None,
0033         XML,
0034         JSON
0035     };
0036 
0037     /** Write annotation to a file, the path is changed accordingly to the output_type.
0038      * @param filepath - path of the file, its extension is changed accordingly to output_type.
0039      * @param output_type - type of output to save.
0040      */
0041     static bool write(const QString& filepath, const QVector<MarkedObject*>& objects, OutputType output_type);
0042 
0043     /** Read given file and return the annotated objects inside it.
0044      * @param filepath - path of the file to load.
0045      */
0046     static QVector<MarkedObject*> read(const QString& filepath);
0047 
0048 private:
0049     /** Read given file.
0050      * @return raw data readen.
0051      * @param filepath - path of the file.
0052      */
0053     static QByteArray getData(const QString& filepath);
0054 
0055     /** Iterate through annnotated data and create a JSON document.
0056      * @return JSON document created.
0057      */
0058     static QString toJSON(const QVector<MarkedObject*>& objects);
0059 
0060     /** Iterate through annnotated data and create a XML document.
0061      * @return XML document created.
0062      */
0063     static QString toXML(const QVector<MarkedObject*>& objects);
0064 
0065     /** Read given JSON document and create MarkedObject's objects accordingly.
0066      * @return created objects.
0067      * @param filepath - path of the JSON document.
0068      */
0069     static QVector<MarkedObject*> readJSON(const QString& filepath);
0070 
0071     /** Read given XML document and create MarkedObject's objects accordingly.
0072      * @return created objects.
0073      * @param filepath - path of the XML document.
0074      */
0075     static QVector<MarkedObject*> readXML(const QString& filepath);
0076 
0077     /** Turns annotated objects into the file format of given OutputType.
0078      * @param outputType - file format to serialize the annotated objects.
0079      */
0080     static QString serialize(const QVector<MarkedObject*>& objects, OutputType outputType);
0081 };
0082 #endif // SERIALIZER_H