@art-suite/art-core-ts-json

中文文档
0.5.2发布于 2026/06/19MITTypeScriptESM + CJS近 30 天下载 3254

A TypeScript JSON utility library

原始使用文档
文档目录8 节
  1. Why This Module?
  2. Example Installation and Use (Required)
  3. Functional Overview
  4. TypeScript Types for JSON
  5. Type Guards and Utilities
  6. Conversion Utilities
  7. JSON to JS/TS Code
  8. API Documentation Reference

@art-suite/art-core-ts-json

Seamless, type-safe utilities for working with JSON in TypeScript.

Why This Module?

Working with JSON in JavaScript and TypeScript is common, but the standard APIs lack type safety, ergonomic helpers, and robust conversion tools. Developers often need to validate, convert, and manipulate JSON data, but TypeScript's type system doesn't natively support JSON structures, and conversions between JSON and other JavaScript types can be tedious and error-prone.

  • TypeScript types for JSON: Uses type-fest's Json Types.
  • Common tooling for TypeScript support: Includes type guards and helpers for working with JSON data in a type-safe way.
  • Conversion utilities: Makes it easy to convert between JSON and other JavaScript types, including recursive conversion and normalization.

Example Installation and Use (Required)

Install with npm:

npm install @art-suite/art-core-ts-json

Basic usage:

import {
  JsonValue,
  JsonObject,
  isJsonObject,
  isJsonArray,
  toJsonValue,
  toJsonObject,
  toJsonPropsObject,
  asJsonObject,
  jsonToJs,
} from "@art-suite/art-core-ts-json";

// Type-safe checks
const value: unknown = { foo: "bar" };
if (isJsonObject(value)) {
  // value is now typed as JsonObject
}

// Convert any value to a valid JsonValue (recursively)
const json: JsonValue = toJsonValue(new Date());
// Convert to JsonObject (recursively)
const obj: JsonObject = toJsonObject({ foo: 1, bar: "baz" });
// Get only scalar properties
const props = toJsonPropsObject({ foo: 1, bar: "baz", nested: { a: 2 } });

// Fast, shallow type check
asJsonObject({ foo: 1 }); // returns { foo: 1 }
asJsonObject("not an object"); // returns {}

// Convert JSON back to JS/TS code string
jsonToJs({ foo: "bar", arr: [1, 2, 3] });

Functional Overview

TypeScript Types for JSON

  • JsonValue — Any valid JSON value (object, array, string, number, boolean, or null)
  • JsonObject — JSON object ({ [key: string]: JsonValue })
  • JsonArray — JSON array (JsonValue[])
  • JsonPrimitive — JSON scalar (string, number, boolean, or null)
  • JsonPropsObject — JSON object with only scalar values

Type Guards and Utilities

  • isJsonObject(value) — Checks if a value is a JSON object
  • isJsonArray(value) — Checks if a value is a JSON array
  • isJsonPrimitive(value) — Checks if a value is a JSON scalar
  • isJsonString(value) — Checks if a value is a JSON string
  • isJsonNumber(value) — Checks if a value is a JSON number
  • isJsonBoolean(value) — Checks if a value is a JSON boolean

Conversion Utilities

  • toJsonValue(value, normalizeKeys?) — Recursively converts any value to a valid JsonValue
  • toJsonObject(value, normalizeKeys?) — Recursively converts any value to a valid JsonObject
  • toJsonObjectOrNull(value, normalizeKeys?) — Like toJsonObject, but returns null for nullish input
  • toJsonPropsObject(value) — Returns a JSON object with only scalar properties
  • asJsonObject(value) — Fast, shallow type check for JSON objects

JSON to JS/TS Code

  • jsonToJs(value) — Converts a JSON value to a pretty-printed JS/TS code string

API Documentation Reference

For detailed information on all exported functions and their parameters, please refer to the TypeScript typings and JSDoc comments within the source code.

版本与发布记录

@art-suite/art-core-ts-json 共发布 25 个版本,首次发布于 2025/05/12;过去 12 个月发布了 6 个版本。

版本发布日期距今
0.5.2latest2026/06/194 个月前
0.5.12026/03/297 个月前
0.5.02026/03/277 个月前
0.4.102025/12/0511 个月前
0.4.92025/12/0511 个月前
0.4.82025/12/0411 个月前

关于 @art-suite/art-core-ts-json 的常见问题

@art-suite/art-core-ts-json 是什么?

A TypeScript JSON utility library

如何安装 @art-suite/art-core-ts-json?

在项目目录运行 npm install @art-suite/art-core-ts-json,或使用 pnpm add / yarn add / bun add。

@art-suite/art-core-ts-json 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@art-suite/art-core-ts-json 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@art-suite/art-core-ts-json 可以商用吗?

@art-suite/art-core-ts-json 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@art-suite/art-core-ts-json 的下载量有多少?

根据 npm 官方下载统计,@art-suite/art-core-ts-json 在 2026/10/07 – 2026/11/05 这 30 天内被下载 3,254 次,日均约 108 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@art-suite/art-core-ts-json 还在维护吗?

最新版本 0.5.2 发布于 2026/06/19(4 个月前);至今共发布 25 个版本,其中过去 12 个月发布了 6 个;首次发布于 2025/05/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。