Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zerosnake0/jzon
A golang json library inspired by jsoniter
https://github.com/zerosnake0/jzon
decoder encoder golang iterator json marshaler streamer unmarshaller
Last synced: about 2 months ago
JSON representation
A golang json library inspired by jsoniter
- Host: GitHub
- URL: https://github.com/zerosnake0/jzon
- Owner: zerosnake0
- License: mit
- Created: 2019-11-12T10:42:41.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-01T09:29:48.000Z (over 1 year ago)
- Last Synced: 2024-07-31T20:52:10.404Z (5 months ago)
- Topics: decoder, encoder, golang, iterator, json, marshaler, streamer, unmarshaller
- Language: Go
- Homepage:
- Size: 424 KB
- Stars: 11
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - jzon - JSON library with standard compatible API/behavior. (JSON / Search and Analytic Databases)
- awesome-go-extra - jzon - 11-12T10:42:41Z|2022-08-02T18:21:40Z| (JSON / Advanced Console UIs)
README
[![Go Report Card](https://goreportcard.com/badge/github.com/zerosnake0/jzon)](https://goreportcard.com/report/github.com/zerosnake0/jzon)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/zerosnake0/jzon)](https://pkg.go.dev/github.com/zerosnake0/jzon)
[![Github Workflow](https://github.com/zerosnake0/jzon/workflows/Test/badge.svg)](https://github.com/zerosnake0/jzon/actions?query=workflow%3ATest)
[![Build Status](https://travis-ci.org/zerosnake0/jzon.svg?branch=master)](https://travis-ci.org/zerosnake0/jzon)
[![codecov](https://codecov.io/gh/zerosnake0/jzon/branch/master/graph/badge.svg)](https://codecov.io/gh/zerosnake0/jzon)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/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](https://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 streamerOn 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 optionsSome 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:```go
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 involvedYou can check the tests for more detailed info about the difference
## How to use
### Standard library like
```go
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
```go
iter := jzon.NewIterator()
defer iter.Release()
iter.Reset(b)
jzon.ReadVal(&data)
```### Streamer
```go
var w io.Writerstreamer := jzon.NewStreamer()
defer streamer.Release()
streamer.Reset(w)
streamer.Value(&data)
streamer.Flush()
```### Custom Decoder
see `decoder_test.go`
```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`
```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()
```