Giter Club home page Giter Club logo

fj4echo's Introduction

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/tomruk/fj4echo

import (
    "fmt"
    "github.com/tomruk/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/tomruk/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")
}

fj4echo's People

Contributors

tomruk avatar

Stargazers

code0o0 avatar Nikolai Gut avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.