* @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? */
| 755 | * @param convertNumbers Should every number-like value be converted to a number? |
| 756 | */ |
| 757 | int 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 | } |