On Wed, Oct 3, 2018 at 1:53 AM Nilson Santos Figueiredo Jr.
<[email protected]> wrote:
>
> On Wed, Oct 3, 2018 at 7:38 AM, Tatu Saloranta <[email protected]> wrote:
> > On Tue, Oct 2, 2018 at 10:34 PM Nilson Santos Figueiredo Jr.
> > > I have a particular use-case where we're trying to optimize some code 
> > > which uses Jackson to generate less garbage.
> > > The desired outcome here is to do something similar to what 
> > > com.fasterxml.jackson.databind.ser.std.RawSerializer does, but avoiding 
> > > the toString() conversion.
> > > Everything is working fine, except I can't avoid the duplication of 
> > > byte[] buffers due to not having access to the private _writeBytes - I 
> > > either need to convert those to char[] or to String. This is of course 
> > > not ideal, as it then becomes a source of garbage.
> > I am not sure I follow exactly what you are attempting to do here, so
> > could you give an example of hypothetical calls you would make?
> > What kind of input would come in as byte[], other than binary data to
> > be base64 encoded?
>
> Sure - I basically want to do exactly what
> JsonGenerator.writeRaw(char[] cbuf, int offset, int len) does, but
> with a byte[] buffer instead.
>
> The source of this byte[] is from a previous HTTP call to somewhere
> else, so it's already allocated. With the current API, I'd need to
> either create a new String or convert the byte[] to char[], both of
> which would required further allocations, which is specifically what
> I'm trying to avoid. The exact context is that this is a middleman
> service which gets JSON from some other service does some basic
> processing, but returns large chunks of this JSON to the caller
> unchanged.
>
> I currently have a workaround in place which is less than ideal: 1)
> create JsonGenerator based on some OutputStream; 2) write some stuff
> using JsonGenerator methods (writeStartObject, WriteXXXField, etc); 3)
> JsonGenerator.flush(); 4) write remaining stuff directly to the
> underlying OutputStream; 5) Call JsonGenerator.writeEndObject()
>
> So I want to inject arbitrary content in the middle of the JSON output
> and I need to do it generating little to no additional garbage.

Ah ok. Yes, I can see how that would be potentially useful.

Challenge/concern here is just that since output target is not
necessarily byte-based (may be `Writer`),
there is potential for exception (or expensive encoding). But I would
accept the possibility of method
that only works for case of having byte-based target.

However: there is another option that might work better for you:

    public void writeRawValue(SerializableString text) throws IOException;

where you can use your own implementation of `SerializableString`.

... however, come to think of it now, this probably requires implementation of

   https://github.com/FasterXML/jackson-core/issues/484

to actually avoid forcing allocation, as current implementation does
not (yet) use append method but requires
byte[].

Having said that, if you want you can just file the request for adding
`writeRaw(byte[] b, int offset, int len)` into `JsonGenerator`.
I can then consider adding that in 2.10.

-+ Tatu +-

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to