rawjson

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2023 License: Apache-2.0 Imports: 5 Imported by: 1

README

Go Raw JSON Build Status GoDoc License

Provide the raw bytes json.

Install

$ go get -u github.com/xgfone/go-rawjson

Documentation

Overview

Package rawjson provides the raw bytes json.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// The capacity size of the buffer.
	BufCapSize = 512

	// If true, compact the raw bytes json。
	Compact = true
)

Functions

This section is empty.

Types

type Bytes

type Bytes []byte

Bytes represent a raw byte json.

Example
/// Empty
fmt.Println("\nEmpty")

bs, _ := Bytes(nil).MarshalJSON()
fmt.Printf("1: [%s]\n", string(bs))

bs, _ = Bytes("").MarshalJSON()
fmt.Printf("2: [%s]\n", string(bs))

buf := bytes.NewBuffer(nil)
Bytes(nil).WriteTo(buf)
fmt.Printf("3: [%s]\n", buf.String())

buf.Reset()
Bytes(nil).WriteTo(buf)
fmt.Printf("4: [%s]\n", buf.String())

/// No Compact
fmt.Println("\nNo Compact")
Compact = false

bs, _ = Bytes(` {"a" : 123 , "b" : " a b c "} `).MarshalJSON()
fmt.Printf("5: [%s]\n", string(bs))

/// Compact
fmt.Println("\nCompact")
Compact = true

bs, _ = Bytes(` {"a" : 123 , "b" : " a b c "} `).MarshalJSON()
fmt.Printf("6: [%s]\n", string(bs))

bs, _ = Bytes(` 123 `).MarshalJSON()
fmt.Printf("7: [%s]\n", string(bs))

bs, _ = Bytes(` " 123 " `).MarshalJSON()
fmt.Printf("8: [%s]\n", string(bs))
Output:


Empty
1: [""]
2: [""]
3: [""]
4: [""]

No Compact
5: [ {"a" : 123 , "b" : " a b c "} ]

Compact
6: [{"a":123,"b":" a b c "}]
7: [123]
8: [" 123 "]

func (Bytes) MarshalJSON

func (bs Bytes) MarshalJSON() (b []byte, err error)

MarshalJSON implements the interface json.Marshaler.

If Compact is true, compact the bytes. If the bytes is empty, return []byte(`""`) instead.

func (Bytes) MarshalText

func (bs Bytes) MarshalText() ([]byte, error)

MarshalText implements the interface encoding.TextMarshaler, which is equal to bs.MarshalJSON().

func (Bytes) WriteTo

func (bs Bytes) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements the interface io.WriterTo.

Jump to

Keyboard shortcuts

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