jsonutil

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Copyright 2019 The OpenPitrix Authors. All rights reserved. Use of this source code is governed by a Apache license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(y []byte, o interface{}) error

func Encode

func Encode(o interface{}) ([]byte, error)

func ToString

func ToString(o interface{}) string

Types

type Json

type Json interface {
	// Interface returns the underlying data
	Interface() interface{}
	// Encode returns its marshaled data as `[]byte`
	Encode() ([]byte, error)
	// EncodePretty returns its marshaled data as `[]byte` with indentation
	EncodePretty() ([]byte, error)
	// Implements the json.Marshaler interface.
	MarshalJSON() ([]byte, error)
	// Set modifies `Json` map by `key` and `value`
	// Useful for changing single key/value in a `Json` object easily.
	Set(key string, val interface{})
	// SetPath modifies `Json`, recursively checking/creating map keys for the supplied path,
	// and then finally writing in the value
	SetPath(branch []string, val interface{})
	// Del modifies `Json` map by deleting `key` if it is present.
	Del(key string)
	// Get returns a pointer to a new `Json` object
	// for `key` in its `map` representation
	//
	// useful for chaining operations (to traverse a nested JSON):
	//    js.Get("top_level").Get("dict").Get("value").Int()
	Get(key string) Json
	// GetPath searches for the item as specified by the branch
	// without the need to deep dive using Get()'s.
	//
	//   js.GetPath("top_level", "dict")
	GetPath(branch ...string) Json
	// CheckGet returns a pointer to a new `Json` object and
	// a `bool` identifying success or failure
	//
	// useful for chained operations when success is important:
	//    if data, ok := js.Get("top_level").CheckGet("inner"); ok {
	//        log.Println(data)
	//    }
	CheckGet(key string) (Json, bool)
	// Map type asserts to `map`
	Map() (map[string]interface{}, error)
	// Array type asserts to an `array`
	Array() ([]interface{}, error)
	// Bool type asserts to `bool`
	Bool() (bool, error)
	// String type asserts to `string`
	String() (string, error)
	// Bytes type asserts to `[]byte`
	Bytes() ([]byte, error)
	// StringArray type asserts to an `array` of `string`
	StringArray() ([]string, error)

	// Implements the json.Unmarshaler interface.
	UnmarshalJSON(p []byte) error
	// Float64 coerces into a float64
	Float64() (float64, error)
	// Int coerces into an int
	Int() (int, error)
	// Int64 coerces into an int64
	Int64() (int64, error)
	// Uint64 coerces into an uint64
	Uint64() (uint64, error)
}

func NewJson

func NewJson(y []byte) (Json, error)

func ToJson

func ToJson(o interface{}) Json

FIXME: need improve performance

Jump to

Keyboard shortcuts

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