-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Allow registering a hook to modify/pre-process String values read #2971
Comments
Example usage suggestion, made it verbose(not lambda) to express that we would need interface class Bean {
public int hello;
}
// NEW FEATURE
ValuePreprocessor processor = new ValuePreprocessor() {
@Override
public String preprocess(String name, String expected) {
// do something... like trimming
return
}
}
// USAGE
@Test
public test() {
// polluted input
String input = a2q(" {`hello`:123} #_F#EFJ)#")
// configure it
ObjectMapper mapper = JsonMapper.builder()
.registerValuePreprocessor(processor)
.build();
// use it, successfully
assertNotNull(mapper.readValue(input, Bean.class));
assertThrows(SomeDeserException.class,
() -> mapper.reader()
.withoutValuePreprocessor() // <---- sometimes we may not want it?
.readValue(input, Bean.class));
} |
I'd prefer if the new class was called ValuePreprocessor and it was aimed at allowing values to be modified. So in I don't know what to do about arrays like
If we want to make the names preprocessable, we could add a separate feature to support a NamePreprocessor. |
Sure, I don't have preference for name yet.
Seems like |
If you have JSON with this: You get a call to the ValuePreprocessor with this: The ValuePreprocessor is user provided and the user may have code in their ValuePreprocessor where they want to uppercase values but only when the name value is equal to |
Thank you for the explanation 🙏🏼 I first thought the preprocessor would take in the whole input (like JSON String, P.S. Modified example usage accordingly. |
I could be wrong but I really don't see the point in jackson pre-processing the entire input as one string. A user can trivially do that already in their own code before asking Jackson to parse the modified input. But the ValuePreprocessor as I describe it is useful because the JSON parser has added some value by working out the structure of the input. |
I concur 👍🏼 |
Correct: this would apply to But I suspect that is more generally usable approach. |
(note: counterpart to FasterXML/jackson-core#355 -- see more discussion there)
For some use cases -- for example, variable substitution -- it would be beneficial to be able to register a handler that can replace parts of incoming String values before other databinding functionality handles it.
Since there are various ways this could occur, I'll add a databinding issue here; the original
jackson-core
one (see above) may be most likely way to get there, but even if so there probably needs to be a way to register such handler on per-call basis forObjectReader
s (and/or maybeObjectMapper
).The text was updated successfully, but these errors were encountered: