New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Give access to root object in serializer #38
Comments
Try public class LocationSerializer implements ObjectSerializer<Location> {
@Override
public boolean supports(@NonNull Class<? super Location> type) {
return Location.class.isAssignableFrom(type);
}
@Override
public void serialize(@NonNull Location location, @NonNull SerializationData data, @NonNull GenericsDeclaration generics) {
data.setValue((location.getWorld() == null ? null : location.getWorld().getName()) + " " + location.getX() + " " + location.getY()
+ " " + location.getZ() + " " + location.getYaw() + " " + location.getPitch());
// data.add("world", location.getWorld(), World.class);
// data.add("x", location.getX());
// data.add("y", location.getY());
// data.add("z", location.getZ());
// data.add("yaw", location.getYaw());
// data.add("pitch", location.getPitch());
}
@Override
public Location deserialize(@NonNull DeserializationData data, @NonNull GenericsDeclaration generics) {
if (data.hasValue()) {
String valueString = data.getValue(String.class);
String[] parts = valueString.split(" ");
World world = data.getConfigurer().resolveType(
parts[0],
GenericsDeclaration.of(String.class),
World.class,
GenericsDeclaration.of(World.class),
SerdesContext.of(data.getConfigurer())
);
double x = Double.parseDouble(parts[1]);
double y = Double.parseDouble(parts[2]);
double z = Double.parseDouble(parts[3]);
float yaw = Float.parseFloat(parts[4]);
float pitch = Float.parseFloat(parts[5]);
return new Location(world, x, y, z, pitch, yaw);
}
World world = data.get("world", World.class);
double x = data.get("x", Double.class);
double y = data.get("y", Double.class);
double z = data.get("z", Double.class);
float yaw = data.get("yaw", Float.class);
float pitch = data.get("pitch", Float.class);
return new Location(world, x, y, z, pitch, yaw);
}
} |
So currently I can achieve message: "String" but not: message:
- "Line 1"
- "Line 2" Right? |
The The public <T> T getDirect(@NonNull String key, @NonNull GenericsDeclaration genericType) {
Object object = this.data.get(key);
return (T) this.configurer.resolveType(object, null, genericType.getType(), genericType, SerdesContext.of(this.configurer));
} public <T> T getValueDirect(@NonNull GenericsDeclaration genericType) {
return this.getDirect(ObjectSerializer.VALUE, genericType);
} There is nothing preventing you from calling public <T> List<T> getAsList(@NonNull String key, @NonNull Class<T> listValueType) {
GenericsDeclaration genericType = GenericsDeclaration.of(List.class, Collections.singletonList(listValueType));
return (List<T>) this.getAsCollection(key, genericType);
} public <T> Collection<T> getAsCollection(@NonNull String key, @NonNull GenericsDeclaration genericType) {
if (!Collection.class.isAssignableFrom(genericType.getType())) {
throw new IllegalArgumentException("genericType.type must be a superclass of Collection: " + genericType);
}
return this.getDirect(key, genericType);
} |
Well, the |
I'd like to access root object in serializer. I'll give an exmaple:
I have object... For eg. ComplexMessage that contains ChatMessage & ActionbarMessage . In normal serializer I would serialize it to something like that:
which is fine when I have 2 messages types used
But when I have only 1 message type (for eg. chat) I ends up with
but I wants it's to be simpler - and serialize it as String:
The text was updated successfully, but these errors were encountered: