Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
Update packages
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielOaks committed May 9, 2017
1 parent b4a117d commit 61ff02c
Show file tree
Hide file tree
Showing 15 changed files with 966 additions and 72 deletions.
51 changes: 49 additions & 2 deletions github.com/tidwall/gjson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<p align="center">get a json value quickly</a></p>

GJSON is a Go package that provides a [fast](#performance) and [simple](#get-a-value) way to get values from a json document.
It has features such as [one line retrieval](#get-a-value), [dot notation paths](#path-syntax), [iteration](#iterate-through-an-object-or-array), and [map unmarshalling](#unmarshal-to-a-map).
It has features such as [one line retrieval](#get-a-value), [dot notation paths](#path-syntax), [iteration](#iterate-through-an-object-or-array).

Getting Started
===============
Expand All @@ -26,7 +26,7 @@ $ go get -u github.com/tidwall/gjson
This will retrieve the library.

## Get a value
Get searches json for the specified path. A path is in dot syntax, such as "name.last" or "age". This function expects that the json is well-formed and validates. Invalid json will not panic, but it may return back unexpected results. When the value is found it's returned immediately.
Get searches json for the specified path. A path is in dot syntax, such as "name.last" or "age". This function expects that the json is well-formed. Bad json will not panic, but it may return back unexpected results. When the value is found it's returned immediately.

```go
package main
Expand Down Expand Up @@ -233,6 +233,53 @@ if gjson.Get(json, "name.last").Exists(){
}
```

## Unmarshalling

There's a `gjson.Unmarshal` function which loads json data into a value.
It's a general replacement for `json.Unmarshal` and you can typically
see a 2-3x boost in performance without the need for external generators.

This function works almost identically to `json.Unmarshal` except that
`gjson.Unmarshal` will automatically attempt to convert JSON values to any
Go type. For example, the JSON string "100" or the JSON number 100 can be
equally assigned to Go string, int, byte, uint64, etc. This rule applies to
all types.


```go
package main

import (
"fmt"

"github.com/tidwall/gjson"
)

type Animal struct {
Type string `json:"type"`
Sound string `json:"sound"`
Age int `json:"age"`
}

var json = `{
"type": "Dog",
"Sound": "Bark",
"Age": "11"
}`

func main() {
var dog Animal
gjson.Unmarshal([]byte(json), &dog)
fmt.Printf("type: %s, sound: %s, age: %d\n", dog.Type, dog.Sound, dog.Age)
}
```

This will print:

```
type: Dog, sound: Bark, age: 11
```

## Unmarshal to a map

To unmarshal to a `map[string]interface{}`:
Expand Down
Loading

0 comments on commit 61ff02c

Please sign in to comment.