Code Monkey home page Code Monkey logo

megajson's Introduction

megajson Build Status Coverage Status

High performance Go JSON encoder and decoder.

Notice: This tool is alpha. Please report bugs to the GitHub page.

Overview

Go's builtin JSON support works great to provide simple, runtime JSON encoding and decoding. However, it's based on reflection so it has a few drawbacks:

  • Performance - The reflection library is slow and isn't optimized by the compiler at compile time.

  • Public Fields Only - The reflection library can only reflect on exported fields. That means that you can't marshal private fields to JSON.

Megajson is built to get around some of these limitations. It's a code generation tool that uses the go/parser and go/ast packages to write custom encoders and decoders for your types. These encoders and decoders know your types so the reflection package is not necessary.

Performance

Megajson encodes and decodes at approximately two times the speed of the encoding/json package using the built-in encoding/json test data in Go 1.2. This is just a benchmark though. Your mileage may vary.

Please test megajson encoders using real data for actual results. This library is primarily focused on performance so performance improvement pull requests are very welcome.

Installation

Installing megajson is easy. Simply go get from the command line:

$ go get github.com/benbjohnson/megajson

And you're ready to go.

Usage

Running megajson is simple. Just provide the files or directories that you want to generate encoders and decoders for:

$ megajson mypkg/my_file.go

Two new files will be generated:

mypkg/my_file_encoder.go
mypkg/my_file_decoder.go

They live in the same package as your my_file.go code so they're ready to go.

Once your encoders and decoders are generated, you can use them just like the json.Encoder and json.Decoder except they're named after your types. For a struct type inside my_file.go called MyStruct, the generated code can be used like this:

err := NewMyStructEncoder(writer).Encode(val)
err := NewMyStructDecoder(reader).Decode(&val)

Supported Types

The following struct field types are supported:

  • string
  • int, int64
  • uint, uint64
  • float32, float64
  • bool
  • Pointers to structs which have been megajsonified.
  • Arrays of pointers to structs which have megajsonified.

If you have a type that you would like to see supported, please add an issue to the GitHub page.

megajson's People

Contributors

benbjohnson avatar ulfurinn avatar kisielk avatar ngaut avatar

Stargazers

CodeCore avatar

Watchers

James Cloos avatar  avatar  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.