Skip to content
Snippets Groups Projects
ToJSON.cpp 2.83 KiB
Newer Older
  • Learn to ignore specific revisions
  • 数学の武士's avatar
    数学の武士 committed
    #include "ToJSON.h"
    #include "Event.h"
    
    #include <fstream>
    
    数学の武士's avatar
    数学の武士 committed
    #include <sstream>
    
    数学の武士's avatar
    数学の武士 committed
    
    using namespace std;
    
    string JSON::Error = "";
    
    string JSON::SeparateRows = "}, \n";
    string JSON::SeparateSubRows = ", \n";
    
    string JSON::FinishRows = "}\n\t}";
    string JSON::FinishSubRows = "}";
    string JSON::Finish = "}\n";
    
    string JSON::StartRow(const string& name)
    {
        string row = "\n{ \"" + name + "\" : ";
    
        return row;
    }
    
    string JSON::StartSubRow(const string& name)
    {
        string row = "\t\t\"" + name + "\" : ";
    
        return row;
    }
    
    string JSON::StartTypedBlock(const string& name)
    {
        string row = "\n\t\"" + name + "\" : \n\t{ \n";
    
        return row;
    }
    
    
    数学の武士's avatar
    数学の武士 committed
    string Number2String(const double Val)
    
    数学の武士's avatar
    数学の武士 committed
    {
    
    数学の武士's avatar
    数学の武士 committed
        ostringstream StrTSValue;
        StrTSValue << Val << scientific;
        string NumberAsString = StrTSValue.str();
    
    数学の武士's avatar
    数学の武士 committed
    
    
    数学の武士's avatar
    数学の武士 committed
        return NumberAsString;
    }
    
    数学の武士's avatar
    数学の武士 committed
    
    
    数学の武士's avatar
    数学の武士 committed
    bool JSON::GenerateRow(EventData Event, string &ResRow, const string& EndRowJSON)
    {
        string EventModeName = JSON::StartSubRow("Event mode") + "\"" + Event.GetEventModeName() + "\"" + JSON::SeparateSubRows;
        string FullElapsedTime = JSON::StartSubRow(string("Full elapsed time")) + Number2String(Event.elapsed_time) + JSON::SeparateSubRows;
    
    数学の武士's avatar
    数学の武士 committed
    
        double mean_elapsed = Event.GetMeanElapsedTime();
    
    数学の武士's avatar
    数学の武士 committed
        string MeanElapsedTime = JSON::StartSubRow(string("Mean elapsed time")) + Number2String(mean_elapsed) + JSON::SeparateSubRows;
    
    数学の武士's avatar
    数学の武士 committed
        string TimeSeries = JSON::StartSubRow(string("Time series")) + "[";
    
        int TimeSeriesLen = Event.time_series.size();
        for (int i = 0; i < TimeSeriesLen - 1; i++)
        {
    
    数学の武士's avatar
    数学の武士 committed
            // ostringstream StrTSValue;
            // StrTSValue << Event.time_series[i];
            // string strObj = StrTSValue.str();
            // // printf("Event.time_series[i] = %e, AccurateValue %s\n", Event.time_series[i], AccurateValue);
            // TimeSeries += strObj + ", ";
    
            TimeSeries += Number2String(Event.time_series[i]) + ", ";
    
    数学の武士's avatar
    数学の武士 committed
        }
    
        if(TimeSeriesLen != 0)
        {
    
    数学の武士's avatar
    数学の武士 committed
            // ostringstream StrTSValue;
            // StrTSValue << Event.time_series[TimeSeriesLen - 1];
            // string strObj = StrTSValue.str();
            TimeSeries += Number2String(Event.time_series[TimeSeriesLen - 1]);
    
    数学の武士's avatar
    数学の武士 committed
        }
        TimeSeries += "]" ;
    
    数学の武士's avatar
    数学の武士 committed
        // printf("TimeSeries %s\n", TimeSeries.c_str());
    
    数学の武士's avatar
    数学の武士 committed
    
    
        string JSONrow = JSON::StartSubRow(Event.event_name) + "{" + EventModeName + FullElapsedTime + MeanElapsedTime + TimeSeries + EndRowJSON;
    
    数学の武士's avatar
    数学の武士 committed
        
        ResRow = JSONrow;
    
        return true;
    }
    
    bool JSON::WriteOutput(const string& Filename, const string& OutputData)
    {
        ofstream file;
    
    数学の武士's avatar
    数学の武士 committed
        file.open(Filename.c_str(), ios::out);
    
    数学の武士's avatar
    数学の武士 committed
    
        if (!file) 
        {
            string error_definition = "Can't open file " + Filename + "\n";
            JSON::Error = error_definition;
            return false;
        }
    
        file << OutputData;
    
    数学の武士's avatar
    数学の武士 committed
        // file.write((char *)&OutputData, sizeof(string));
    
    数学の武士's avatar
    数学の武士 committed
        file.close();
    
    
    数学の武士's avatar
    数学の武士 committed
        // printf("OutputData %s\n", OutputData.c_str());
    
    
    数学の武士's avatar
    数学の武士 committed
        return true;
    }