jsonconv

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2021 License: BSD-3-Clause Imports: 3 Imported by: 1

README

jsonconv

In the go standard library, encoding/json contains utility functions for encoding and decoding JSON literal strings, but they are unexported. jsonconv provides these functions for use with other software.

The implementations are current as of go 1.16.

Example

// decoding
quoted := []byte(`"foo \"bar\""`)
str, ok := jsonconv.Unquote(quoted)
// str should be `foo "bar"`

// encoding
unquoted := `a \ b`
jsonBytes := jsonconv.Quote(unquoted)
// jsonBytes should be []byte(`"a \\ b"`)

// encoding into a pre-allocated buffer
// var buffer []byte
unquoted := `a \ b`
buffer = jsonconv.AppendQuote(buffer, unquoted)
// []byte(`"a \\ b"`) has been appended to buffer

Why Not strconv?

strconv provides Quote and Unquote functions, but they are intended for handling go literal strings, which are not the same as JSON literal strings.

Documentation

API documentation is available at: https://pkg.go.dev/github.com/7fffffff/jsonconv

License

BSD-3-Clause, same as the go standard library.

Documentation

Overview

Package jsonconv provides functions to encode and decode JSON string literals

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendQuote

func AppendQuote(dest []byte, s string) []byte

AppendQuote appends the double-quoted JSON string literal representing s, to dest and returns the extended buffer

func AppendQuoteBytes

func AppendQuoteBytes(dest, s []byte) []byte

AppendQuoteBytes appends the double-quoted JSON string literal representing s, to dest and returns the extended buffer

func AppendQuoteBytesEscapeHTML

func AppendQuoteBytesEscapeHTML(dest, s []byte) []byte

AppendQuoteBytesEscapeHTML behaves as AppendQuoteBytes but also escapes <, >, and &

func AppendQuoteEscapeHTML

func AppendQuoteEscapeHTML(dest []byte, s string) []byte

AppendQuoteEscapeHTML behaves as AppendQuote but also escapes <, >, and &

func IsValidNumber

func IsValidNumber(s string) bool

IsValidNumber reports whether s is a valid JSON number literal.

func IsValidNumberBytes

func IsValidNumberBytes(s []byte) bool

IsValidNumberBytes reports whether s is a valid JSON number literal.

func Quote added in v0.2.0

func Quote(s string) []byte

Quote returns the double-quoted JSON string literal representing s

func QuoteBytes added in v0.2.0

func QuoteBytes(s []byte) []byte

QuoteBytes returns the double-quoted JSON string literal representing s

func QuoteBytesEscapeHTML added in v0.2.0

func QuoteBytesEscapeHTML(s []byte) []byte

QuoteBytesEscapeHTML behaves as QuoteBytes, but also escapes <, >, and &

func QuoteEscapeHTML added in v0.2.0

func QuoteEscapeHTML(s string) []byte

QuoteEscapeHTML behaves as Quote, but also escapes <, >, and &

func Unquote

func Unquote(s []byte) (t string, ok bool)

Unquote converts a double-quoted JSON string literal s into an actual string t.

func UnquoteBytes

func UnquoteBytes(s []byte) (t []byte, ok bool)

UnquoteBytes converts a double-quoted JSON string literal s to the unquoted and unescaped form

Types

This section is empty.

Jump to

Keyboard shortcuts

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