That fuck shit the fascists are using
at master 87 lines 2.7 kB view raw
1package org.tm.archive.util; 2 3import com.fasterxml.jackson.databind.DeserializationFeature; 4import com.fasterxml.jackson.databind.ObjectMapper; 5import com.fasterxml.jackson.databind.SerializationFeature; 6import com.fasterxml.jackson.databind.type.TypeFactory; 7 8import org.json.JSONException; 9import org.json.JSONObject; 10 11import java.io.IOException; 12import java.io.InputStream; 13import java.io.Reader; 14import java.util.List; 15 16import javax.annotation.Nullable; 17 18public class JsonUtils { 19 20 private static final ObjectMapper objectMapper = new ObjectMapper(); 21 22 static { 23 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 24 objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); 25 objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); 26 com.fasterxml.jackson.module.kotlin.ExtensionsKt.registerKotlinModule(objectMapper); 27 } 28 29 public static <T> T fromJson(byte[] serialized, Class<T> clazz) throws IOException { 30 return fromJson(new String(serialized), clazz); 31 } 32 33 public static <T> T fromJson(String serialized, Class<T> clazz) throws IOException { 34 return objectMapper.readValue(serialized, clazz); 35 } 36 37 public static <T> T fromJson(InputStream serialized, Class<T> clazz) throws IOException { 38 return objectMapper.readValue(serialized, clazz); 39 } 40 41 public static <T> T fromJson(Reader serialized, Class<T> clazz) throws IOException { 42 return objectMapper.readValue(serialized, clazz); 43 } 44 45 public static <T> List<T> fromJsonArray(String serialized, Class<T> clazz) throws IOException { 46 TypeFactory typeFactory = objectMapper.getTypeFactory(); 47 return objectMapper.readValue(serialized, typeFactory.constructCollectionType(List.class, clazz)); 48 } 49 50 public static String toJson(Object object) throws IOException { 51 return objectMapper.writeValueAsString(object); 52 } 53 54 public static ObjectMapper getMapper() { 55 return objectMapper; 56 } 57 58 public static class SaneJSONObject { 59 60 private final JSONObject delegate; 61 62 public SaneJSONObject(JSONObject delegate) { 63 this.delegate = delegate; 64 } 65 66 public @Nullable String getString(String name) throws JSONException { 67 if (delegate.isNull(name)) return null; 68 else return delegate.getString(name); 69 } 70 71 public long getLong(String name) throws JSONException { 72 return delegate.getLong(name); 73 } 74 75 public boolean getBoolean(String name) throws JSONException { 76 return delegate.getBoolean(name); 77 } 78 79 public boolean isNull(String name) { 80 return delegate.isNull(name); 81 } 82 83 public int getInt(String name) throws JSONException { 84 return delegate.getInt(name); 85 } 86 } 87}