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

deserialize an object using a facade pattern #121

Open
ellsworthrw opened this issue Dec 1, 2015 · 1 comment
Open

deserialize an object using a facade pattern #121

ellsworthrw opened this issue Dec 1, 2015 · 1 comment

Comments

@ellsworthrw
Copy link

Ran into a wall trying to write a converter for a class using a facade pattern. Here are my classes:
@JsonObject
class Ribbon {
@JsonField
List tiles
}

class TileAsset {
private Metadata metadata
public TileAsset(Metadata metadata) {
mMetadata = metadata;
}
}

@JsonObject
class Metadata {
}

I am trying to write a type converter but have not found a way to implement it. This is an attempt:

public static class TileAssetConverter implements TypeConverter {
@OverRide
public TileAsset parse(JsonParser jsonParser) throws IOException {
Mlog.d(TAG, "parse(%s)", jsonParser.getInputSource());
if (jsonParser.getInputSource() instanceof InputStream) {
// PROBLEM: jsonParser.getInputSource() is always null: need a way to get the input stream
// Next problem: is that jackson's parser needs to advance past this object when done
Metadata metadata = LoganSquare.parse((InputStream) jsonParser.getInputSource(), Metadata.class);
return new TileAsset(metadata);
}
return null;
}

    @Override
    public void serialize(TileAsset object, String fieldName, boolean writeFieldNameForObject, JsonGenerator jsonGenerator) throws IOException {
        if (fieldName != null) {
            jsonGenerator.writeStringField(fieldName, Utils.toJSON(object.mMetadata));
        } else {
            jsonGenerator.writeString(Utils.toJSON(object.mMetadata));
        }
    }
}
@ellsworthrw
Copy link
Author

The following works but it is a hack and is brittle:

public static class TileAssetConverter implements TypeConverter<TileAsset> {
    @Override
    public TileAsset parse(JsonParser jsonParser) throws IOException {
        Metadata metadata = JsonMapperLoaderImpl.COM_MOVENETWORKS_MODEL_METADATA__JSONOBJECTMAPPER.parse(jsonParser);
        return new TileAsset(metadata);
    }

}

There has to be a more safe way of doing this.

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

No branches or pull requests

1 participant