jsonify

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

jsonify

Package jsonify provides utility functions for JSON encoding of various types, including protobuf messages and standard Go types.

Go Reference Go Report Card

It uses a custom jsoniter configuration for improved performance and consistent output.

Features

  • Fast JSON encoding using jsoniter
  • Support for protobuf messages
  • Consistent output with sorted map keys
  • Easy-to-use API with both error-returning and panic-on-error versions

Installation

To install jsonify, use go get:

go get github.com/goaux/jsonify

Usage

Here are some examples of how to use jsonify:

package main

import (
    "fmt"
    "github.com/goaux/jsonify"
)

func main() {
    // Encoding to []byte
    data := map[string]interface{}{
        "name": "John Doe",
        "age":  30,
    }

    bytes, err := jsonify.Bytes(data)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Bytes: %s\n", bytes)

    // Encoding to string
    str, err := jsonify.String(data)
    if err != nil {
        panic(err)
    }
    fmt.Printf("String: %s\n", str)

    // Using MustBytes and MustString (panics on error)
    fmt.Printf("MustBytes: %s\n", jsonify.MustBytes(data))
    fmt.Printf("MustString: %s\n", jsonify.MustString(data))
}

API

  • Bytes(v any) ([]byte, error): Encodes the given value as JSON and returns it as a byte slice.
  • MustBytes(v any) []byte: Similar to Bytes but panics if an error occurs during encoding.
  • String(v any) (string, error): Encodes the given value as JSON and returns it as a string.
  • MustString(v any) string: Similar to String but panics if an error occurs during encoding.

Documentation

Overview

Package jsonify provides utility functions for JSON encoding of various types, including protobuf messages and standard Go types.

It uses a custom jsoniter configuration for improved performance and consistent output.

custom jsoniter configuration:

var config = jsoniter.Config{
	SortMapKeys:            true,
	ValidateJsonRawMessage: true,
}.Froze()

This configuration is similar to jsoniter.ConfigCompatibleWithStandardLibrary. The only difference is that EscapeHTML is set to false.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(v any) ([]byte, error)

Bytes encodes the given value as JSON and returns it as a byte slice.

It handles json.RawMessage, proto.Message, and other types differently. For json.RawMessage, it returns the raw bytes. For proto.Message, it uses protojson for marshaling. For other types, it uses a custom jsoniter configuration.

Example
package main

import (
	"fmt"

	"github.com/goaux/jsonify"
)

func main() {
	b, _ := jsonify.Bytes(map[string]interface{}{"A": true, "B": "<b>"})
	fmt.Printf(">%s<\n", b)
}
Output:

>{"A":true,"B":"<b>"}<

func MustBytes

func MustBytes(v any) []byte

MustBytes is similar to Bytes but panics if an error occurs during encoding.

It's useful when you're certain that the encoding will succeed.

Example
package main

import (
	"fmt"

	"github.com/goaux/jsonify"
)

func main() {
	fmt.Printf(">%s<\n", jsonify.MustBytes(map[string]any{"A": true, "B": "<b>"}))
}
Output:

>{"A":true,"B":"<b>"}<

func MustString

func MustString(v any) string

MustString is similar to String but panics if an error occurs during encoding.

It's useful when you're certain that the encoding will succeed.

Example
package main

import (
	"fmt"

	"github.com/goaux/jsonify"
)

func main() {
	fmt.Println(">" + jsonify.MustString(map[string]any{"A": true, "B": "<b>"}) + "<")
}
Output:

>{"A":true,"B":"<b>"}<

func String

func String(v any) (string, error)

String encodes the given value as JSON and returns it as a string.

It handles json.RawMessage, proto.Message, and other types differently. For json.RawMessage, it returns the raw message as a string. For proto.Message, it uses protojson for marshaling. For other types, it uses a custom jsoniter configuration.

Example
package main

import (
	"fmt"

	"github.com/goaux/jsonify"
)

func main() {
	s, _ := jsonify.String(map[string]interface{}{"A": true, "B": "<b>"})
	fmt.Printf(">%s<\n", s)
}
Output:

>{"A":true,"B":"<b>"}<

Types

This section is empty.

Jump to

Keyboard shortcuts

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