ffjson generates static MarshalJSON and UnmarshalJSON functions for structures in Go. The generated functions reduce the reliance unpon runtime reflection to do serialization and are generally 2 to 3 times faster. In cases where ffjson doesn't understand a Type involved, it falls back to encoding/json, meaning it is a safe drop in replacement. By using ffjson your JSON serialization just gets faster with no additional code changes.
When you change your struct, you will need to run ffjson again (or make it part of your build tools).
- 2014-03-31: First Release and Background
If myfile.go contains the struct types you would like to be faster, and assuming GOPATH is set to a reasonable value for an existing project (meaning that in this particular example if myfile.go is in the myproject directory, the project should be under $GOPATH/src/myproject), you can just run:
go get -u github.com/pquerna/ffjson
ffjson myfile.go
git add myfile_ffjson.go
ffjson generates code based upon existing struct types. For example, ffjson foo.go will by default create a new file foo_ffjson.go that contains serialization functions for all structs found in foo.go.
Usage of ffjson:
ffjson [options] [input_file]
ffjson generates Go code for optimized JSON serialization.
-w="": Write generate code to this path instead of ${input}_ffjson.go.MarshalJSONis 2x to 3x faster thanencoding/json.UnmarshalJSONis 2x to 3x faster thanencoding/json.
- NEW Unmarshal Support: Since v0.9,
ffjsonsupports Unmarshaling of structures. - Drop in Replacement: Because
ffjsonimplements the interfaces already defined byencoding/jsonthe performance enhancements are transparent to users of your structures. - Supports all types:
ffjsonhas native support for most of Go's types -- for any type it doesn't support with fast paths, it falls back to usingencoding/json. This means all structures should work out of the box. If they don't, open a issue! - ffjson: skip: If you have a structure you want
ffjsonto ignore, addffjson: skipto the doc string for this structure.
Please open issues in Github for ideas, bugs, and general thoughts. Pull requests are of course preferred :)
ffjson is licensed under the Apache License, Version 2.0