Giter Club home page Giter Club logo

jzon's Introduction

Go Report Card PkgGoDev Github Workflow Build Status codecov pre-commit

jzon

A high performance json library for Golang

Why another jsoniter?

The code I write here is very similar to github.com/json-iterator/go, so you may ask why reinvent the wheel.

For sure that I benefit a lot from the jsoniter library, but i found some inconvenience for me to use it in some condition, for example:

  • the iterator methods ReadString accepts null, there is no method which accepts exactly string. I have to do some extra check before calling.
  • some behavior is not compatible with the standard library.
  • I want a chained streamer

On the other hand, I also want to learn how the jsoniter works, so there is this repo.

What's different from jsoniter?

Here are some of the differences:

  • the iterator methods accept the exact type, for example ReadString accepts only string, not null
  • the behavior is almost the same as the standard library (when an error returns, the behavior may differ from the standard library)
  • the error of the iterator is returned instead of being saved inside iterator
  • the decoder/encoder interface has additional options, like struct tag options

Some features of jsoniter are not implemented, and may be not implemented in the future neither. I choose only the ones I need to implement.

Compatibility with standard library

I tried implemented a version which is completely compatible with the standard library:

https://github.com/zerosnake0/jzon/tree/reflect

The benchmark shows that it's much faster than the standard library. However it is still much slower than the current version, which cannot be exactly the same as standard library (at least in my POV).

The major incompatibility is about the two following interfaces:

  • json.Marshaler
  • encoding.TextMarshaler

The method on pointer receiver may be called with an unaddressable value, for example:

type field struct {}

func (*field) MarshalJSON() ([]byte, error)

type st struct {
    F field
}

json.Marshal(st{}) // will not call field.MarshalJSON
jzon.Marshal(st{}) // will call field.MarshalJSON

So the user should be care when marshaling a value when method on pointer receiver is involved

You can check the tests for more detailed info about the difference

How to use

Standard library like

import "github.com/zerosnake0/jzon"

// Unmarshal
err := jzon.Unmarshal(b, &data)

// Marshal
b, err := jzon.Marshal(&data)

// Decoder
dec := jzon.NewDecoder(reader)
defer dec.Release()
err := dec.Decode(&data)

// Encoder
enc := jzon.NewEncoder(writer)
defer enc.Release()
err := enc.Encode(&data)

Iterator

iter := jzon.NewIterator()
defer iter.Release()
iter.Reset(b)
jzon.ReadVal(&data)

Streamer

var w io.Writer

streamer := jzon.NewStreamer()
defer streamer.Release()
streamer.Reset(w)
streamer.Value(&data)
streamer.Flush()

Custom Decoder

see decoder_test.go

type testIntDecoder struct{}

func (*testIntDecoder) Decode(ptr unsafe.Pointer, it *Iterator, opts *DecOpts) error {
    ...
}

dec := NewDecoderConfig(&DecoderOption{
    ValDecoders: map[reflect.Type]ValDecoder{
        reflect.TypeOf(int(0)): (*testIntDecoder)(nil),
    },
    CaseSensitive: true,
})

// standard library like
err := dec.Unmarshal(b, &data)

// iterator
iter := dec.NewIterator()
defer iter.Release()

Custom Encoder

see encoder_test.go

type testIntEncoder struct{}

func (*testIntEncoder) IsEmpty(ptr unsafe.Pointer) bool {
    ...
}

func (*testIntEncoder) Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) {
    ...
}

enc := NewEncoderConfig(&EncoderOption{
    ValEncoders: map[reflect.Type]ValEncoder{
        reflect.TypeOf(int(0)): (*testIntEncoder)(nil),
    },
})

// standard library like
b, err := enc.Marshal(&data)

// streamer
streamer := enc.NewStreamer()
defer streamer.Release()

jzon's People

Contributors

zerosnake0 avatar

Watchers

 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.