|
| 1 | +// |
| 2 | +// JsonRowConverter.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | + |
| 8 | +struct JsonRowConverter { |
| 9 | + let columns: [String] |
| 10 | + let columnTypes: [ColumnType] |
| 11 | + |
| 12 | + private static let maxRows = 50_000 |
| 13 | + |
| 14 | + func generateJson(rows: [[String?]]) -> String { |
| 15 | + let cappedRows = rows.prefix(Self.maxRows) |
| 16 | + let rowCount = cappedRows.count |
| 17 | + |
| 18 | + if rowCount == 0 { |
| 19 | + return "[]" |
| 20 | + } |
| 21 | + |
| 22 | + // Estimate capacity: ~100 bytes per cell as rough heuristic |
| 23 | + var result = String() |
| 24 | + result.reserveCapacity(rowCount * columns.count * 100) |
| 25 | + |
| 26 | + result.append("[\n") |
| 27 | + |
| 28 | + for (rowIdx, row) in cappedRows.enumerated() { |
| 29 | + result.append(" {\n") |
| 30 | + |
| 31 | + for (colIdx, column) in columns.enumerated() { |
| 32 | + result.append(" \"") |
| 33 | + result.append(escapeString(column)) |
| 34 | + result.append("\": ") |
| 35 | + |
| 36 | + guard row.indices.contains(colIdx), let value = row[colIdx] else { |
| 37 | + result.append("null") |
| 38 | + appendPropertySuffix(to: &result, colIdx: colIdx) |
| 39 | + continue |
| 40 | + } |
| 41 | + |
| 42 | + let colType: ColumnType |
| 43 | + if columnTypes.indices.contains(colIdx) { |
| 44 | + colType = columnTypes[colIdx] |
| 45 | + } else { |
| 46 | + colType = .text(rawType: nil) |
| 47 | + } |
| 48 | + |
| 49 | + result.append(formatValue(value, type: colType)) |
| 50 | + appendPropertySuffix(to: &result, colIdx: colIdx) |
| 51 | + } |
| 52 | + |
| 53 | + result.append(" }") |
| 54 | + if rowIdx < rowCount - 1 { |
| 55 | + result.append(",") |
| 56 | + } |
| 57 | + result.append("\n") |
| 58 | + } |
| 59 | + |
| 60 | + result.append("]") |
| 61 | + return result |
| 62 | + } |
| 63 | + |
| 64 | + private func appendPropertySuffix(to result: inout String, colIdx: Int) { |
| 65 | + if colIdx < columns.count - 1 { |
| 66 | + result.append(",") |
| 67 | + } |
| 68 | + result.append("\n") |
| 69 | + } |
| 70 | + |
| 71 | + private func formatValue(_ value: String, type: ColumnType) -> String { |
| 72 | + switch type { |
| 73 | + case .integer: |
| 74 | + return formatInteger(value) |
| 75 | + case .decimal: |
| 76 | + return formatDecimal(value) |
| 77 | + case .boolean: |
| 78 | + return formatBoolean(value) |
| 79 | + case .json: |
| 80 | + return formatJson(value) |
| 81 | + case .blob: |
| 82 | + return formatBlob(value) |
| 83 | + case .text, .date, .timestamp, .datetime, .enumType, .set, .spatial: |
| 84 | + return quotedEscaped(value) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private func formatInteger(_ value: String) -> String { |
| 89 | + if let intVal = Int64(value) { |
| 90 | + return String(intVal) |
| 91 | + } |
| 92 | + if let doubleVal = Double(value), doubleVal == doubleVal.rounded(.towardZero), !doubleVal.isInfinite, !doubleVal.isNaN { |
| 93 | + return String(Int64(doubleVal)) |
| 94 | + } |
| 95 | + return quotedEscaped(value) |
| 96 | + } |
| 97 | + |
| 98 | + private func formatDecimal(_ value: String) -> String { |
| 99 | + if let doubleVal = Double(value), !doubleVal.isInfinite, !doubleVal.isNaN { |
| 100 | + return String(format: "%g", doubleVal) |
| 101 | + } |
| 102 | + return quotedEscaped(value) |
| 103 | + } |
| 104 | + |
| 105 | + private func formatBoolean(_ value: String) -> String { |
| 106 | + switch value.lowercased() { |
| 107 | + case "true", "1", "yes", "on": |
| 108 | + return "true" |
| 109 | + case "false", "0", "no", "off": |
| 110 | + return "false" |
| 111 | + default: |
| 112 | + return quotedEscaped(value) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private func formatJson(_ value: String) -> String { |
| 117 | + guard let data = value.data(using: .utf8) else { |
| 118 | + return quotedEscaped(value) |
| 119 | + } |
| 120 | + do { |
| 121 | + _ = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) |
| 122 | + return value |
| 123 | + } catch { |
| 124 | + return quotedEscaped(value) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private func formatBlob(_ value: String) -> String { |
| 129 | + guard let data = value.data(using: .utf8) else { |
| 130 | + return quotedEscaped(value) |
| 131 | + } |
| 132 | + let encoded = data.base64EncodedString() |
| 133 | + return "\"\(encoded)\"" |
| 134 | + } |
| 135 | + |
| 136 | + private func quotedEscaped(_ value: String) -> String { |
| 137 | + "\"\(escapeString(value))\"" |
| 138 | + } |
| 139 | + |
| 140 | + private func escapeString(_ value: String) -> String { |
| 141 | + var result = String() |
| 142 | + result.reserveCapacity((value as NSString).length) |
| 143 | + |
| 144 | + for scalar in value.unicodeScalars { |
| 145 | + switch scalar { |
| 146 | + case "\"": |
| 147 | + result.append("\\\"") |
| 148 | + case "\\": |
| 149 | + result.append("\\\\") |
| 150 | + case "\n": |
| 151 | + result.append("\\n") |
| 152 | + case "\r": |
| 153 | + result.append("\\r") |
| 154 | + case "\t": |
| 155 | + result.append("\\t") |
| 156 | + default: |
| 157 | + if scalar.value < 0x20 { |
| 158 | + result.append(String(format: "\\u%04X", scalar.value)) |
| 159 | + } else { |
| 160 | + result.append(Character(scalar)) |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return result |
| 166 | + } |
| 167 | +} |
0 commit comments