Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a plan to expose the internal decoder for buffer use case? #711

Closed
spacewander opened this issue Nov 19, 2024 · 4 comments
Closed

Comments

@spacewander
Copy link
Contributor

In the current implementation, each Unmarshal will create a new decoder:

sonic/sonic.go

Lines 113 to 124 in 4a3e8af

func (cfg frozenConfig) UnmarshalFromString(buf string, val interface{}) error {
dec := decoder.NewDecoder(buf)
dec.SetOptions(cfg.decoderOpts)
err := dec.Decode(val)
/* check for errors */
if err != nil {
return err
}
return dec.CheckTrailings()
}

There is already a Reset method in the underlying decoder, however, only the streaming decoder can call it:

self.Decoder.Reset(string(self.buf[s:e]))

In our scene, we process fixed JSON string and want to cache the decoder instead of creating a new one each call. Wrapping the fixed JSON strings as an io stream is unavailable so we can't use StreamDecoder.

It would be helpful to provide a way to expose the internal decoder, for example, under the name "BufferDecoder" and so on. We would like to contribute if this is acceptable.

@AsterDY
Copy link
Collaborator

AsterDY commented Nov 20, 2024

I didn't get it. Decoder only holds the string you pass, there is no buffer..

@spacewander
Copy link
Contributor Author

What I mean by using "buffer" is to refer to a new kind of Decoder that is different from the current exposed Decoder.

In the current implementation, the NewDecoder always returns a StreamDecoder:

sonic/sonic.go

Lines 139 to 143 in e2ff8ac

func (cfg frozenConfig) NewDecoder(reader io.Reader) Decoder {
dec := decoder.NewStreamDecoder(reader)
dec.SetOptions(cfg.decoderOpts)
return dec
}

The StreamDecoder accepts an IO stream, and decodes data from it.

Meanwhile, there could be a BufferDecoder, which accepts a JSON string and uses Reset to clear the state when it wants to decode another JSON string.

@AsterDY
Copy link
Collaborator

AsterDY commented Nov 21, 2024

Why not directly use sonic/decoder/NewDecoder()? It can do everythin including set options

@spacewander
Copy link
Contributor Author

Thanks! That's what I need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants