I am passing a string from Javascript to a React Native native Java module and then back to Javascript. However, any high Unicode characters such as emojis become corrupted after passing it to Java and turn into a pair of question marks.
For example, the string "testing123"
becomes "testing123??"
How can I fix this so that the characters retain their values?
EDIT: The string is being processed by a React Native background upload library. Here is an excerpt of the code from that library that passes the text (which is in the parameters
field) to the Java module:
import { NativeModules, DeviceEventEmitter } from 'react-native'
export type StartUploadArgs = {
url: string,
path: string,
method?: 'PUT' | 'POST',
// Optional, because raw is default
type?: 'raw' | 'multipart',
// This option is needed for multipart type
field?: string,
customUploadId?: string,
// parameters are supported only in multipart type
parameters?: { [string]: string },
headers?: Object,
notification?: NotificationArgs
}
const NativeModule = NativeModules.VydiaRNFileUploader || NativeModules.RNFileUploader // iOS is VydiaRNFileUploader and Android is NativeModules
//...
export const startUpload = (options: StartUploadArgs): Promise<string> => NativeModule.startUpload(options)
And here is an excerpt of the Java code that handles the string:
@ReactMethod
public void startUpload(ReadableMap options, final Promise promise) {
//...
HttpUploadRequest<?> request;
if (requestType.equals("raw")) {
request = new BinaryUploadRequest(this.getReactApplicationContext(), customUploadId, url)
.setFileToUpload(filePath);
} else {
if (!options.hasKey("field")) {
promise.reject(new IllegalArgumentException("field is required field for multipart type."));
return;
}
if (options.getType("field") != ReadableType.String) {
promise.reject(new IllegalArgumentException("field must be string."));
return;
}
request = new MultipartUploadRequest(this.getReactApplicationContext(), customUploadId, url)
.addFileToUpload(filePath, options.getString("field"));
}
request.setMethod(method)
.setMaxRetries(2)
.setDelegate(statusDelegate);
//...
if (options.hasKey("parameters")) {
if (requestType.equals("raw")) {
promise.reject(new IllegalArgumentException("Parameters supported only in multipart type"));
return;
}
ReadableMap parameters = options.getMap("parameters");
ReadableMapKeySetIterator keys = parameters.keySetIterator();
while (keys.hasNextKey()) {
String key = keys.nextKey();
if (parameters.getType(key) != ReadableType.String) {
promise.reject(new IllegalArgumentException("Parameters must be string key/values. Value was invalid for '" + key + "'"));
return;
}
request.addParameter(key, parameters.getString(key));
}
}
//...
String uploadId = request.startUpload();
promise.resolve(uploadId);
}