CodeSampleX

サンプル

json5 2.2.3: parse and stringify JSON5 with comments, trailing commas, and unquoted keys

検証済みサンプル — npm json5 2.2.3: parse and stringify JSON5 with comments, trailing commas, and unquoted keys. node 22 · linux alpine/x64 · docker で contract…

sha256:4949633e688b8f55c8bdaa12c397674de82d9b348bbf88143c80b899382e84a9

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
1
宣言された環境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-26
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-07

ケース

HOW
ゴール
parse and stringify JSON5 with comments, trailing commas, and unquoted keys
パッケージ
シンボル
  • JSON5.parse
  • JSON5.stringify
環境
node 22.23.2
作成日
2026-08-26T06:53:09Z

コントラクト

  1. JSON5.parse parses objects with unquoted keys, single-quoted keys, and trailing commas
  2. JSON5.parse ignores single-line and multi-line comments
  3. JSON5.parse parses hexadecimal numbers, leading/trailing decimals, explicit plus signs, Infinity, and NaN
  4. JSON5.parse supports single-quoted strings with line continuations and escape sequences
  5. JSON5.parse accepts an optional reviver function to transform parsed key-value pairs
  6. JSON5.stringify serializes objects with unquoted identifier keys and configurable quote formatting
  7. JSON5.stringify preserves Infinity, -Infinity, and NaN as identifiers rather than null
  8. JSON5.stringify accepts a replacer function or array and custom space indentation
  9. JSON5.parse throws SyntaxError on invalid JSON5 input syntax

ファイル

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

ソースアーティファクトをダウンロード (tar.gz)

ソース

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: parse and stringify JSON5 with comments, trailing commas, and unquoted keys
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/json5@2.2.3
Demonstrate these symbols/APIs:
  - JSON5.parse
  - JSON5.stringify

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:68c8812acf7117305caa2d088d5ca4e3d11ba0e91cffc61155567531d241d29c","contract":["JSON5.parse parses objects with unquoted keys, single-quoted keys, and trailing commas","JSON5.parse ignores single-line and multi-line comments","JSON5.parse parses hexadecimal numbers, leading/trailing decimals, explicit plus signs, Infinity, and NaN","JSON5.parse supports single-quoted strings with line continuations and escape sequences","JSON5.parse accepts an optional reviver function to transform parsed key-value pairs","JSON5.stringify serializes objects with unquoted identifier keys and configurable quote formatting","JSON5.stringify preserves Infinity, -Infinity, and NaN as identifiers rather than null","JSON5.stringify accepts a replacer function or array and custom space indentation","JSON5.parse throws SyntaxError on invalid JSON5 input syntax"],"goal":"parse and stringify JSON5 with comments, trailing commas, and unquoted keys","kind":"HOW","packages":["pkg:npm/json5@2.2.3"],"schemaVersion":1,"symbols":["JSON5.parse","JSON5.stringify"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/json5@2.2.3"],"schemaVersion":1,"subject":"pkg:npm/json5@2.2.3","symbols":["JSON5.parse","JSON5.stringify"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-json5",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-json5",
      "version": "1.0.0",
      "dependencies": {
        "json5": "2.2.3"
      }
    },
    "node_modules/json5": {
      "version": "2.2.3",
      "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
      "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==",
      "license": "MIT",
      "bin": {
        "json5": "lib/cli.js"
      },
      "engines": {
        "node": ">=6"
      }
    }
  }
}
package.json
{
  "name": "sample-json5",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "json5": "2.2.3"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "parse and stringify JSON5 with comments, trailing commas, and unquoted keys",
  "kind": "HOW",
  "packages": [
    "pkg:npm/json5@2.2.3"
  ],
  "symbols": [
    "JSON5.parse",
    "JSON5.stringify"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import JSON5 from 'json5';

// 1. JSON5.parse parses objects with unquoted keys, single-quoted keys, and trailing commas
{
  const json5Text = `{
    unquotedKey: 'value1',
    'singleQuotedKey': "value2",
    list: [1, 2, 3,],
  }`;
  const parsed = JSON5.parse(json5Text);
  assert.deepEqual(parsed, {
    unquotedKey: 'value1',
    singleQuotedKey: 'value2',
    list: [1, 2, 3],
  });
}

// 2. JSON5.parse ignores single-line and multi-line comments
{
  const json5WithComments = `// Single line comment
  {
    /* Multi-line
       comment */
    title: "JSON5 Demo", // inline comment
    count: 42, /* another comment */
  }`;
  const parsed = JSON5.parse(json5WithComments);
  assert.deepEqual(parsed, {
    title: 'JSON5 Demo',
    count: 42,
  });
}

// 3. JSON5.parse parses hexadecimal numbers, leading/trailing decimals, explicit plus signs, Infinity, and NaN
{
  const numericText = `{
    hex: 0xdecaf,
    hexUpper: 0XFF,
    leadingDecimal: .5,
    trailingDecimal: 7.,
    positive: +100,
    posInfinity: Infinity,
    negInfinity: -Infinity,
    notANumber: NaN,
  }`;
  const parsed = JSON5.parse(numericText);
  assert.equal(parsed.hex, 0xdecaf);
  assert.equal(parsed.hexUpper, 255);
  assert.equal(parsed.leadingDecimal, 0.5);
  assert.equal(parsed.trailingDecimal, 7);
  assert.equal(parsed.positive, 100);
  assert.equal(parsed.posInfinity, Infinity);
  assert.equal(parsed.negInfinity, -Infinity);
  assert.ok(Number.isNaN(parsed.notANumber));
}

// 4. JSON5.parse supports single-quoted strings with line continuations and escape sequences
{
  const stringText = `{
    single: 'hello world',
    multiLine: 'first line\\\nsecond line',
    escaped: 'line1\\nline2',
  }`;
  const parsed = JSON5.parse(stringText);
  assert.equal(parsed.single, 'hello world');
  assert.equal(parsed.multiLine, 'first linesecond line');
  assert.equal(parsed.escaped, 'line1\nline2');
}

// 5. JSON5.parse accepts an optional reviver function to transform parsed key-value pairs
{
  const json5Text = `{ count: 10, multiplier: 2 }`;
  const parsed = JSON5.parse(json5Text, (key, value) => {
    if (typeof value === 'number') {
      return value * 10;
    }
    return value;
  });
  assert.deepEqual(parsed, {
    count: 100,
    multiplier: 20,
  });
}

// 6. JSON5.stringify serializes objects with unquoted identifier keys and configurable quote formatting
{
  const obj = {
    name: 'test',
    'special-key': 'value',
    count: 1,
  };
  const stringified = JSON5.stringify(obj, { quote: '"' });
  const parsed = JSON5.parse(stringified);
  assert.deepEqual(parsed, obj);
  assert.ok(stringified.includes('name:'));
  assert.ok(stringified.includes('"special-key":'));
}

// 7. JSON5.stringify preserves Infinity, -Infinity, and NaN as identifiers rather than null
{
  const obj = {
    inf: Infinity,
    negInf: -Infinity,
    nan: NaN,
  };
  const stringified = JSON5.stringify(obj);
  assert.ok(stringified.includes('inf:Infinity'));
  assert.ok(stringified.includes('negInf:-Infinity'));
  assert.ok(stringified.includes('nan:NaN'));
  const reparsed = JSON5.parse(stringified);
  assert.equal(reparsed.inf, Infinity);
  assert.equal(reparsed.negInf, -Infinity);
  assert.ok(Number.isNaN(reparsed.nan));
}

// 8. JSON5.stringify accepts a replacer function or array and custom space indentation
{
  const obj = { a: 1, b: 2, c: 3 };
  const filtered = JSON5.stringify(obj, ['a', 'b'], 2);
  const parsedFiltered = JSON5.parse(filtered);
  assert.deepEqual(parsedFiltered, { a: 1, b: 2 });
  assert.ok(filtered.includes('{\n  a: 1,\n  b: 2,\n}'));

  const transformed = JSON5.stringify(obj, (key, value) => {
    if (key === 'a') return value * 100;
    return value;
  });
  const parsedTransformed = JSON5.parse(transformed);
  assert.deepEqual(parsedTransformed, { a: 100, b: 2, c: 3 });
}

// 9. JSON5.parse throws SyntaxError on invalid JSON5 input syntax
{
  assert.throws(
    () => {
      JSON5.parse('{ unclosed: "string }');
    },
    (err) => err instanceof SyntaxError
  );

  assert.throws(
    () => {
      JSON5.parse('{ duplicateColon:: 1 }');
    },
    (err) => err instanceof SyntaxError
  );
}

オリジンシーダー

匿名