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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:45:31+00:00 2026-05-25T06:45:31+00:00

I have an object that contains a few unserializable fields that I want to

  • 0

I have an object that contains a few unserializable fields that I want to serialize. They are from a separate API that I cannot change, so making them Serializable is not an option. The main problem is the Location class. It contains four things that can be serialized that I’d need, all ints. How can I use read/writeObject to create a custom serialization method that can do something like this:

// writeObject:
List<Integer> loc = new ArrayList<Integer>();
loc.add(location.x);
loc.add(location.y);
loc.add(location.z);
loc.add(location.uid);
// ... serialization code

// readObject:
List<Integer> loc = deserialize(); // Replace with real deserialization
location = new Location(loc.get(0), loc.get(1), loc.get(2), loc.get(3));
// ... more code

How can I do this?

  • 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-05-25T06:45:31+00:00Added an answer on May 25, 2026 at 6:45 am

    Java supports Custom Serialization. Read the section Customize the Default Protocol.

    To quote:

    There is, however, a strange yet crafty solution. By using a built-in
    feature of the serialization mechanism, developers can enhance the
    normal process by providing two methods inside their class files.
    Those methods are:

    • private void writeObject(ObjectOutputStream out) throws
      IOException;
    • private void readObject(ObjectInputStream in) throws
      IOException, ClassNotFoundException;

    In this method, what you could do is serialize it into other forms if you need to such as the ArrayList for Location that you illustrated or JSON or other data format/method and reconstruct it back on readObject()

    With your example, you add the following code:

    
    
    private void writeObject(ObjectOutputStream oos)
    throws IOException {
        // default serialization 
        oos.defaultWriteObject();
        // write the object
        List loc = new ArrayList();
        loc.add(location.x);
        loc.add(location.y);
        loc.add(location.z);
        loc.add(location.uid);
        oos.writeObject(loc);
    }
    
    private void readObject(ObjectInputStream ois)
    throws ClassNotFoundException, IOException {
        // default deserialization
        ois.defaultReadObject();
        List loc = (List)ois.readObject(); // Replace with real deserialization
        location = new Location(loc.get(0), loc.get(1), loc.get(2), loc.get(3));
        // ... more code
    
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an object that contains a few fields that are Lists of
I have a object that contains data from a DB. The object has a
I have an object that contains a list as on of its fields, the
I have a javascript object that contains a few objects that contain associative arrays.
I have an object that contains about half a dozen properties. I expect to
I have an object that contains a array. On initialization of this object, the
For example lets say I have a JSON object that contains states and cities.
I have a Grails command object that contains an emailAddresses field, e.g. public class
I have a C# string object that contains the code of a generic method,
If I have an IEnumerable object in my .net code that contains a single

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.