Skip to content

from json

awekrx edited this page May 29, 2026 · 1 revision

from-json

Import

import { fromJson } from '@dev-suite/decorators/from-json'

Category

  • parameter

Use Case

Parse JSON string argument into object.

Replaces

  • JSON.parse in every endpoint
  • Repeated parse error handling

Example 1

Without decorator

class WebhookController {
  ingest(payload: string) {
    const parsed = JSON.parse(payload);
    return this.handler.handle(parsed);
  }
}

With decorator

import { fromJson } from '@dev-suite/decorators/from-json';

class WebhookController {
  ingest(@fromJson() payload: WebhookPayload) {
    return this.handler.handle(payload);
  }
}

Why better

  • Centralizes cross-cutting behavior.
  • Method/class/property code stays focused on domain logic.

Example 2

Without decorator

class JobController {
  run(configRaw: string) {
    const config = JSON.parse(configRaw);
    return this.jobs.run(config);
  }
}

With decorator

import { fromJson } from '@dev-suite/decorators/from-json';

class JobController {
  run(@fromJson() configRaw: JobConfig) {
    return this.jobs.run(configRaw);
  }
}

Why better

  • Second scenario reuses same policy without duplication.
  • Behavior is more consistent and easier to audit.

Clone this wiki locally