fiber icon indicating copy to clipboard operation
fiber copied to clipboard

šŸ”„ Support third-party json components

Open binwan-dev opened this issue 6 years ago • 8 comments

Is your feature request related to a problem?

  1. Can not tolerate conversion between string and number types like Id value always is 0
struct Person{  
    Id int64
}
str:="{"Id":"12344121354"}"
json.Unmarshal([]byte(str),&Person{})
  1. Cannot support ignore json tag for temporary situation
  2. May be third-party json components has Better performance. like jsoniter https://github.com/json-iterator/go

Describe alternatives you've considered Use jsoniter component to resolve problem. like

extra.RegisterFuzzyDecoders()
jsoniter.Unmarshal(c.Fasthttp.Body(),&Person{})

I found that the json component library is used directly in the source code. Is it possible to change it to support custom third-party component libraries?

binwan-dev avatar May 15 '20 11:05 binwan-dev

Thanks for opening your first issue here! šŸŽ‰ Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

welcome[bot] avatar May 15 '20 11:05 welcome[bot]

Welcome @felixwan-git, it might be an option to provide a custom JSON engine in the future. But this won't be added in v1.9.x anytime soon, we used json-niter as our default JSON parser until Gov1.14 was released and had better performance, this was good news because we could have one dependency less.

goos: linux/amd64
BenchmarkJsoniter-4    980358     1061 ns/op   72 B/op    3 allocs/op
BenchmarkJson-4        1177653    1007 ns/op   64 B/op    2 allocs/op

goos: windows/amd64
BenchmarkJsoniter-4    4473760    259 ns/op    72 B/op    3 allocs/op
BenchmarkJson-4        4625077    251 ns/op    64 B/op    2 allocs/op

goos: darwin/amd64
BenchmarkJsoniter-4    2059158    801 ns/op    72 B/op    3 allocs/op
BenchmarkJson-4        2738658    400 ns/op    64 B/op    2 allocs/op

To answer your second question, to ignore fields wile marshaling you can use:

// Field is ignored by this package.
Field int `json:"-"`

// Field appears in JSON as key "myName".
Field int `json:"myName"`

// Field appears in JSON as key "myName" and
// the field is omitted from the object if its value is empty,
// as defined above.
Field int `json:"myName,omitempty"`

// Field appears in JSON as key "Field" (the default), but
// the field is skipped if empty.
// Note the leading comma.
Field int `json:",omitempty"`

Fenny avatar May 15 '20 16:05 Fenny

Im not sure how exactly the json engine is used/called, but wouldn't it be possible to use an interface? This interface would then just specify a marshal/unmarshal function that will be used.

Or you could have two functions, marshal and unmarshal which are by default the ones that are used right now.

You could then have a simple option or setter to provide your own engine or functions.

Lol3rrr avatar May 15 '20 17:05 Lol3rrr

Hi @Fenny usually we can use json tag to complete. But in some cases, especially when the json tag cannot be changed, some special settings are needed to achieve the expectations. For example, the pb.go structure generated by protobuf cannot be used with the default encoding / json library because we cannot change the json tag, but the third-party library can do it. This saves us a lot of development costs because we do n’t need to write New structure to convert interface data. Secondly , when the structure value type is numeric, the incoming string on the web front end cannot be serialized.

Using interface to achieve third-party adaptation should be easy. For example, many component libraries use logs, which can be done through SetLogger ().

binwan-dev avatar May 16 '20 02:05 binwan-dev

@Lol3rrr, this is definitely possible. I'm only wondering what JSON engines are available out there other than jsoniter. If there is a better alternative other than the default JSON package we could consider adding this to Fiber core.

until then i'm closing this issue, feel free to re-open šŸ‘

Fenny avatar May 27 '20 09:05 Fenny

@Fenny how about https://github.com/segmentio/encoding

g5becks avatar Sep 03 '20 20:09 g5becks

Looks promising, I will need to take a further look to see if it's fully compatible with the default libary

Benchmark_JSON_B_Default-16              5580151               213 ns/op              32 B/op          1 allocs/op
Benchmark_JSON_B_Default-16              5659111               213 ns/op              32 B/op          1 allocs/op
Benchmark_JSON_B_Default-16              5659100               213 ns/op              32 B/op          1 allocs/op
Benchmark_JSON_B_Default-16              5606218               213 ns/op              32 B/op          1 allocs/op

Benchmark_JSON_B_Iterator-16             5881000               203 ns/op              40 B/op          2 allocs/op
Benchmark_JSON_B_Iterator-16             5909929               204 ns/op              40 B/op          2 allocs/op
Benchmark_JSON_B_Iterator-16             5852350               203 ns/op              40 B/op          2 allocs/op
Benchmark_JSON_B_Iterator-16             5881038               204 ns/op              40 B/op          2 allocs/op

Benchmark_JSON_B_Segmentio-16           11006770               109 ns/op              32 B/op          1 allocs/op
Benchmark_JSON_B_Segmentio-16           10808454               109 ns/op              32 B/op          1 allocs/op
Benchmark_JSON_B_Segmentio-16           11006679               110 ns/op              32 B/op          1 allocs/op
Benchmark_JSON_B_Segmentio-16           11006709               109 ns/op              32 B/op          1 allocs/op

Fenny avatar Sep 03 '20 20:09 Fenny

// default
Benchmark_Ctx_JSON-16            4596667               260 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSON-16            4603731               259 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSON-16            4652700               259 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSON-16            4598620               259 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSONP-16           3491892               343 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSONP-16           3518269               342 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSONP-16           3487590               343 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSONP-16           3488151               343 ns/op              64 B/op          2 allocs/op

// segmentio
Benchmark_Ctx_JSON-16            7186842               165 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSON-16            7214056               164 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSON-16            7227295               164 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSON-16            7227291               165 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSONP-16           4918628               245 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSONP-16           4857201               245 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSONP-16           4876953               246 ns/op              64 B/op          2 allocs/op
Benchmark_Ctx_JSONP-16           4896874               245 ns/op              64 B/op          2 allocs/op

Fenny avatar Sep 03 '20 20:09 Fenny