Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:47:41+00:00 2026-06-16T01:47:41+00:00

I am serializing an instance of a class with Jackson. What is the best

  • 0

I am serializing an instance of a class with Jackson.
What is the best way to only serialize the fields of the superclass of this instance?

Currently I have to add the JSONIgnore annotation to every method in my subclass,
is there any better way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:47:42+00:00Added an answer on June 16, 2026 at 1:47 am

    Assuming you have control of the serialization process directly via ObjectMapper, you can easily accomplish this by creating an ObjectWriter specific to your superclass. Here is some sample code illustrating this.

    public class Account implements Serializable {
        private String accountNumber;
        private String routingNumber;
        private BigDecimal balance;
    
        // Constructors, setters/getters
    }
    
    public class SavingsAccount extends Account {
        private BigDecimal savingsRate;
    
        // Constructors, setters/getters
    }
    
    
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectWriter writer = mapper.writerWithType(Account.class);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(6400);
    final SavingsAccount account = new SavingsAccount("0031-3402-2189",
        "0009835011203", BigDecimal.valueOf(53500),
         BigDecimal.valueOf(0.3));
    
    writer.writeValue(baos, account);
    final String results = new String(baos.toByteArray());
    baos.close();
    
    System.out.println(results);
    

    Running the above code illustrates that Jackson writes the superclass fields, even though the runtime instance is of the subclass type.

    {"accountNumber":"0031-3402-2189","routingNumber":"0009835011203","balance":53500}
    

    EDIT:

    Thanks, but in my case I try to achieve this behaviour for several
    classes are fields of the outer class that I serialize. Can I define
    the types more in general?

    This scenario is even easier to deal with. Just annotate the appropriate fields in the outer class with @JsonSerialize. If you declare the fields as the superclass type, then set the typing as static. This will flag those fields to Jackson to be serialized using the compile time type as opposed to the runtime type.

    On the other hand, if you declare the fields as the subclass type, then set the using as the superclass.

    The following example uses the classes I defined further above:

    public class BankCustomer implements Serializable {
        @JsonSerialize(typing=Typing.STATIC)
        private Account cdAccount;
    
        @JsonSerialize(using=Account.class)
        private SavingsAccount savingsAccount;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an instance of an EF entity that im serializing to JSON. Is
I am serializing objects to XML using System.Xml.Serialization and this requires me to have
I have the following XML which needs deserializing/serializing: <instance> <dog> <items> <item> <label>Spaniel</label> </item>
I have a little problem with a class I am currently writing a save
What's the practical way of serializing an instance of subclass by using DataContractSerializer? For
I have a class that's currently inheriting from Dictionary and then adds a few
I am having trouble serializing an object to xml. I have one class that
I have a big .net class and a few xslt files. I'm serializing my
I have an MVC view model that looks like this: public class DirectorySearchModel {
I have a C# Web Service that is serializing my simple class: [Serializable] [XmlInclude(typeof(Bitmap))]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.