Skip to content

Commit

Permalink
feat: Added stringToList
Browse files Browse the repository at this point in the history
  • Loading branch information
th-release authored May 7, 2024
1 parent aca7c0f commit 40a2daf
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions java/StringUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import java.util.*;

public class StringUtility {
public Map<String, String> stringToJson(String jsonString) throws IOException {
public static Map stringToJson(String jsonString) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(jsonString, Map.class);
}

public String mapToJson(Map<String, String> map) {
public static String mapToJson(Map<String, String> map) {
StringBuilder json = new StringBuilder("{");
for (Map.Entry<String, String> entry : map.entrySet()) {
json.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\",");
Expand All @@ -18,7 +18,7 @@ public String mapToJson(Map<String, String> map) {
return json.toString();
}

public String mapToParams(Map<String, String> maps) {
public static String mapToParams(Map<String, String> maps) {
StringBuilder paramString = new StringBuilder();
for (Map.Entry<String, String> entry : maps.entrySet()) {
paramString.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
Expand All @@ -31,4 +31,9 @@ public static <T> T stringToClass(String jsonString, Class<T> classT) throws IOE
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(jsonString, classT);
}

public static <T> T stringToList(String jsonString) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return (T) objectMapper.readValue(jsonString, new TypeReference<List<T>>() {});
}
}

0 comments on commit 40a2daf

Please sign in to comment.