List of usage examples for com.fasterxml.jackson.databind ObjectMapper readValue
@SuppressWarnings("unchecked") public <T> T readValue(byte[] src, JavaType valueType) throws IOException, JsonParseException, JsonMappingException
From source file:io.github.retz.mesosc.MesosHTTPFetcher.java
public static Optional<String> extractSlaveBasePath(InputStream stream) throws IOException { ObjectMapper mapper = new ObjectMapper(); Map<String, Map<String, Object>> map = mapper.readValue(stream, java.util.Map.class); String dir = (String) map.get("flags").get("work_dir"); return Optional.of(dir); }
From source file:org.bremersee.sms.ExtensionUtils.java
/** * Transforms a JSON map into an object. * /* w w w.j ava2 s.c o m*/ * @param map * the JSON map * @param valueType * the class of the target object * @param objectMapper * the JSON object mapper (can be null) * @return the target object * @throws IOException * if transformation fails */ public static <T> T jsonMapToObject(Map<String, Object> map, Class<T> valueType, ObjectMapper objectMapper) throws IOException { if (map == null) { return null; } Validate.notNull(valueType, "valueType must not be null"); if (objectMapper == null) { objectMapper = DEFAULT_OBJECT_MAPPER; } return objectMapper.readValue(objectMapper.writeValueAsBytes(map), valueType); }
From source file:org.keycloak.helper.TestsHelper.java
public static boolean ImportTestRealm(String username, String password, String realmJsonPath) throws IOException { ObjectMapper mapper = new ObjectMapper(); ClassLoader classLoader = TestsHelper.class.getClassLoader(); InputStream stream = TestsHelper.class.getResourceAsStream(realmJsonPath); RealmRepresentation realmRepresentation = mapper.readValue(stream, RealmRepresentation.class); Keycloak keycloak = Keycloak.getInstance(keycloakBaseUrl, "master", username, password, "admin-cli"); keycloak.realms().create(realmRepresentation); testRealm = realmRepresentation.getRealm(); ClientInitialAccessCreatePresentation rep = new ClientInitialAccessCreatePresentation(); rep.setCount(2);/*from w ww. j av a 2 s . c o m*/ rep.setExpiration(100); ClientInitialAccessPresentation initialAccess = keycloak.realms().realm(testRealm).clientInitialAccess() .create(rep); initialAccessCode = initialAccess.getToken(); return true; }
From source file:com.networknt.light.server.LightServer.java
/** * This replay is to initialize database when the db is newly created. It load rules, forms and pages * and certain setup that need to make the applications working. To replay the entire event schema, * you have to call it from the Db Admin interface. * *///from w ww .ja v a2 s. com static private void replayEvent() { // need to replay from a file instead if the file exist. Otherwise, load from db and replay. // in this case, we need to recreate db. StringBuilder sb = new StringBuilder(""); //Get file from resources folder ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream("initdb.json"); try (Scanner scanner = new Scanner(is)) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); sb.append(line).append("\n"); } scanner.close(); ObjectMapper mapper = ServiceLocator.getInstance().getMapper(); if (sb.length() > 0) { // if you want to generate the initdb.json from your dev env, then you should make this // file as empty in server resources folder and load all objects again. List<Map<String, Object>> events = mapper.readValue(sb.toString(), new TypeReference<List<HashMap<String, Object>>>() { }); // replay event one by one. for (Map<String, Object> event : events) { RuleEngine.getInstance().executeRule(Util.getEventRuleId(event), event); } } } catch (Exception e) { logger.error("Exception:", e); } }
From source file:org.numenta.nupic.algorithms.CLAClassifier.java
public static CLAClassifier deSerialize(String jsonStrategy) { ObjectMapper om = new ObjectMapper(); CLAClassifier c = null;/*from w w w.j a v a 2 s.c om*/ try { Object o = om.readValue(jsonStrategy, CLAClassifier.class); c = (CLAClassifier) o; } catch (Exception e) { e.printStackTrace(); } return c; }
From source file:com.googlecode.blaisemath.app.MenuConfig.java
/** * Reads menu components from file./*from www. ja v a 2 s.c om*/ * @param cls class with associated resource * @return map, where values are either nested maps, or lists of strings representing actions * @throws IOException if there's an error reading from the config file */ public static Map<String, Object> readConfig(Class cls) throws IOException { String path = "resources/" + cls.getSimpleName() + MENU_SUFFIX + ".yml"; URL rsc = cls.getResource(path); checkNotNull(rsc, "Failed to locate " + path); ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); return mapper.readValue(rsc, Map.class); }
From source file:com.github.braully.graph.DatabaseFacade.java
public static UndirectedSparseGraphTO openGraph(String nameFile) throws IOException { if (nameFile == null) { return null; }/*from ww w .ja v a 2 s . c o m*/ UndirectedSparseGraphTO graph = null; ObjectMapper mapper = new ObjectMapper(); graph = mapper.readValue(new File(DATABASE_DIRECTORY_GRAPH + File.separator + nameFile), UndirectedSparseGraphTO.class); return graph; }
From source file:io.github.retz.mesosc.MesosHTTPFetcher.java
public static Optional<String> extractSlaveAddr(InputStream stream, String slaveId) throws IOException { ObjectMapper mapper = new ObjectMapper(); Map<String, List<Map<String, Object>>> map = mapper.readValue(stream, java.util.Map.class); String pid;/*from www. java 2s. co m*/ for (Map<String, Object> slave : map.get("slaves")) { if (slave.get("id").equals(slaveId)) { pid = (String) slave.get("pid"); String[] tokens = pid.split("@"); assert tokens.length == 2; return Optional.of(tokens[1]); } } return Optional.empty(); }
From source file:za.co.bronkode.jwtbroker.Tokenizer.java
public static Token DecodeToken(String token) { if (IsValidToken(token)) { try {/* ww w . j a v a 2s .c o m*/ String[] items = token.split("\\."); Token t = new Token(); ObjectMapper mapper = new ObjectMapper(); byte[] headerBytes = Base64.getDecoder().decode(items[0]); byte[] claimBytes = Base64.getDecoder().decode(items[1]); String headerjson = new String(headerBytes); String claimjson = new String(claimBytes); t.setHeader(mapper.readValue(headerjson, TokenHeader.class)); t.setClaim(mapper.readValue(claimjson, claimClass)); return t; } catch (IOException ex) { Logger.getLogger(Tokenizer.class.getName()).log(Level.SEVERE, null, ex); } } else { return null; } return null; }
From source file:org.kitesdk.apps.streaming.StreamDescription.java
public static StreamDescription parseJson(String json) { ObjectMapper mapper = new ObjectMapper(); JsonNode parent;/*from www .j a v a 2 s .co m*/ try { parent = mapper.readValue(json, JsonNode.class); } catch (JsonParseException e) { throw new ValidationException("Invalid JSON", e); } catch (JsonMappingException e) { throw new ValidationException("Invalid JSON", e); } catch (IOException e) { throw new AppException(e); } StreamDescription.Builder builder = new StreamDescription.Builder(); String jobName = parent.get(NAME).asText(); builder.jobName(jobName); String className = parent.get(JOBCLASS).asText(); try { Class jobClass = Thread.currentThread().getContextClassLoader().loadClass(className); builder.jobClass(jobClass); } catch (ClassNotFoundException e) { throw new AppException(e); } // Read the streams. ArrayNode streams = (ArrayNode) parent.get(STREAMS); for (JsonNode stream : streams) { String name = stream.get(NAME).asText(); ObjectNode props = (ObjectNode) stream.get(PROPS); Map<String, String> properties = Maps.newHashMap(); for (Iterator<Map.Entry<String, JsonNode>> it = props.fields(); it.hasNext();) { Map.Entry<String, JsonNode> property = it.next(); properties.put(property.getKey(), property.getValue().asText()); } builder.withStream(name, properties); } // Read the views. ArrayNode views = (ArrayNode) parent.get(VIEWS); for (JsonNode view : views) { String name = view.get(NAME).asText(); String uri = view.get(URI_PROP).asText(); builder.withView(name, uri); } return builder.build(); }