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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:20:59+00:00 2026-06-11T08:20:59+00:00

How do you receive serialization events? You can define void writeObject(ObjectOutputStream out) { //

  • 0

How do you receive serialization events? You can define

void writeObject(ObjectOutputStream out) {
  // handle event
  out.defaultWriteObject(this);
}

in java serialization and this method will be called when your object is serialized. How do you do the same thing in Kryo? Both KryoSerializable and Externalizable have the problem of default serialization: once you invoked your event handler, you want the default read/write object. But there is no such thing! ? You may invoke FieldSerializer in read(Kryo, Input) to read the fields of the object, but it will produce you a new object instead of populating the current one. For this reason, I tried to introduce a custom serializer:

Serializer def = kryo.getDefaultSerializer(A.class)
kryo.addDefaultSerializer(A.class, new Serializer() {
    public void write(Kryo kryo, Output output, Object object) {
        ((A)object).serializationEvent();
        def.write(kryo, output, object);

But, I mentioned that through subclasses of A receive the serializationEvent() event, only A.class fields are serialized. So, this does not work for class B extends A. I also tried the solution proposed by Natan: register(A.class, new FieldSerializer(A.class, myhandler. This serializes all fields, including subclasses, but, the custom serializer is not invoked for subclasses at all. So, I decided that Kryo customization works only for final classes. Nathan says that this conclusion is “invalid” and KryoSerializable solution “application-specific” and thinking otherwise “rude”. Despite of such resolution, I decided to publish the general method I discovered.

  • 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-11T08:21:00+00:00Added an answer on June 11, 2026 at 8:21 am

    I have discovered two solutions. At first, overriding writeReferenceOrNull can work

    Kryo kryo = new Kryo() {
        public boolean writeReferenceOrNull (Output output, Object object, boolean mayBeNull) {
            if (object instanceof A) {
                ((A) object).serializationEvent();
            }
    
            return super.writeReferenceOrNull(output, object, mayBeNull);
        }
    

    But, it needs source code visibility change and Natan says that this works only when references are enabled (in the default case) and recommends a more reliable approach instead: overriding the newDefaultSerializer:

    public class EventFiringKryo extends Kryo {
        protected Serializer newDefaultSerializer(Class type) {
            final Serializer def = super.newDefaultSerializer(type);
            Serializer custom = new Serializer() {
    
                public void write(Kryo kryo, Output output, Object object) {
                    System.err.println("writing " + object + ":" + object.getClass().getSimpleName());
                    if (object instanceof A)
                        ((A)object).serializationEvent();
                    def.write(kryo, output, object);
                }
    
                public Object read(Kryo kryo, Input input, Class type) {
                    Object result = def.read(kryo, input, type);
                    if (result instanceof SomeAnotherType)
                        result.canInitializeSomethingElse();
                    return result;
                }
            };
            return custom;
        }
    
    }
    

    In addition to being effective, this method does not suffer from the need to carefully register all classes that implement the interface that you are going to invoke.

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

Sidebar

Related Questions

I receive this error when I click on the section of my Tab-Bar application
I receive TypeError: Can't convert 'float' object to str implicitly while using Gambler.pot +=
Lets say I have a WCF service that a client can use to receive
I receive this error when I try to use { to generate Javascript objects
I am struggling few hours with this and can't find a solution to the
I have a few Java classes that implements Serialization, and corresponding AS objects (self-generated
I am programming a networked turn based java game, and use the java serialization
I receive a date in the format below from a json file. Im not
I receive the following error in Microsoft SQL Server Report Builder Error While generating
We receive (#4) Application request limit reached error when a page is showing the

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.