bluelinelabs / LoganSquare

Screaming fast JSON parsing and serialization library for Android.
Apache License 2.0
3.21k stars 306 forks source link

Unable to create converter for class #170

Open galacticappster04 opened 8 years ago

galacticappster04 commented 8 years ago

I did everything, I followed everything. Here is my class, I am getting error and crash when I run my app:

package com.taskout.taskout.http.model.filteroptions;

import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;

@JsonObject(fieldDetectionPolicy = JsonObject.FieldDetectionPolicy.ANNOTATIONS_ONLY)
public class Session
{
    @JsonField private String login;
    @JsonField private String password;
    @JsonField private String apikey;

    public void setApiKey(String apiKey)
    {
        this.apikey = apiKey;
    }

    public void setLogin(String login)
    {
        this.login = login;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getLogin()
    {
        return login;
    }

    public String getPassword()
    {
        return password;
    }

    public String getAPIKey()
    {
        return apikey;
    }

    public Session(String username, String pw)
    {
        login = username;
        password = pw;
    }
}

I do not understand that is suppose to be simple and I almost pulled my hair out just to figure out whats wrong. Can't figure out what I missed either.

mannodermaus commented 8 years ago

Add a package-private empty constructor to your class. The generated code instantiates your object with new Session(), and then configures its fields one after the other:

@JsonObject(fieldDetectionPolicy = JsonObject.FieldDetectionPolicy.ANNOTATIONS_ONLY)
public class Session {
    // Everything else...

    Session() {
        // Used for LoganSquare deserialization
    }
}

If you don't add an empty constructor, you will be presented with this error:

Error:(16, 24) error: constructor Session in class Session cannot be applied to given types;
required: String,String
found: no arguments
reason: actual and formal argument lists differ in length

@EricKuck This error could be prevented by either adding a Lint rule that checks for empty constructors in classes annotated with @JsonObject, or having the annotation processor check for its presence, and raising a more convenient error if absent. (Possibly both of these should be implemented together!)