/** * FileWriterASCIIJson.cc * Created on: 21 oct. 2014 * Author: AKKA */ #include "FileWriterASCIIJson.hh" #include namespace AMDA { namespace ParamOutputImpl { namespace Download { namespace FileWriter { FileWriterASCIIJson::FileWriterASCIIJson(AMDA::Parameters::ParameterManager& pParameterManager) : FileWriterASCIIAbstract(pParameterManager) { } FileWriterASCIIJson::~FileWriterASCIIJson(void) { if (_outputFile.is_open()) writeEndFile(); } std::string FileWriterASCIIJson::getExtension(void) { return "json"; } std::string FileWriterASCIIJson::getDataFillCharacter(void) { return ""; } void FileWriterASCIIJson::writeBeginFile(void) { isFirstInfo.clear(); if (!_outputFile.is_open()) return; _outputFile << "{"; isFirstInfo.push_back(true); } void FileWriterASCIIJson::writeEndFile(void) { if (!_outputFile.is_open()) return; _outputFile << "}"; isFirstInfo.clear(); } void FileWriterASCIIJson::writeBeginGeneralDescription(void) { if (!_outputFile.is_open()) return; _outputFile << "\"infos\" : {"; isFirstInfo.back() = false; isFirstInfo.push_back(true); } void FileWriterASCIIJson::writeEndGeneralDescription(void) { if (!_outputFile.is_open()) return; _outputFile << "}"; isFirstInfo.pop_back(); } void FileWriterASCIIJson::writeErrorInfo(std::string msg) { if (!_outputFile.is_open()) return; std::string msgCorrection = msg; boost::replace_all(msgCorrection, "\n", " "); boost::replace_all(msgCorrection, "\"", " "); writeSingleInfo("error", msgCorrection, 0); } void FileWriterASCIIJson::writeBeginInfoGroup(std::string title, int /*level*/) { if (!_outputFile.is_open()) return; if (!isFirstInfo.back()) _outputFile << ", "; if (title.empty()) _outputFile << "{"; else { std::string titleWithoutSpace = title; boost::replace_all(titleWithoutSpace, " ", "_"); _outputFile << "\"" << titleWithoutSpace << "\": {"; } isFirstInfo.back() = false; isFirstInfo.push_back(true); } void FileWriterASCIIJson::writeEndInfoGroup(int /*level*/) { if (!_outputFile.is_open()) return; _outputFile << "}"; isFirstInfo.pop_back(); } void FileWriterASCIIJson::writeSingleInfo(std::string key, std::string value, int /*level*/) { if (!_outputFile.is_open()) return; if (!isFirstInfo.back()) _outputFile << ", "; _outputFile << "\"" << key << "\" : \"" << value << "\""; isFirstInfo.back() = false; } void FileWriterASCIIJson::writeBeginInfoList(std::string title, int /*level*/) { if (!_outputFile.is_open()) return; if (!isFirstInfo.back()) _outputFile << ", "; _outputFile << "\"" << title << "\" : ["; isFirstInfo.back() = false; isFirstInfo.push_back(true); } void FileWriterASCIIJson::writeEndInfoList(void) { if (!_outputFile.is_open()) return; _outputFile << "]"; isFirstInfo.pop_back(); } void FileWriterASCIIJson::writeBeginFieldsDescription(void) { if (!_outputFile.is_open()) return; if (!isFirstInfo.empty() && !isFirstInfo.back()) _outputFile << ", "; _outputFile << "\"fields\" : ["; isFirstInfo.back() = false; isFirstInfo.push_back(true); } void FileWriterASCIIJson::writeEndFieldsDescription(void) { if (!_outputFile.is_open()) return; _outputFile << "]"; isFirstInfo.pop_back(); } void FileWriterASCIIJson::writeFieldDescription(std::string paramId) { if (!_outputFile.is_open()) return; if(!isFirstInfo.back()) _outputFile << ", "; _outputFile << "{"; _outputFile << "\"id\" : \"" << paramId << "\""; _outputFile << ", \"name\" : \"" << _fieldInfoMap[paramId].name << "\""; _outputFile << ", \"ucd\" : \"" << _fieldInfoMap[paramId].ucd << "\""; _outputFile << ", \"type\" : \""; switch (_fieldInfoMap[paramId].type) { case DT_FLOAT: _outputFile << "float"; break; case DT_SHORT: _outputFile << "short"; break; case DT_INT: _outputFile << "int"; break; case DT_DOUBLE: _outputFile << "double"; break; case DT_LONGDOUBLE: _outputFile << "long double"; break; case DT_LOGICAL: _outputFile << "logical"; break; case DT_TIME: _outputFile << "time"; break; default: _outputFile << "unknown"; } std::stringstream size; if ((_fieldInfoMap[paramId].type != DT_TIME) && !_fieldInfoMap[paramId].componentsList.empty() && (_fieldInfoMap[paramId].dimSize1 * _fieldInfoMap[paramId].dimSize2 != (int)_fieldInfoMap[paramId].componentsList.size())) { //some components are not write size << _fieldInfoMap[paramId].componentsList.size(); } else { //all components are write size << _fieldInfoMap[paramId].dimSize1; if (_fieldInfoMap[paramId].dimSize2 > 1) { size << "*"; size << _fieldInfoMap[paramId].dimSize2; } } _outputFile << "\", \"size\" : \"" << size.str() << "\""; _outputFile << "}"; isFirstInfo.back() = false; } void FileWriterASCIIJson::writeBeginData(void) { if (!_outputFile.is_open()) return; if (!isFirstInfo.empty() && !isFirstInfo.back()) _outputFile << ", "; _outputFile << "\"datas\" : ["; isFirstInfo.back() = false; isFirstInfo.push_back(true); } void FileWriterASCIIJson::writeEndData(void) { if (!_outputFile.is_open()) return; _outputFile << "]"; isFirstInfo.pop_back(); } void FileWriterASCIIJson::writeDataRecord(std::string record) { if (!_outputFile.is_open()) return; if (!isFirstInfo.back()) _outputFile << ", "; _outputFile << "[" << record << "]"; isFirstInfo.back() = false; } std::string FileWriterASCIIJson::getDataStartTag(bool isTimeData) { if (isTimeData) return "\""; return ", \""; } std::string FileWriterASCIIJson::getDataStopTag(void) { return "\""; } std::string FileWriterASCIIJson::getDataValueSeparator(void) { return ", "; } } /* namespace FileWriter */ } /* namespace Download */ } /* namespace ParamOutputImpl */ } /* namespace AMDA */