json

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: MIT Imports: 10 Imported by: 3

README

json

This package provides support for encoding and decoding JSON format.

Examples

package main

import (
	"fmt"
	"io/ioutil"
	
	"github.com/mithrandie/go-text"
	"github.com/mithrandie/go-text/json"
)

func main() {
	data, err := ioutil.ReadFile("example.json")
	if err != nil {
		panic("file open error")
	}
	
	d := json.NewDecoder()
	structure, escapeType, err := d.Decode(string(data))
	if err != nil {
		panic("json decode error")
	}
	
	e := json.NewEncoder()
	e.EscapeType = escapeType
	e.LineBreak = text.LF
	e.PrettyPrint = true
	e.Palette = json.NewJsonPalette()
	
	encoded := e.Encode(structure)
	fmt.Println(encoded)
}

Documentation

Overview

Package json is a Go library to read and write JSON format.

Index

Constants

View Source
const (
	BeginArray     = '['
	BeginObject    = '{'
	EndArray       = ']'
	EndObject      = '}'
	NameSeparator  = ':'
	ValueSeparator = ','
	QuotationMark  = '"'
	EscapeMark     = '\\'
)
View Source
const (
	FalseValue = "false"
	TrueValue  = "true"
	NullValue  = "null"
)
View Source
const (
	ObjectKeyEffect = "json_object_key"
	StringEffect    = "json_string"
	NumberEffect    = "json_number"
	BooleanEffect   = "json_boolean"
	NullEffect      = "json_null"
)
View Source
const BOOLEAN = 57348
View Source
const EOF = -1
View Source
const FLOAT = 57350
View Source
const INTEGER = 57351
View Source
const NULL = 57349
View Source
const NUMBER = 57346
View Source
const STRING = 57347

Variables

View Source
var WhiteSpaces = []rune{
	32,
	9,
	10,
	13,
}

Functions

func EncodeRune

func EncodeRune(r rune) []byte

func Escape

func Escape(s string) string

func EscapeAll

func EscapeAll(s string) string

func EscapeWithHexDigits

func EscapeWithHexDigits(s string) string

func GetEscapeBuf

func GetEscapeBuf() *bytes.Buffer

func NewJsonPalette

func NewJsonPalette() *color.Palette

func NewSyntaxError

func NewSyntaxError(message string, token Token) error

func ParseJson

func ParseJson(src string, useInteger bool) (Structure, EscapeType, error)

func PutEscapeBuf

func PutEscapeBuf(b *bytes.Buffer)

func Quote

func Quote(s string) string

Types

type Array

type Array []Structure

func (Array) Encode

func (ar Array) Encode() string

type Boolean

type Boolean bool

func (Boolean) Encode

func (b Boolean) Encode() string

func (Boolean) Raw

func (b Boolean) Raw() bool

type DecodeError added in v1.5.0

type DecodeError struct {
	// contains filtered or unexported fields
}

func NewDecodeError added in v1.5.0

func NewDecodeError(line int, char int, message string) *DecodeError

func (DecodeError) Char added in v1.5.0

func (e DecodeError) Char() int

func (DecodeError) Error added in v1.5.0

func (e DecodeError) Error() string

func (DecodeError) Line added in v1.5.0

func (e DecodeError) Line() int

func (DecodeError) Message added in v1.5.0

func (e DecodeError) Message() string

type Decoder

type Decoder struct {
	// Returns numeric values as Integer or Float instead of Number if true.
	UseInteger bool
}

func NewDecoder

func NewDecoder() *Decoder

func (Decoder) Decode

func (d Decoder) Decode(src string) (Structure, EscapeType, error)

type Encoder

type Encoder struct {
	EscapeType     EscapeType
	PrettyPrint    bool
	NanInfHandling NanInfHandling
	FloatFormat    FloatFormat
	LineBreak      text.LineBreak
	IndentSpaces   int
	Palette        *color.Palette
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder() *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(structure Structure) (string, error)

type EscapeType

type EscapeType int
const (
	Backslash        EscapeType = 0
	HexDigits        EscapeType = 1
	AllWithHexDigits EscapeType = 2
)

func Unescape

func Unescape(s string) (string, EscapeType)

type Float

type Float = Number

type FloatFormat added in v1.5.4

type FloatFormat int8
const (
	NoExponent FloatFormat = iota
	ENotationForLargeExponents
)

type Integer

type Integer int64

func (Integer) Encode

func (n Integer) Encode() string

func (Integer) Raw

func (n Integer) Raw() int64

type Lexer

type Lexer struct {
	Scanner
	// contains filtered or unexported fields
}

func (*Lexer) Error

func (l *Lexer) Error(e string)

func (*Lexer) Lex

func (l *Lexer) Lex(lval *yySymType) int

type NanInfHandling added in v1.5.4

type NanInfHandling int8
const (
	ConvertToNull NanInfHandling = iota
	CreateError
	ConvertToStringNotation
)

type Null

type Null struct{}

func (Null) Encode

func (n Null) Encode() string

type Number

type Number float64

func (Number) Encode

func (n Number) Encode() string

func (Number) IsInf added in v1.5.4

func (n Number) IsInf() bool

func (Number) IsNaN added in v1.5.4

func (n Number) IsNaN() bool

func (Number) IsNegativeInfinity added in v1.5.4

func (n Number) IsNegativeInfinity() bool

func (Number) IsPositiveInfinity added in v1.5.4

func (n Number) IsPositiveInfinity() bool

func (Number) Raw

func (n Number) Raw() float64

type Object

type Object struct {
	Members []ObjectMember
}

func NewObject

func NewObject(capacity int) Object

func (*Object) Add

func (obj *Object) Add(key string, val Structure)

func (Object) Encode

func (obj Object) Encode() string

func (*Object) Exists

func (obj *Object) Exists(key string) bool

func (*Object) Keys added in v1.3.2

func (obj *Object) Keys() []string

func (*Object) Len

func (obj *Object) Len() int

func (*Object) Range added in v1.3.2

func (obj *Object) Range(fn func(key string, value Structure) bool)

func (*Object) Update

func (obj *Object) Update(key string, val Structure)

func (*Object) Value

func (obj *Object) Value(key string) Structure

func (*Object) Values added in v1.3.2

func (obj *Object) Values() []Structure

type ObjectMember

type ObjectMember struct {
	Key   string
	Value Structure
}

type Scanner

type Scanner struct {
	UseInteger bool
	// contains filtered or unexported fields
}

func (*Scanner) EscapeType

func (s *Scanner) EscapeType() EscapeType

func (*Scanner) Init

func (s *Scanner) Init(src string, useInteger bool) *Scanner

func (*Scanner) Scan

func (s *Scanner) Scan() (Token, error)

type String

type String string

func (String) Encode

func (s String) Encode() string

func (String) Raw

func (s String) Raw() string

type Structure

type Structure interface {
	Encode() string
}

type SyntaxError

type SyntaxError struct {
	Line    int
	Column  int
	Message string
}

func (SyntaxError) Error

func (e SyntaxError) Error() string

type Token

type Token struct {
	Token   int
	Literal string
	Line    int
	Column  int
}

type UnsupportedValueError added in v1.5.4

type UnsupportedValueError struct {
	Value string
}

func NewUnsupportedValueError added in v1.5.4

func NewUnsupportedValueError(value string) *UnsupportedValueError

func (UnsupportedValueError) Error added in v1.5.4

func (e UnsupportedValueError) Error() string

Jump to

Keyboard shortcuts

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