ffjson generates static MarshalJSON functions for structures. The generated functions reduce the reliance unpon runtime reflection to do serialization. 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).
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, you can just run:
go get -u github.com/pquerna/ffjson
ffjson myfile.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 funcions 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, depending on the structure.UnmarshalJSONis not yet implemented.
- Drop in Replacement: Because
ffjsonimplements the interfaces already defined byencoding/jsonthe performance enhancements are transparent to users of your structures. - No additional dependencies:
ffjsongenerated code depends on nothing but standard library provided modules. - 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.
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