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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:10:10+00:00 2026-05-19T22:10:10+00:00

How can I implement serialization on my own. Meaning I don’t want my class

  • 0

How can I implement serialization on my own. Meaning I don’t want my class to implement serializable. But I want to implement serialization myself. So that without implementing serializable I can transfer objects over network or write them to a file and later retrieve them in same state. I want to do it since I want to learn and explore things.

  • 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-19T22:10:11+00:00Added an answer on May 19, 2026 at 10:10 pm

    Serialization is the process of translating the structure of an object into another format that could be easily transfered across network or could be stored in a file. Java serializes objects into a binary format. This is not necessary if bandwidth/disk-space is not a problem. You can simply encode your objects as XML:

    // Code is for illustration purpose only, I haven't compiled it!!!
    
    public class Person {
        private String name;
        private int age;
        // ...
    
       public String serializeToXml() {
           StringBuilder xml = new StringBuilder();   
           xml.append("<person>");
           xml.append("<attribute name=\"age\" type=\"int\">").append(age);
           xml.append("</attribute>");
           xml.append("<attribute name=\"name\" type=\"string\">").append(name);
           xml.append("</attribute>"); 
           xml.append("</person>");
           return xml.toString(); 
    }
    

    Now you can get an object’s XML representation and “serialize” it to a file or a network connection. A program written in any language that can parse XML can “deserialize” this object into its own data structure.

    If you need a more compact representation, you can think of binary encoding:

      // A naive binary serializer. 
      public byte[] serializeToBytes() {
          ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    
          // Object name and number of attributes.
          // Write the 4 byte length of the string and the string itself to
          // the ByteArrayOutputStream.
          writeString("Person", bytes);
          bytes.write(2); // number of attributes;
    
          // Serialize age
          writeString("age", bytes);
          bytes.write(1); // type = 1 (i.e, int)
          writeString(Integer.toString(age), bytes);  
    
          // serialize name
          writeString("name", bytes);
          bytes.write(2); // type = 2 (i.e, string) 
          writeString(name, bytes);
    
          return bytes.toByteArray();
      }
    
      private static void writeString(String s, ByteArrayOutputStream bytes) {
          bytes.write(s.length());
          bytes.write(s.toBytes());
      }
    

    To learn about a more compact binary serialization scheme, see the Java implementation of Google Protocol Buffers.

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

Sidebar

Related Questions

I can implement both variants - it's easy. But I'm interested: what approach is
I know that classes can implement various special methods, such as __iter__ , __setitem__
I'd like to know if I can implement a method on MemoryCache that removes
I'm trying to implement my own serialization / var_dump style function in PHP. It
I am trying to implement struts2-jquery-grid. But I am stuck with the serialization issue.
I have to implement ISerializable in a derived class (to do some custom serialization/deserialization)
I have a class like this that I want to save in ViewState for
I am trying to implement a class for serialization (XML for now). The idea
How can implement reordring in winforms datagridview by using the feature of drag and
Can someone please tell me how I can implement the following line of pseudo-code.

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.