Skip to content

[util-ajax] Support CharSequence instead of String for JSON.parse #13019

Closed
@scrat98

Description

@scrat98

Jetty version(s)
12.0.x

Enhancement Description

Currently, JSON.parse accepts a String, but since none of the actual String functionality is used, we could use a CharSequence instead. JSON reads char by char, without batch read, so it should be okay.

Enhancement use case

My use case is the following: transform Flow.Publisher<Chunk> to Flow.Publisher<T>. The format of messages is "length prefixed json" [payload size][json payload in utf8 encoding]

For that I'm using CharsetDecoder to make a transformation like Sequence<ByteBuffer> to Sequence<CharBuffer>.

CharBuffer is a CharSequence and therefore it's quite simple to wrap Sequence<CharSequence> to CharSequence without intermediate copying, but using String involves additional allocations, because it will transform char[] to byte[] internally (due to the string compaction and to avoid String mutation, since it must be immutable).

Also using CharsetStringBuilder is not an option, since I'm also using ByteBufferPool to allocate output CharBuffer, but Utf8StringBuilder for example, will always allocate new StringBuilder that will allocate every time new byte[] without possibility to reuse it.

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions