Skip to content

Commit

Permalink
Fix android native GSON parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Aug 28, 2024
1 parent 7c605c5 commit 351d57b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
6 changes: 6 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ android {
defaultConfig {
minSdkVersion 19
}

buildTypes {
release {
consumerProguardFiles 'proguard-rules.pro'
}
}
}

dependencies {
Expand Down
24 changes: 24 additions & 0 deletions android/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Keep all classes in your plugin's package
-keep class com.gdelataillade.alarm.** { *; }

# Keep all classes related to Gson and prevent them from being obfuscated
-keep class com.google.gson.** { *; }
-keep class sun.misc.Unsafe { *; }
-keepattributes Signature
-keepattributes *Annotation*

# Prevent stripping of methods/fields annotated with specific annotations, if needed.
-keepclassmembers class * {
@com.google.gson.annotations.SerializedName <fields>;
}

# Preserve classes that might be used in reflection or through indirect means
-keepclassmembers class **.R$* {
<fields>;
}

# Avoid stripping enums, if your plugin uses them
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,35 @@ data class AlarmSettings(
) {
companion object {
fun fromJson(json: Map<String, Any>): AlarmSettings? {
try {
val modifiedJson = json.toMutableMap()
return try {
val gson = Gson()

// Convert dateTime from microseconds to Date
val modifiedJson = json.toMutableMap()
val dateTimeMicroseconds = modifiedJson["dateTime"] as? Long
if (dateTimeMicroseconds == null) {
if (dateTimeMicroseconds != null) {
val dateTimeMilliseconds = dateTimeMicroseconds / 1000
modifiedJson["dateTime"] = Date(dateTimeMilliseconds)
} else {
Log.e("AlarmSettings", "dateTime is missing or not a Long")
return null
}
val dateTimeMilliseconds = dateTimeMicroseconds / 1000
val date = Date(dateTimeMilliseconds.toLong())
modifiedJson["dateTime"] = date

// Convert notificationActionSettings from Map to NotificationActionSettings object

// Deserialize NotificationActionSettings
val notificationActionSettingsMap = modifiedJson["notificationActionSettings"] as? Map<String, Any>
if (notificationActionSettingsMap == null) {
if (notificationActionSettingsMap != null) {
modifiedJson["notificationActionSettings"] = NotificationActionSettings.fromJson(notificationActionSettingsMap)
} else {
Log.e("AlarmSettings", "notificationActionSettings is missing or not a Map")
return null
}
val notificationActionSettings = NotificationActionSettings.fromJson(notificationActionSettingsMap)
if (notificationActionSettings == null) {
Log.e("AlarmSettings", "Failed to parse notificationActionSettings")
return null
}
modifiedJson["notificationActionSettings"] = notificationActionSettings


// Convert the modified map to JSON string and deserialize it to an AlarmSettings object
val gson = Gson()
val jsonString = gson.toJson(modifiedJson)
val result = gson.fromJson(jsonString, AlarmSettings::class.java)
if (result == null) {
Log.e("AlarmSettings", "Failed to deserialize JSON to AlarmSettings")
}
return result
gson.fromJson(jsonString, AlarmSettings::class.java)
} catch (e: Exception) {
Log.e("AlarmSettings", "Error parsing JSON to AlarmSettings: ${e.message}")
return null
null
}
}
}
Expand Down

0 comments on commit 351d57b

Please sign in to comment.