@@ -33,23 +33,29 @@ public CommandExecutor(NewEnforcer enforcer, String inputMethodName, String[] in
33
33
* @return A JSON formatted string representing the key-value pairs from the input string.
34
34
*/
35
35
public static String convertToJson (String input ) {
36
- input = input .trim ().substring (1 , input .length () - 1 ).trim ();
37
- StringBuilder jsonBuilder = new StringBuilder ("{" );
38
- String [] pairs = input .split ("," );
39
- for (String pair : pairs ) {
40
- pair = pair .trim ();
41
- String [] keyValue = pair .split (":" );
42
- if (keyValue .length == 2 ) {
43
- String key = keyValue [0 ].trim ();
44
- String value = keyValue [1 ].trim ();
45
- jsonBuilder .append ("\" " ).append (key ).append ("\" :" ).append (value ).append ("," );
36
+ input = input .trim ();
37
+ // Handle the simple format {key: value}
38
+ if (!input .contains ("\" " )) {
39
+ input = input .substring (1 , input .length () - 1 ).trim ();
40
+ StringBuilder jsonBuilder = new StringBuilder ("{" );
41
+ String [] pairs = input .split ("," );
42
+ for (String pair : pairs ) {
43
+ pair = pair .trim ();
44
+ String [] keyValue = pair .split (":" );
45
+ if (keyValue .length == 2 ) {
46
+ String key = keyValue [0 ].trim ();
47
+ String value = keyValue [1 ].trim ();
48
+ jsonBuilder .append ("\" " ).append (key ).append ("\" :" ).append (value ).append ("," );
49
+ }
46
50
}
51
+ if (jsonBuilder .length () > 1 ) {
52
+ jsonBuilder .deleteCharAt (jsonBuilder .length () - 1 );
53
+ }
54
+ jsonBuilder .append ("}" );
55
+ return jsonBuilder .toString ();
47
56
}
48
- if (jsonBuilder .length () > 1 ) {
49
- jsonBuilder .deleteCharAt (jsonBuilder .length () - 1 );
50
- }
51
- jsonBuilder .append ("}" );
52
- return jsonBuilder .toString ();
57
+
58
+ return input ;
53
59
}
54
60
55
61
public String outputResult () throws InvocationTargetException , IllegalAccessException , JsonProcessingException {
@@ -104,16 +110,17 @@ public String outputResult() throws InvocationTargetException, IllegalAccessExce
104
110
105
111
Object [] extraConvertedParams = new Object [inputVal .length ];
106
112
boolean hasJson = false ;
107
- try {
113
+ try {
108
114
ObjectMapper objectMapper = new ObjectMapper ();
109
- if (inputVal .length > 0 && inputVal [0 ].trim ().startsWith ("{" )) {
110
- Map <String , Object > objectMap = objectMapper .readValue (convertToJson (inputVal [0 ]), new TypeReference <Map <String , Object >>() {
111
- });
112
- extraConvertedParams [0 ] = objectMap ;
113
- if (inputVal .length >= 1 ) {
114
- System .arraycopy (inputVal , 1 , extraConvertedParams , 1 , inputVal .length - 1 );
115
+ for (int i = 0 ; i < inputVal .length ; i ++) {
116
+ if (inputVal [i ].trim ().startsWith ("{" )) {
117
+ Map <String , Object > objectMap = objectMapper .readValue (convertToJson (inputVal [i ]), new TypeReference <Map <String , Object >>() {
118
+ });
119
+ extraConvertedParams [i ] = objectMap ;
120
+ hasJson = true ;
121
+ } else {
122
+ extraConvertedParams [i ] = inputVal [i ];
115
123
}
116
- hasJson = true ;
117
124
}
118
125
} catch (Exception e ) {
119
126
e .printStackTrace ();
0 commit comments