Fix tests, reinstate old workaround.

This commit is contained in:
Sam Rose 2025-03-03 14:29:17 +00:00
parent 4286a05827
commit fd8929daae
No known key found for this signature in database
2 changed files with 23 additions and 6 deletions

View File

@ -1246,10 +1246,7 @@ if (descriptions.length) {
})
describe.each([
[
RowExportFormat.CSV,
(val: any) => JSON.stringify(val).replace(/"/g, "'"),
],
[RowExportFormat.CSV, (val: any) => JSON.stringify(val)],
[RowExportFormat.JSON, (val: any) => val],
])("import validation (%s)", (_, userParser) => {
const basicSchema: TableSchema = {

View File

@ -270,6 +270,26 @@ function parseJsonExport<T>(value: any) {
if (typeof value !== "string") {
return value
}
const parsed = JSON.parse(value)
return parsed as T
try {
const parsed = JSON.parse(value)
return parsed as T
} catch (e: any) {
if (
e.message.startsWith("Expected property name or '}' in JSON at position ")
) {
// In order to store JSON within CSVs what we used to do is replace double
// quotes with single quotes. This results in invalid JSON, so the line
// below is a workaround to parse it. However, this method of storing JSON
// was never valid, and we don't do it anymore. However, people may have
// exported data and stored it, hoping to be able to restore it later, so
// we leave this in place to support that.
const parsed = JSON.parse(value.replace(/'/g, '"'))
return parsed as T
}
// It is not valid JSON
throw e
}
}