14
14
*******************************************************************************/
15
15
package com .alvarium ;
16
16
17
- import java .io .Serializable ;
18
- import java .util .Base64 ;
17
+ import com .alvarium .serializers .AlvariumPersistence ;
19
18
20
- import com .alvarium .contracts .Annotation ;
21
- import com .alvarium .serializers .AnnotationConverter ;
22
- import com .google .gson .Gson ;
23
- import com .google .gson .GsonBuilder ;
24
- import com .google .gson .JsonElement ;
19
+ import java .io .Serializable ;
25
20
26
21
/**
27
22
* A java bean that encapsulates the content sent through the stream providers
@@ -32,6 +27,7 @@ public class PublishWrapper implements Serializable {
32
27
private final String messageType ;
33
28
private final Object content ;
34
29
30
+
35
31
public PublishWrapper (SdkAction action , String messageType , Object content ) {
36
32
this .action = action ;
37
33
this .messageType = messageType ;
@@ -50,33 +46,12 @@ public Object getContent() {
50
46
return content ;
51
47
}
52
48
49
+
53
50
/**
54
- * The content field in the returned JSON will be Base64 string encoded
51
+ * The content field in the returned JSON will be Base64 string encoded
55
52
* @return String representation of the PublishWrapper JSON
56
53
*/
57
54
public String toJson () {
58
- Gson gson = new GsonBuilder ()
59
- .registerTypeAdapter (Annotation .class , new AnnotationConverter ())
60
- .disableHtmlEscaping ()
61
- .create ();
62
-
63
- // Change the content field to a base64 encoded string before serializing to json
64
- final JsonElement decodedContent = gson .toJsonTree (this .content );
65
- final String encodedContent ;
66
-
67
- // `toString()` will work if the content is a primitive type, but will add additional
68
- // quotes (e.g. "foo" will be "\"foo\"") but `getAsString()` will produce correct behavior but
69
- // using `getAsString()` on a non-primitive type will throw an exception.
70
- // This condition ensures that the correct method is called on the correct type
71
- if (decodedContent .isJsonPrimitive ()) {
72
- encodedContent = Base64 .getEncoder ().encodeToString (decodedContent .getAsString ().getBytes ());
73
- } else {
74
- encodedContent = Base64 .getEncoder ().encodeToString (decodedContent .toString ().getBytes ());
75
- }
76
-
77
- // new publish wrapper returned as JSON string with encoded content
78
- // to prevent setting the object content value
79
- final PublishWrapper wrapper = new PublishWrapper (action , messageType , encodedContent );
80
- return gson .toJson (wrapper );
55
+ return AlvariumPersistence .GSON .toJson (this );
81
56
}
82
57
}
0 commit comments