fast

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 License: MIT Imports: 4 Imported by: 0

README

Fast JSON Serializer

This is a package that contains code to use the fastest JSON serializer possible. The idea of combining sonic and go-json into one package seemed very gorgeous to me, so I wrote fj4echo. Now I'm applying the same concept to this library.

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

import (
    sio "github.com/karagenc/socket.io-go"
    jsonparser "github.com/karagenc/socket.io-go/parser/json"
    "github.com/karagenc/socket.io-go/parser/json/serializer/fast"
)

func main() {
    io := sio.NewServer(&sio.ServerConfig{
        ParserCreator: jsonparser.NewCreator(0, fast.New()),
    })

    io.Run()
}

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

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

Customization

import (
    sio "github.com/karagenc/socket.io-go"
    jsonparser "github.com/karagenc/socket.io-go/parser/json"
    "github.com/karagenc/socket.io-go/parser/json/serializer/fast"
    "github.com/bytedance/sonic"
    "github.com/goccy/go-json"
)

func main() {
    io := sio.NewServer(&sio.ServerConfig{
        ParserCreator: jsonparser.NewCreator(0, fast.NewWithConfig(fast.Config{
            SonicConfig: sonic.Config{
                CopyString:  true,
                SortMapKeys: false,
            },

            GoJSON: fast.GoJSONConfig{
                EncodeOptions: []json.EncodeOptionFunc{
                    json.UnorderedMap(),
                },
                DecodeOptions: []json.DecodeOptionFunc{
                    json.DecodeFieldPriorityFirstWin(),
                },
            },
        })),
    })

    io.Run()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func NewWithConfig

func NewWithConfig(config Config) serializer.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