Let's say I have a struct like below:-
//easyjson:json
type JSONData struct {
Data []string
}
I want to un-marshal the below json to JSONData
struct
{"Data" : ["One", "Two", "Three"]}
Can someone let me know how can I use easyjson to un-marshal a json in Golang? I could not find any example in their README
I don't know why you trying to use easyjson. encoding/json is pretty fine to work with. But though here is the answer for you.
NB: It would be better if you use encoding/json.
//easyjson:json
type JSONData struct {
Data []string
}
After define this struct run easyjson <fileName-JSONData-is-defined>.go
. this will create an extra go file containg
func (v JSONData) MarshalJSON() ([]byte, error)
func (v JSONData) MarshalEasyJSON(w *jwriter.Writer)
func (v *JSONData) UnmarshalJSON(data []byte) errorfunc (v *JSONData)
func UnmarshalEasyJSON(l *jlexer.Lexer)
those methods. Then (un-)marshal using
d := &JSONData{}
d.UnmarshalJSON([]byte(`{"Data" : ["One", "Two", "Three"]} `))
// Or you could also use
// json.Unmarshal(data, d) this will also call this d.UnmarshalJSON
fmt.Println(d)
A full example is here.
I installed using the instructions from the README.md file:
go get github.com/mailru/easyjson/...
Then I created a directory in my GOPATH for this example:
$GOPATH/github.com/jpudney/stack-overflow/40587860
tree .
.
├── main.go
└── mypackage
├── example.go
└── example_easyjson.go
In mypackage/example.go I have the following code:
package mypackage
//easyjson:json
type JSONData struct {
Data []string
}
Then, within the mypackage directory I ran the following command from the README:
easyjson -all
<file>
.go
Where I replace <file>
with example. So ended up running the following:
easyjson -all example.go
This results in a file called example_easyjson.go
which contains the generated code.
I then created a main.go
file to us the generated code. The contents of which is:
package main
import (
"encoding/json"
"fmt"
"github.com/jpudney/stack-overflow/40587860/mypackage"
)
func main() {
var data mypackage.JSONData
jsonBlob := `{"Data" : ["One", "Two", "Three"]}`
err := json.Unmarshal([]byte(jsonBlob), &data)
if err != nil {
panic(err)
}
fmt.Println(data.Data)
}
I build and ran this file and it outputted the data as expected:
$ go build -o test
$ ./test
[One Two Three]
Well, easyJson is 4 times faster than normal json(as per its documets) ,in our organization we have used it extensively and yes its faster. Here is a small example to get started. my current directory name is easyJson
vim easyjson.go
package main
import "fmt"
import "time"
import ej "random/golang/easyJson/model"
func main() {
t1 := time.Now()
var d ej.Data
d.Name = "sharathbj"
d.Age = 23
data, _ := d.MarshalJSON()
fmt.Println(string(data))
fmt.Println("elapsedTime:", time.Now().Sub(t1))
}
create a directory named model ,where your structures are defined and new go file models.go
mkdir model
vim models.go
package easyJson
//easyjson:json
type Data struct {
Name string `json:"name"`
Age int `json:"age"`
}
Now run command to create a easyjson file (-all specified to reference all the structure inside a given file)
easyjson -all model/models.go
now a new file will be generated models_easyjson.go using which marshaling/unmarshaling will be referenced
go run easyjson.go
To compare easyjson with normal encoding/json ,below is the code
vim normaljson.go
package main
import (
"fmt"
"time"
"encoding/json"
model "random/golang/easyJson/model"
)
func main() {
t1 := time.Now()
var d model.Data
d.Name = "sharathbj"
d.Age = 23
data, _ := json.Marshal(d)
fmt.Println(string(data))
fmt.Println("elapsedTime:", time.Now().Sub(t1))
}
Clearly easyjson is 7 micro second faster than normal json ,and you will see its impact when you do it for bigger structures , u can see the source code below .
https://github.com/sharathbj/random/tree/master/golang/easyJson
Chears!!