0

I work with Firebase for my app and I've got one error. Json which I take from Firebase usual looks like this:

    "post": {
    "title": "title",
    "date": 1459423087916,
    "comments": [
      {
        "text": "sometext"
      },
      {
        "text": "sometext"
      }
    ]
  }

but sometimes it looks like this:

    "post": {
    "title": "title",
    "date": 1459423087916
  }

And I always take this error:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

Is there any way to handle it?

My Object model class:

public class Post {

    private String title;

    private long date;

    private List<Comment> comments;

    // constructor, getters and setters
}
alex
  • 23
  • 6
  • Put a `@JsonIgnoreProperties(ignoreUnknown=true)` at the top of your class. See http://stackoverflow.com/questions/32108969/why-do-i-get-failed-to-bounce-to-type-when-i-turn-json-from-firebase-into-java – Frank van Puffelen Mar 31 '16 at 13:41
  • Unfortunately, when I try to get Post without comments I still take this error( – alex Mar 31 '16 at 13:52
  • I can't reproduce. See my "answer" below (it's way too much code to put in a comment). – Frank van Puffelen Mar 31 '16 at 15:54

2 Answers2

1

I can't reproduce the problem with this simple approach:

public static class Comment {
    public String text;
    public String toString() { return text; }
}
public static class Post {
    private String title;
    private long date;
    private List<Comment> comments;

    public String getTitle() { return title; }
    public long getDate() { return date; }
    public List<Comment> getComments() { return comments; }
    public String toString() {
        return "title="+title+" date="+date+" comments="+(comments!=null?comments.toString():"<null>");
    }
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final TextView view = new TextView(this);
    setContentView(view);

    Firebase.setAndroidContext(this);
    Firebase ref = new Firebase("https://stackoverflow.firebaseio.com/36332909");

    ref.addChildEventListener(new ChildEventListener() {
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            view.append(dataSnapshot.getValue(Post.class).toString()+"\n");
        }

        public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
        public void onChildRemoved(DataSnapshot dataSnapshot) { }
        public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
        public void onCancelled(FirebaseError firebaseError) { }
    });
}

This gives me this output:

Activity on Android that shows the loaded items

I also added it to my Android app here: https://github.com/puf/firebase-stackoverflow-android. See https://github.com/puf/firebase-stackoverflow-android/blob/master/app/src/main/java/com/firebasedemo/stackoverflow/Activity36332909.java for the full code of the activity.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I don't parse JSON from String. I use Firebase. Look here: https://www.firebase.com/docs/android/guide/retrieving-data.html#section-types – alex Mar 31 '16 at 18:31
  • *I know that section of the documentation reasonable well, given that I wrote it. ;-)* Look at the last link in my answer, it provides a repo where I read the exact same JSON from the database. I don't get the same error you get and the data shows up in my activity – Frank van Puffelen Mar 31 '16 at 19:07
  • Thanks for help :) – alex Mar 31 '16 at 21:03
  • What was the problem? – Frank van Puffelen Mar 31 '16 at 21:15
  • I don't know. Really. I tried your example with my information and it works. Maybe somewhere in code was a mistake. Anyway, thank you – alex Apr 01 '16 at 14:16
0

Your json objects are invalid, they don't have outer curly braces {"post": { }} But i will just suppose that you are showing us only some part of your json. The default Jackson behavior is to set private List<Comment> comments; to null if there is no such field in json string. Here is demo:

public class Main5 {
private static final String jsonA = " {\n" +
        "    \"title\": \"title\",\n" +
        "    \"date\": 1459423087916,\n" +
        "    \"comments\": [\n" +
        "      {\n" +
        "        \"text\": \"sometext\"\n" +
        "      },\n" +
        "      {\n" +
        "        \"text\": \"sometext\"\n" +
        "      }\n" +
        "    ]\n" +
        "  }";

private static final String jsonB = " {\n" +
        "    \"title\": \"title\",\n" +
        "    \"date\": 1459423087916\n" +
        "  }";


public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    Post tl = mapper.readValue(jsonA, Post.class);
    System.out.println(tl);

    Post t2 = mapper.readValue(jsonB, Post.class);
    System.out.println(t2);
}

public static class Post {
    private String title;
    private long date;
    private List<Comment> comments;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public long getDate() {
        return date;
    }

    public void setDate(long date) {
        this.date = date;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    @Override
    public String toString() {
        return "Post{" +
                "title='" + title + '\'' +
                ", date=" + date +
                ", comments=" + comments +
                '}';
    }

}
public static class Comment {
    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "text='" + text + '\'' +
                '}';
    }
}
}

So i think you have to fix your json, or you have annotations that prevent default behavior.

zafrost
  • 576
  • 3
  • 3
  • Yeah, you are right - this JSON just for example. I tried to use annotations, but it didn't help me. Any way I have solved this problem: I just change structure of my JSON in Firebase. Now "comments" is other reference. But this problem is still actual and unfortunately your solution can't help me because I use firebase and don't parse my JSON like a string. I use something like this: https://www.firebase.com/docs/android/guide/retrieving-data.html#section-types – alex Mar 31 '16 at 15:07