忽略 Java 中的引号

Ignoring quotes in Java

提问人:Yevhenii Novoseletskyi 提问时间:8/31/2023 更新时间:8/31/2023 访问量:27

问:

下面是一个场景:

用户将此 json 对象发送到 Web 服务器 (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" }

然后,此输入将用于此脚本:

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());
            }
    }
  }
    `;

此代码编译为以下 Java 类:

    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());
            }
    }
  }
    

正如您在这一行中看到的:输入中使用的“\ 运算符被忽略。String actualResult_0 = new StringOperations().concatenate(Hello, hello);

我的问题是如何确保报价保持不变。

我知道潜在的解决方案是使用这样的语法:但我想避免它,因为当使用其他语言作为输入时,这不是必需的 "tests": ["new StringOperations().concatenate(\\\"hello\\\", \\\"hello\\\")"],

我尝试使用不同的方法来保持字符串的位置,例如,我避免了在expectedResults中使用以下行的双引号: 字符串 expectedResult_${index} = ${JSON.stringify(JSON.stringify(expectedResults[index]))} 但例如,上面它不是 Anought。

JSON 字符串 双引号

评论

0赞 Rogue 8/31/2023
{"code": "class StringOperations { public String concatenate(String a, String b) { /* install malware */ } }" }.此外,在 java 代码中使用 ur 和 变量时是未定义的。Hellohello
0赞 Yevhenii Novoseletskyi 8/31/2023
我想你没有阅读问题陈述,因为 Hello 和 hello 不是变量而是字符串,但引号被忽略了。这个问题我正在尝试解决
0赞 Rogue 8/31/2023
只是为了测试,请尝试在输入中也转义反斜杠:等。\\\"Hello\\\"
0赞 Yevhenii Novoseletskyi 9/1/2023
像这样传递它时: { “code”: “class StringOperations {\n public String concatenate(String a, String b) {\n return a + b;\n }\n}”, “tests”: [“new StringOperations().concatenate(\\\”hello\\\“, \\\”hello\\\“)”], “expectedResults”: [“hellohello”], “language”: “java” } 编译版本不会忽略引号。我的问题是我想对每种语言使用相同的输入结构,并且对于其他一些语言,没有必要使用带有反斜杠的\\\“Hello\\\”

答: 暂无答案