官网开发中 0803
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* 安全解析后端 JSON 字段
|
||||
*
|
||||
* dashboard 的 JSON 列(s2_features、s1_faq_categories、options_json 等)
|
||||
* 经 Drizzle ORM 返回时可能是字符串或已解析对象,统一转为对象数组。
|
||||
*/
|
||||
export function parseJson<T>(value: string | T | null | undefined, fallback: T): T {
|
||||
if (value == null) return fallback;
|
||||
if (typeof value !== 'string') return value as T;
|
||||
try {
|
||||
const parsed = JSON.parse(value) as T;
|
||||
return parsed;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user