1.2.0 • Published 3 years ago

@lancercomet/suntori.generator v1.2.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 years ago

SunTori codegen

SunTori npm version

A codegen to create SunTori codes from JSON.

import { generate } from '@lancercomet/suntori.generator'

const result = await generate({
  jsonObject: {
    name: 'John Smith',
    age: 200,
    address: [
      {
        name: 'Home',
        locaiton: 'The earth'
      }
    ]
  },
  rootClassName: 'User'
})

Result:

@Serializable()
class UserAddress {
  @JsonProperty('name')
  name: string = ''

  @JsonProperty('location')
  location: string = ''
}

@Serializable()
class User {
  @JsonProperty('name')
  name: string = ''

  @JsonProperty('age')
  age: number = 0

  @JsonProperty({
    name: 'address',
    type: UserAddress
  })
  address: UserAddress[] = []
}

Options

  • jsonObject: never The JSON payload. Should be a JavaScript Object.
  • rootClassName: string = 'Root' The name for generated class. And it will be used as prefix for all subclasses.
  • useCamelCase: boolean = true Use camelCase during code generation.
  • addReadonly: boolean = true Add readonly modifier to all members of a class.

About null

All null JSON fields would be converted to the unknown, as well as empty arrays.

await generate({
  jsonObject: {
    a: null,
    b: []
  }
})

// To

@Serializable()
class Root {
  @JsonProperty('a')
  a: unknown = null

  @JsonProperty('b')
  b: unknown[] = []
}

License

Apache-2.0