xjson

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

GoKit - xjson

JSON kits for Golang development.

Features

  • Easy load to json and dump to string
  • Load and dump with file is supported
  • Modify the json data is simple
  • One line retrieval with MustXXX
  • Get by dot notation key is supported

Installation

go get -u github.com/likexian/gokit

Importing

import (
    "github.com/likexian/gokit/xjson"
)

Documentation

Visit the docs on GoDoc

Example

Dump the struct data to JSON string
// Define Status struct
type Status struct {
    Code    int64  `json:"code"`
    Message string `json:"message"`
}

// Init status
status := Status{1, "Success"}

// Dump status to json string
j := xjson.New(status)
s, err := j.Dumps()
if err == nil {
    fmt.Println("JSON text is:", s)
}

// OR dumps using the easy way
s, err := xjson.Dumps(status)
if err == nil {
    fmt.Println("JSON text is:", s)
}
Dump the map data to JSON string
// Init a map data
data := map[string]interface{}{
    "code": 1,
    "message": "success",
    "result": {
        "Name": "Li Kexian"
    }
}

// Dump to string in the easy way
s, err := xjson.Dumps(status)
if err == nil {
    fmt.Println("JSON text is:", s)
}
Load the JSON string
// JSON strig
text := `{"Code": 1, "Message": "Success", "Result": {"Student": [{"Name": "Li Kexian"}]}}`

// Load json string
j, err := xjson.Loads(text)
if err == nil {
    fmt.Println("Code is:", j.Get("Code").MustInt(0))
    fmt.Println("Message is:", j.Get("Message").MustString(""))
    fmt.Println("First Student name is:", j.Get("Result.Student.0.Name").MustString("-"))
}

License

Copyright 2012-2021 Li Kexian

Licensed under the Apache License 2.0

Donation

If this project is helpful, please share it with friends.

If you want to thank me, you can give me a cup of coffee.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTooManyArguments is too many arguments error
	ErrTooManyArguments = errors.New("xjson: too many arguments")
	// ErrInvalidArgumentType is invalid argument type error
	ErrInvalidArgumentType = errors.New("xjson: argument type is invalid")
	// ErrInvalidValueType is invalid value type error
	ErrInvalidValueType = errors.New("xjson: valud type is invalid")
	// ErrAssertToMap is assert to map error
	ErrAssertToMap = errors.New("xjson: assert to map failed")
	// ErrAssertToArray is assert to array error
	ErrAssertToArray = errors.New("xjson: assert to array failed")
	// ErrAssertToBool is assert to bool error
	ErrAssertToBool = errors.New("xjson: assert to bool failed")
	// ErrAssertToString is assert to string error
	ErrAssertToString = errors.New("xjson: assert to string failed")
	// ErrAssertToStringArray is assert to string array error
	ErrAssertToStringArray = errors.New("xjson: assert to string array failed")
)

Functions

func Author

func Author() string

Author returns package author

func Dump

func Dump(path string, data interface{}) error

Dump dumps json object to a file

func Dumps

func Dumps(data interface{}) (string, error)

Dumps marshal json object to string

func License

func License() string

License returns package license

func PrettyDumps

func PrettyDumps(data interface{}) (string, error)

PrettyDumps marshal json object to string, with identation

func Version

func Version() string

Version returns package version

Types

type JSON

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

JSON storing json data

func Load

func Load(path string) (*JSON, error)

Load loads data from file, returns a json object

func Loads

func Loads(text string) (*JSON, error)

Loads unmarshal json from string, returns json object

func New

func New(args ...interface{}) *JSON

New returns a pointer to a new JSON object

data_json := New()
data_json := New(type Data struct{data string}{"zzz"})
data_json := New(map[string]interface{}{"iam": "Li Kexian"})

func (*JSON) Array

func (j *JSON) Array() (result []interface{}, err error)

Array returns as array from json object

func (*JSON) Bool

func (j *JSON) Bool() (result bool, err error)

Bool returns as bool from json object

func (*JSON) Del

func (j *JSON) Del(key string)

Del delete key-value from json object, dot(.) separated key is supported

json.Del("status")
json.Del("status.code")
! NOT SUPPORTED json.Del("result.intlist.3")

func (*JSON) Dump

func (j *JSON) Dump(path string) (err error)

Dump dumps json object to a file

func (*JSON) Dumps

func (j *JSON) Dumps() (result string, err error)

Dumps marshal json object to string

func (*JSON) Float64

func (j *JSON) Float64() (result float64, err error)

Float64 returns as float64 from json object

func (*JSON) Get

func (j *JSON) Get(key string) *JSON

