Skip to content

Support array types with Converter #42

@FabianTerhorst

Description

@FabianTerhorst

Im getting the following error when im query the entity that is using an converter:

 io.objectbox.exception.DbException: Entity is expected to have this constructor: Group(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;ZLjava/lang/String;ZZZZL[J;)V

The constructor:

public Group(long id, String ..., String ..., String ..., boolean ..., String ...,
            boolean ..., boolean ..., boolean ..., boolean ..., long[] deviceIds)

The Entity converter usage:

@Convert(converter = DeviceIdConverter.class, dbType = String.class)
private long[] deviceIds;

The Converter Class:

public class DeviceIdConverter implements PropertyConverter<long[], String> {

    private static final ByteString SEMICOLON = ByteString.decodeBase64(";");

    @Override
    public long[] convertToEntityProperty(String s) {
        if (s == null) {
            return new long[0];
        }
        try {
            long[] ids = new long[0];
            Buffer buffer = new Buffer();
            buffer.writeUtf8(s);
            long index;
            while ((index = buffer.indexOf(SEMICOLON)) != -1) {
                ids = add(Long.parseLong(buffer.readUtf8(index - 1)), ids);
            }
            return ids;
        } catch (IOException io) {
            io.printStackTrace();
            return new long[0];
        }
    }

    public long[] add(long id, long[] values) {
        long[] anotherArray = new long[values.length + 1];
        System.arraycopy(values, 0, anotherArray, 0, values.length);
        anotherArray[values.length] = id;
        return anotherArray;
    }

    @Override
    public String convertToDatabaseValue(long[] integers) {
        Buffer buffer = new Buffer();
        for (long value : integers) {
            buffer.write(SEMICOLON);
            buffer.writeUtf8(String.valueOf(value));
        }
        return buffer.toString();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions