# typeof-jsonc

> Convert json or jsonc to typescript type (interface)

Latest version **1.1.10** (published 2019-06-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install typeof-jsonc
pnpm add typeof-jsonc
yarn add typeof-jsonc
bun add typeof-jsonc
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.10 |
| Published | 2019-06-05 |
| First published | 2018-11-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 657.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 73 |
| Author | wulunyi |
| Maintainers | wulunyi |
| Keywords | json, jsonc, ts, typescript, types, interface |

## Links

- npm: https://www.npmjs.com/package/typeof-jsonc
- Repository: https://github.com/wulunyi/typeof-jsonc
- npm.io page: https://npm.io/package/typeof-jsonc

## Dependencies (4)

- [dts-dom](https://npm.io/package/dts-dom.md) ^3.2.0
- [esprima](https://npm.io/package/esprima.md) ^4.0.1
- [change-case](https://npm.io/package/change-case.md) ^3.0.2
- [jsonc-parser](https://npm.io/package/jsonc-parser.md) ^2.1.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.10 (latest) — 2019-06-05
- 1.1.11-beta.2 (beta) — 2019-07-02
- 1.1.9-0 (es3) — 2019-05-26
- 1.1.5-0 (next) — 2019-04-05
- 1.1.11-beta.1 — 2019-06-15
- 1.1.11-beta.0 — 2019-06-15
- 1.1.9 — 2019-06-01
- 1.1.8 — 2019-05-11
- 1.1.8-0 — 2019-05-11
- 1.1.7 — 2019-04-26
- 1.1.6 — 2019-04-26
- 1.1.5 — 2019-04-15
- 1.1.4 — 2019-03-28
- 1.1.4-0 — 2019-03-24
- 1.1.3 — 2019-03-22
- … 12 more at https://npm.io/package/typeof-jsonc/versions

## README

# typeof-jsonc [![npm version](https://badge.fury.io/js/typeof-jsonc.svg)](https://badge.fury.io/js/typeof-jsonc)

`typeof-jsonc` is a library for Convert json or jsonc string to typescript type (interface)

[Online Playgrounds](http://lanfeng.fun/#/typeofjsonc)

## Support

- Basic types
- Array types (support auto merge)
- Same struct auto merge
- Comments
- JsDocComments
- Custom naming
- Json
- Jsonc
- Not standard jsonc or json

## Usage

### install

```
npm install typeof-jsonc -S
```

or

```
yarn add typeof-jsonc -S
```

### Demo

`Jsonc string`

```jsonc
{
  "barr": [
    // aaa
    "aaa",
    "bbb"
  ],
  "name": "lanfeng", // this is name
  // this is demo
  "demo": {
    "hello": "world"
  },
  /** this is arr */
  "arr": [
    {
      "age": 1
    },
    2
  ],
  "a": "hello",
  "b": ["a", "b"]
}
```

`Convert script`

```typescript
import fs from 'fs';
import path from 'path';

import { typeofJsonc } from '../src/index';

const text = fs.readFileSync(path.resolve('./demo/test'), {
  encoding: 'utf-8',
});

console.log(
  typeofJsonc(text, 'Response', {
    prefix: 'I',
    rootFlags: 1,
    disallowComments: false,
    addExport: true,
    singleLineJsDocComments: true,
  }),
);
```

`Output`

```typescript
export interface IResponse {
  /**  aaa  */
  barr: string[];
  /**  this is name  */
  name: string;
  /**  this is demo  */
  demo: IDemo;
  /**  this is arr  */
  arr: (IArr | number)[];
  a: string;
  b: string[];
}

/**  this is arr  */
export interface IArr {
  age: number;
}

export interface IDemo {
  hello: string;
}
```

## API

- typeofJsonc(jsonc: string, name: string, options?: Options)

```typescript
interface Options {
  /** type name prefix */
  prefix?: string; // default ''
  /**  customer named */
  onName?: (name: string) => string;
  /** Add export keywords */
  addExport?: boolean; // default false
  /**
   * Identifiers are e.g. legal variable names. They may not be reserved words
   * None = 0,
   * Module = 1,
   * InAmbientNamespace = 2,
   */
  rootFlags?: number; // default 0
  disallowComments?: boolean; // defalut true
  singleLineJsDocComments?: boolean; // default false
}
```

| option                  | type                     | default                              | desc                                                                                                                 |
| ----------------------- | ------------------------ | ------------------------------------ | -------------------------------------------------------------------------------------------------------------------- |
| prefix                  | string                   | ""                                   | type name prefix                                                                                                     |
| onName                  | (name: string) => string | (name: string) => `${prefix}${name}` | Custom naming If this option set prefix will not work                                                                |
| addExport               | boolean                  | false                                | Add export keywords, when set true rootFlags set will not work                                                       |
| rootFlags               | number                   | 0                                    | Identifiers are e.g. legal variable names. They may not be reserved words None = 0 Module = 1 InAmbientNamespace = 2 |
| disallowComments        | boolean                  | true                                 | Whether to prohibit the generation of comments                                                                       |
| singleLineJsDocComments | boolean                  | false                                | Single-line display when single-line comment                                                                         |

## Version

### 1.1.9

- Implementation refactor
- Same struct auto merge

### 1.1.8

- Fix addExport bug
- Pack umd and es lib

### 1.1.6

- Support singleLineJsDocComments options

### 1.1.5

- Support convert not standard jsonc or json

`Standard jsonc or json demo`

```jsonc
{
  "name": "test",
  "age": 13,
  "loves": ["A", "B"]
}
```

`Not standard jsonc or json demo`

```
{
  name: '',
  age: 13,
  loves: ['A', 'B']
}
```

## License

MIT

---
_Source: https://npm.io/package/typeof-jsonc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
