GithubHelp home page GithubHelp logo

isabella232 / go-fastjson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from elastic/go-fastjson

0.0 0.0 0.0 14 KB

Package fastjson provides a Go library and code generator for fast JSON encoding.

License: Other

Go 100.00%

go-fastjson's Introduction

Travis-CI

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

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:

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:

generate-fastjson .

Output:

// 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
}

go-fastjson's People

Contributors

axw avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.