-
Notifications
You must be signed in to change notification settings - Fork 321
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
Make the MessagePackParser#type field public #800
Conversation
It's useful when you need to distinguishe bytes array from extension type
@ArtDu Can you elaborate your use case? |
We use extensions values and byte arrays as well to store data in Tarantool(https://en.wikipedia.org/wiki/Tarantool). private boolean isExtensionValue(Object object) {
return object instanceof MessagePackExtensionType;
}
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
if (p.currentTokenId() != JsonTokenId.ID_EMBEDDED_OBJECT || !isExtensionValue(p.getEmbeddedObject())) {
return super.deserialize(p, ctxt);
}
MessagePackExtensionType ext = p.readValueAs(MessagePackExtensionType.class);
byte type = ext.getType();
...
} Instead of it we can use type variable that is stored in MessagePackParser @Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
MessagePackParser mpParser = (MessagePackParser)p;
if (mpParser.getType().equals(Type.EXT)) {
return super.deserialize(p, ctxt);
}
...
} |
@ArtDu Thanks for sharing the details.
BTW, as you use jackson-databind interface, I guess you serialize Java objects to MessagePack format data and/or deserialize MessagePack format data to Java object vise verse. So, I think you know all the type of values when deserializing MessagePack format data since you know how the data is serialized. For instance, let's say you serialize an instance of this class public static class Foo {
String name;
byte[] data;
} and then, you know the type of |
In some cases it works for me, but I also need a serializer. That's why I use Also
We have two types of interaction:
|
@ArtDu I think understand your use case. Honestly speaking, the usage seems a bit too dynamic in terms of type-safety and to not mach jackson-databind interface. I don't think it's a common use case, and I'm hesitating to approve the change. As for Regarding the sample code you shared, I think you know the value is an embedded object at least in @Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
// At this point, the current value must be an embedded object, right?
Object embedded = p.getEmbeddedObject();
if (!(embedded instanceof MessagePackExtensionType)) {
// This should be `byte[]`.
handleByteArrayAsXxxxx(embedded);
return;
}
MessagePackExtensionType ext = (MessagePackExtensionType) embedded;
byte type = ext.getType();
...
} |
This w/a works fine from experience. Since that I close this PR and ticket
|
It's useful when you need to distinguishe bytes array from extension type
Closes #799