Skip to content

Json

JSON utilities with Effect Schema integration.

Provides type-safe JSON operations including type guards, parsing, encoding, and validation using Effect Schema. Supports JSON primitives, objects, and recursive value structures with comprehensive error handling.

Import

typescript
import { Json } from '@wollybeard/kit'
typescript
import * as Json from '@wollybeard/kit/json'

Codec

[C] codec

typescript
Codec<Value>

Codec for JSON values with pretty-printing. Uses Effect's parseJson for decoding.

[C] encode

typescript
Encode<Value>

Encode a JSON value to a pretty-printed string.

[C] decode

typescript
Decode<Value>

Parse a JSON string to a typed value. Uses Effect's parseJson for better error messages.

Schemas

[C] Primitive

typescript
{
  parse: ;
  ;((value: unknown) => string | number | boolean | null)
}

Exported schemas for parsing JSON types. These are used in tests and provide parse methods.

[C] Value

typescript
{
  parse: ;
  ;((value: unknown) => Value)
}

[C] PrimitiveSchema

typescript
Union<[typeof String, typeof Number, typeof Boolean, typeof Null]>

JSON primitive value schema. Matches: string, number, boolean, or null.

[C] ValueSchema

typescript
Schema<Value, Value, never>

JSON value schema. Matches any valid JSON value: primitives, objects, or arrays (recursively).

[C] ObjectSchema

typescript
Record$<typeof String, Schema<Value, Value, never>>

JSON object schema. Matches objects with string keys and JSON values.

[C] parseJsonSchema

typescript
SchemaClass<unknown, string, never>

Schema for parsing JSON strings to unknown values. Uses Effect's parseJson for better error handling.

[F] parseJsonAs

typescript
<A>(schema: Schema<A, A, never>): transform<SchemaClass<unknown, string, never>, Schema<A, A, never>>

Schema for parsing JSON with type validation.

[C] ObjectParser

typescript
{ parse: (value: unknown) => { readonly[x: string]: Value; }; }

Type Guards

[F] isPrimitive

typescript
(value: unknown): boolean

Type guard to check if a value is a JSON primitive.

[F] isValue

typescript
(value: unknown): boolean

Type guard to check if a value is a valid JSON value.

[F] isObject

typescript
(value: unknown): boolean

Type guard to check if a value is a JSON object.

Types

[U] Primitive

typescript
type Primitive = string | number | boolean | null

JSON primitive type. Matches: string, number, boolean, or null.

[T] Obj

typescript
type Obj = { [key in string]?: Value }

JSON object type.

[U] Value

typescript
type Value = Primitive | Obj | Value[]

JSON value type. Matches any valid JSON value: primitives, objects, or arrays (recursively).

[T] Object

typescript
type Obj = { [key in string]?: Value }

JSON object type.