Merge pull request #86 from kenshaw/update-documentation

Cleaning up and Updating Documentation
This commit is contained in:
Vasily Romanov
2017-02-16 16:46:46 +03:00
committed by GitHub
8 changed files with 285 additions and 155 deletions
+257 -132
View File
@@ -1,193 +1,318 @@
# easyjson [![Build Status](https://travis-ci.org/mailru/easyjson.svg?branch=master)](https://travis-ci.org/mailru/easyjson)
easyjson allows to (un-)marshal JSON golang structs without the use of reflection by generating marshaller code.
Package easyjson provides a fast and easy way to marshal/unmarshal Go structs
to/from JSON without the use of reflection. In performance tests, easyjson
outperforms the standard `encoding/json` package by a factor of 4-5x, and other
JSON encoding packages by a factor of 2-3x.
One of the aims of the library is to keep generated code simple enough so that it can be easily optimized or fixed. Another goal is to provide users with ability to customize the generated code not available in 'encoding/json', such as generating snake_case names or enabling 'omitempty' behavior by default.
easyjson aims to keep generated Go code simple enough so that it can be easily
optimized or fixed. Another goal is to provide users with the ability to
customize the generated code by providing options not available with the
standard `encoding/json` package, such as generating "snake_case" names or
enabling `omitempty` behavior by default.
## usage
```
go get github.com/mailru/easyjson/...
## Usage
```sh
# install
go get -u github.com/mailru/easyjson/...
# run
easyjson -all <file>.go
```
This will generate `<file>_easyjson.go` with marshaller/unmarshaller methods for structs. `GOPATH` variable needs to be set up correctly, since the generation invokes a `go run` on a temporary file (this is a really convenient approach to code generation borrowed from https://github.com/pquerna/ffjson).
The above will generate `<file>_easyjson.go` containing the appropriate marshaler and
unmarshaler funcs for all structs contained in `<file>.go`.
## options
```
Usage of .root/bin/easyjson:
Please note that easyjson requires a full Go build environment and the `GOPATH`
environment variable to be set. This is because easyjson code generation
invokes `go run` on a temporary file (an approach to code generation borrowed
from [ffjson](https://github.com/pquerna/ffjson)).
## Options
```txt
Usage of easyjson:
-all
generate un-/marshallers for all structs in a file
generate marshaler/unmarshalers for all structs in a file
-build_tags string
build tags to add to generated file
build tags to add to generated file
-leave_temps
do not delete temporary files
do not delete temporary files
-no_std_marshalers
don't generate MarshalJSON/UnmarshalJSON methods
don't generate MarshalJSON/UnmarshalJSON funcs
-noformat
do not run 'gofmt -w' on output file
do not run 'gofmt -w' on output file
-omit_empty
omit empty fields by default
omit empty fields by default
-output_filename string
specify the filename of the output
-pkg
process the whole package instead of just the given file
-snake_case
use snake_case names instead of CamelCase by default
use snake_case names instead of CamelCase by default
-stubs
only generate stubs for marshallers/unmarshallers methods
only generate stubs for marshaler/unmarshaler funcs
```
Using `-all` will generate (un-)marshallers for all structs in the file. By default, structs need to have a line beginning with `easyjson:json` in their docstring, e.g.:
```
Using `-all` will generate marshalers/unmarshalers for all Go structs in the
file. If `-all` is not provided, then only those structs whose preceeding
comment starts with `easyjson:json` will have marshalers/unmarshalers
generated. For example:
```go
//easyjson:json
struct A{}
```
`-snake_case` tells easyjson to generate snake\_case field names by default (unless explicitly overriden by a field tag). The CamelCase to snake\_case conversion algorithm should work in most cases (e.g. HTTPVersion will be converted to http_version). There can be names like JSONHTTPRPC where the conversion will return an unexpected result (jsonhttprpc without underscores), but such names require a dictionary to do the conversion and may be ambiguous.
Additional option notes:
`-build_tags` will add corresponding build tag line for the generated file.
## marshaller/unmarshaller interfaces
* `-snake_case` tells easyjson to generate snake\_case field names by default
(unless overridden by a field tag). The CamelCase to snake\_case conversion
algorithm should work in most cases (ie, HTTPVersion will be converted to
"http_version").
easyjson generates MarshalJSON/UnmarshalJSON methods that are compatible with interfaces from 'encoding/json'. They are usable with 'json.Marshal' and 'json.Unmarshal' functions, however actually using those will result in significantly worse performance compared to custom interfaces.
* `-build_tags` will add the specified build tags to generated Go sources.
`MarshalEasyJSON` / `UnmarshalEasyJSON` methods are generated for faster parsing using custom Lexer/Writer structs (`jlexer.Lexer` and `jwriter.Writer`). The method signature is defined in `easyjson.Marshaler` / `easyjson.Unmarshaler` interfaces. These interfaces allow to avoid using any unnecessary reflection or type assertions during parsing. Functions can be used manually or with `easyjson.Marshal<...>` and `easyjson.Unmarshal<...>` helper methods.
## Generated Marshaler/Unmarshaler Funcs
`jwriter.Writer` struct in addition to function for returning the data as a single slice also has methods to return the size and to send the data to an `io.Writer`. This is aimed at a typical HTTP use-case, when you want to know the `Content-Length` before actually starting to send the data.
For Go struct types, easyjson generates the funcs `MarshalEasyJSON` /
`UnmarshalEasyJSON` for marshaling/unmarshaling JSON. In turn, these satisify
the `easyjson.Marshaler` and `easyjson.Unmarshaler` interfaces and when used in
conjunction with `easyjson.Marshal` / `easyjson.Unmarshal` avoid unnecessary
reflection / type assertions during marshaling/unmarshaling to/from JSON for Go
structs.
There are helpers in the top-level package for marhsaling/unmarshaling the data using custom interfaces to and from writers, including a helper for `http.ResponseWriter`.
easyjson also generates `MarshalJSON` and `UnmarshalJSON` funcs for Go struct
types compatible with the standard `json.Marshaler` and `json.Unmarshaler`
interfaces. Please be aware that using the standard `json.Marshal` /
`json.Unmarshal` for marshaling/unmarshaling will incur a significant
performance penalty when compared to using `easyjson.Marshal` /
`easyjson.Unmarshal`.
## custom types
If `easyjson.Marshaler` / `easyjson.Unmarshaler` interfaces are implemented by a type involved in JSON parsing, the type will be marshaled/unmarshaled using these methods. `easyjson.Optional` interface allows for a custom type to integrate with 'omitempty' logic.
Additionally, easyjson exposes utility funcs that use the `MarshalEasyJSON` and
`UnmarshalEasyJSON` for marshaling/unmarshaling to and from standard readers
and writers. For example, easyjson provides `easyjson.MarshalToHTTPResponseWriter`
which marshals to the standard `http.ResponseWriter`. Please see the [GoDoc
listing](https://godoc.org/github.com/mailru/easyjson) for the full listing of
utility funcs that are available.
As an example, easyjson includes an `easyjson.RawMessage` analogous to `json.RawMessage`.
## Controlling easyjson Marshaling and Unmarshaling Behavior
Also, there are 'optional' wrappers for primitive types in `easyjson/opt` package. These are useful in the case when it is necessary to distinguish between missing and default value for the type. Wrappers allow to avoid pointers and extra heap allocations in such cases.
## memory pooling
Go types can provide their own `MarshalEasyJSON` and `UnmarshalEasyJSON` funcs
that satisify the `easyjson.Marshaler` / `easyjson.Unmarshaler` interfaces.
These will be used by `easyjson.Marshal` and `easyjson.Unmarshal` when defined
for a Go type.
The library uses a custom buffer which allocates data in increasing chunks (128-32768 bytes). Chunks of 512 bytes and larger are reused with the help of `sync.Pool`. The maximum size of a chunk is bounded to reduce redundancy in memory allocation and to make the chunks more reusable in the case of large buffer sizes.
Go types can also satisify the `easyjson.Optional` interface, which allows the
type to define its own `omitempty` logic.
The buffer code is in `easyjson/buffer` package the exact values can be tweaked by a `buffer.Init()` call before the first serialization.
## Type Wrappers
## limitations
* The library is at an early stage, there are likely to be some bugs and some features of 'encoding/json' may not be supported. Please report such cases, so that they may be fixed sooner.
* Object keys are case-sensitive (unlike encodin/json). Case-insentive behavior will be implemented as an option (case-insensitive matching is slower).
* Unsafe package is used by the code. While a non-unsafe version of easyjson can be made in the future, using unsafe package simplifies a lot of code by allowing no-copy []byte to string conversion within the library. This is used only during parsing and all the returned values are allocated properly.
* Floats are currently formatted with default precision for 'strconv' package. It is obvious that it is not always the correct way to handle it, but there aren't enough use-cases for floats at hand to do anything better.
* During parsing, parts of JSON that are skipped over are not syntactically validated more than required to skip matching parentheses.
* No true streaming support for encoding/decoding. For many use-cases and protocols, data length is typically known on input and needs to be known before sending the data.
easyjson provides additional type wrappers defined in the `easyjson/opt`
package. These wrap the standard Go primitives and in turn satisify the
easyjson interfaces.
## benchmarks
Most benchmarks were done using a sample 13kB JSON (9k if serialized back trimming the whitespace) from https://dev.twitter.com/rest/reference/get/search/tweets. The sample is very close to real-world data, quite structured and contains a variety of different types.
The `easyjson/opt` type wrappers are useful when needing to distinguish between
a missing value and/or when needing to specifying a default value. Type
wrappers allow easyjson to avoid additional pointers and heap allocations and
can significantly increase performance when used properly.
For small request benchmarks, an 80-byte portion of the regular sample was used.
## Memory Pooling
For large request marshalling benchmarks, a struct containing 50 regular samples was used, making a ~500kB output JSON.
easyjson uses a buffer pool that allocates data in increasing chunks from 128
to 32768 bytes. Chunks of 512 bytes and larger will be reused with the help of
`sync.Pool`. The maximum size of a chunk is bounded to reduce redundant memory
allocation and to allow larger reusable buffers.
Benchmarks are available in the repository and are run on 'make'.
easyjson's custom allocation buffer pool is defined in the `easyjson/buffer`
package, and the default behavior pool behavior can be modified (if necessary)
through a call to `buffer.Init()` prior to any marshaling or unmarshaling.
Please see the [GoDoc listing](https://godoc.org/github.com/mailru/easyjson/buffer)
for more information.
## Issues, Notes, and Limitations
* easyjson is still early in its development. As such, there are likely to be
bugs and missing features when compared to `encoding/json`. In the case of a
missing feature or bug, please create a GitHub issue. Pull requests are
welcome!
* Unlike `encoding/json`, object keys are case-sensitive. Case-insensitive
matching is not currently provided due to the significant performance hit
when doing case-insensitive key matching. In the future, case-insensitive
object key matching may be provided via an option to the generator.
* easyjson makes use of `unsafe`. While a "safe" version of easyjson could
be written, `unsafe` simplifies the code and provides significant performance
benefits by allowing no-copy conversion from `[]byte` to `string`. That said,
`unsafe` is used only when unmarshaling and parsing JSON, and any `unsafe`
operations / memory allocations done will be safely deallocated by easyjson.
* Floats are formatted using the default precision from Go's `strconv` package.
As such, easyjson will not correctly handle high precision floats when
marshaling/unmarshaling JSON. Note, however, that there are very few/limited
uses where this behavior is not sufficient for general use. That said, a
different package may be needed if precise marshaling/unmarshaling of high
precision floats to/from JSON is required.
* While unmarshaling, the JSON parser does the minimal amount of work needed to
skip over unmatching parens, and as such full validation is not done for the
entire JSON value being unmarshaled/parsed.
* Currently there is no true streaming support for encoding/decoding as
typically for many uses/protocols the final, marshaled length of the JSON
needs to be known prior to sending the data. Currently this is not possible
with easyjson's architecture.
## Benchmarks
Most benchmarks were done using the example [13kB example JSON](https://dev.twitter.com/rest/reference/get/search/tweets)
(9k after eliminating whitespace). This example is similar to real-world data,
is well-structured, and contains a healthy variety of different types, making
it ideal for JSON serialization benchmarks.
Note:
* For small request benchmarks, an 80 byte portion of the above example was
used.
* For large request marshaling benchmarks, a struct containing 50 regular
samples was used, making a ~500kB output JSON.
Benchmarks are available in the repository and can be run by invoking `make`.
### easyjson vs. encoding/json
easyjson seems to be 5-6 times faster than the default json serialization for unmarshalling, 3-4 times faster for non-concurrent marshalling. Concurrent marshalling is 6-7x faster if marshalling to a writer.
easyjson is roughly 5-6 times faster than the standard `encoding/json` for
unmarshaling, and 3-4 times faster for non-concurrent marshaling. Concurrent
marshaling is 6-7x faster if marshaling to a writer.
### easyjson vs. ffjson
easyjson uses the same approach for code generation as ffjson, but a significantly different approach to lexing and generated code. This allows easyjson to be 2-3x faster for unmarshalling and 1.5-2x faster for non-concurrent unmarshalling.
easyjson uses the same approach for JSON marshaling as
[ffjson](https://github.com/pquerna/ffjson), but takes a significantly
different approach to lexing and parsing JSON during unmarshaling. This means
easyjson is roughly 2-3x faster for unmarshaling and 1.5-2x faster for
non-concurrent unmarshaling.
ffjson seems to behave weird if used concurrently: for large request pooling hurts performance instead of boosting it, it also does not quite scale well. These issues are likely to be fixable and until that comparisons might vary from version to version a lot.
As of this writing, `ffjson` seems to have issues when used concurrently:
specifically, large request pooling hurts `ffjson`'s performance and causes
scalability issues. These issues with `ffjson` can likely be fixed, but as of
writing remain outstanding/known issues with `ffjson`.
easyjson is similar in performance for small requests and 2-5x times faster for large ones if used with a writer.
easyjson and `ffjson` have similar performance for small requests, however
easyjson outperforms `ffjson` by roughly 2-5x times for large requests when
used with a writer.
### easyjson vs. go/codec
github.com/ugorji/go/codec library provides compile-time helpers for JSON generation. In this case, helpers are not exactly marshallers as they are encoding-independent.
[go/codec](https://github.com/ugorji/go/codec) provides
compile-time helpers for JSON generation. In this case, helpers do not work
like marshalers as they are encoding-independent.
easyjson is generally ~2x faster for non-concurrent benchmarks and about 3x faster for concurrent encoding (without marshalling to a writer). Unsafe option for generated helpers was used.
easyjson is generally 2x faster than `go/codec` for non-concurrent benchmarks
and about 3x faster for concurrent encoding (without marshaling to a writer).
As an attempt to measure marshalling performance of 'go/codec' (as opposed to allocations/memcpy/writer interface invocations), a benchmark was done with resetting lenght of a byte slice rather than resetting the whole slice to nil. However, the optimization in this exact form may not be applicable in practice, since the memory is not freed between marshalling operations.
In an attempt to measure marshaling performance of `go/codec` (as opposed to
allocations/memcpy/writer interface invocations), a benchmark was done with
resetting length of a byte slice rather than resetting the whole slice to nil.
However, the optimization in this exact form may not be applicable in practice,
since the memory is not freed between marshaling operations.
### easyjson vs 'ujson' python module
ujson is using C code for parsing, so it is interesting to see how plain golang compares to that. It is imporant to note that the resulting object for python is slower to access, since the library parses JSON object into dictionaries.
easyjson seems to be slightly faster for unmarshalling (finally!) and 2-3x faster for marshalling.
[ujson](https://github.com/esnme/ultrajson) is using C code for parsing, so it
is interesting to see how plain golang compares to that. It is imporant to note
that the resulting object for python is slower to access, since the library
parses JSON object into dictionaries.
### benchmark figures
The data was measured on 4 February, 2016 using current ffjson and golang 1.6. Data for go/codec was added on 4 March 2016, benchmarked on the same machine.
easyjson is slightly faster for unmarshaling and 2-3x faster than `ujson` for
marshaling.
#### Unmarshalling
| lib | json size | MB/s | allocs/op | B/op
|--------|-----------|------|-----------|-------
|standard| regular | 22 | 218 | 10229
|standard| small | 9.7 | 14 | 720
|--------|-----------|------|-----------|-------
|easyjson| regular | 125 | 128 | 9794
|easyjson| small | 67 | 3 | 128
|--------|-----------|------|-----------|-------
|ffjson | regular | 66 | 141 | 9985
|ffjson | small | 17.6 | 10 | 488
|--------|-----------|------|-----------|-------
|codec | regular | 55 | 434 | 19299
|codec | small | 29 | 7 | 336
|--------|-----------|------|-----------|-------
|ujson | regular | 103 | N/A | N/A
### Benchmark Results
#### Marshalling, one goroutine.
| lib | json size | MB/s | allocs/op | B/op
|----------|-----------|------|-----------|-------
|standard | regular | 75 | 9 | 23256
|standard | small | 32 | 3 | 328
|standard | large | 80 | 17 | 1.2M
|----------|-----------|------|-----------|-------
|easyjson | regular | 213 | 9 | 10260
|easyjson* | regular | 263 | 8 | 742
|easyjson | small | 125 | 1 | 128
|easyjson | large | 212 | 33 | 490k
|easyjson* | large | 262 | 25 | 2879
|----------|-----------|------|-----------|-------
|ffjson | regular | 122 | 153 | 21340
|ffjson** | regular | 146 | 152 | 4897
|ffjson | small | 36 | 5 | 384
|ffjson** | small | 64 | 4 | 128
|ffjson | large | 134 | 7317 | 818k
|ffjson** | large | 125 | 7320 | 827k
|----------|-----------|------|-----------|-------
|codec | regular | 80 | 17 | 33601
|codec*** | regular | 108 | 9 | 1153
|codec | small | 42 | 3 | 304
|codec*** | small | 56 | 1 | 48
|codec | large | 73 | 483 | 2.5M
|codec*** | large | 103 | 451 | 66007
|----------|-----------|------|-----------|-------
|ujson | regular | 92 | N/A | N/A
\* marshalling to a writer,
`ffjson` results are from February 4th, 2016, using the latest `ffjson` and go1.6.
`go/codec` results are from March 4th, 2016, using the latest `go/codec` and go1.6.
#### Unmarshaling
| lib | json size | MB/s | allocs/op | B/op |
|:---------|:----------|-----:|----------:|------:|
| standard | regular | 22 | 218 | 10229 |
| standard | small | 9.7 | 14 | 720 |
| | | | | |
| easyjson | regular | 125 | 128 | 9794 |
| easyjson | small | 67 | 3 | 128 |
| | | | | |
| ffjson | regular | 66 | 141 | 9985 |
| ffjson | small | 17.6 | 10 | 488 |
| | | | | |
| codec | regular | 55 | 434 | 19299 |
| codec | small | 29 | 7 | 336 |
| | | | | |
| ujson | regular | 103 | N/A | N/A |
#### Marshaling, one goroutine.
| lib | json size | MB/s | allocs/op | B/op |
|:----------|:----------|-----:|----------:|------:|
| standard | regular | 75 | 9 | 23256 |
| standard | small | 32 | 3 | 328 |
| standard | large | 80 | 17 | 1.2M |
| | | | | |
| easyjson | regular | 213 | 9 | 10260 |
| easyjson* | regular | 263 | 8 | 742 |
| easyjson | small | 125 | 1 | 128 |
| easyjson | large | 212 | 33 | 490k |
| easyjson* | large | 262 | 25 | 2879 |
| | | | | |
| ffjson | regular | 122 | 153 | 21340 |
| ffjson** | regular | 146 | 152 | 4897 |
| ffjson | small | 36 | 5 | 384 |
| ffjson** | small | 64 | 4 | 128 |
| ffjson | large | 134 | 7317 | 818k |
| ffjson** | large | 125 | 7320 | 827k |
| | | | | |
| codec | regular | 80 | 17 | 33601 |
| codec*** | regular | 108 | 9 | 1153 |
| codec | small | 42 | 3 | 304 |
| codec*** | small | 56 | 1 | 48 |
| codec | large | 73 | 483 | 2.5M |
| codec*** | large | 103 | 451 | 66007 |
| | | | | |
| ujson | regular | 92 | N/A | N/A |
\* marshaling to a writer,
\*\* using `ffjson.Pool()`,
\*\*\* reusing output slice instead of resetting it to nil
#### Marshalling, concurrent.
| lib | json size | MB/s | allocs/op | B/op
|----------|-----------|-------|-----------|-------
|standard | regular | 252 | 9 | 23257
|standard | small | 124 | 3 | 328
|standard | large | 289 | 17 | 1.2M
|----------|-----------|-------|-----------|-------
|easyjson | regular | 792 | 9 | 10597
|easyjson* | regular | 1748 | 8 | 779
|easyjson | small | 333 | 1 | 128
|easyjson | large | 718 | 36 | 548k
|easyjson* | large | 2134 | 25 | 4957
|----------|-----------|------|-----------|-------
|ffjson | regular | 301 | 153 | 21629
|ffjson** | regular | 707 | 152 | 5148
|ffjson | small | 62 | 5 | 384
|ffjson** | small | 282 | 4 | 128
|ffjson | large | 438 | 7330 | 1.0M
|ffjson** | large | 131 | 7319 | 820k
|----------|-----------|------|-----------|-------
|codec | regular | 183 | 17 | 33603
|codec*** | regular | 671 | 9 | 1157
|codec | small | 147 | 3 | 304
|codec*** | small | 299 | 1 | 48
|codec | large | 190 | 483 | 2.5M
|codec*** | large | 752 | 451 | 77574
\* marshalling to a writer,
#### Marshaling, concurrent.
| lib | json size | MB/s | allocs/op | B/op |
|:----------|:----------|-----:|----------:|------:|
| standard | regular | 252 | 9 | 23257 |
| standard | small | 124 | 3 | 328 |
| standard | large | 289 | 17 | 1.2M |
| | | | | |
| easyjson | regular | 792 | 9 | 10597 |
| easyjson* | regular | 1748 | 8 | 779 |
| easyjson | small | 333 | 1 | 128 |
| easyjson | large | 718 | 36 | 548k |
| easyjson* | large | 2134 | 25 | 4957 |
| | | | | |
| ffjson | regular | 301 | 153 | 21629 |
| ffjson** | regular | 707 | 152 | 5148 |
| ffjson | small | 62 | 5 | 384 |
| ffjson** | small | 282 | 4 | 128 |
| ffjson | large | 438 | 7330 | 1.0M |
| ffjson** | large | 131 | 7319 | 820k |
| | | | | |
| codec | regular | 183 | 17 | 33603 |
| codec*** | regular | 671 | 9 | 1157 |
| codec | small | 147 | 3 | 304 |
| codec*** | small | 299 | 1 | 48 |
| codec | large | 190 | 483 | 2.5M |
| codec*** | large | 752 | 451 | 77574 |
\* marshaling to a writer,
\*\* using `ffjson.Pool()`,
\*\*\* reusing output slice instead of resetting it to nil
+4
View File
@@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sort"
)
const genPackage = "github.com/mailru/easyjson/gen"
@@ -59,6 +60,7 @@ func (g *Generator) writeStub() error {
fmt.Fprintln(f, ")")
}
sort.Strings(g.Types)
for _, t := range g.Types {
fmt.Fprintln(f)
if !g.NoStdMarshalers {
@@ -114,6 +116,8 @@ func (g *Generator) writeMain() (path string, err error) {
if g.NoStdMarshalers {
fmt.Fprintln(f, " g.NoStdMarshalers()")
}
sort.Strings(g.Types)
for _, v := range g.Types {
fmt.Fprintln(f, " g.Add(pkg.EasyJSON_exporter_"+v+"(nil))")
}
+3 -3
View File
@@ -18,11 +18,11 @@ import (
var buildTags = flag.String("build_tags", "", "build tags to add to generated file")
var snakeCase = flag.Bool("snake_case", false, "use snake_case names instead of CamelCase by default")
var noStdMarshalers = flag.Bool("no_std_marshalers", false, "don't generate MarshalJSON/UnmarshalJSON methods")
var noStdMarshalers = flag.Bool("no_std_marshalers", false, "don't generate MarshalJSON/UnmarshalJSON funcs")
var omitEmpty = flag.Bool("omit_empty", false, "omit empty fields by default")
var allStructs = flag.Bool("all", false, "generate un-/marshallers for all structs in a file")
var allStructs = flag.Bool("all", false, "generate marshaler/unmarshalers for all structs in a file")
var leaveTemps = flag.Bool("leave_temps", false, "do not delete temporary files")
var stubs = flag.Bool("stubs", false, "only generate stubs for marshallers/unmarshallers methods")
var stubs = flag.Bool("stubs", false, "only generate stubs for marshaler/unmarshaler funcs")
var noformat = flag.Bool("noformat", false, "do not run 'gofmt -w' on output file")
var specifiedName = flag.String("output_filename", "", "specify the filename of the output")
var processPkg = flag.Bool("pkg", false, "process the whole package instead of just the given file")
+1 -1
View File
@@ -434,7 +434,7 @@ func (g *Generator) genStructDecoder(t reflect.Type) error {
return nil
}
func (g *Generator) genStructUnmarshaller(t reflect.Type) error {
func (g *Generator) genStructUnmarshaler(t reflect.Type) error {
switch t.Kind() {
case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct:
default:
+1 -1
View File
@@ -323,7 +323,7 @@ func (g *Generator) genStructEncoder(t reflect.Type) error {
return nil
}
func (g *Generator) genStructMarshaller(t reflect.Type) error {
func (g *Generator) genStructMarshaler(t reflect.Type) error {
switch t.Kind() {
case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct:
default:
+14 -13
View File
@@ -22,7 +22,7 @@ type FieldNamer interface {
GetJSONFieldName(t reflect.Type, f reflect.StructField) string
}
// Generator generates the requested marshallers/unmarshallers.
// Generator generates the requested marshaler/unmarshalers.
type Generator struct {
out *bytes.Buffer
@@ -40,8 +40,8 @@ type Generator struct {
// package path to local alias map for tracking imports
imports map[string]string
// types that marshallers were requested for by user
marshallers map[reflect.Type]bool
// types that marshalers were requested for by user
marshalers map[reflect.Type]bool
// types that encoders were already generated for
typesSeen map[reflect.Type]bool
@@ -64,12 +64,12 @@ func NewGenerator(filename string) *Generator {
"encoding/json": "json",
},
fieldNamer: DefaultFieldNamer{},
marshallers: make(map[reflect.Type]bool),
marshalers: make(map[reflect.Type]bool),
typesSeen: make(map[reflect.Type]bool),
functionNames: make(map[string]reflect.Type),
}
// Use a file-unique prefix on all auxiliary functions to avoid
// Use a file-unique prefix on all auxiliary funcs to avoid
// name clashes.
hash := fnv.New32()
hash.Write([]byte(filename))
@@ -110,7 +110,7 @@ func (g *Generator) OmitEmpty() {
g.omitEmpty = true
}
// addTypes requests to generate en-/decoding functions for the given type.
// addTypes requests to generate encoding/decoding funcs for the given type.
func (g *Generator) addType(t reflect.Type) {
if g.typesSeen[t] {
return
@@ -123,14 +123,15 @@ func (g *Generator) addType(t reflect.Type) {
g.typesUnseen = append(g.typesUnseen, t)
}
// Add requests to generate (un-)marshallers and en-/decoding functions for the type of given object.
// Add requests to generate marshaler/unmarshalers and encoding/decoding
// funcs for the type of given object.
func (g *Generator) Add(obj interface{}) {
t := reflect.TypeOf(obj)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
g.addType(t)
g.marshallers[t] = true
g.marshalers[t] = true
}
// printHeader prints package declaration and imports.
@@ -139,7 +140,7 @@ func (g *Generator) printHeader() {
fmt.Println("// +build ", g.buildTags)
fmt.Println()
}
fmt.Println("// AUTOGENERATED FILE: easyjson marshaller/unmarshallers.")
fmt.Println("// AUTOGENERATED FILE: easyjson marshaler/unmarshalers.")
fmt.Println()
fmt.Println("package ", g.pkgName)
fmt.Println()
@@ -153,7 +154,7 @@ func (g *Generator) printHeader() {
sort.Strings(aliases)
fmt.Println("import (")
for _, alias := range g.imports {
for _, alias := range aliases {
fmt.Printf(" %s %q\n", alias, byAlias[alias])
}
@@ -186,14 +187,14 @@ func (g *Generator) Run(out io.Writer) error {
return err
}
if !g.marshallers[t] {
if !g.marshalers[t] {
continue
}
if err := g.genStructMarshaller(t); err != nil {
if err := g.genStructMarshaler(t); err != nil {
return err
}
if err := g.genStructUnmarshaller(t); err != nil {
if err := g.genStructUnmarshaler(t); err != nil {
return err
}
}
+2 -2
View File
@@ -5,8 +5,8 @@ import (
"github.com/mailru/easyjson/jwriter"
)
// RawMessage is a raw piece of JSON (number, string, bool, object, array or null) that is extracted
// without parsing and output as is during marshalling.
// RawMessage is a raw piece of JSON (number, string, bool, object, array or
// null) that is extracted without parsing and output as is during marshaling.
type RawMessage []byte
// MarshalEasyJSON does JSON marshaling using easyjson interface.
+3 -3
View File
@@ -1,8 +1,8 @@
package tests
import (
"testing"
"fmt"
"testing"
)
func TestRequiredField(t *testing.T) {
@@ -17,11 +17,11 @@ func TestRequiredField(t *testing.T) {
err := v.UnmarshalJSON([]byte(tc.json))
if tc.errorMessage == "" {
if err != nil {
t.Errorf("%s. UnmarshallJSON didn`t expect error: %v", tc.json, err)
t.Errorf("%s. UnmarshalJSON didn`t expect error: %v", tc.json, err)
}
} else {
if fmt.Sprintf("%v", err) != tc.errorMessage {
t.Errorf("%s. UnmarshallJSON expected error: %v. got: %v", tc.json, tc.errorMessage, err)
t.Errorf("%s. UnmarshalJSON expected error: %v. got: %v", tc.json, tc.errorMessage, err)
}
}
}