jsonOfstream.cpp 5.4 KB
Newer Older
G
Gines Hidalgo 已提交
1
#include <openpose/filestream/jsonOfstream.hpp>
G
gineshidalgo99 已提交
2 3 4

namespace op
{
G
gineshidalgo99 已提交
5 6
    void enterAndTab(std::ofstream& ofstream, const bool humanReadable, const long long bracesCounter,
                     const long long bracketsCounter)
G
gineshidalgo99 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    {
        try
        {
            if (humanReadable)
            {
                ofstream << "\n";
                for (auto i = 0ll ; i < bracesCounter + bracketsCounter ; i++)
                    ofstream << "\t";
            }
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

    JsonOfstream::JsonOfstream(const std::string& filePath, const bool humanReadable) :
        mHumanReadable{humanReadable},
        mBracesCounter{0},
        mBracketsCounter{0},
G
Gines Hidalgo 已提交
27
        upOfstream{new std::ofstream{filePath}}
G
gineshidalgo99 已提交
28 29 30
    {
        try
        {
G
Gines Hidalgo 已提交
31
            if (!filePath.empty() && !upOfstream->is_open())
G
gineshidalgo99 已提交
32 33 34 35 36 37 38 39
                error("Json file could not be opened.", __LINE__, __FUNCTION__, __FILE__);
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

40 41 42 43 44 45 46
    JsonOfstream::JsonOfstream(JsonOfstream&& jsonOfstream) :
        mHumanReadable{jsonOfstream.mHumanReadable},
        mBracesCounter{jsonOfstream.mBracesCounter},
        mBracketsCounter{jsonOfstream.mBracketsCounter}
    {
        try
        {
G
Gines Hidalgo 已提交
47 48
            upOfstream = std::move(jsonOfstream.upOfstream);
            // std::swap(upOfstream, jsonOfstream.upOfstream);
49 50 51 52 53 54 55 56 57 58 59 60 61 62
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

    JsonOfstream& JsonOfstream::operator=(JsonOfstream&& jsonOfstream)
    {
        try
        {
            mHumanReadable = jsonOfstream.mHumanReadable;
            mBracesCounter = jsonOfstream.mBracesCounter;
            mBracketsCounter = jsonOfstream.mBracketsCounter;
G
Gines Hidalgo 已提交
63 64
            upOfstream = std::move(jsonOfstream.upOfstream);
            // std::swap(upOfstream, jsonOfstream.upOfstream);
65 66 67 68 69 70 71 72 73 74
            // Return
            return *this;
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
            return *this;
        }
    }

G
gineshidalgo99 已提交
75 76 77 78
    JsonOfstream::~JsonOfstream()
    {
        try
        {
G
gineshidalgo99 已提交
79 80
            if (upOfstream != nullptr)
                enterAndTab(*upOfstream, mHumanReadable, mBracesCounter, mBracketsCounter);
G
gineshidalgo99 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

            if (mBracesCounter != 0 || mBracketsCounter != 0)
            {
                std::string errorMessage = "Json file wronly generated";
                if (mBracesCounter != 0)
                    errorMessage += ", number \"{\" != number \"}\": " + std::to_string(mBracesCounter) + ".";
                else if (mBracketsCounter != 0)
                    errorMessage += ", number \"[\" != number \"]\": " + std::to_string(mBracketsCounter) + ".";
                else
                    errorMessage += ".";
                error(errorMessage, __LINE__, __FUNCTION__, __FILE__);
            }
        }
        catch (const std::exception& e)
        {
96
            errorDestructor(e.what(), __LINE__, __FUNCTION__, __FILE__);
G
gineshidalgo99 已提交
97 98 99 100 101 102 103 104
        }
    }

    void JsonOfstream::objectOpen()
    {
        try
        {
            mBracesCounter++;
G
Gines Hidalgo 已提交
105
            *upOfstream << "{";
G
gineshidalgo99 已提交
106 107 108 109 110 111 112 113 114 115 116 117
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

    void JsonOfstream::objectClose()
    {
        try
        {
            mBracesCounter--;
G
Gines Hidalgo 已提交
118 119
            enterAndTab(*upOfstream, mHumanReadable, mBracesCounter, mBracketsCounter);
            *upOfstream << "}";
G
gineshidalgo99 已提交
120 121 122 123 124 125 126 127 128 129 130 131
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

    void JsonOfstream::arrayOpen()
    {
        try
        {
            mBracketsCounter++;
G
Gines Hidalgo 已提交
132 133
            *upOfstream << "[";
            enterAndTab(*upOfstream, mHumanReadable, mBracesCounter, mBracketsCounter);
G
gineshidalgo99 已提交
134 135 136 137 138 139 140 141 142 143 144 145
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

    void JsonOfstream::arrayClose()
    {
        try
        {
            mBracketsCounter--;
G
Gines Hidalgo 已提交
146 147
            enterAndTab(*upOfstream, mHumanReadable, mBracesCounter, mBracketsCounter);
            *upOfstream << "]";
G
gineshidalgo99 已提交
148 149 150 151 152 153 154
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

155 156 157 158 159 160 161 162 163 164 165 166 167
    void JsonOfstream::version(const std::string& version)
    {
        try
        {
            key("version");
            plainText(version);
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

G
gineshidalgo99 已提交
168 169 170 171
    void JsonOfstream::key(const std::string& string)
    {
        try
        {
G
Gines Hidalgo 已提交
172 173
            enterAndTab(*upOfstream, mHumanReadable, mBracesCounter, mBracketsCounter);
            *upOfstream << "\"" + string + "\":";
G
gineshidalgo99 已提交
174 175 176 177 178 179 180 181 182 183 184
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

    void JsonOfstream::enter()
    {
        try
        {
G
Gines Hidalgo 已提交
185
            enterAndTab(*upOfstream, mHumanReadable, mBracesCounter, mBracketsCounter);
G
gineshidalgo99 已提交
186 187 188 189 190 191 192
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }
}