Тёмный

Spring Boot & JsonNode: How to use it and when to turn to creating Custom Deserializers 

Dan Vega
Подписаться 61 тыс.
Просмотров 5 тыс.
50% 1

Опубликовано:

 

15 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 13   
@Jamous999
@Jamous999 Год назад
Thank you so much for this nice way to handle json ❤❤❤
@alexandern9266
@alexandern9266 Год назад
First of all thanks for the fantastic content you make! Can you please make a short video about the staff you use (like mic, cam, ...) to make your awesome videos?
@victorh.torres2713
@victorh.torres2713 Год назад
Awesome! Thank you.
@FredrikRambris
@FredrikRambris Год назад
The linked Git repo points to a spring-boot-read-yaml project
@DanVega
@DanVega Год назад
Thank you, fixed!
@igorputnik4216
@igorputnik4216 Год назад
Hi. What ide and plugins do you use?
@sudipta_samanta
@sudipta_samanta Год назад
How to handle the check if the key is present or not in the json node? If it is very nested, it would not be nice to check for null every step.
@asterixcode
@asterixcode Год назад
Funny thing I had to come with a solution like this video at work a few weeks ago, then got problems with nulls and yes, did null checks on every step. Maybe there is a smarter way, but who knows. Example: JsonNode detailsNode = node.get("details"); if (detailNode.has("usagePublicId") && !detailNode.get("usagePublicId").isNull()) { detailAvsc.setUsagePublicId(detailNode.get("usagePublicId").asText()); }
@asterixcode
@asterixcode Год назад
if you are using the objectMapper, then in the setup of the test "objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);" will do the trick (not entirely sure for nulls, but at least for not mapped fields, it does), but for a custom deserializer, then no idea other than checking as above if it has the field & if it's not null, for every step.
@asterixcode
@asterixcode Год назад
just saw the comment from FredrikRambris below... maybe Optional might be an option :D
@luisdanielmesa
@luisdanielmesa Год назад
I don't like Spring, but it seems that you want something like jsonPath...
@FredrikRambris
@FredrikRambris Год назад
Or @Test void parseFile() throws JsonProcessingException { var tree = objectMapper.readTree(json); var posts = Optional.ofNullable(tree.get("data")) .flatMap(n -> Optional.ofNullable(n.get("allPost"))) .flatMap(n -> Optional.ofNullable(n.get("edges"))) .filter(JsonNode::isArray) .map(Iterable::spliterator) .map(s -> StreamSupport.stream(s, false)) .stream() .flatMap(s -> s) .map(n -> n.get("node")) .map(n -> { try { return objectMapper.treeToValue(n, Post.class); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }) .toList(); assertThat(posts).isNotEmpty(); } Post.class is a simple record of a post. The magic is treeToValue. Use traversing functions to get to the actual object and then use standard Jackson deserialization to your objects.
Далее
Let's Fix The Fatal Flaw in TypeScript's Union Types!
13:45
Serialize and Deserialize JSON using Jackson
35:51
Просмотров 7 тыс.