dt_automate/vendor/github.com/Andrew-M-C/go.jsonvalue
2025-02-27 10:48:32 +08:00
..
internal json更新 2025-02-27 10:48:32 +08:00
any_ltgo17.go json更新 2025-02-27 10:48:32 +08:00
CHANGELOG_zh-cn.md json更新 2025-02-27 10:48:32 +08:00
CHANGELOG.md json更新 2025-02-27 10:48:32 +08:00
codecov.yml json更新 2025-02-27 10:48:32 +08:00
compare.go json更新 2025-02-27 10:48:32 +08:00
conv.go json更新 2025-02-27 10:48:32 +08:00
errors.go json更新 2025-02-27 10:48:32 +08:00
get.go json更新 2025-02-27 10:48:32 +08:00
import_export.go json更新 2025-02-27 10:48:32 +08:00
insert_append_delete_must.go json更新 2025-02-27 10:48:32 +08:00
insert_append_delete.go json更新 2025-02-27 10:48:32 +08:00
iteration.go json更新 2025-02-27 10:48:32 +08:00
jsonvalue_internal.go json更新 2025-02-27 10:48:32 +08:00
jsonvalue.go json更新 2025-02-27 10:48:32 +08:00
LICENSE json更新 2025-02-27 10:48:32 +08:00
marshal.go json更新 2025-02-27 10:48:32 +08:00
marshaler_unmarshaler.go json更新 2025-02-27 10:48:32 +08:00
new.go json更新 2025-02-27 10:48:32 +08:00
option.go json更新 2025-02-27 10:48:32 +08:00
README.md json更新 2025-02-27 10:48:32 +08:00
set_must.go json更新 2025-02-27 10:48:32 +08:00
set.go json更新 2025-02-27 10:48:32 +08:00
sort.go json更新 2025-02-27 10:48:32 +08:00
unmarshal.go json更新 2025-02-27 10:48:32 +08:00

Jsonvalue - A Fast and Convenient Alternation of Go map[string]interface

Workflow codecov Go report CodeBeat

GoDoc Latest License

Package jsonvalue is for handling unstructured JSON data or customizing JSON marshaling. It is far more faster and convenient than using interface{} with encoding/json.

Please refer to pkg site or wiki for detailed usage and examples.

Especially, please check for jsonvalue's programming scenarios.

Import

Use following statements to import jsonvalue:

import (
	jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)

Quick Start

Sometimes we want to create a complex JSON object like:

{
  "someObject": {
    "someObject": {
      "someObject": {
        "message": "Hello, JSON!"
      }
    }
  }
}

With jsonvalue, It is quite simple to implement this:

	v := jsonvalue.NewObject()
	v.MustSet("Hello, JSON").At("someObject", "someObject", "someObject", "message")
	fmt.Println(v.MustMarshalString())
	// Output:
	// {"someObject":{"someObject":{"someObject":{"message":"Hello, JSON!"}}}

Similarly, it is quite easy to create sub-arrays like:

[
  {
    "someArray": [
      "Hello, JSON!"
    ]
  }
]
	v := jsonvalue.NewArray()
	v.MustSet("Hello, JSON").At(0, "someObject", 0)
	fmt.Println(v.MustMarshalString())
	// Output:
	// [{"someObject":["Hello, JSON"]}]

In opposite, to parse and read the first JSON above, you can use jsonvalue like this:

	const raw = `{"someObject": {"someObject": {"someObject": {"message": "Hello, JSON!"}}}}`
	s := jsonvalue.MustUnmarshalString(s).GetString("someObject", "someObject", "someObject", "message")
	fmt.Println(s)
	// Output:
	// Hello, JSON!

However, it is quite complex and annoying in automatically creating array. I strongly suggest using SetArray() to create the array first, then use Append() or Insert() to set array elements. Please refer go godoc or Wiki.