Skip to content
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

Fixed Serialization issues #9

Closed
wants to merge 10 commits into from

Conversation

MKDEVELOPEMENT
Copy link

module now runs without errors

}

/*@Override
public ChangeDialogAction deserialize(PersistedData data) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could clean it up. :-)

public CloseDialogAction deserialize(PersistedData data, DeserializationContext context) {
return new CloseDialogAction();
public Optional<CloseDialogAction> deserialize(PersistedData data) {
return Optional.empty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional.empty() is returned only when there was no value to deserialize/there was an error deserializing the value. In this case, you should return Optional.of(new CloseDialogAction()).

}

@Override
public PersistedData serialize(ChangeDialogAction action, PersistedDataSerializer context) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete this and override serializeNonNull instead.


@Override
public PersistedData serialize(ChangeDialogAction action, SerializationContext context) {
protected PersistedData serializeNonNull(ChangeDialogAction value, PersistedDataSerializer serializer) {
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this definition to your serialize definition. Also, it is not advised to return null PersisitedData instances -- use serializer.serializeNull() if you want to return null serialized values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eviltak I am just a bit confused, what exactly do you mean by change to my serialize definition

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not being clear enough. By that I meant move the code in serialize to serializeNonNull, and delete the serialize method.


@Override
public PersistedData serialize(CloseDialogAction action, SerializationContext context) {
public PersistedData serialize(CloseDialogAction action, PersistedDataSerializer context) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above -- override serializeNonNull instead.

}

@Override
protected PersistedData serializeNonNull(CloseDialogAction value, PersistedDataSerializer serializer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above -- move serialize's definition here.

Map<String, PersistedData> data = ImmutableMap.of(
"type", context.create(action.getClass().getSimpleName()),
"prefab", context.create(action.getPrefab()));
"type", context.serialize(action.getClass().getSimpleName()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this "type" mapping anymore, it can be removed. You could also choose to serialize this as a single string representing the prefab, like return context.serialize(action.getPrefab())

Map<String, PersistedData> data = ImmutableMap.of(
"type", context.create(action.getClass().getSimpleName()));
"type", context.serialize(action.getClass().getSimpleName()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above -- don't need "type" anymore, this can be serialized as a return context.serializeNull() (since there is nothing to serialize)


@Override
public PersistedData serialize(NewDialogAction action, SerializationContext context) {
public PersistedData serialize(NewDialogAction action, PersistedDataSerializer context) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above -- move to serializeNonNull.

}

@Override
protected PersistedData serializeNonNull(NewDialogAction value, PersistedDataSerializer serializer) {
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above -- move serialize's definition here

"type", context.create(action.getClass().getSimpleName()),
"target", context.create(action.getTarget()));
"type", context.serialize(action.getClass().getSimpleName()),
"target", context.serialize(action.getTarget()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above -- "type" mapping is unnecessary, you could simply return context.serialize(action.getTarget())

}

@Override
public ChangeDialogAction deserialize(PersistedData data, DeserializationContext context) {
public Optional<ChangeDialogAction> deserialize(PersistedData data) {
PersistedDataMap root = data.getAsValueMap();
String prefab = root.get("prefab").getAsString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are only serializing the prefab string, data will not be a value map any more -- it will simply be a string. String prefab = data.getAsString() should work. Also, make sure to add a failing condition if the PersistedData is not as expected. For this case, you might want to do something like

if (!data.isString()) {
    // Optional.empty() signifies that the data could not be deserialized
    return Optional.empty()
}

Without this condition, getAsString will throw an exception and the program may crash.

import org.terasology.persistence.typeHandling.RegisterTypeHandler;
import org.terasology.persistence.typeHandling.SerializationContext;
import org.terasology.persistence.typeHandling.SimpleTypeHandler;
import org.terasology.persistence.typeHandling.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this star import. You can also change your IntelliJ glob import setting to a very high number to prevent IntelliJ from doing this automatically :)

}

@Override
public NewDialogAction deserialize(PersistedData data, DeserializationContext context) {
public Optional<NewDialogAction> deserialize(PersistedData data) {
PersistedDataMap root = data.getAsValueMap();
String target = root.get("target").getAsString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above -- change deserialization code to use the format in which this type of value is serialized, and use failing conditions to prevent exceptions.

Copy link
Member

@eviltak eviltak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you for your work!

@eviltak
Copy link
Member

eviltak commented Nov 8, 2018

Also @MKDEVELOPEMENT in the future please do not merge master manually into a PR branch -- this is not needed and all conflicts can be resolved when the PR is merged.

@MKDEVELOPEMENT
Copy link
Author

Noted, @eviltak

@Cervator
Copy link
Member

Closing in favor of #11 - thanks again for the work and inspiration during code review :-)

@Cervator Cervator closed this Aug 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants