Skip to content

SuperJson

This guide explains how to replace the default oRPC RPC serializer with SuperJson.

INFO

While the default oRPC serializer is faster and more efficient, SuperJson is widely adopted and may be preferred for compatibility.

SuperJson Serializer

WARNING

The SuperJsonSerializer supports only the data types that SuperJson handles, plus AsyncIteratorObject at the root level for Event Iterator. It does not support all RPC supported types.

ts
import { , , , ,  } from '@orpc/client'
import type {  } from '@orpc/client/standard'
import {  } from '@orpc/shared'
import  from 'superjson'

export class  implements <, keyof > {
  (: unknown): object {
    if (()) {
      return (, {
        : async (: unknown) => .(),
        : async () => {
          return new ({
            : .(().()),
            : ,
          })
        },
      })
    }

    return .()
  }

  (: any): unknown {
    if (()) {
      return (, {
        : async  => .(),
        : async () => {
          if (!( instanceof ))
            return 

          const  = .(. as any)

          if (()) {
            return (, { :  })
          }

          return new ({
            : ,
            : ,
          })
        },
      })
    }

    return .()
  }
}

SuperJson Handler

ts
import type {  } from '@orpc/client/standard'
import type { ,  } from '@orpc/server'
import type {  } from '@orpc/server/fetch'
import {  } from '@orpc/server/fetch'
import {  } from '@orpc/server/plugins'
import type {  } from '@orpc/server/standard'
import { , ,  } from '@orpc/server/standard'

export interface < extends >
  extends <>, <<>, 'plugins'> {
  /**
   * Enable or disable the StrictGetMethodPlugin.
   *
   * @default true
   */
  ?: boolean
}

export class < extends > extends <> {
  constructor(: <any, >, : <<>> = {}) {
    . ??= []

    const  = . ?? true

    if () {
      ..(new ())
    }

    const  = new ()
    const  = new ()
    const  = new ( as any)

    super(new (, , , ), )
  }
}
ts
import type {  } from '@orpc/client'
import { ,  } from '@orpc/client/standard'
import type { , ,  } from '@orpc/client/standard'
import type {  } from '@orpc/client/fetch'
import {  } from '@orpc/client/fetch'

export interface < extends >
  extends <>,
  <<>, 'plugins'>,
  <> { }

export class < extends > extends <> {
  constructor(: <>) {
    const  = new ()
    const  = new ()
    const  = new ( as any, )

    super(, , )
  }
}

Released under the MIT License.