gleezr / Slink

Encrypted shared preferences for android
6 stars 1 forks source link

issues with google gson #1

Closed fouadkada closed 7 years ago

fouadkada commented 7 years ago

Hey,

I am using Slink to persist some sensitive data to sharedpreferences. im persisting the json text from the Gson library. when i fetch the persisted json i get an error.

below is how i am persisting the data

preferences
                .edit()
                .putString("USER", new Gson().toJson(user))
                .commit();

below is how i am reading the data

  String userJson = preferences.getString("USER", null);
        if (userJson != null && !userJson.isEmpty()) {
            return new Gson().fromJson(userJson, User.class);
        }
        return null;

and below is the stacktrace i get when i read from shared preferences:

E/AndroidRuntime: FATAL EXCEPTION: SharedPreferencesImpl-load
                  Process: com.example.exampleapp, PID: 13347
                  com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1191 path $
                      at com.google.gson.Gson.assertFullConsumption(Gson.java:863)
                      at com.google.gson.Gson.fromJson(Gson.java:853)
                      at com.google.gson.Gson.fromJson(Gson.java:801)
                      at com.gleezr.slink.Slink.loadFromDisk(Slink.java:186)
                      at com.gleezr.slink.Slink.access$000(Slink.java:62)
                      at com.gleezr.slink.Slink$1.run(Slink.java:122)
                   Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1191 path $
                      at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1559)
                      at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1401)
                      at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:542)
                      at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
                      at com.google.gson.Gson.assertFullConsumption(Gson.java:859)
                      at com.google.gson.Gson.fromJson(Gson.java:853) 
                      at com.google.gson.Gson.fromJson(Gson.java:801) 
                      at com.gleezr.slink.Slink.loadFromDisk(Slink.java:186) 
                      at com.gleezr.slink.Slink.access$000(Slink.java:62) 
                      at com.gleezr.slink.Slink$1.run(Slink.java:122) 

any idea how to fix this?

yanivgal commented 7 years ago

Sorry for the delay, the issue you're experiencing is a known bug. Please use the newer version (1.0.2).

Yaniv.

fouadkada commented 7 years ago

@yanivgal The error above seems to be fixed with version 1.0.2. However there seems to be a new problem now.

when you persist a String using Slink, like the following:

securePreferences
                    .edit()
                    .putString("VERIFICATION_CODE", verificationCode)
                    .commit();

on app restart the previously persisted key is not available; for example the following returns null

String verificationCode = securePreferences
                .getString("VERIFICATION_CODE", null);
        if (verificationCode != null && !verificationCode.isEmpty()) {
            return verificationCode;
        }
        return null;
yanivgal commented 7 years ago

Yeah, sorry about that. Please check v1.0.3.

fouadkada commented 7 years ago

works as expected. Thanks