I had to convert java.util.Map to jsweet.lang.Object before encoding it as JSON using stringify .
Here is the code to send java.util.Map as JSON to the server using JSweet:
void postJson(Map<String, String> map, String url) { XMLHttpRequest request = new XMLHttpRequest(); // Post asynchronously request.open("POST", url, true); request.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); // Encode the data as JSON before sending String mapAsJson = JSON.stringify(toJsObject(map)); request.send(mapAsJson); } jsweet.lang.Object toJsObject(Map<String, String> map) { jsweet.lang.Object jsObject = new jsweet.lang.Object(); // Put the keys and values from the map into the object for (Entry<String, String> keyVal : map.entrySet()) { jsObject.$set(keyVal.getKey(), keyVal.getValue()); } return jsObject; }
Use it as follows:
Map<String, String> message = new HashMap<>(); message.put("content", "client says hi"); postJson(message, "http://myServer:8080/newMessage");
source share