Deserializing List of Objects with Mirah for Codename One

95 Views Asked by At

I am trying to use mirah for JSON to POJO mapping in an codenameone application. It works finde when i want to map a Simple JSON like
{"id":"1","name":"foo","classification":"10"}
With this class:

public class Brand {
    private String id; 
    private String name;
    private String classification;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getClassification() {
        return classification;
    }

    public void setClassification(String classification) {
        this.classification = classification;
    }    
}

Now I want to wrap it in a Message Object, where i have a list of brands:

import java.util.List;

public class Message { 
    public List<Brand> brands;

    public List<Brand> getBrands() {
        return brands;
    }

    public void setBrands(List<Brand> brands) {
        this.brands = brands;
    }        
}

I use this Mirah Script for mapping:

data_mapper Message:MessageMapper
data_mapper Brand:BrandMapper

like shannah described here.

My code where I call my Webservice:

    MessageMapper scheduleMapper = new MessageMapper();
    DataMapper.createContext(Arrays.asList(
            scheduleMapper,
            new BrandMapper()
        ), new DataMapper.Decorator() {

        public void decorate(DataMapper mapper) {
            mapper.setReadKeyConversions(Arrays.asList(DataMapper.CONVERSION_NONE));
        }
    });



    try {
    Message  message = scheduleMapper.readJSONFromURL("http://localhost/php-REST-DigitaleMusterplatte/api.php/brands", Message.class);
        System.out.println(message);
    } catch (IOException ex) {
        Log.e(ex);
    }

This is the json response:
{"brands":[{"id":"1","name":"foo","classification":"10"},{"id":"2","name":"bar","classification":"20"}]}

I get this exception:

java.lang.RuntimeException: Failed to get key brands for class interface java.util.List because it was not a registered object type
at ca.weblite.codename1.mapper.DataMapper.get(DataMapper.java:507)
at com.mycompany.app.dmp.models.MessageMapper.readMap(/Volumes/Windows VMS/Documents/Shared/NetBeansProjects/mirah_macro_utils/MirahMacroUtils/src/ca/weblite/mirah/utils/DataMapperBuilder.mirah)
at ca.weblite.codename1.mapper.DataMapper.readMap(DataMapper.java:719)
at ca.weblite.codename1.mapper.DataMapper.readJSON(DataMapper.java:780)
at ca.weblite.codename1.mapper.DataMapper.readJSON(DataMapper.java:792)
at ca.weblite.codename1.mapper.DataMapper.readJSONFromConnection(DataMapper.java:767)
at ca.weblite.codename1.mapper.DataMapper.readJSONFromURL(DataMapper.java:754)
at com.mycompany.myapp.MyApplication.start(MyApplication.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.codename1.impl.javase.Executor$1$1.run(Executor.java:123)
at com.codename1.ui.Display.processSerialCalls(Display.java:1152)
at com.codename1.ui.Display.mainEDTLoop(Display.java:969)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

The demo application OSCONScheduler works fine.

1

There are 1 best solutions below

0
On BEST ANSWER

This looks like a bug. But try changing brands to be private instead of public. It could be getting confused over whether to use your accessor/mutable or to use the public var.