-
Notifications
You must be signed in to change notification settings - Fork 48
Add a generic enum-as-number serdes #59
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
base: jackson29-proto3
Are you sure you want to change the base?
Conversation
This is particularly useful when you want to serialize a specific field on a specific type in this manner, without modifying the root object mapper config.
3c779f6
to
f584f64
Compare
import com.google.common.base.Preconditions; | ||
import com.google.protobuf.ProtocolMessageEnum; | ||
|
||
public final class ProtobufEnumAsNumber { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to reorganize this into two separate classes if you prefer that style.
int intValue = jsonParser.getIntValue(); | ||
Class<?> enumClass = Preconditions.checkNotNull(javaType, "javaType").getRawClass(); | ||
try { | ||
return (ProtocolMessageEnum) enumClass.getMethod("forNumber", int.class).invoke(null, intValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's worth memoizing the class → method mapping, but I'm not super concerned about the performance implications of this implementation in the context I'd like to use this (deserializing from SQL via Rosetta).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be moved into the Deserializer
constructor but I don't remember off the top of my head whether contextual deserializers get cached automatically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved it into createContextual()
. 9d513ec
try { | ||
return (ProtocolMessageEnum) enumClass.getMethod("forNumber", int.class).invoke(null, intValue); | ||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
context.reportWrongTokenException(ProtocolMessageEnum.class, JsonToken.VALUE_NUMBER_INT, wrongTokenMessage(context)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I borrowed this pattern from the other deserializer implementations, but I'm not sure if this is really a wrong-token exception.
import com.hubspot.jackson.datatype.protobuf.util.TestProtobuf; | ||
import com.hubspot.jackson.datatype.protobuf.util.TestProtobuf.Enum; | ||
|
||
public class ProtobufEnumAsNumberTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to add more test cases if you think there are major blind spots.
lgtm, might be worth moving the reflective method lookup to the |
Hm, not sure if there's a way to move the reflective method lookup out of
|
fb43105
to
f7322f1
Compare
I got this building again. Feel free to merge and cut the necessary releases whenever, I'm not in a massive hurry to get this feature out. |
This is particularly useful when you want to serialize a specific field on a
specific type in this manner, without modifying the root object mapper config.
@jhaber