ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - zerosnake0/jzon: A golang json library inspired by jsoniter
A golang json library inspired by jsoniter. Contribute to zerosnake0/jzon development by creating an account on GitHub.
Visit Site

GitHub - zerosnake0/jzon: A golang json library inspired by jsoniter

GitHub - zerosnake0/jzon: A golang json library inspired by jsoniter

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()

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory