Skip to content
0

katanakit-js / useTryJsonParse

Function: useTryJsonParse() ​

ts
function useTryJsonParse<T>(text): SafeResult<T>;

Defined in: src/core/services/result.service.ts:53

Parses JSON without throwing; failures come back as a Safe Result.

Consumers keep their own fallback policy (raw string, null, retry, …) by checking ok instead of nesting try/catch.

Type Parameters ​

Type ParameterDefault typeDescription
TunknownExpected parsed shape.

Parameters ​

ParameterTypeDescription
textstringRaw JSON text.

Returns ​

SafeResult<T>

{ data, error, ok } with error.status = 0 on parse failure.

Example ​

ts
const parsed = useTryJsonParse<{ id: number }>(raw);
return parsed.ok ? parsed.data : raw;

Released under the MIT License.