0.3.8 • Published 10 months ago

class-json v0.3.8

Weekly downloads
9
License
MIT
Repository
github
Last release
10 months ago

Comprehensive JSON library for a class.

with TypeScript support

  1. Decorators 1.1 Converting

    * `@Json.type(Ctor: Function, options?)`
    * `@Json.array(Ctor: Function, options?)`
    * `@Json.name(jsonName: string)`
    * `@Json.ignore()`
    * `@Json.converter(converter: IJsonConverter)`

    1.2 Validation

    * `@Rule.required(message?)`
    * `@Rule.minLength(count: number, message?)`
    * `@Rule.maxLength(count: number, message?)`
    * `@Rule.minimum(val: number, message?)`
    * `@Rule.maximum(val: number, message?)`
    * `@Rule.pattern(rgx: RegExp, message?)`
    * `@Rule.validate(validator: IValueValidator)`
    
    Message Type:
    
    * string
    * string templates: `Interpolations: ~[value] ~[property] ~[model.foo]`
    * Custom Function: `(value, model) => string`
import { Json, Rule } from 'class-json'

class Transaction {

    @Json.type(BigInt)
    @Rule.minimum(100n)
    value: bigint

    @Json.type(Date)
    @Ruke.required()
    executeAt: Date
}
  1. Classes

    • Serializable<T>
interface Serializable<T> {

    constructor (partial: Partial<T>)

    /** Create an instance from json, all Types from decorators will be restored. */
    static fromJSON (json): T

    /** Use Rules from decorators to validate the instance */
    static validate (instance: T): IRuleError[]

    /** Serialize instance to JSON object. All Types from decorators will be properly converted to JSON supported types */
    toJSON(): object
}

Constructor: accepts partial object, example:

// instead of:
let foo = new Foo();
foo.lorem = 'lorem';
foo.ipsum = 'ipsum';

// more convinient way would be:
let foo = new Foo({
    lorem: 'lorem',
    ipsum: 'ipsum'
});
  1. Namespaces

    • JsonConvert
      • toJSON(model, settings)
      • fromJSON<T>(model, settings): T

© 2021 Atmajs

0.3.8

10 months ago

0.3.7

3 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

4 years ago

0.2.99

4 years ago

0.2.98

4 years ago

0.2.97

4 years ago

0.3.0

4 years ago

0.3.1

4 years ago

0.2.95

4 years ago

0.2.94

4 years ago

0.2.93

4 years ago

0.2.92

4 years ago

0.2.90

4 years ago

0.2.89

4 years ago

0.2.88

4 years ago

0.2.85

4 years ago

0.2.87

4 years ago

0.2.84

4 years ago

0.2.83

4 years ago

0.2.82

4 years ago

0.2.81

4 years ago

0.2.80

4 years ago

0.2.79

4 years ago

0.2.78

4 years ago

0.2.76

4 years ago

0.2.75

4 years ago

0.2.74

4 years ago

0.2.73

4 years ago

0.2.72

4 years ago

0.2.71

4 years ago

0.2.69

4 years ago

0.2.68

4 years ago

0.2.67

4 years ago

0.2.66

4 years ago

0.2.65

4 years ago

0.2.63

4 years ago

0.2.64

4 years ago

0.2.61

4 years ago

0.2.60

4 years ago

0.2.59

4 years ago

0.2.58

4 years ago

0.2.55

5 years ago