🧠
Funkcia Docs
🧠
Funkcia Docs
  • Welcome
  • Data types
    • Option
      • Error Propagation
      • Do Notation
    • Result
      • Error Propagation
      • Do Notation
    • OptionAsync
      • Error Propagation
      • Do Notation
    • ResultAsync
      • Error Propagation
      • Do Notation
  • Modules
    • Exceptions
    • Functions
    • JSON
    • Predicate
    • URI
    • URL
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Modules

JSON

Funkcia provides a safe JSON module to work with value encoding/decoding without breaking your application.

SafeJSON

SafeJSON provides a safe wrapper around JavaScript's native JSON methods, returning Result types instead of throwing exceptions, preserving all signature overloads of the original namespace.

parse

Converts a JavaScript Object Notation (JSON) string into an object, wrapped in a Result.

import { SafeJSON } from 'funkcia/json';

//       ┌─── Result<unknown, SyntaxError>
//       â–¼
const goodJson = SafeJSON.parse('{ "name": "John" }');
// Output: Ok({ name: "John" })

const invalidJson = SafeJSON.parse('{ "name": John }');
// Output: Error(SyntaxError: Unexpected token 'J', "{ "name": John }" is not valid JSON)

stringify

Converts a JavaScript value to a JavaScript Object Notation (JSON) string, wrapped in a Result.

import { SafeJSON } from 'funkcia/json';

// With function replacer
//      ┌─── Result<string, TypeError>
//      â–¼
const result = SafeJSON.stringify(
  { name: "John", age: 30 },
  (key, value) => typeof value === 'number' ? value.toString() : value,
  2
);
// Output: Ok('{\n  "name": "John",\n  "age": "30"\n}')

// With array replacer
//      ┌─── Result<string, TypeError>
//      â–¼
const result = SafeJSON.stringify(
  { name: "John", age: 30, email: "john@example.com" },
  ['name', 'age'],
  2
);
// Output: Ok('{\n  "name": "John",\n  "age": 30\n}')
PreviousFunctionsNextPredicate

Last updated 3 months ago

Was this helpful?