This may be useful i.e. when your service should download file and send it inside json to other service without storing whole file in memory.
In my case service downloads a file from S3-like service and makes request to our email provider to send it as attachment (base64 encoded).
Currently I'm using this snippet:
func (a *Attach) MarshalJSONObject(e *gojay.Encoder) {
e.StringKey("name", a.Name)
e.StringKeyOmitEmpty("encoding", a.Encoding)
// attach content handling
if a.Content != nil {
e.AppendByte(',') // begin new field
e.AppendBytes([]byte(`"content"`)) // appends "content" key
e.AppendByte(':') // delimiter between key and value
e.AppendByte('"') // string begins
_, err := io.Copy((*encoderWriter)(e), a.Content) // big string (unsafe, no escaping)
if err != nil {
panic(err) // caught by caller
}
e.AppendByte('"') // string ends
}
}
This PR adds similar methods for decoding (may be useful i.e. when you get response where file goes as base64-encoded string and this file should be uploaded to somewhere).
Side changes:
decoder: better buffer reusability when parsing escape sequences
encoder: flush buffer when it's size reaches a limit provided in SetBufFlushThreshold if target writer provided (to reduce memory consumption). Should close #78
This may be useful i.e. when your service should download file and send it inside json to other service without storing whole file in memory. In my case service downloads a file from S3-like service and makes request to our email provider to send it as attachment (base64 encoded). Currently I'm using this snippet:
My changes allows to shorten it to this:
This PR adds similar methods for decoding (may be useful i.e. when you get response where file goes as base64-encoded string and this file should be uploaded to somewhere).
Side changes:
SetBufFlushThreshold
if target writer provided (to reduce memory consumption). Should close #78