Skip to content

Commit

Permalink
fix: fixed serialization of strings twice (#390)
Browse files Browse the repository at this point in the history
* fix: fixed serialization of strings twice

* test: added unit tests for string conversion to map and list

* chore: minor improvements

* chore: added comments about the new public functions in Utils

* chore: minor improvements
  • Loading branch information
desusai7 authored Feb 7, 2024
1 parent a792ce2 commit 4bc1d7d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,28 @@ public static String getDeviceId(Application application) {
return null;
}

@Nullable
private static String serializeObject(Object obj) {
String json;
if (obj instanceof String) {
json = (String) obj;
} else {
json = RudderGson.serialize(obj);
}
return json;
}

/**
* Convert an object to a map
* <p> Serialize the object to a json string and then convert it to a map </p>
* <p> If the object is a string, it is directly converted to a map </p>
* <p> If the object results in an invalid json string after serialization, an empty map is returned </p>
*
* @param obj the object to convert
* @return the map representation of the object
*/
public static Map<String, Object> convertToMap(Object obj) {
String json = RudderGson.serialize(obj);
String json = serializeObject(obj);
if (json == null) {
return new HashMap<>();
}
Expand All @@ -106,9 +126,18 @@ public static Map<String, Object> convertToMap(Object obj) {
return map == null ? new HashMap<>() : map;
}


/**
* Convert an object to a list
* <p> Serialize the object to a json string and then convert it to a list </p>
* <p> If the object is a string, it is directly converted to a list </p>
* <p> If the object results in an invalid json string after serialization, an empty list is returned </p>
*
* @param obj the object to convert
* @return the list representation of the object
*/
public static List<Map<String, Object>> convertToList(Object obj) {
String json = RudderGson.serialize(obj);
String json = serializeObject(obj);

if (json == null) {
return new ArrayList<>();
}
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/java/com/rudderstack/android/sdk/core/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.rudderstack.android.sdk.core;

import static org.hamcrest.MatcherAssert.assertThat;

import com.rudderstack.android.sdk.core.util.Utils;

import org.junit.Test;

import java.util.List;
import java.util.Map;

public class UtilsTest {
@Test
public void testStringConversiontToMap() {
String rudderTraits = "{\"dirty\":{\"general\":45.0,\"minValue\":4.9E-324,\"maxValue\":1.7976931348623157E308,\"double\":45.0,\"positiveInfinity\":\"Infinity\",\"nan\":\"NaN\",\"float\":45.0,\"list\":[\"Infinity\",\"-Infinity\",1.7976931348623157E308,4.9E-324,\"NaN\",45.0,45.0],\"map\":{\"general\":45.0,\"minValue\":4.9E-324,\"maxValue\":1.7976931348623157E308,\"double\":45.0,\"positiveInfinity\":\"Infinity\",\"nan\":\"NaN\",\"negativeInfinity\":\"-Infinity\"},\"long\":45.0,\"int\":45.0,\"negativeInfinity\":\"-Infinity\"},\"anonymousId\":\"c5ee2cf1-97a3-4744-80fc-faabce7a7e51\",\"name\":\"Mr. User1\",\"id\":\"new user 2\",\"userId\":\"new user 2\",\"email\":\"[email protected]\"}";
Map<String,Object> map = Utils.convertToMap(rudderTraits);
assertThat("Map should not be empty", map.size() > 0);
}

@Test
public void testStringConversionToArray() {
String externalIds = "[{\"id\":\"idValue\",\"type\":\"idTYpe\"}]";
List<Map<String, Object>> list = Utils.convertToList(externalIds);
assertThat("List should not be empty", list.size() > 0);
}
}

0 comments on commit 4bc1d7d

Please sign in to comment.