Nikhil-n17 opened a new issue, #2193:
URL: https://github.com/apache/fury/issues/2193
### Question
I am encountering challenges during the deserialization process of data
serialized with class registration. As part of our migration to a new project,
which involved DTO refactoring, we need to exclude the class metadata from the
serialized data. Specifically, the deserialization process should not attempt
to locate classes that are part of the serialized data but should only utilize
the DTO provided during deserialization.
How can we modify the deserialisation fury instance to exclude class
metadata present in the serialised data and only use the provided DTO ?
Your assistance would be greatly appreciated and would help unblock us.
Thanks in advance.
Code to reproduce
```
package org.debug.fury;
import io.fury.Fury;
import io.fury.ThreadLocalFury;
import io.fury.ThreadSafeFury;
import io.fury.config.CompatibleMode;
import io.fury.config.Language;
import java.util.ArrayList;
import java.util.List;
class Person {
public String name;
public int age;
public Address address;
public List<PhoneNumber> phoneNumbers;
public Person(String name, int age, Address address, List<PhoneNumber>
phoneNumbers) {
this.name = name;
this.age = age;
this.address = address;
this.phoneNumbers = phoneNumbers;
}
}
class Address {
public String street;
public String city;
public String country;
public Address(String street, String city, String country) {
this.street = street;
this.city = city;
this.country = country;
}
}
class PhoneNumber {
public String type;
public String number;
public PhoneNumber(String type, String number) {
this.type = type;
this.number = number;
}
}
// New DTO with different naming structure but compatible schema
class PersonV2 {
public String fullName;
public int yearsOld;
public AddressV2 address;
public List<PhoneNumberV2> phoneNumbers;
public PersonV2(String fullName, int yearsOld, AddressV2 address,
List<PhoneNumberV2> phoneNumbers) {
this.fullName = fullName;
this.yearsOld = yearsOld;
this.address = address;
this.phoneNumbers = phoneNumbers;
}
}
class AddressV2 {
public String streetAddress;
public String city;
public String country;
public AddressV2(String streetAddress, String city, String country) {
this.streetAddress = streetAddress;
this.city = city;
this.country = country;
}
}
class PhoneNumberV2 {
public String phoneType;
public String phoneNumber;
public PhoneNumberV2(String phoneType, String phoneNumber) {
this.phoneType = phoneType;
this.phoneNumber = phoneNumber;
}
}
public class FuryCompressionExample {
// Create the ThreadLocalFury instance for serialization with class
registration enabled
public static final ThreadSafeFury serializationFury =
new ThreadLocalFury(
classLoader -> {
Fury f =
Fury.builder()
.withLanguage(Language.JAVA)
.withClassLoader(classLoader)
.registerGuavaTypes(false)
.withCompatibleMode(CompatibleMode.COMPATIBLE)
.requireClassRegistration(true) //
Enable class registration for serialization
.build();
f.register(Person.class);
f.register(Address.class);
f.register(PhoneNumber.class);
return f;
});
// Create the ThreadLocalFury instance for deserialization with class
registration disabled
public static final ThreadSafeFury deserializationFury =
new ThreadLocalFury(
classLoader -> {
Fury f =
Fury.builder()
.withLanguage(Language.JAVA)
.withClassLoader(classLoader)
.registerGuavaTypes(false)
.withCompatibleMode(CompatibleMode.COMPATIBLE) // Use COMPATIBLE mode to avoid
metadata issues
.requireClassRegistration(false) //
Disable class registration for deserialization
.build();
return f;
});
public static void main(String[] args) {
Address address = new Address("123 Main St", "Springfield", "USA");
List<PhoneNumber> phoneNumbers = new ArrayList<>();
phoneNumbers.add(new PhoneNumber("Mobile", "123-456-7890"));
phoneNumbers.add(new PhoneNumber("Home", "987-654-3210"));
Person person = new Person("John Doe", 30, address, phoneNumbers);
// Serialize the Person object using the Fury instance
byte[] serializedData =
serializationFury.serializeJavaObject(person);
System.out.println("Serialized Data (Base64): " +
java.util.Base64.getEncoder().encodeToString(serializedData));
// Now let's deserialize it back to the PersonV2 object using the
deserializationFury instance
try {
PersonV2 personV2 = deserializePersonV2(serializedData);
System.out.println(personV2);
} catch (Exception e) {
System.out.println("Error during deserialization: " +
e.getMessage());
e.printStackTrace(); // Print the full stack trace to identify
the source of the issue
}
}
// Custom deserialization method for PersonV2
public static PersonV2 deserializePersonV2(byte[] data) throws Exception
{
// Deserialize raw data using Fury without class registration
return deserializationFury.deserializeJavaObject(data,
PersonV2.class);
}
}
```
Error
```
Serialized Data (Base64):
/1koTMYAAAhKb2huIERvZSWoSgI8eDl5C4OCwpD/AJ4CWfzNmgAAC1NwcmluZ2ZpZWxks9woWt82AAAAAAsxMjMgTWFpbiBTdLPw9R3jsgkAAAADVVNB/v///////3+8L/3v5T0wWP8AQgIEoAJZ6MzfAAAGTW9iaWxls9RoCeMxAAAAAAwxMjMtNDU2LTc4OTD+////////f1nozN8AAARIb21ls9RoCeMxAAAAAAw5ODctNjU0LTMyMTD+////////f/7///////9/
Error during deserialization: Cannot read field "serializer" because
"classInfo" is null
java.lang.NullPointerException: Cannot read field "serializer" because
"classInfo" is null
at io.fury.resolver.ClassResolver.getClassInfo(ClassResolver.java:1060)
at io.fury.resolver.ClassResolver.readClassInfo(ClassResolver.java:1588)
at
org.debug.fury.PersonV2FuryCompatibleCodec_2_498931366_987547666.readField$(PersonV2FuryCompatibleCodec_2_498931366_987547666.java:180)
at
org.debug.fury.PersonV2FuryCompatibleCodec_2_498931366_987547666.readSeparateTypesHashFields$(PersonV2FuryCompatibleCodec_2_498931366_987547666.java:206)
at
org.debug.fury.PersonV2FuryCompatibleCodec_2_498931366_987547666.read(PersonV2FuryCompatibleCodec_2_498931366_987547666.java:285)
at io.fury.Fury.readDataInternal(Fury.java:899)
at io.fury.Fury.deserializeJavaObject(Fury.java:1060)
at io.fury.Fury.deserializeJavaObject(Fury.java:1042)
at
io.fury.ThreadLocalFury.deserializeJavaObject(ThreadLocalFury.java:108)
at
org.debug.fury.FuryCompressionExample.deserializePersonV2(FuryCompressionExample.java:145)
at
org.debug.fury.FuryCompressionExample.main(FuryCompressionExample.java:134)
```
pom
```
<dependency>
<groupId>org.furyio</groupId>
<artifactId>fury-core</artifactId>
<version>0.4.1</version>
</dependency>
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]