Documentation ¶
Index ¶
- Variables
- func MarshalValue(v Value, opts *MarshalOptions) ([]byte, error)
- func Unmarshal(src []byte, dst interface{}, opts ...UnmarshalOption) error
- func UnmarshalValue(v Value, dst interface{}, opts ...UnmarshalOption) error
- type Array
- type Boolean
- type GroupedNumbericKeys
- type GroupedNumericKey
- type MarshalOptions
- type Null
- type Number
- func (n Number) Float32() float32
- func (n Number) Float64() float64
- func (n Number) Int() int
- func (n Number) Int32() int32
- func (n Number) Int64() int64
- func (n Number) Interface() interface{}
- func (v Number) Ref() Position
- func (n Number) String() (string, error)
- func (_ Number) Type() Type
- func (n Number) Uint() uint
- func (n Number) Uint32() uint32
- func (n Number) Uint64() uint64
- type Object
- func (o Object) GroupNumericKeys(regex *regexp.Regexp, matchCount int) (GroupedNumbericKeys, error)
- func (o Object) HasKey(keyName string) bool
- func (o Object) Interface() interface{}
- func (o Object) Keys() []string
- func (v Object) Ref() Position
- func (_ Object) String() (string, error)
- func (o Object) ToMap() map[string]interface{}
- func (_ Object) Type() Type
- type ParseError
- type Parser
- type Position
- type String
- type Type
- type UnmarshalOption
- type Unmarshaler
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotStringable means that value cannot be converted to string representation. ErrNotStringable = errors.New("value not stringable") )
Functions ¶
func MarshalValue ¶
func MarshalValue(v Value, opts *MarshalOptions) ([]byte, error)
MarshalValue returns the JSON encoding of passed jsonreflect.Value
Accepts optional argument which allows to specify indent.
func Unmarshal ¶
func Unmarshal(src []byte, dst interface{}, opts ...UnmarshalOption) error
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. Accepts additional options to customise unmarshal process.
Method supports the same tag and behavior as standard json.Unmarshal method.
See UnmarshalValue documentation for information about extended behavior.
func UnmarshalValue ¶
func UnmarshalValue(v Value, dst interface{}, opts ...UnmarshalOption) error
UnmarshalValue maps JSON value to passed value. Accepts additional options to customise unmarshal process.
Method supports the same tag and behavior as standard json.Unmarshal method.
Supported additional tags:
- `json:"..."` tag used to collect all orphan values in JSON object to specified field.
Supported special unmarshal types:
- If destination value is jsonreflect.Value, unmarshaler will map original value.
- If destination value is jsonreflect.Unmarshaler, unmarshaler will call Unmarshaler.UnmarshalJSONValue.
Types ¶
type Array ¶
type Array struct { // Length is array length Length int // Items contains items list Items []Value // contains filtered or unexported fields }
Array represents JSON items list
type Boolean ¶
type Boolean struct { Value bool // contains filtered or unexported fields }
Boolean is boolean value
func (Boolean) Interface ¶
func (b Boolean) Interface() interface{}
Interface() implements json.Value
type GroupedNumbericKeys ¶
type GroupedNumbericKeys []GroupedNumericKey
func (GroupedNumbericKeys) Len ¶
func (gks GroupedNumbericKeys) Len() int
func (GroupedNumbericKeys) Less ¶
func (gks GroupedNumbericKeys) Less(i, j int) bool
func (GroupedNumbericKeys) Swap ¶
func (gks GroupedNumbericKeys) Swap(i, j int)
type GroupedNumericKey ¶
type MarshalOptions ¶
type MarshalOptions struct { // Indent is indentation to apply for output Indent string }
MarshalOptions contains additional marshal options
type Number ¶
type Number struct { // IsFloat is floating point number flag IsFloat bool // IsSigned is signed number flag IsSigned bool // contains filtered or unexported fields }
Number represents json float64 number value
type Object ¶
type Object struct { // Items is key-value pair of object values Items map[string]Value // contains filtered or unexported fields }
Object represents key-value pair of object field and value
func ToObject ¶
ToObject casts generic value to jsonreflect.Object. Passed value should be object type.
Basically, it's alias to:
val, ok := v.(*Object)
func (Object) GroupNumericKeys ¶
GroupNumericKeys groups set of keys with similar numeric prefix or suffix as array by pattern.
Example:
Keys above can be grouped by numeric suffix:
"fan1", "fan2", "fan1_2"
Using this call:
re := regexp.MustCompile(`^fan([\d]+)[_]?([\d]+)?$`) result, err := obj.GroupNumericKeys(re, 2)
Example ¶
// Group similar keys by numeric suffix. // Suffix will be used as array index. // // see more examples in keys_test.go re := regexp.MustCompile(`^fan([\d]+)?$`) src := []byte(`{"fan3": 310, "fan1": 110, "fan2": 210, "foo": "bar"}`) doc, _ := NewParser(src).Parse() // Group keys by "fan[0-9]" pattern with regex and only 1 group match obj := doc.(*Object) grouped, _ := obj.GroupNumericKeys(re, 1) for _, item := range grouped { fmt.Println(item.Order, item.Key, obj.Items[item.Key].Interface()) }
Output: [1] fan1 110 [2] fan2 210 [3] fan3 310
type ParseError ¶
func NewInvalidExprError ¶
func NewInvalidExprError(start, end int, val []byte) ParseError
func NewParseError ¶
func NewParseError(pos Position, msg string, args ...interface{}) ParseError
func NewUnexpectedCharacterError ¶
func NewUnexpectedCharacterError(start, end int, char byte) ParseError
func (ParseError) Error ¶
func (p ParseError) Error() string
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is JSON parser
func NewParserFromReader ¶
NewParserFromReader reads data from passed reader and returns reader instance
func (*Parser) Parse ¶
Parse parses passed JSON and returns parsed value.
If passed JSON is empty, a nil value returned
Example ¶
// Plain JSON document parsing data, err := ioutil.ReadFile("testdata/obj_simple.json") must(err) // or NewParserFromReader() to read from io.Reader result, err := NewParser(data).Parse() must(err) // every json.Value has .Interface() method fmt.Println(result.Interface()) // iterate over object obj := result.(*Object) for k, v := range obj.Items { fmt.Printf("- %s %T: %v\n", k, v, v.Interface()) } // or simply return map fmt.Println(obj.ToMap())
Output:
type String ¶
type String struct {
// contains filtered or unexported fields
}
String represents JSON string
type Type ¶
type Type uint
Type represents value type
const ( // TypeUnknown is invalid value type TypeUnknown Type = iota // TypeNull is null value type TypeNull // TypeBoolean is boolean value type TypeBoolean // TypeNumber is number value type TypeNumber // TypeString is string value type TypeString // TypeObject is object value type TypeObject // TypeArray is array value type TypeArray )
type UnmarshalOption ¶
type UnmarshalOption func(fn *unmarshalParams)
UnmarshalOption is unmarshal option
var ( // NoStrict disables unmarshal strict mode. // // When strict mode is disabled, unmarshaler will try to cast JSON value // to destination value. // // Supported possible casts (without strict mode): // // If destination type is numeric, unmarshaler will try // // to parse source value as number. // string -> any numeric value // // // Any numeric value can be casted to string // any numeric value -> string // // // Any valid boolean can be casted to string (and vice versa) // boolean <-> string // NoStrict UnmarshalOption = func(fn *unmarshalParams) { fn.strict = false } // DangerouslySetPrivateFields allows unmarshaler to modify private fields // which have `json` tag. // // Use it if you really know what to do, you have been warned. // // We are not responsible for corrupted memory, dead hard drives, thermonuclear war, // or you getting fired because the production database went down. // // Please do some research if you have any concerns about this option. DangerouslySetPrivateFields UnmarshalOption = func(fn *unmarshalParams) { fn.dangerouslySetPrivateFields = true } )
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal a JSON value description of themselves.
type Value ¶
type Value interface { // Ref returns reference to value in source Ref() Position // Type returns value type Type() Type // Interface returns interface{} value Interface() interface{} // String returns string representation of a value String() (string, error) // contains filtered or unexported methods }
Value is abstract JSON document value