Get returns the pointer to json object by key, dot(.) separated key is supported

json.Get("status").Int()
json.Get("status.code").Int()
json.Get("result.intlist.3").Int()

func (*JSON) Has

func (j *JSON) Has(key string) bool

Has check json object has key, dot(.) separated key is supported

json.Has("status")
json.Has("status.code")
json.Has("result.intlist.3")

func (*JSON) Index

func (j *JSON) Index(i int) *JSON

Index returns a pointer to the index of json object

json.Get("int_list").Index(1).Int()

func (*JSON) Int

func (j *JSON) Int() (result int, err error)

Int returns as int from json object

func (*JSON) Int64

func (j *JSON) Int64() (result int64, err error)

Int64 returns as int64 from json object

func (*JSON) IsArray

func (j *JSON) IsArray() bool

IsArray returns json object is an array

func (*JSON) IsMap

func (j *JSON) IsMap() bool

IsMap returns json object is a map

func (*JSON) Len

func (j *JSON) Len() int

Len returns len of json object, -1 if type invalid or error

func (*JSON) Load

func (j *JSON) Load(path string) error

Load loads data from file, returns a json object

func (*JSON) Loads

func (j *JSON) Loads(text string) error

Loads unmarshal json from string, returns json object

func (*JSON) Map

func (j *JSON) Map() (result map[string]interface{}, err error)

Map returns as map from json object

func (*JSON) MustArray

func (j *JSON) MustArray(args ...[]interface{}) []interface{}

MustArray returns as array from json object with optional default value if error return default(if set) or panic

func (*JSON) MustBool

func (j *JSON) MustBool(args ...bool) bool

MustBool returns as bool from json object with optional default value if error return default(if set) or panic

func (*JSON) MustFloat64

func (j *JSON) MustFloat64(args ...float64) float64

MustFloat64 returns as float64 from json object with optional default value if error return default(if set) or panic

func (*JSON) MustInt

func (j *JSON) MustInt(args ...int) int

MustInt returns as int from json object with optional default value if error return default(if set) or panic

func (*JSON) MustInt64

func (j *JSON) MustInt64(args ...int64) int64

MustInt64 returns as int64 from json object with optional default value if error return default(if set) or panic

func (*JSON) MustMap

func (j *JSON) MustMap(args ...map[string]interface{}) map[string]interface{}

MustMap returns as map from json object with optional default value if error return default(if set) or panic

func (*JSON) MustString

func (j *JSON) MustString(args ...string) string

MustString returns as string from json object with optional default value if error return default(if set) or panic

func (*JSON) MustStringArray

func (j *JSON) MustStringArray(args ...[]string) []string

MustStringArray returns as string from json object with optional default value if error return default(if set) or panic

func (*JSON) MustTime

func (j *JSON) MustTime(args ...interface{}) time.Time

MustTime returns as time.Time from json object if error return default(if set) or panic

json.Time()                                                 // No format,  No default
json.Time("2006-01-02 15:04:05")                            // Has format, No default
json.Time(time.Unix(1548907870, 0))                         // No format,  Has default
json.Time("2006-01-02 15:04:05", time.Unix(1548907870, 0))  // Has format, Has default

func (*JSON) MustUint64

func (j *JSON) MustUint64(args ...uint64) uint64

MustUint64 returns as uint64 from json object with optional default value if error return default(if set) or panic

func (*JSON) PrettyDumps

func (j *JSON) PrettyDumps() (result string, err error)

PrettyDumps marshal json object to string, with identation

func (*JSON) Set

func (j *JSON) Set(key string, value interface{})

Set set key-value to json object, dot(.) separated key is supported

json.Set("status", 1)
json.Set("status.code", 1)
! NOT SUPPORTED json.Set("result.intlist.3", 666)

func (*JSON) SetHTMLEscape

func (j *JSON) SetHTMLEscape(escape bool)

SetHTMLEscape set html escape for escaping of <, >, and & in JSON strings

func (*JSON) String

func (j *JSON) String() (result string, err error)

String returns as string from json object

func (*JSON) StringArray

func (j *JSON) StringArray() (result []string, err error)

StringArray returns as string array from json object

func (*JSON) Time

func (j *JSON) Time(args ...string) (result time.Time, err error)

Time returns as time.Time from json object optional args is to set the time string parsing format, time.RFC3339 by default if the time is of int, optional args must not set

json.Time()
json.Time("2006-01-02 15:04:05")

func (*JSON) Uint64

func (j *JSON) Uint64() (result uint64, err error)

Uint64 returns as uint64 from json object

Jump to

Keyboard shortcuts

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