fj4echo

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 5 Imported by: 2

README

Fast JSON Serialization for Echo

By default, Echo uses Go's stdlib encoding/json, which have more performant alternatives such as jsoniter, go-json, and sonic. In almost all of the cases encoding/json is sufficient, and I never needed a faster JSON encoding for my backend. But while playing with Echo, I discovered that it has support for custom JSON serialization (JSONSerializer interface), and the idea of combining sonic and go-json seemed gorgeous to me, so I created this package.

Selection of JSON library is platform dependent. If the platform supports sonic (at the time of writing sonic supports AMD64 and Linux, macOS, & Windows), then sonic is used, otherwise go-json is used.

Also note that go-json's version is 0.10.0 at the time of writing. Please consider your backend's stability before using it.

Usage

go get -u github.com/karagenc/fj4echo

import (
    "fmt"
    "github.com/karagenc/fj4echo"
    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()
    e.JSONSerializer = fj4echo.New()

    serializerType := fj4echo.Type()
    fmt.Printf("Serializer: %s\n", serializerType.Name())

    e.Start(":8000")
}

You can programmatically check which serializer is being used with SerializerType enum (see serializer_type.go):

serializerType := fj4echo.Type()
switch serializerType {
    case fj4echo.SerializerTypeSonic:
        fmt.Println("sonic is choosen. This means that the processor is amd64")
    case fj4echo.SerializerTypeGoJSON:
        fmt.Println("go-json is choosen")
}

Customization

Serializers can be customized. Keep in mind that you might jeopardize security or cause your backend to consume more memory. Please read and gain an understanding of the settings before you change them:

fj4echo uses a default configuration, and you can find it and explanations of the choices behind its settings inside the DefaultConfig function (in config.go).

The default configuration was written with the following things in mind:

  • fj4echo was designed for backend, not for other cases. This means:
    • Do not compromise security.
    • Try not to compromise network latency.

To use a custom configuration, use NewWithConfig function:

import (
    "fmt"
    "github.com/karagenc/fj4echo"
    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()
    config := fj4echo.Config{
        SonicConfig: sonic.Config{
            // I want to live in a dangerous world, so I enable this.
            EscapeHTML: true,
            // Redundant, and the last thing needed on this Earth.
            SortMapKeys: true,
        },
        GoJSON: GoJSONConfig{
            EncodeOptions: []json.EncodeOptionFunc{
                // I want to live in a dangerous world, so I disable HTML escape.
                json.DisableHTMLEscape(),
            },
        },
    }
    e.JSONSerializer = fj4echo.NewWithConfig(config)

    e.Start(":8000")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() echo.JSONSerializer

func NewWithConfig

func NewWithConfig(config Config) echo.JSONSerializer

Types

type Config

type Config struct {
	SonicConfig sonic.Config
	GoJSON      GoJSONConfig
}

func DefaultConfig

func DefaultConfig() Config

type GoJSONConfig

type GoJSONConfig struct {
	EncodeOptions []json.EncodeOptionFunc
	DecodeOptions []json.DecodeOptionFunc
}

type SerializerType

type SerializerType int
const (
	SerializerTypeSonic SerializerType = iota
	SerializerTypeGoJSON
)

func Type

func Type() SerializerType

func (SerializerType) Name

func (t SerializerType) Name() string

Jump to

Keyboard shortcuts

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