MCPcopy Create free account
hub / github.com/Tablecruncher/tablecruncher / exportJSON

Method exportJSON

src/csvtable.cpp:757–824  ·  view source on GitHub ↗

* @param path Export to path * @param cb Callback that updates statusbar * @param win pointer to CsvWindow * @param convertNumbers Should every number-like value be converted to a number? */

Source from the content-addressed store, hash-verified

755 * @param convertNumbers Should every number-like value be converted to a number?
756 */
757int CsvTable::exportJSON(std::string path, void (*cb)(const char*, void *), void *win, bool convertNumbers) {
758 table_index_t rowCount, colCount;
759 int retCode = saveReturnCode::SAVE_OKAY;
760 std::ofstream output(path, std::ios::binary);
761 const int MAX_MSG_LEN = 500;
762 char msg[MAX_MSG_LEN + 1];
763 std::map<std::string, std::string> item;
764 Helper::parseNumberStruct parsedNum;
765
766 if( output ) {
767 rowCount = storage.rows();
768 colCount = storage.columns();
769 output.clear();
770 nlohmann::json json;
771 output << "[";
772 for( table_index_t i = 0; i < rowCount; i++) {
773 json.clear();
774 if( hasCustomHeaderRow ) {
775 for (table_index_t c = 0; c < colCount; ++c) {
776 if( convertNumbers ) {
777 parsedNum = Helper::parseNumber( getCell(i,c) );
778 if( parsedNum.myType == Helper::parseNumberType::INT ) {
779 json[headerRow->at(c)] = parsedNum.myInteger;
780 } else if( parsedNum.myType == Helper::parseNumberType::FLOAT ) {
781 json[headerRow->at(c)] = parsedNum.myFloat;
782 } else {
783 json[headerRow->at(c)] = getCell(i,c);
784 }
785 } else {
786 json[headerRow->at(c)] = getCell(i,c);
787 }
788 }
789 output << json.dump();
790 } else {
791 for (table_index_t c = 0; c < colCount; ++c) {
792 if( convertNumbers ) {
793 parsedNum = Helper::parseNumber( getCell(i,c) );
794 if( parsedNum.myType == Helper::parseNumberType::INT ) {
795 json.push_back( parsedNum.myInteger );
796 } else if( parsedNum.myType == Helper::parseNumberType::FLOAT ) {
797 json.push_back( parsedNum.myFloat );
798 } else {
799 json.push_back(getCell(i,c));
800 }
801 } else {
802 json.push_back(getCell(i,c));
803 }
804 }
805 output << json.dump();
806 }
807 if( i == rowCount - 1 ) { // letzte Zeile
808 break;
809 }
810 output << ",";
811 if( (i % 20000) == 0 ) {
812 snprintf(msg, MAX_MSG_LEN, "Saved %d lines to file.", i);
813 cb(msg, win);
814 }

Callers 1

saveFileMethod · 0.80

Calls 7

rowsMethod · 0.80
columnsMethod · 0.80
atMethod · 0.80
push_backMethod · 0.80
closeMethod · 0.80
clearMethod · 0.45
dumpMethod · 0.45

Tested by

no test coverage detected