Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed serialization of strings twice #390

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,28 @@ public static String getDeviceId(Application application) {
return null;
}

desusai7 marked this conversation as resolved.
Show resolved Hide resolved
@Nullable
private static String serializeObject(Object obj) {
String json;
if (obj instanceof String) {
json = (String) obj;
} else {
json = RudderGson.serialize(obj);
}
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
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>
desusai7 marked this conversation as resolved.
Show resolved Hide resolved
*
* @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
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);
}
}
Loading