Documentation
¶
Overview ¶
Package jsonvalue is for JSON parsing and setting. It is used in situations those Go structures cannot achieve, or "map[string]interface{}" could not do properbally.
As a quick start:
v := jsonvalue.NewObject()
v.SetString("Hello, JSON").At("someObject", "someObject", "someObject", "message") // automatically create sub objects
fmt.Println(v.MustMarshalString()) // marshal to string type
// Output:
// {"someObject":{"someObject":{"someObject":{"message":"Hello, JSON!"}}}
If you want to parse raw JSON data, use Unmarshal()
raw := []byte(`{"message":"hello, world"}`)
v, err := jsonvalue.Unmarshal(raw)
s, _ := v.GetString("message")
fmt.Println(s)
// Output:
// hello, world
Index ¶
- Constants
- func DefaultStringSequence(parent *ParentInfo, key1, key2 string, v1, v2 *V) bool
- type Append
- type ArrayLessFunc
- type Error
- type Insert
- type Key
- type KeyPath
- type MarshalLessFunc
- type Opt
- type ParentInfo
- type Set
- type V
- func NewArray() *V
- func NewBool(b bool) *V
- func NewFloat32(f float32, prec int) *V
- func NewFloat64(f float64, prec int) *V
- func NewInt(i int) *V
- func NewInt32(i int32) *V
- func NewInt64(i int64) *V
- func NewNull() *V
- func NewObject(keyValues ...map[string]interface{}) *V
- func NewString(s string) *V
- func NewUint(u uint) *V
- func NewUint32(u uint32) *V
- func NewUint64(u uint64) *V
- func Unmarshal(b []byte) (ret *V, err error)
- func UnmarshalString(s string) (*V, error)
- func (v *V) Append(child *V) *Append
- func (v *V) AppendArray() *Append
- func (v *V) AppendBool(b bool) *Append
- func (v *V) AppendFloat32(f float32, prec int) *Append
- func (v *V) AppendFloat64(f float64, prec int) *Append
- func (v *V) AppendInt(i int) *Append
- func (v *V) AppendInt32(i int32) *Append
- func (v *V) AppendInt64(i int64) *Append
- func (v *V) AppendNull() *Append
- func (v *V) AppendObject() *Append
- func (v *V) AppendString(s string) *Append
- func (v *V) AppendUint(u uint) *Append
- func (v *V) AppendUint32(u uint32) *Append
- func (v *V) AppendUint64(u uint64) *Append
- func (v *V) Bool() bool
- func (v *V) Delete(firstParam interface{}, otherParams ...interface{}) error
- func (v *V) Float32() float32
- func (v *V) Float64() float64
- func (v *V) Get(firstParam interface{}, otherParams ...interface{}) (*V, error)
- func (v *V) GetArray(firstParam interface{}, otherParams ...interface{}) (*V, error)
- func (v *V) GetBool(firstParam interface{}, otherParams ...interface{}) (bool, error)
- func (v *V) GetFloat32(firstParam interface{}, otherParams ...interface{}) (float32, error)
- func (v *V) GetFloat64(firstParam interface{}, otherParams ...interface{}) (float64, error)
- func (v *V) GetInt(firstParam interface{}, otherParams ...interface{}) (int, error)
- func (v *V) GetInt32(firstParam interface{}, otherParams ...interface{}) (int32, error)
- func (v *V) GetInt64(firstParam interface{}, otherParams ...interface{}) (int64, error)
- func (v *V) GetNull(firstParam interface{}, otherParams ...interface{}) error
- func (v *V) GetObject(firstParam interface{}, otherParams ...interface{}) (*V, error)
- func (v *V) GetString(firstParam interface{}, otherParams ...interface{}) (string, error)
- func (v *V) GetUint(firstParam interface{}, otherParams ...interface{}) (uint, error)
- func (v *V) GetUint32(firstParam interface{}, otherParams ...interface{}) (uint32, error)
- func (v *V) GetUint64(firstParam interface{}, otherParams ...interface{}) (uint64, error)
- func (v *V) GreaterThanInt64Max() bool
- func (v *V) Insert(child *V) *Insert
- func (v *V) InsertArray() *Insert
- func (v *V) InsertBool(b bool) *Insert
- func (v *V) InsertFloat32(f float32, prec int) *Insert
- func (v *V) InsertFloat64(f float64, prec int) *Insert
- func (v *V) InsertInt(i int) *Insert
- func (v *V) InsertInt32(i int32) *Insert
- func (v *V) InsertInt64(i int64) *Insert
- func (v *V) InsertNull() *Insert
- func (v *V) InsertObject() *Insert
- func (v *V) InsertString(s string) *Insert
- func (v *V) InsertUint(u uint) *Insert
- func (v *V) InsertUint32(u uint32) *Insert
- func (v *V) InsertUint64(u uint64) *Insert
- func (v *V) Int() int
- func (v *V) Int32() int32
- func (v *V) Int64() int64
- func (v *V) IsArray() bool
- func (v *V) IsBoolean() bool
- func (v *V) IsFloat() bool
- func (v *V) IsInteger() bool
- func (v *V) IsNegative() bool
- func (v *V) IsNull() bool
- func (v *V) IsNumber() bool
- func (v *V) IsObject() bool
- func (v *V) IsPositive() bool
- func (v *V) IsString() bool
- func (v *V) Len() int
- func (v *V) Marshal(opt ...Opt) (b []byte, err error)
- func (v *V) MarshalString(opt ...Opt) (s string, err error)
- func (v *V) MustMarshal(opt ...Opt) []byte
- func (v *V) MustMarshalString(opt ...Opt) string
- func (v *V) RangeArray(callback func(i int, v *V) bool)
- func (v *V) RangeObjects(callback func(k string, v *V) bool)
- func (v *V) Set(child *V) *Set
- func (v *V) SetArray() *Set
- func (v *V) SetBool(b bool) *Set
- func (v *V) SetFloat32(f float32, prec int) *Set
- func (v *V) SetFloat64(f float64, prec int) *Set
- func (v *V) SetInt(i int) *Set
- func (v *V) SetInt32(i int32) *Set
- func (v *V) SetInt64(i int64) *Set
- func (v *V) SetNull() *Set
- func (v *V) SetObject() *Set
- func (v *V) SetString(s string) *Set
- func (v *V) SetUint(u uint) *Set
- func (v *V) SetUint32(u uint32) *Set
- func (v *V) SetUint64(u uint64) *Set
- func (v *V) SortArray(lessFunc ArrayLessFunc)
- func (v *V) String() string
- func (v *V) Uint() uint
- func (v *V) Uint32() uint32
- func (v *V) Uint64() uint64
Examples ¶
Constants ¶
const ( // ErrNilParameter identifies input paremeter is nil ErrNilParameter = Error("nil parameter") // ErrValueUninitialized identifies that a V object is not initialized ErrValueUninitialized = Error("jsonvalue instance is not initialized") // ErrRawBytesUnrecignized identifies all unexpected raw bytes ErrRawBytesUnrecignized = Error("unrecognized raw text") // ErrNotValidBoolValue shows that a value starts with 't' or 'f' is not eventually a bool value ErrNotValidBoolValue = Error("not a valid bool object") // ErrNotValidNulllValue shows that a value starts with 'n' is not eventually a bool value ErrNotValidNulllValue = Error("not a valid null object") // ErrOutOfRange identifies that given position for a JSON array is out of range ErrOutOfRange = Error("out of range") // ErrNotFound shows that given target is not found in Delete() ErrNotFound = Error("target not found") // ErrTypeNotMatch shows that value type is not same as GetXxx() ErrTypeNotMatch = Error("not match given type") // ErrNotArrayValue shows that operation target value is not an array ErrNotArrayValue = Error("not an array typed value") // ErrIllegalString shows that it is not a legal JSON string typed value ErrIllegalString = Error("illegal string") )
Variables ¶
This section is empty.
Functions ¶
func DefaultStringSequence ¶ added in v1.0.2
func DefaultStringSequence(parent *ParentInfo, key1, key2 string, v1, v2 *V) bool
DefaultStringSequence simply use strings.Compare() to define the sequence of various key-value pairs of an object value. This function is used in Opt.MarshalLessFunc.
Types ¶
type Append ¶
type Append struct {
// contains filtered or unexported fields
}
Append type is for InTheEnd() or InTheBeginning() function. Please refer to related functions.
Shoud ONLY be generated by V.Append() function
func (*Append) InTheBeginning ¶
InTheBeginning completes the following operation of Append().
Example ¶
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
s := `{"obj":{"arr":[1,2,3,4,5]}}`
v, _ := jsonvalue.UnmarshalString(s)
// append a zero in the bebinning of v.obj.arr
v.AppendInt(0).InTheBeginning("obj", "arr")
s = v.MustMarshalString()
fmt.Println(s)
}
Output: {"obj":{"arr":[0,1,2,3,4,5]}}
func (*Append) InTheEnd ¶
InTheEnd completes the following operation of Append().
Example ¶
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
s := `{"obj":{"arr":[1,2,3,4,5]}}`
v, _ := jsonvalue.UnmarshalString(s)
// append a zero in the end of v.obj.arr
v.AppendInt(0).InTheEnd("obj", "arr")
s = v.MustMarshalString()
fmt.Println(s)
}
Output: {"obj":{"arr":[1,2,3,4,5,0]}}
type ArrayLessFunc ¶ added in v1.0.2
ArrayLessFunc is used in SortArray()
type Error ¶
type Error string
Error is equavilent to string and used to create some error constants in this package. Error constants: http://godoc.org/github.com/Andrew-M-C/go.jsonvalue/#pkg-constants
type Insert ¶
type Insert struct {
// contains filtered or unexported fields
}
Insert type is for After() and Before() function. Please refer for realated function.
Should be generated ONLY BY V.Insert function!
func (*Insert) After ¶
After completes the following operation of Insert().
The last parameter identifies the postion where a new JSON is inserted after, it should ba an interger, no matter signed or unsigned. If the position is zero or positive interger, it tells the index of an array. If the position is negative, it tells the backward index of an array.
For example, 0 represents the first, and -2 represents the second last.
Example ¶
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
s := `{"obj":{"arr":["hello","world"]}}`
v, _ := jsonvalue.UnmarshalString(s)
// insert a word in the middle, which is after the first word of the array
v.InsertString("my").After("obj", "arr", 0)
fmt.Println(v.MustMarshalString())
}
Output: {"obj":{"arr":["hello","my","world"]}}
func (*Insert) Before ¶
Before completes the following operation of Insert().
The last parameter identifies the postion where a new JSON is inserted after, it should ba an interger, no matter signed or unsigned. If the position is zero or positive interger, it tells the index of an array. If the position is negative, it tells the backward index of an array.
For example, 0 represents the first, and -2 represents the second last.
Example ¶
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
s := `{"obj":{"arr":["hello","world"]}}`
v, _ := jsonvalue.UnmarshalString(s)
// insert a word in the middle, which is before the second word of the array
v.InsertString("my").Before("obj", "arr", 1)
fmt.Println(v.MustMarshalString())
}
Output: {"obj":{"arr":["hello","my","world"]}}
type Key ¶ added in v1.0.2
type Key struct {
// contains filtered or unexported fields
}
Key is the element of KeyPath
func (*Key) IsInt ¶ added in v1.0.2
IsInt tells if current key is a integer, which indicates a child of an array
type KeyPath ¶ added in v1.0.2
type KeyPath []*Key
KeyPath identifies a full path of keys of object in jsonvalue
type MarshalLessFunc ¶ added in v1.0.2
type MarshalLessFunc func(nilableParent *ParentInfo, key1, key2 string, v1, v2 *V) bool
MarshalLessFunc is used in marshaling, for sorting marshaled data
type Opt ¶
type Opt struct {
// OmitNull tells how to handle null json value. The default value is false.
// If OmitNull is true, null value will be omitted when marshaling.
OmitNull bool
// MarshalLessFunc is used to handle sequences of marshaling. Since object is
// implemented by hash map, the sequence of keys is unexpectable. For situations
// those need settled JSON key-value sequence, please use MarshalLessFunc.
//
// Note: Elements in an array value would NOT trigger this function as they are
// already sorted.
//
// We provides a example DefaultStringSequence.
MarshalLessFunc MarshalLessFunc
// MarshalKeySequence is used to handle sequance of marshaling. This is much simpler
// than MarshalLessFunc, just pass a string slice identifying key sequence. For keys
// those are not in this slice, they would be appended in the end according to result
// of Go string comparing.
MarshalKeySequence []string
// contains filtered or unexported fields
}
Opt is the option of jsonvalue.
Example ¶
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
raw := `{"null":null}`
v, _ := jsonvalue.UnmarshalString(raw)
s := v.MustMarshalString()
fmt.Println(s)
s = v.MustMarshalString(jsonvalue.Opt{OmitNull: true})
fmt.Println(s)
}
Output: {"null":null} {}
type ParentInfo ¶ added in v1.0.2
ParentInfo show informations of parent of a json value
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set type is for At() only. Please refer to At() function.
This should be generated by V.Set functions ONLY.
func (*Set) At ¶
At completes the following operation of Set(). It defines posttion of value in Set() and return the new value set.
The usage of At() is perhaps the most important. This function will recursivly search for child value, and set the new value specified by Set() or SetXxx() series functions. Please unfold and read the following examples, they are important.
type V ¶
type V struct {
// contains filtered or unexported fields
}
V is the main type of jsonvalue, representing a JSON value.
func NewFloat32 ¶
NewFloat32 returns an initialied num jsonvalue object by float32 type. The precision prec controls the number of digits. Use -1 in prec for automatically precision.
func NewFloat64 ¶
NewFloat64 returns an initialied num jsonvalue object by float64 type. The precision prec controls the number of digits. Use -1 in prec for automatically precision.
Example ¶
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
f := 123.123456789
var v *jsonvalue.V
v = jsonvalue.NewFloat64(f, 9)
fmt.Println(v)
v = jsonvalue.NewFloat64(f, 6)
fmt.Println(v)
v = jsonvalue.NewFloat64(f, 10)
fmt.Println(v)
}
Output: 123.123456789 123.123457 123.1234567890
func NewObject ¶
NewObject returns an object-typed jsonvalue object. If keyValues is specified, it will also create some key-values in the object. Now we supports basic types only. Such as int/uint series, string, bool, nil.
func Unmarshal ¶
Unmarshal parse raw bytes(encoded in UTF-8 or pure AscII) and returns a *V instance.
func UnmarshalString ¶
UnmarshalString is equavilent to Unmarshal(string(b))
func (*V) AppendArray ¶
AppendArray is equivalent to Append(jsonvalue.NewArray())
func (*V) AppendBool ¶
AppendBool is equivalent to Append(jsonvalue.NewBool(b))
func (*V) AppendFloat32 ¶
AppendFloat32 is equivalent to Append(jsonvalue.NewFloat32(b))
func (*V) AppendFloat64 ¶
AppendFloat64 is equivalent to Append(jsonvalue.NewFloat64(b))
func (*V) AppendInt32 ¶
AppendInt32 is equivalent to Append(jsonvalue.NewInt32(b))
func (*V) AppendInt64 ¶
AppendInt64 is equivalent to Append(jsonvalue.NewInt64(b))
func (*V) AppendNull ¶
AppendNull is equivalent to Append(jsonvalue.NewNull())
func (*V) AppendObject ¶
AppendObject is equivalent to Append(jsonvalue.NewObject())
func (*V) AppendString ¶
AppendString is equivalent to Append(jsonvalue.NewString(s))
func (*V) AppendUint ¶
AppendUint is equivalent to Append(jsonvalue.NewUint(b))
func (*V) AppendUint32 ¶
AppendUint32 is equivalent to Append(jsonvalue.NewUint32(b))
func (*V) AppendUint64 ¶
AppendUint64 is equivalent to Append(jsonvalue.NewUint64(b))
func (*V) Float32 ¶
Float32 returns represented float32 value. If value is not a number, returns zero.
func (*V) Float64 ¶
Float64 returns represented float64 value. If value is not a number, returns zero.
func (*V) Get ¶
Get returns JSON value in specified position. Param formats are like At()
Example ¶
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
s := `{"objA":{"objB":{"message":"Hello, world!"}}}`
v, _ := jsonvalue.UnmarshalString(s)
msg, _ := v.Get("objA", "objB", "message")
fmt.Println(msg.String())
}
Output: Hello, world!
func (*V) GetArray ¶
GetArray is equalivent to v, err := Get(...); raise err if v.IsArray() == false
func (*V) GetFloat32 ¶
GetFloat32 is equalivent to v, err := Get(...); v.Int()
func (*V) GetFloat64 ¶
GetFloat64 is equalivent to v, err := Get(...); v.Int()
func (*V) GetObject ¶
GetObject is equalivent to v, err := Get(...); raise err if v.IsObject() == false
func (*V) GreaterThanInt64Max ¶
GreaterThanInt64Max return true when ALL conditions below are met:
- It is a number value.
- It is a positive interger.
- Its value is greater than 0x7fffffffffffffff.
Example ¶
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
v1 := jsonvalue.NewUint64(uint64(9223372036854775808)) // 0x8000000000000000
v2 := jsonvalue.NewUint64(uint64(9223372036854775807)) // 0x7FFFFFFFFFFFFFFF
v3 := jsonvalue.NewInt64(int64(-9223372036854775807))
fmt.Println(v1.GreaterThanInt64Max())
fmt.Println(v2.GreaterThanInt64Max())
fmt.Println(v3.GreaterThanInt64Max())
}
Output: true false false
func (*V) InsertArray ¶
InsertArray is equivalent to Insert(jsonvalue.NewArray())
func (*V) InsertBool ¶
InsertBool is equivalent to Insert(jsonvalue.NewBool(b))
func (*V) InsertFloat32 ¶
InsertFloat32 is equivalent to Insert(jsonvalue.NewFloat32(b))
func (*V) InsertFloat64 ¶
InsertFloat64 is equivalent to Insert(jsonvalue.NewFloat64(b))
func (*V) InsertInt32 ¶
InsertInt32 is equivalent to Insert(jsonvalue.NewInt32(b))
func (*V) InsertInt64 ¶
InsertInt64 is equivalent to Insert(jsonvalue.NewInt64(b))
func (*V) InsertNull ¶
InsertNull is equivalent to Insert(jsonvalue.NewNull())
func (*V) InsertObject ¶
InsertObject is equivalent to Insert(jsonvalue.NewObject())
func (*V) InsertString ¶
InsertString is equivalent to Insert(jsonvalue.NewString(s))
func (*V) InsertUint ¶
InsertUint is equivalent to Insert(jsonvalue.NewUint(b))
func (*V) InsertUint32 ¶
InsertUint32 is equivalent to Insert(jsonvalue.NewUint32(b))
func (*V) InsertUint64 ¶
InsertUint64 is equivalent to Insert(jsonvalue.NewUint64(b))
func (*V) IsNegative ¶
IsNegative tells whether value is a negative number
func (*V) IsPositive ¶
IsPositive tells whether value is a positive number
func (*V) MarshalString ¶
MarshalString is same with Marshal, but returns string
func (*V) MustMarshal ¶
MustMarshal is the same as Marshal, but panics if error pccurred
func (*V) MustMarshalString ¶
MustMarshalString is the same as MarshalString, but panics if error pccurred
func (*V) RangeArray ¶
RangeArray goes through each children when this is an array value
Return true in callback to continue range iteration, while false to break.
Example ¶
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
s := `[1,2,3,4,5,6,7,8,9,10]`
v, _ := jsonvalue.UnmarshalString(s)
v.RangeArray(func(i int, v *jsonvalue.V) bool {
fmt.Println(v)
return i < 5
})
}
Output: 1 2 3 4 5 6
func (*V) RangeObjects ¶
RangeObjects goes through each children when this is an object value
Return true in callback to continue range iteration, while false to break.
func (*V) Set ¶
Set starts setting a child JSON value. Please refer to examples of "func (set *Set) At(...)"
https://godoc.org/github.com/Andrew-M-C/go.jsonvalue/#Set.At
func (*V) SetFloat32 ¶
SetFloat32 is equivalent to Set(jsonvalue.NewFloat32(b))
func (*V) SetFloat64 ¶
SetFloat64 is equivalent to Set(jsonvalue.NewFloat64(b))
func (*V) SortArray ¶ added in v1.0.2
func (v *V) SortArray(lessFunc ArrayLessFunc)
SortArray is used to re-arrange sequence of the array. Invokers should pass less function for sorting. Nothing would happens either lessFunc is nil or v is not an array.
func (*V) String ¶
String returns represented string value or the description for the jsonvalue.V instance if it is not a string.
Example ¶
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
v := jsonvalue.NewObject()
v.SetString("Hello, string").At("object", "message")
fmt.Println(v)
child, _ := v.Get("object")
fmt.Println(child)
child, _ = v.Get("object", "message")
fmt.Println(child)
}
Output: {object: {message: Hello, string}} {message: Hello, string} Hello, string