0

Hi when I had used below code then it was working fine:

Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map<String, String> params = new HashMap<>();
params.put("behavior", "allow");
params.put("downloadPath", downloadPath);
commandParams.put("params", params);
HttpClient httpClient = HttpClientBuilder.create().build();

ObjectMapper objectMapper = new ObjectMapper();
String command = objectMapper.writeValueAsString(commandParams);

Now I want to remove all dependencies from my project so i tried using this approach from this link but it doesn't work as key and value pair have "(double quotes) in it.:

    Map<String, Object> commandParams = new HashMap<>();
    commandParams.put("cmd", "Page.setDownloadBehavior");
    Map<String, String> params = new HashMap<>();
    params.put("behavior", "allow");
    params.put("downloadPath", downloadPath);
    commandParams.put("params", params);
    HttpClient httpClient = HttpClientBuilder.create().build();

    String command ="{"+commandParams.entrySet().stream().map(e -> "\""+e.getKey() + "\"" + ":\"" + String.valueOf(e.getValue()) + "\"").collect(Collectors.joining(", "))+"}";

So i tried saving the String command directly as it would have been after using ObjectMapper Class from jackson jars using below code but this also doesn't work:

    Map<String, Object> commandParams = new HashMap<>();
    commandParams.put("cmd", "Page.setDownloadBehavior");
    Map<String, String> params = new HashMap<>();
    params.put("behavior", "allow");
    params.put("downloadPath", downloadPath);
    commandParams.put("params", params);
    HttpClient httpClient = HttpClientBuilder.create().build();

    String command = "{\"cmd\":\"Page.setDownloadBehavior\",\"params\":{\"downloadPath\":\"C:\\\\Users\\\\I334253\\\\Downloads\\\\Test_Download\",\"behavior\":\"allow\"}}";

The output of Command variable after using ObjectMapper Class was:

{"cmd":"Page.setDownloadBehavior","params":{"downloadPath":"C:\\Users\\I334253\\Downloads\\Test_Download","behavior":"allow"}}

I tried going through the jackson-databind github project but its simply too much for me to understand at this level. Please let me know how i can achieve this.

Archana
  • 57
  • 7
  • It's too much to understand because it's a lot of work to write a json parser. So why would you want to implement your own? – ernest_k Feb 07 '19 at 15:56
  • 1
    i personally prefer writing POJO class and do this stuff in overridden toString() method – Ryuzaki L Feb 07 '19 at 15:59
  • @Deadpool any site or blog you could refer to me so that I can understand these things and implement one for my self. As I am not much familiar. – Archana Feb 07 '19 at 16:28

1 Answers1

0

Escaping works with backslash, so do:

String command = commandParams.entrySet().stream()
    .map(e -> "\"" + escape(e.getKey()) + "\"" + ":\""
        + escape(String.valueOf(e.getValue())) + "\"")
    .collect(Collectors.joining(", ", "{", "}"));


static String escape(String s) {
    return s.replace("\\", "\\\\") // Single backslash
        .replace("\"", "\\\"");    // Double quote
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138