Promotion
University
Learning
Courses
Tasks
Surveys & Quizzes
Games
Help
Butt-kicking schedule
Internship
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Article
  • Reviews
  • About us
  • CS50
Start
Start learning
  • Articles
  • Authors
  • All groups
  • All Articles List
JavaRush /Java Blog /Random EN /Serialization in Jackson. How can I explicitly specify wh...
ColdDeath
Level 34
Москва
  • 28 February 2021
  • 604 views
  • 0 comments

Serialization in Jackson. How can I explicitly specify which subclass to use?

Published in the Random EN group
Frontend-университет
Having read lectures at level 33, I can’t understand how, when serializing into JSON using Jackson, specify an additional field, type, for classes with the same fields, how to do this in lecture 4. I indicate that I want to enter the type field, but nothing happens Serialization in Jackson.  How can I explicitly specify which subclass to use?  - 1 . Please tell me what am I doing wrong? public class Solution { public static void main(String[] args) throws IOException { Cat cat = new Cat(); cat.name = "Murka"; cat.age = 5; cat.weight = 3; Dog dog = new Dog(); dog.name = "Killer"; dog.age = 8; dog.owner = "Bill Jefferson"; ArrayList pets = new ArrayList(); pets.add(cat); pets.add(dog); StringWriter writer = new StringWriter(); convertToJSON(writer, pets); System.out.println(writer.toString()); //[{"name":"Murka","age":5,"weight":3},{"name":"Killer","age":8,"owner":"Bill Jefferson"}] } public static void convertToJSON(StringWriter writer, Object object) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(writer, object); } @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") @JsonSubTypes({ @JsonSubTypes.Type(value = Cat.class, name = "cat"), @JsonSubTypes.Type(value = Dog.class , name = "dog") }) public static class Cat extends Pet { public int age; public int weight; } public static class Dog extends Pet { public int age; public String owner; } public static class Pet { public String name; } }
Python-университет
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
Company
  • About us
  • Contacts
  • Reviews
  • FAQ
  • Support
JavaRush JavaRush is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ of tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning, and a Java developer’s career.
Follow us
Interface language
Русский
English
Українська
Programmers Are Made, Not Born © 2025 JavaRush
Download App
  • Google Play
  • App Store
Mastercard Visa
Programmers Are Made, Not Born © 2025 JavaRush