Johan Kero
Johan Kero

Reputation: 11

Realm and Expected BEGIN_OBJECT but was NUMBER at path $[0].location.coordinates[0]

I'm trying to store a coordnates (array of double) using Realm-java,but I'm not able to do it.

Here is an example of json that I'm trying to parse:

{"_id":"597cd98b3af0b6315576d717",
"comarca":"string",
"font":null,
"imatge":"string",
"location":{
           "coordinates":[41.64642,1.1393],
           "type":"Point"
},
"marca":"string",
"municipi":"string",
"publisher":"string",
"recursurl":"string",
"tematica":"string",
"titol":"string"
}

My global object code is like that

public class Images  extends RealmObject implements Serializable {
@PrimaryKey
private String _id;
private String recursurl;
private String titol;
private String municipi;
private String comarca;
private String marca;
private String imatge;
@Nullable
private Location location;
private String tematica;
private String font;
private String parentRoute;

public Location getLocation() {return location;}

public void setLocation(Location location) {this.location = location;}

public String getParentRoute() {
    return parentRoute;
}

public void setParentRoute(String parentRoute) {
    this.parentRoute = parentRoute;
}

public String get_id() {
    return _id;
}

public void set_id(String _id) {
    this._id = _id;
}

public String getFont() {
    return font;
}

public void setFont(String font) {
    this.font = font;
}

public String getRecursurl() {
    return recursurl;
}

public void setRecursurl(String recursurl) {
    this.recursurl = recursurl;
}

public String getTitol() {
    return titol;
}

public void setTitol(String titol) {
    this.titol = titol;
}

public String getMunicipi() {
    return municipi;
}

public void setMunicipi(String municipi) {
    this.municipi = municipi;
}

public String getComarca() {
    return comarca;
}

public void setComarca(String comarca) {
    this.comarca = comarca;
}

public String getMarca() {
    return marca;
}

public void setMarca(String marca) {
    this.marca = marca;
}

public String getImatge() {
    return imatge;
}

public void setImatge(String imatge) {
    this.imatge = imatge;
}


public String getTematica() {
    return tematica;
}

public void setTematica(String tematica) {
    this.tematica = tematica;
}

And Location is a composite of type and a realmlist

Location.java

public class Location  extends RealmObject implements Serializable {
private String type;
private RealmList<RealmDoubleObject> coordinates;


public Location() {
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public RealmList<RealmDoubleObject> getCoordinates() {
    return coordinates;
}

public void setCoordinates(RealmList<RealmDoubleObject> coordinates) {
    this.coordinates = coordinates;
}

}

RealmDoubleObject.java

public class RealmDoubleObject extends RealmObject implements Serializable{
private Double value;

public RealmDoubleObject() {
}

public Double getDoublevalue() {
    return value;
}

public void setDoublevalue(Double value) {
    this.value = value;
}

}

The error is com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at path $[0].location.coordinates[0] but I'm not able to figure out why this number is not "fitting" by RealmDoubleObject.

For those that not familiar with realm RealmList doesn't work and you have to build your own realm object.

Thank you. I hope to find some Realm experts here!

Upvotes: 0

Views: 691

Answers (1)

Johan Kero
Johan Kero

Reputation: 11

SOLVED: using Gson deserializer it can be done

First we have to initialize the gson object like this

Gson gson = new GsonBuilder()
            .setExclusionStrategies(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(FieldAttributes f) {
                    return f.getDeclaringClass().equals(RealmObject.class);
                }

                @Override
                public boolean shouldSkipClass(Class<?> clazz) {
                    return false;
                }
            })
            .registerTypeAdapter(new TypeToken<RealmList<RealmDoubleObject>>() {}.getType(), new TypeAdapter<RealmList<RealmDoubleObject>>() {

                @Override
                public void write(JsonWriter out, RealmList<RealmDoubleObject> value) throws IOException {
                    // Ignore
                }

                @Override
                public RealmList<RealmDoubleObject> read(JsonReader in) throws IOException {
                    RealmList<RealmDoubleObject> list = new RealmList<RealmDoubleObject>();
                    in.beginArray();
                    while (in.hasNext()) {
                        Double valor = in.nextDouble();
                        list.add(new RealmDoubleObject(valor));
                    }
                    in.endArray();
                    return list;
                }
            })
            .create();

And then we have to put some other constructor method

public RealmDoubleObject(double v) {
    this.value = v;
}

and this is all.

Thanks for the help @EpicPandaForce

Upvotes: 1

Related Questions