Repository: rollbar/rollbar-java
Pull request #42 (2017-06-29 00:24:33)

RollbarSerializer.serializeValue(). Default serialisation.
Hello guys!
Thanks a lot for the library, it is really helpful. 
I wanted to use exception as a custom parameter. Something like:
```
Rollbar rollbar = ...
Exception ex = ...
...................
Map<String, Object> custom = new HashMap<String, Object>();
custom.put("exception", ex)
rollbar.error("Some message", custom);
``` 
I faced the problem: my request failed because the JSON in the request was invalid.

I implemented workaround in my software, but once I already spent some time looking into your code I've decided to suggest the fix :) In my opinion, this serialiser should not produce invalid JSON. I see two ways of  how to avoid invalid JSON: 
1. Just throw an exception if something can not be serialised. In another words, force client to implement serialisation for the classes which ```com.rollbar.utilities.RollbarSerializer```can not serialise. Client always can implement ```com.rollbar.utilities.JsonSerializable``` for his classes. 
2. Implement default serialisation. So, any class will be somehow serialised. 

I liked the second option more and implemented it. The implementation is in this PR. 

Bye! 



Patch:
diff --git a/rollbar-utilities/src/main/java/com/rollbar/utilities/RollbarSerializer.java b/rollbar-utilities/src/main/java/com/rollbar/utilities/RollbarSerializer.java
index b4150ec..7a99d1f 100644
--- a/rollbar-utilities/src/main/java/com/rollbar/utilities/RollbarSerializer.java
+++ b/rollbar-utilities/src/main/java/com/rollbar/utilities/RollbarSerializer.java
@@ -82,9 +82,15 @@ public class RollbarSerializer implements JsonSerializer {
         }
         else if (value instanceof Object[]) {
             serializeArray(builder, (Object[]) value, level);
+        } else {
+            serializeDefault(builder, value);
         }
     }
 
+    private static void serializeDefault(StringBuilder builder, Object value) {
+        builder.append(String.format("\"%s\"", value));
+    }
+
     private static void serializeNumber(StringBuilder builder, Number value) {
         builder.append(value);
     }


Test patch:
diff --git a/rollbar-payload/src/test/java/com/rollbar/payload/RollbarSerializerTest.java b/rollbar-payload/src/test/java/com/rollbar/payload/RollbarSerializerTest.java
index be1945b..9f9d5cc 100644
--- a/rollbar-payload/src/test/java/com/rollbar/payload/RollbarSerializerTest.java
+++ b/rollbar-payload/src/test/java/com/rollbar/payload/RollbarSerializerTest.java
@@ -106,6 +106,25 @@ public class RollbarSerializerTest {
         }
     }
 
+    @Test
+    public void TestCustomTypeSerialize() {
+        final CustomClass custom = new CustomClass();
+        final Message msg = new Message("customParams");
+        final Body body = new Body(msg.put("someParam", custom));
+        final Data data = new Data(environment, body);
+        final Payload payload = new Payload(accessToken, data);
+        final String json = new RollbarSerializer().serialize(payload);
+        final JsonObject parsed = (JsonObject) new JsonParser().parse(json);
+        assertEquals(
+            custom.toString(),
+            parsed
+                .getAsJsonObject("data")
+                .getAsJsonObject("body")
+                .getAsJsonObject("message")
+                .getAsJsonPrimitive("someParam").getAsString()
+        );
+    }
+
     public Throwable getError() {
         try {
             throwException();
@@ -135,4 +154,15 @@ public class RollbarSerializerTest {
             throw new Exception("Wrapper Exception", e);
         }
     }
+
+    /**
+     * Class which is used for checking custom classes serialization.
+     */
+    public class CustomClass {
+
+        @Override
+        public String toString() {
+            return "custom string representation";
+        }
+    }
 }
\ No newline at end of file