42
42
import org .eclipse .ditto .protocol .JsonifiableAdaptable ;
43
43
import org .eclipse .ditto .protocol .ProtocolFactory ;
44
44
import org .mozilla .javascript .Callable ;
45
+ import org .mozilla .javascript .ConsString ;
45
46
import org .mozilla .javascript .Context ;
46
47
import org .mozilla .javascript .ContextFactory ;
47
48
import org .mozilla .javascript .NativeArray ;
51
52
import org .mozilla .javascript .Scriptable ;
52
53
import org .mozilla .javascript .Undefined ;
53
54
import org .mozilla .javascript .typedarrays .NativeArrayBuffer ;
55
+ import org .slf4j .Logger ;
56
+ import org .slf4j .LoggerFactory ;
54
57
55
58
/**
56
59
* Mapping function for outgoing messages based on JavaScript.
@@ -63,6 +66,7 @@ public final class ScriptedOutgoingMapping implements MappingFunction<Adaptable,
63
66
private static final String EXTERNAL_MESSAGE_BYTE_PAYLOAD = "bytePayload" ;
64
67
65
68
private static final String OUTGOING_FUNCTION_NAME = "mapFromDittoProtocolMsgWrapper" ;
69
+ private static final Logger log = LoggerFactory .getLogger (ScriptedOutgoingMapping .class );
66
70
67
71
@ Nullable private final ContextFactory contextFactory ;
68
72
@ Nullable private final Scriptable scope ;
@@ -159,11 +163,24 @@ private static JsonObject toJsonObject(final NativeObject nativeObject) {
159
163
nativeObject .forEach ((key , value ) -> {
160
164
try {
161
165
if (value instanceof String ) {
162
- objectBuilder .set (key .toString (), JsonFactory .readFrom (value .toString ()));
166
+ objectBuilder .set (key .toString (), JsonFactory .newValue (value .toString ()));
167
+ } else if (value instanceof ConsString consString ) {
168
+ objectBuilder .set (key .toString (), JsonFactory .newValue (consString .toString ()));
163
169
} else if (value instanceof NativeArray nativeArray ) {
164
170
objectBuilder .set (key .toString (), toJsonArray (nativeArray ));
165
171
} else if (value instanceof NativeObject nativeSubObject ) {
166
172
objectBuilder .set (key .toString (), toJsonObject (nativeSubObject ));
173
+ } else if (value instanceof Boolean boolValue ) {
174
+ objectBuilder .set (key .toString (), JsonFactory .newValue (boolValue ));
175
+ } else if (value instanceof Integer intValue ) {
176
+ objectBuilder .set (key .toString (), JsonFactory .newValue (intValue ));
177
+ } else if (value instanceof Double doubleValue ) {
178
+ objectBuilder .set (key .toString (), JsonFactory .newValue (doubleValue ));
179
+ } else {
180
+ if (log .isDebugEnabled ()){
181
+ log .debug ("Unsupported type: {}, adding as string: {}" , value .getClass ().getName (), value );
182
+ }
183
+ objectBuilder .set (key .toString (), value .toString ());
167
184
}
168
185
} catch (final JsonParseException e ) {
169
186
objectBuilder .set (key .toString (), value .toString ());
0 commit comments