It’s pretty convenient and makes the code easier to read. It’s worth mentioning that we’ve used Assertj’s returns() and from() to assert multiple properties in one single assertion. returns(expectedDate, from(UserIgnoreCase::getCreatedDate)) returns("Smith", from(UserIgnoreCase::getLastName)) returns("John", from(UserIgnoreCase::getFirstName)) ![]() SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSzz") ĭate expectedDate = fmt.parse("T07:53:34.740+0000") But this means that you have to make sure that all components in use have matching major versions: specifically, Jackson 2.x code does NOT understand or support Jackson 1.x annotations, or vice versa. Now, if we deserialize our JSON input to UserIgnoreCase, it works as expected: UserIgnoreCase result = new ObjectMapper().readValue(JSON_STRING, UserIgnoreCase.class) These versions can co-exist as they are located in different Java packages and use different jar naming and Maven group/artifact ids. ![]() the rest is the same as the User class Private String =, pattern = Date createdDate Next, let’s create a UserIgnoreCase class with this annotation and feature: = _CASE_INSENSITIVE_PROPERTIES) To use this feature, we can include it in a class-level annotation. The ACCEPT_CASE_INSENSITIVE_PROPERTIES feature tells the deserializer to match property names case-insensitively. It has predefined a set of options to specify property serialization or deserialization behaviors. The annotation allows us to set a set of JsonFormat.Feature values via the with attribute: = JsonFormat.Feature … ).įurthermore, JsonFormat.Feature is an enum. So next, let’s explore how to achieve that using the annotation. To solve this kind of problem, we must make our deserializer perform case-insensitive deserialization. Now, if we deserialize this JSON document to a User object, UnrecognizedPropertyException will be raised: assertThatThrownBy(() -> new ObjectMapper().readValue(JSON_STRING, User.class)).isInstanceOf(UnrecognizedPropertyException.class) Īs the test above shows, we’ve used Assertj’s exception assertion to verify that the expected exception is thrown. Let’s understand the problem quickly through an example.įirst, let’s say we have a JSON document as the input: static final String JSON_STRING = we can see, the three properties “FIRSTNAME”, “lastname”, and “cReAtEdDaTe” follow completely different letter case rules. ![]() The default deserializer cannot automatically recognize the property names in different letter cases.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |