Skip to content

A better alternation JSON to map[string]interface{}

License

Notifications You must be signed in to change notification settings

vkill-w/go.jsonvalue

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Workflow codecov Go report Codebeat
GoDoc Latest License

Jsonvalue is a Golang package for JSON parsing. It is used in situations those Go structures cannot achieve, or map[string]interface{} could not do properly.

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 achieve this:

	v := jsonvalue.NewObject()
	v.Set("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.Set("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(v.MustMarshalString())
	// 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.

Other Badges Coverage Status

About

A better alternation JSON to map[string]interface{}

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%