0

I am testing to save current QBUser object into shared preferences through Gson, meaning I would like to save all QBUser fields locally for later use.

I noticed that some fields are defined as serializable and some not. Is there a reason for this? For background information, I am using exclusion pattern that only takes fields with SerializedName annotation defined and using it with my other fields in the class.

See print out:

// Compiled from QBUser.java (version 1.6 : 50.0, super bit)
public class com.quickblox.module.users.model.QBUser extends com.quickblox.core.model.QBEntity {

  // Field descriptor #63 Ljava/lang/String;
  @com.google.gson.annotations.SerializedName(value="full_name")
  protected java.lang.String fullName;

  // Field descriptor #63 Ljava/lang/String;
  protected java.lang.String email;

  // Field descriptor #63 Ljava/lang/String;
  protected java.lang.String login;

  // Field descriptor #63 Ljava/lang/String;
  protected java.lang.String phone;

  // Field descriptor #63 Ljava/lang/String;
  protected java.lang.String website;

  // Field descriptor #73 Ljava/util/Date;
  @com.google.gson.annotations.SerializedName(value="last_request_at")
  protected java.util.Date lastRequestAt;

  // Field descriptor #63 Ljava/lang/String;
  @com.google.gson.annotations.SerializedName(value="external_user_id")
  protected java.lang.String externalId;

  // Field descriptor #63 Ljava/lang/String;
  @com.google.gson.annotations.SerializedName(value="facebook_id")
  protected java.lang.String facebookId;

  // Field descriptor #63 Ljava/lang/String;
  @com.google.gson.annotations.SerializedName(value="twitter_id")
  protected java.lang.String twitterId;

  // Field descriptor #82 Ljava/lang/Integer;
  @com.google.gson.annotations.SerializedName(value="blob_id")
  protected java.lang.Integer blobId;

  // Field descriptor #63 Ljava/lang/String;
  @com.google.gson.annotations.SerializedName(value="user_tags")
  protected java.lang.String tags;

  // Field descriptor #63 Ljava/lang/String;
  protected java.lang.String password;

  // Field descriptor #63 Ljava/lang/String;
  protected java.lang.String oldPassword;

So we can see that for instance full_name can be saved fine with Gson but for instance login and email_address are not.

In comparison to QB iOS SDK, the QBUser class is implementing NSCoding into full extent which allows me to nicely store current user object to NSUserDefaults which is kind of the same as Android's SharedPreferences.

If this is not the correct way to store the user locally to the device, all tips are appreciated! :)

Matti Vilola
  • 185
  • 7
  • Why do think user's fields are not serializable? What version of qb library are you using? QbUser implements Serializable interface. SerializedName annotations are used by Gson library to retrieve fields from json string. Please add snippet of code where you use serializing qb user. – vfite Mar 13 '14 at 14:06
  • Quick response, I need to fetch code example and some logs for further details. Well, I am using latest Android SDK in master branch. I try to use QBUser through GSON but basically it fails to include all data and especially fails when trying to de-serialize it in "sdf" entry which means SimpleDateFormat. I mean, if using GSON without any parameters/exclusions. Then, I tried with "SerializedName" annotation to exclude some fields I don't want to store (like I do with my custom classes) and I thought this would work with QBUser class also (see above), but no luck.. Does this work for you then? – Matti Vilola Mar 13 '14 at 21:20
  • 1
    So.By default Gson means that field name in Java class match with json field, like field name "email" in json string presents as "email". If field in class doesn't match with field in Json than you should insert SerializedName annotation with corresponding field in Json. See Gson documentation https://sites.google.com/site/gson/gson-user-guide. – vfite Mar 17 '14 at 15:31
  • Thanks for the comment @vfite. The problem, which is now bypassed, was that I used it as an exclusion criteria in purpose for other classes. For QBUser being "not working" was actually me not specifying the time format for date fields (updatedAt + createdAt). Either excluding these or specifying the format fixes the issue I encountered. My confusion :( – Matti Vilola Mar 18 '14 at 08:20

0 Answers0