Ignoring quotes in Java

29 Views Asked by At

Here is a scenario:

User sends this json object to the web server (NodeJS)

{ "code": "class StringOperations {\n public String concatenate(String a, String b) {\n return a + b;\n }\n}", "tests": ["new StringOperations().concatenate(\"Hello\", \"world\"], "expectedResults": ["Helloworld"], "language": "java" }

This input is then used in this script:

export const getJavaScript = (code, tests, expectedResults) => `
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.HashMap;
    import com.google.gson.Gson;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonArray;

    ${code}
    public class Main {
      
        public static void main(String[] args) {
          java.util.List<java.util.Map<String, Object>> results = new java.util.ArrayList<>();
            try {
    
                ${tests
                  .map(
                    (test, index) => `
                    String actualResult_${index} = ${test};
                    String expectedResult_${index} = ${JSON.stringify(
                      JSON.stringify(expectedResults[index]),
                    )};
                        java.util.Map<String, Object> testResult_${index} = new java.util.HashMap<>();
                        if (actualResult_${index}.equals(expectedResult_${index})) {
                          testResult_${index}.put(\\\"success\\\", true);
                        } else {
                            testResult_${index}.put(\\\"success\\\", false);
                          }
                          testResult_${index}.put(\\\"actualResult\\\", actualResult_${index});
                          testResult_${index}.put(\\\"expectedResult\\\", expectedResult_${index});
                        results.add(testResult_${index});
                    `,
                  )
                  .join('\n')}

                  System.out.println(new com.google.gson.Gson().toJson(results));
                  
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
    }
  }
    `;

this code compiles into this java class:

    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.HashMap;
    import com.google.gson.Gson;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonArray;

    class StringOperations {
    public String concatenate(String a, String b) {
        return a + b;
    }
}
    public class Main {
      
        public static void main(String[] args) {
          java.util.List<java.util.Map<String, Object>> results = new java.util.ArrayList<>();
            try {
    
                
                    String actualResult_0 = new StringOperations().concatenate(Hello, hello);
                    String expectedResult_0 = "helloworld";
                        java.util.Map<String, Object> testResult_0 = new java.util.HashMap<>();
                        if (actualResult_0.equals(expectedResult_0)) {
                          testResult_0.put("success", true);
                        } else {
                            testResult_0.put("success", false);
                          }
                          testResult_0.put("actualResult", actualResult_0);
                          testResult_0.put("expectedResult", expectedResult_0);
                        results.add(testResult_0);
                    

                  System.out.println(new com.google.gson.Gson().toJson(results));
                  
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
    }
  }
    

As you can see on this line: String actualResult_0 = new StringOperations().concatenate(Hello, hello); the "\ operator used in input is ignored.

My question is how to make sure quotes stay in place.

I know potential solution is to use syntax like this: "tests": ["new StringOperations().concatenate(\\\"hello\\\", \\\"hello\\\")"], but I want to avoid it since when using other languages as input this is not required

I tried using different methods to keep string in place, for example I avoided double quote in expectedResults with this line: String expectedResult_${index} = ${JSON.stringify(JSON.stringify(expectedResults[index]))} but for example above it's not anought.

0

There are 0 best solutions below