Skip to content

How to use @Conversion with List fields #169

@NeusFear

Description

@NeusFear

I have a section of a TOML file like this:

[dependencies]
required = ["examplemod:1.0.0"]
optional = ["anothermod:1.0.0"]

which I wish to parse with an ObjectConverter. I've attempted the below code to do so, but I get an error to do with not being able to case ArrayList to Pair, which I suspect to be because the field is a list and the conversion is just for a single entry in that list.

See applicable section of converter code:

    public static class ModDependencies {
        //A Pair defined as <Namespace, Version>
        @Conversion(VersionToStringConverter.class)
        List<Pair<String, Version>> required;
        @Conversion(VersionToStringConverter.class)
        List<Pair<String, Version>> optional;
    }

    static class VersionToStringConverter implements Converter<String, Pair<String, Version>> {

        @Override
        public String convertToField(Pair<String, Version> stringVersionPair) {
            return stringVersionPair.getValue0() + ":" + stringVersionPair.getValue1();
        }

        @Override
        public Pair<String, Version> convertFromField(String s) {
            String[] split = s.split(":");
            return new Pair<>(split[0], Version.parse(split[1]));
        }
    }

What is the correct way to parse a list of items to an object with an ObjectConverter when the items in the list need to be converted by a Converter?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions