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 7967763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:58:26+00:00 2026-06-04T06:58:26+00:00

I have an object from old Java code, and I now changed the serialized

  • 0

I have an object from old Java code, and I now changed the serialized object code. I want to be able to read both the old files and the new files. I need a branching statement in readObject to do something like:

if (next object is int -- just poking, it might be an Object)
{
    // we know we are in version 1
} else {
    // read new version of object
}

is that possible to do?

  • 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-04T06:58:27+00:00Added an answer on June 4, 2026 at 6:58 am

    Ok so basically the question is “How can we check with an ObjectInputStream whether the next field is a primitive or an object?” and the answer as far as I can see is: You can’t.

    Which means the best solution I can see to keep backwards compatibility is to never remove primitives from the original version – keeping useless information blows up the size a bit, but otherwise that’s easy.

    To add new fields, I can see two ways:

    1. Keep the earlier message format identical and only add new objects at the end – you can then easily distinguish different versions by the message size (or more exactly you’ll get an IOException when reading data of v2 when you get a v1 object). That’s much simpler and generally preferred.

    2. You can change objects from v1 to v2 and then do a simple instanceof check. If you want to add primitives is to store their wrapper versions (i.e. Integer et al). Can save you some bytes, but Java’s serialization protocol was never efficient to begin with, so really that’s unnecessary complicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Or is it? I have a thread object from: Thread myThread = new Thread(pObject);
I have the following code that loads a user's Active Directory DirectoryEntry object from
I have a Java class that is stored in an HttpSession object that's serialized
I have some existing code that accepts a java.sql.ResultSet that contains info retrieved from
New to php .I'm working on an old code in which I have to
As an extension of Java ClassNotFoundException when reading object from a stream I have
I have a stdClass object created from json_decode that won't return the right number
I have an object I'm trying to populate from another object (that is, iterate
--Summary (shortened)-- I have a controller that loads a profile object from the corresponding
Suppose we have a class. We create an object from the class and when

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.