-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 676cec7
Showing
11 changed files
with
1,209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language: go | ||
go_import_path: go.elastic.co/fastjson | ||
|
||
go: | ||
- stable | ||
- "1.8.x" | ||
|
||
script: | ||
- go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Copyright 2018 Elasticsearch BV | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--- | ||
|
||
Copyright (c) 2016 Mail.Ru Group | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
[](https://travis-ci.org/elastic/go-fastjson) | ||
|
||
# fastjson: fast JSON encoder for Go | ||
|
||
Package fastjson provides a library and code generator for fast JSON encoding. | ||
|
||
The supplied code generator (cmd/generate-fastjson) generates JSON marshalling | ||
methods for all exported types within a specified package. | ||
|
||
## Requirements | ||
|
||
Go 1.8+ | ||
|
||
## License | ||
|
||
Apache 2.0. | ||
|
||
## Installation | ||
|
||
```bash | ||
go get -u go.elastic.co/fastjson/... | ||
``` | ||
|
||
## Code generation | ||
|
||
Package fastjson is intended to be used with the accompanying code generator, | ||
cmd/generate-fastjson. This code generator will parse the Go code of a | ||
specified package, and write out fastjson marshalling method (MarshalFastJSON) | ||
definitions for the exported types in the package. | ||
|
||
You can provide your own custom marshalling logic for a type by defining a | ||
MarshalFastJSON method for it. The generator will not generate methods for | ||
those types with existing marshalling methods. | ||
|
||
### Usage | ||
|
||
``` | ||
generate-fastjson <package> | ||
-f remove the output file if it exists | ||
-o string | ||
file to which output will be written (default "-") | ||
``` | ||
|
||
### Custom omitempty extension | ||
|
||
The standard `json` struct tags defined by `encoding/json` are honoured, | ||
enabling you to generate fastjson-marshalling code for your existing code. | ||
|
||
We extend the `omitempty` option by enabling you to define an unexported | ||
method on your type `T`, `func (T) isZero() bool`, which will be called | ||
to determine whether or not the value is considered empty. This enables | ||
`omitempty` on non-pointer struct types. | ||
|
||
### Example | ||
|
||
Given the following package: | ||
|
||
```go | ||
package example | ||
|
||
type Foo struct { | ||
Bar Bar `json:",omitempty"` | ||
} | ||
|
||
type Bar struct { | ||
Baz Baz | ||
Qux *Qux `json:"quux,omitempty"` | ||
} | ||
|
||
func (b Bar) isZero() bool { | ||
return b == (Bar{}) | ||
} | ||
|
||
type Baz struct { | ||
} | ||
|
||
func (Baz) MarshalFastJSON(w *fastjson.Writer) error { | ||
} | ||
|
||
type Qux struct{} | ||
``` | ||
|
||
Assuming we're in the package directory, we would generate the methods like so, | ||
which will write a Go file to stdout: | ||
|
||
```bash | ||
generate-fastjson . | ||
``` | ||
|
||
Output: | ||
```go | ||
// Code generated by "generate-fastjson". DO NOT EDIT. | ||
|
||
package example | ||
|
||
import ( | ||
"go.elastic.co/fastjson" | ||
) | ||
|
||
func (v *Foo) MarshalFastJSON(w *fastjson.Writer) error { | ||
w.RawByte('{') | ||
if !v.Bar.isZero() { | ||
w.RawString("\"Bar\":") | ||
if err := v.Bar.MarshalFastJSON(w); err != nil && firstErr == nil { | ||
firstErr == err | ||
} | ||
} | ||
w.RawByte('}') | ||
return nil | ||
} | ||
|
||
func (v *Bar) MarshalFastJSON(w *fastjson.Writer) error { | ||
var firstErr error | ||
w.RawByte('{') | ||
w.RawString("\"Baz\":") | ||
if err := v.Baz.MarshalFastJSON(w); err != nil && firstErr == nil { | ||
firstErr = err | ||
} | ||
if v.Qux != nil { | ||
w.RawString(",\"quux\":") | ||
if err := v.Qux.MarshalFastJSON(w); err != nil && firstErr == nil { | ||
firstErr == err | ||
} | ||
} | ||
w.RawByte('}') | ||
return firstErr | ||
} | ||
|
||
func (v *Qux) MarshalFastJSON(w *fastjson.Writer) error { | ||
w.RawByte('{') | ||
w.RawByte('}') | ||
return nil | ||
} | ||
``` |
Oops, something went wrong.