Options
All
  • Public
  • Public/Protected
  • All
Menu

The JsonConverter wraps the provided serializers, deserializers and/or stringifiers and revivers into a utility object which can convert between JSON and a given TypeScript type, providing a unified API which also handles lists.

Remember that, when all is said and done, parsing JSON into typed objects relies on a type assertion. If the server does not actually provide the data the deserializers claim it does, there's nothing neither the compiler, nor the runtime, can do about it. So the type provided by this utility class is only as strong and correct as the deserializers given.

Type parameters

  • ItemT

Hierarchy

  • JsonConverter

Index

Constructors

constructor

  • new JsonConverter<ItemT>(config?: { deserializer?: (jsonObject: Record<string, any>) => ItemT; replacer?: (k: string, v: any) => any; reviver?: (k: string, v: any) => any; serializer?: (item: Partial<ItemT>) => Record<string, any> }): JsonConverter<ItemT>
  • Type parameters

    • ItemT

    Parameters

    • Optional config: { deserializer?: (jsonObject: Record<string, any>) => ItemT; replacer?: (k: string, v: any) => any; reviver?: (k: string, v: any) => any; serializer?: (item: Partial<ItemT>) => Record<string, any> }

      An object carrying the wanted reviver, replacer and (de)serializer to handle an object type.

      When serializing an object, this utility will first transform the object using the given serializer (if any), and then JSON.stringify the transformed object using the given replacer (if any).

      Conversely, when deserializing an object, the utility will first JSON.parse the JSON using the given reviver (if any), and then transform the resulting object using the given deserializer (if any).

      • Optional deserializer?: (jsonObject: Record<string, any>) => ItemT
          • (jsonObject: Record<string, any>): ItemT
          • Parameters

            • jsonObject: Record<string, any>

            Returns ItemT

      • Optional replacer?: (k: string, v: any) => any
          • (k: string, v: any): any
          • Parameters

            • k: string
            • v: any

            Returns any

      • Optional reviver?: (k: string, v: any) => any
          • (k: string, v: any): any
          • Parameters

            • k: string
            • v: any

            Returns any

      • Optional serializer?: (item: Partial<ItemT>) => Record<string, any>
          • (item: Partial<ItemT>): Record<string, any>
          • Parameters

            • item: Partial<ItemT>

            Returns Record<string, any>

    Returns JsonConverter<ItemT>

Methods

objectToString

  • objectToString(o: undefined | null | Partial<ItemT> | Partial<ItemT>[]): string
  • Parameters

    • o: undefined | null | Partial<ItemT> | Partial<ItemT>[]

    Returns string

stringToArray

  • stringToArray(json: undefined | null | string): ItemT[]
  • Parameters

    • json: undefined | null | string

    Returns ItemT[]

stringToObject

  • stringToObject(json: undefined | null | string): undefined | ItemT
  • Parameters

    • json: undefined | null | string

    Returns undefined | ItemT