| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.utils; |
| 2 | |
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | import com.fasterxml.jackson.databind.JsonNode; |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 7 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 8 | |
| 9 | import java.io.File; |
| 10 | import java.io.IOException; |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.Iterator; |
| 13 | import java.util.List; |
| 14 | import java.util.Map; |
| 15 | |
| 16 | /** |
| 17 | * @author hanl |
| 18 | * @date 28/01/2014 |
| 19 | */ |
| 20 | public class JsonUtils { |
| 21 | private static ObjectMapper mapper = new ObjectMapper(); |
| 22 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 23 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 24 | private JsonUtils () {} |
| 25 | |
| 26 | |
| 27 | public static String toJSON (Object values) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 28 | try { |
| 29 | return mapper.writeValueAsString(values); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 30 | } |
| 31 | catch (JsonProcessingException e) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 32 | return ""; |
| 33 | } |
| 34 | } |
| 35 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 36 | |
| 37 | public static JsonNode readTree (String s) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 38 | try { |
| 39 | return mapper.readTree(s); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 40 | } |
| 41 | catch (IOException e) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 42 | return null; |
| 43 | } |
| 44 | } |
| 45 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 46 | |
| 47 | public static ObjectNode createObjectNode () { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 48 | return mapper.createObjectNode(); |
| 49 | } |
| 50 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 51 | |
| 52 | public static ArrayNode createArrayNode () { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 53 | return mapper.createArrayNode(); |
| 54 | } |
| 55 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 56 | |
| 57 | public static JsonNode valueToTree (Object value) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 58 | return mapper.valueToTree(value); |
| 59 | } |
| 60 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 61 | |
| 62 | public static <T> T read (String json, Class<T> cl) throws IOException { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 63 | return mapper.readValue(json, cl); |
| 64 | } |
| 65 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 66 | |
| 67 | public static <T> T readFile (String path, Class<T> clazz) |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 68 | throws IOException { |
| 69 | return mapper.readValue(new File(path), clazz); |
| 70 | } |
| 71 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 72 | |
| 73 | public static void writeFile (String path, String content) |
| 74 | throws IOException { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 75 | mapper.writeValue(new File(path), content); |
| 76 | } |
| 77 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 78 | |
| 79 | public static <T> T readSimple (String json, Class<T> cl) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 80 | try { |
| 81 | return mapper.readValue(json, cl); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 82 | } |
| 83 | catch (IOException e) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 84 | return null; |
| 85 | } |
| 86 | } |
| 87 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 88 | |
| 89 | public static List<Map<String, Object>> convertToList (String json) |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 90 | throws JsonProcessingException { |
| 91 | List d = new ArrayList(); |
| 92 | JsonNode node = JsonUtils.readTree(json); |
| 93 | if (node.isArray()) { |
| 94 | Iterator<JsonNode> nodes = node.iterator(); |
| 95 | while (nodes.hasNext()) { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 96 | Map<String, Object> map = mapper.treeToValue(nodes.next(), |
| 97 | Map.class); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 98 | d.add(map); |
| 99 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 100 | } |
| 101 | else if (node.isObject()) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 102 | Map<String, Object> map = mapper.treeToValue(node, Map.class); |
| 103 | d.add(map); |
| 104 | } |
| 105 | return d; |
| 106 | } |
| 107 | |
| 108 | } |