-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
writeRaw binary content #914
Comments
I do not recommend Also note that the answer may depend on specific format backend, at least when using last-effort methods like |
I mean specifically Currently, if I have an already encoded valid byte array (e.g. in Smile format), in order to append it, I need first to parse it (i.e. with |
Ok. That makes sense then. At this points, PRs would be welcome and/or specific per-format tickets to denote where exactly functionality is missing. As a work-around one can usually hold on to |
Thanks! I think it would be even better adding new methods to This would allow further enhancements at databind level by adding support for
What is your opinion about it? |
From API perspective it is sort of tricky, given that now we have set of methods that work/don't-work on different backends (text vs binary). I specifically would be against supporting binary-content - to -text-format (which some users would no doubt want :) ) because it could or could not work based on backend (not for But then again without this, I think my initial thinking is that I would want to defer variant(s) that take streams, and start with just We might also need to either use an existing It might make sense to divide into first supporting low-level ( I probably won't have tons of time to work on this myself but if you or anyone else has time, I would find time to code review and help with work. |
Sounds good to me, thanks for sharing! I will draft a PR as I find time. |
Hi @cowtowncoder , AFAICS, this can be already achieved with For example, the implementation of jackson-core/src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java Lines 735 to 744 in b762c2e
The problem is that class RawUserDataValue implements SerializableString {
private final byte[] data;
RawUserDataValue(byte[] data) {
this.data = data;
}
@Override
public String getValue() {
throw new UnsupportedOperationException();
}
@Override
public int charLength() {
throw new UnsupportedOperationException();
}
@Override
public char[] asQuotedChars() {
throw new UnsupportedOperationException();
}
@Override
public byte[] asUnquotedUTF8() {
return data;
}
@Override
public byte[] asQuotedUTF8() {
throw new UnsupportedOperationException();
}
@Override
public int appendQuotedUTF8(byte[] buffer, int offset) {
throw new UnsupportedOperationException();
}
@Override
public int appendQuoted(char[] buffer, int offset) {
throw new UnsupportedOperationException();
}
@Override
public int appendUnquotedUTF8(byte[] buffer, int offset) {
final int length = data.length;
if ((offset + length) > buffer.length) {
return -1;
}
System.arraycopy(data, 0, buffer, offset, length);
return length;
}
@Override
public int appendUnquoted(char[] buffer, int offset) {
throw new UnsupportedOperationException();
}
@Override
public int writeQuotedUTF8(OutputStream out) {
throw new UnsupportedOperationException();
}
@Override
public int writeUnquotedUTF8(OutputStream out) throws IOException {
final int length = data.length;
out.write(data, 0, length);
return length;
}
@Override
public int putQuotedUTF8(ByteBuffer buffer) {
throw new UnsupportedOperationException();
}
@Override
public int putUnquotedUTF8(ByteBuffer buffer) {
final int length = data.length;
if (length > buffer.remaining()) {
return -1;
}
buffer.put(data, 0, length);
return length;
}
} and append raw value as bytes to the generator this way: JsonGenerator gen;
byte[] rawValueToAppend;
// ...
gen.writeRawValue(new RawUserDataValue(rawValueToAppend)); This works for example with IMHO, this solution is enough from a functional standpoint. Nonetheless, we can consider about making the API a bit more clear w.r.t. this, handling differently serialized string content (e.g. |
@rashtao True. I am bit worried about the fact that |
I think it is very untidy to create a new class that implements so little. Could we just add an extra method to UTF8JsonGenerator that takes a byte array as input. We already have UTF8 related methods that take byte arrays as inputs. |
I guess there's more than one way to skin a cat -- and if this should then be a method in I am not sure I agree with addition of a class necessarily being worse than adding a method at |
Right, I agree, adding new methods to support this is definitively more clear, i.e. as discussed above: #914 (comment) |
What is the recommended way to write raw binary content? As far as I can see
com.fasterxml.jackson.core.JsonGenerator#writeRaw()
variants only supportString
/char[]
/SerializableString
arguments, which are not suitable for binary formats.Is there any plan to extend it and accept binary arguments, i.e.
byte[]
/InputStream
?The text was updated successfully, but these errors were encountered: