gleamunison/json

The JSON boundary: one codec, one renderer, one escaper.

encode/decode are the binary codec (gleamunison_json). term_to_json and json_quote are the string renderer that the CLI envelope and the HTTP response envelope share — declared here once, because two envelopes that disagree about how a value becomes JSON, or how a string is escaped, are two contracts and not one.

Types

A JSON-RPC request id as data: an integer, a string, or absent. Notifications arrive without an id and are never answered.

pub type JsonId {
  IntId(Int)
  StrId(String)
  NoId
}

Constructors

  • IntId(Int)
  • StrId(String)
  • NoId

Values

pub fn decode(bin: BitArray) -> Result(a, BitArray)
pub fn encode(term: a) -> Result(BitArray, BitArray)
pub fn error_respond(
  id_text: String,
  code: Int,
  message: String,
) -> String

The JSON-RPC error envelope. The message is escaped here, once.

pub fn id_of_dynamic(id: dynamic.Dynamic) -> String

Render the id carried by an LSP frame, whose decoded JSON value has no static type: an integer renders bare, a string quoted, anything else (absent, null, or malformed) renders null.

pub fn id_text(id: JsonId) -> String

Render a request id as JSON text: bare integer, quoted string, or null.

pub fn json_quote(text: String) -> String

JSON-escape a string. The FFI owns escaping (control characters, UTF-8), so the envelope and the values it wraps agree on one encoder.

pub fn respond(id_text: String, result: String) -> String

The JSON-RPC success envelope. The result is already JSON text.

pub fn term_to_json(term: dynamic.Dynamic, depth: Int) -> String

Render a runtime term as JSON: native where the shape is unambiguous, tagged for constructors, opaque where JSON has no honest representation. depth bounds the recursion.

✨ Search Document