API reference@evolu/commonType › JsonValueFromJson

const JsonValueFromJson: transform(
  "JsonValueFromJson",
  Json,
  JsonValue,
  {
    from: (value) => ok(parseJson(value)),
    to: stringifyJsonValue,
  },
);

Defined in: packages/common/src/Type.ts:14307

Transformation Type that parses Json into JsonValue.

Decoding unknown input first validates the Json Brand. Starting from the typed Json parent is infallible. Encoding canonicalizes JSON text.

Example

import { assertEqual, assertOk, JsonValueFromJson } from "@evolu/common";

const result = JsonValueFromJson.fromUnknown('{ "name": "Ada" }');

assertOk(result, { name: "Ada" });
assertEqual(JsonValueFromJson.to(result.value), '{"name":"Ada"}');