Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonMappingException: No suitable constructor found

I am implementing a firebase example as given in their documentations. I am facing this error:

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.XYZ.$BlogPost]: can not instantiate from JSON object (need to add/enable type information?)

Here is my code:

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        Firebase.setAndroidContext(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Get a reference to our posts
        Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
        // Attach an listener to read the data at our posts reference
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");
                for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                    BlogPost post = postSnapshot.getValue(BlogPost.class);
                    System.out.println(post.getAuthor() + " - " + post.getTitle());
                }
            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });
    }
public class BlogPost {
        private String author;
        private String title;
        public BlogPost() {
            // empty default constructor, necessary for Firebase to be able to deserialize blog posts
        }
        public String getAuthor() {
            return author;
        }
        public String getTitle() {
            return title;
        }
    }
}

I have gone through many questions on the same thing saying to include empty constructor necessary to deserialize the JSON. I have included that but still I am not able to resolve the issue. This is the JSON I am trying to deserialize:

{  
   "-JRHTHaIs-jNPLXOQivY":{  
      "author":"gracehop",
      "title":"Announcing COBOL, a New Programming Language"
   },
   "-JRHTHaKuITFIhnj02kE":{  
      "author":"alanisawesome",
      "title":"The Turing Machine"
   }
}

I don't know what I am missing in my code. Any help regrading this is appreciated.

like image 413
brainbreaker Avatar asked Dec 24 '22 11:12

brainbreaker


1 Answers

I'm guessing your BlogPost class is an inner class of your activity. In that case Java adds a hidden field to each object that refers to the containing object. And this implicit field is of course not present in your JSON data.

To solve this, you should either keep the BlogPost class in a separate file (so that it's not an inner class) or mark it as a static class (which removes the implicit field):

public static class BlogPost {
like image 187
Frank van Puffelen Avatar answered Dec 27 '22 10:12

Frank van Puffelen