json

package
v0.27.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2021 License: BSD-3-Clause Imports: 1 Imported by: 1

README

JSON dependencies

There might be interest in changing the json encoder from "encoding/json", the standard implementation, for different reasons. Some projects offer different build tags; Gin allows you to utilise -tags=jsoniter to swap out their internal use of "encoding/json" with jsoniter. However, this introduces N dependencies and their respective implementation to work as a drop in replacement.

A simpler manner can be dep injection; by introducing interfaces/function pointers in the Disgord config:

client := disgord.New(disgord.Config{ JSONUnmarshaler: json.Unmarshal })

This may seem familiar with people of OOP background. The issue is that you need to throw this reference into every implementation that touches json encoding. It fails as soon as you need custom unmarshal/marshal methods.

github.com/diamondburned/arikawa, another Discord lib, treats this issue by exporting json related variables inside a json package. Allowing devs to directly overwrite the default value. I like it, so I'm going with a variation of that.

To change the json dependency, you simply import the disgord/json package and adjust the exported variables as needed.

Here the standard json implementation is swapped out with jsoniter:

import (
    "github.com/andersfylling/disgord/json"
    jsoniter "github.com/json-iterator/go"
)

var j = jsoniter.ConfigCompatibleWithStandardLibrary

json.Marshal = j.Marshal
json.Unmarshal = j.Unmarshal

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Marshal       = json.Marshal
	Unmarshal     = json.Unmarshal
	MarshalIndent = json.MarshalIndent
	Indent        = json.Indent
	NewDecoder    = json.NewDecoder
	NewEncoder    = json.NewEncoder
)

Functions

This section is empty.

Types

type Marshaler

type Marshaler interface {
	MarshalJSON() ([]byte, error)
}

type RawMessage

type RawMessage = json.RawMessage

type Unmarshaler

type Unmarshaler interface {
	UnmarshalJSON([]byte) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL