private Flux<Object> decodeInternal( JsonObjectDecoder objectDecoder, Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Object[] hints) { Assert.notNull(inputStream, "'inputStream' must not be null"); Assert.notNull(elementType, "'elementType' must not be null"); TypeFactory typeFactory = this.mapper.getTypeFactory(); JavaType javaType = typeFactory.constructType(elementType.getType()); ObjectReader reader = this.mapper.readerFor(javaType); return objectDecoder .decode(inputStream, elementType, mimeType, hints) .map( dataBuffer -> { try { Object value = reader.readValue(dataBuffer.asInputStream()); DataBufferUtils.release(dataBuffer); return value; } catch (IOException e) { return Flux.error(new CodecException("Error while reading the data", e)); } }); }
@SuppressWarnings("unchecked") private Sysprop fetchFxRatesJSON() { Map<String, Object> map = new HashMap<String, Object>(); Sysprop s = new Sysprop(); ObjectReader reader = ParaObjectUtils.getJsonReader(Map.class); try { CloseableHttpClient http = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(SERVICE_URL); HttpResponse res = http.execute(httpGet); HttpEntity entity = res.getEntity(); if (entity != null && Utils.isJsonType(entity.getContentType().getValue())) { JsonNode jsonNode = reader.readTree(entity.getContent()); if (jsonNode != null) { JsonNode rates = jsonNode.get("rates"); if (rates != null) { map = reader.treeToValue(rates, Map.class); s.setId(FXRATES_KEY); s.setProperties(map); // s.addProperty("fetched", Utils.formatDate("dd MM yyyy HH:mm", Locale.UK)); dao.create(s); } } EntityUtils.consume(entity); } logger.debug("Fetched rates from OpenExchange for {}.", new Date().toString()); } catch (Exception e) { logger.error("TimerTask failed: {}", e); } return s; }
/** * Parse a JSON response to extract an entity document. * * <p>TODO This method currently contains code to work around Wikibase issue * https://phabricator.wikimedia.org/T73349. This should be removed once the issue is fixed. * * @param entityNode the JSON node that should contain the entity document data * @return the entitiy document, or null if there were unrecoverable errors * @throws IOException * @throws JsonProcessingException */ private EntityDocument parseJsonResponse(JsonNode entityNode) throws JsonProcessingException, IOException { try { JacksonTermedStatementDocument ed = mapper.treeToValue(entityNode, JacksonTermedStatementDocument.class); ed.setSiteIri(this.siteIri); return ed; } catch (JsonProcessingException e) { logger.warn( "Error when reading JSON for entity " + entityNode.path("id").asText("UNKNOWN") + ": " + e.toString() + "\nTrying to manually fix issue https://phabricator.wikimedia.org/T73349."); String jsonString = entityNode.toString(); jsonString = jsonString .replace("\"sitelinks\":[]", "\"sitelinks\":{}") .replace("\"labels\":[]", "\"labels\":{}") .replace("\"aliases\":[]", "\"aliases\":{}") .replace("\"claims\":[]", "\"claims\":{}") .replace("\"descriptions\":[]", "\"descriptions\":{}"); ObjectReader documentReader = this.mapper.reader(JacksonTermedStatementDocument.class); JacksonTermedStatementDocument ed; ed = documentReader.readValue(jsonString); ed.setSiteIri(this.siteIri); return ed; } }
protected EndpointConfig initReader(ObjectMapper mapper) { // first common config if (_activeView != null) { _reader = mapper.readerWithView(_activeView); } else { _reader = mapper.reader(); } if (_rootName != null) { _reader = _reader.withRootName(_rootName); } // Then deser features if (_deserEnable != null) { _reader = _reader.withFeatures(_deserEnable); } if (_deserDisable != null) { _reader = _reader.withoutFeatures(_deserDisable); } /* Important: we are NOT to close the underlying stream after * mapping, so we need to instruct parser: */ _reader.getJsonFactory().disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); return this; }
/* (non-Javadoc) * @see com.feinno.message.HttpConvertibleOutput#fromHttpResponse(com.ning.http.client.Response, java.lang.Throwable) */ @Override public void fromHttpResponse(Response response, Throwable throwable) { if (throwable != null) { LOGGER.error(throwable.getMessage()); return; } if (response.getStatusCode() == 200) { try { String content; if ((content = response.getResponseBody("UTF-8")) != null) { ObjectReader reader = mapper.readerForUpdating(this); reader.readValue(content); } } catch (Exception e) { LOGGER.error(e.getMessage()); error_code = "0"; setError_msg(e.getMessage()); return; } } else { try { ObjectReader reader = mapper.readerForUpdating(this); reader.readValue(response.getResponseBody("UTF-8")); return; } catch (Exception e) { LOGGER.error(response.getStatusCode() + "\n" + e.getMessage()); error_code = "0"; return; } } }
/** * 解析返回的内容,默认已经200返回了。如果解析不了,那么this中只有error对象不为空 * * @param response * @throws IOException */ private void parseResponse(Response response) throws Exception { String content; if ((content = response.getResponseBody("UTF-8")) != null) { try { statuses = (mapper.readValue(content, type)); } catch (JsonMappingException e) { // 正常的status解析不出来,估计就是error返回了,解析一下error吧! ObjectReader reader = mapper.readerForUpdating(this); reader.readValue(content); } } }
private void _testMapsWithLinefeeds(boolean useBytes) throws Exception { String CSV = "A,B,C\n" + "data11,data12\n" + "data21,data22,data23\r\n" + "data31,\"data32 data32\ndata32 data32\",data33\n" + "data41,\"data42 data42\r\ndata42\",data43\n"; CsvSchema cs = CsvSchema.emptySchema().withHeader(); ObjectReader or = MAPPER.readerFor(HashMap.class).with(cs); MappingIterator<Map<String, String>> mi; if (useBytes) { mi = or.readValues(CSV.getBytes("UTF-8")); } else { mi = or.readValues(CSV); } assertTrue(mi.hasNext()); Map<String, String> map = mi.nextValue(); assertNotNull(map); assertEquals("data11", map.get("A")); assertEquals("data12", map.get("B")); assertEquals(2, map.size()); assertTrue(mi.hasNext()); map = mi.nextValue(); assertNotNull(map); assertEquals(3, map.size()); // then entries with linefeeds assertTrue(mi.hasNext()); map = mi.nextValue(); assertNotNull(map); assertEquals(3, map.size()); assertEquals("data31", map.get("A")); assertEquals("data32 data32\ndata32 data32", map.get("B")); assertEquals("data33", map.get("C")); assertTrue(mi.hasNext()); map = mi.nextValue(); assertNotNull(map); assertEquals(3, map.size()); assertEquals("data41", map.get("A")); assertEquals("data42 data42\r\ndata42", map.get("B")); assertEquals("data43", map.get("C")); assertFalse(mi.hasNext()); mi.close(); }
@Override public <O> O deserialise(String source, Class<? extends O> type, Map<String, Object> parameters) throws DeserialiserException { try { ObjectReader reader = reader(type); for (Entry<String, Object> entry : parameters.entrySet()) { reader = reader.withAttribute(entry.getKey(), entry.getValue()); } return reader.readValue(source); } catch (DeserialiserException ex) { throw ex; } catch (IOException ex) { throw new DeserialiserException(ex); } }
public static final AbstractClass fromJson(String json) { ObjectMapper mapper = new ObjectMapper(); ObjectReader reader = mapper.reader(AbstractClass.class); AbstractClass hydrated = null; try { hydrated = reader.readValue(json); } catch (IOException e) { e.printStackTrace(); } System.out.println(hydrated); return hydrated; }
private void _testSimpleExplicit(ObjectReader r, boolean useBytes) throws Exception { r = r.forType(FiveMinuteUser.class); FiveMinuteUser user; final String INPUT = "Bob,Robertson,MALE,AQIDBAU=,false\n"; if (useBytes) { user = r.readValue(INPUT); } else { user = r.readValue(INPUT.getBytes("UTF-8")); } assertEquals("Bob", user.firstName); assertEquals("Robertson", user.lastName); assertEquals(Gender.MALE, user.getGender()); assertFalse(user.isVerified()); assertArrayEquals(new byte[] {1, 2, 3, 4, 5}, user.getUserImage()); }
public static Personnage getPersonnage(final String name, final String realm) { String readCharacter = queryBattle(BattleApiConstants.getCharacterQueryUrl(name, realm)); LOGGER.debug(readCharacter); if (readCharacter == null) { return null; } Personnage p = new Personnage(); try { ObjectMapper mapper = new ObjectMapper(); ObjectReader reader = mapper.readerFor(Personnage.class); p = reader.readValue(readCharacter); } catch (Exception e) { LOGGER.error(e); } return p; }
public void send(GCMMessage messageBase) throws Exception { if (gcmURI == null) { throw new Exception("Error sending push. Google cloud messaging properties not provided."); } HttpPost httpPost = new HttpPost(gcmURI); httpPost.setHeader("Authorization", API_KEY); httpPost.setEntity(new StringEntity(messageBase.toJson(), ContentType.APPLICATION_JSON)); try (CloseableHttpResponse response = httpclient.execute(httpPost)) { HttpEntity entity = response.getEntity(); String responseMsg = EntityUtils.toString(entity); if (response.getStatusLine().getStatusCode() == 200) { GCMResponseMessage gcmResponseMessage = gcmResponseReader.readValue(responseMsg); if (gcmResponseMessage.failure == 1) { if (gcmResponseMessage.results != null && gcmResponseMessage.results.length > 0) { throw new Exception( "Error sending push. Problem : " + gcmResponseMessage.results[0].error); } else { throw new Exception("Error sending push. Token : " + messageBase.getToken()); } } } else { EntityUtils.consume(entity); throw new Exception(responseMsg); } } finally { httpPost.releaseConnection(); } }
@Override public void receiveLoop() { log.debug("receiveLoop starts"); ObjectReader reader = this.mapper.reader(); while (true) { try { JsonNode root = reader.readTree(this.istream); String type = root.path(TYPE).asText(); JsonNode data = root.path(DATA); log.debug("Processing {} with {}", type, data); if (type.equals(GOODBYE)) { log.info("Connection closing from server."); break; } else if (type.equals(HELLO)) { this.receiveHello(data); } else if (type.equals(LOCALE)) { this.receiveHello(data); } else if (type.equals(PONG)) { // silently ignore } else if (!data.isMissingNode() || (root.isArray() && ((ArrayNode) root).size() > 0)) { // process replies with a data node or non-empty arrays JsonClientReply reply = new JsonClientReply(root); Runnable r = new RcvNotifier(reply, mLastSender, this); try { SwingUtilities.invokeAndWait(r); } catch (InterruptedException e) { log.error("Exception notifying listeners of reply: {}", e.getMessage(), e); } catch (InvocationTargetException e) { log.error("Exception notifying listeners of reply: {}", e.getMessage(), e); } } } catch (IOException e) { this.rcvException = true; reportReceiveLoopException(e); break; } catch (NoSuchElementException nse) { // we get an NSE when we are finished with this client // so break out of the loop break; } } ConnectionStatus.instance() .setConnectionState(this.controller.getCurrentPortName(), ConnectionStatus.CONNECTION_DOWN); log.error("Exit from rcv loop"); this.recovery(); }
private List<ArrayNode> downloadResult(String jobId) { return client.jobResult( jobId, TDResultFormat.JSON, input -> { try { List<String> lines = CharStreams.readLines(new InputStreamReader(input)); ObjectReader reader = objectMapper().readerFor(ArrayNode.class); List<ArrayNode> result = new ArrayList<>(); for (String line : lines) { result.add(reader.readValue(line)); } return result; } catch (IOException e) { throw Throwables.propagate(e); } }); }
@Override public Document read(Identifier identifier) throws RESTException, IOException { if (identifier.size() < 3) { throw new IllegalArgumentException( "Identifier not precise enough. Needs ID as well. " + identifier.toString()); } JSONObject response = super.doGetCall(super.createFullURL(identifier)); return r.readValue(response.toJSONString()); }
private LockStatus getLockStatus() { String unLockUri = "http://" + metadata.getHostAndPort() + "/control/lockStatus"; HttpGet httpGet = new HttpGet(unLockUri); try { return objectReader.withType(LockStatus.class).readValue(askWorker(httpGet)); } catch (IOException e) { logger.warn("Error reading worker lock status"); throw Throwables.propagate(e); } }
@Override public QueryResponseWrapper readAll(Identifier identifier, int limit, long offset) throws RESTException, IOException { if (identifier.size() < 2) { throw new IllegalArgumentException( "Identifier not precise enough. Needs Database and Table. " + identifier.toString()); } JSONObject response = super.doGetCall(super.createFullURL(identifier) + "/"); return rQuery.readValue(response.toJSONString()); }
@VisibleForTesting static Map<String, AccessLevel> deserializeListProjectsMap(JsonNode result) { try { return listProjectsReader.<Map<String, AccessLevel>>readValue(result); } catch (JsonProcessingException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
public static Guilde getGuilde() { String readGuildMembersCount = queryBattle(BattleApiConstants.getGuildeMembersQueryUrl()); LOGGER.debug(readGuildMembersCount); if (readGuildMembersCount == null) { return null; } readGuildMembersCount = readGuildMembersCount.substring(readGuildMembersCount.indexOf("(") + 1); readGuildMembersCount = readGuildMembersCount.substring(0, readGuildMembersCount.length() - 2); Guilde g = new Guilde(); try { ObjectMapper mapper = new ObjectMapper(); ObjectReader reader = mapper.readerFor(Guilde.class); g = reader.readValue(readGuildMembersCount); LOGGER.info("Nombre de membres " + g.getMembers().size()); } catch (Exception e) { LOGGER.error(e); } return g; }
@Override public Document create(Table table, Document entity) throws RESTException, ParseException, IOException { JSONParser parser = new JSONParser(); String documentJson = SDKUtils.createJSON(entity.getObject()); JSONObject response = (JSONObject) super.doPostCall( super.createFullURL(table) + "/documents", (JSONObject) parser.parse(documentJson)); return r.readValue(response.toJSONString()); }
public Stream<T> parse(InputStream is, CsvErrorSniffer context) { ObjectMapper mapper = objectMapper.copy(); formatter.initMixIn(mapper); ObjectReader reader = mapper.readerFor(formatter.getTargetClass()); CsvSchema schema = new CsvSchema(formatter); return parseToCsvLine(is) .map( line -> { line.getException() .ifPresent( e -> context.mark(new Location(line.getLineNumber(), OptionalInt.empty()))); Set<String> ignoreField = new HashSet<>(); while (true) { try { return reader.readValue(schema.toJson(line, ignoreField)); } catch (JsonMappingException e) { String path = buildPath(e.getPath()); ; Location location = new Location( line.getLineNumber(), OptionalInt.of(schema.getColumnNumber(path))); if (context.contains(location)) { throw new IllegalStateException("invalid row state: " + e.getLocation()); } context.mark(location); ignoreField.add(path); } catch (IOException e) { context.mark(new Location(line.getLineNumber(), OptionalInt.empty())); try { return formatter.getTargetClass().newInstance(); } catch (ReflectiveOperationException e2) { throw new ReflectiveOperationRuntimeException(e2); } } } }); }
private static List<Race> getListRaces() { String sRaces = queryBattle(BattleApiConstants.getRacesQueryUrl()); LOGGER.debug(sRaces); if (sRaces == null) { return null; } List<Race> races = new ArrayList<Race>(); try { ObjectMapper mapper = new ObjectMapper(); ObjectReader reader = mapper.readerFor(new TypeReference<List<Race>>() {}).withRootName("races"); races = reader.readValue(sRaces); LOGGER.debug("Nombre de races " + races.size()); for (Race race : races) { LOGGER.debug(race.toString()); } } catch (Exception e) { LOGGER.error(e); } return races; }
private static <T> Optional<T> get( final JsonNode jsonResult, final String key, final Class<T> type) { final Optional<JsonNode> fieldNode = Optional.ofNullable(jsonResult.get(key)); return fieldNode.map( f -> { try { return EVENT_PARAMETERS_READER.treeToValue(f, type); } catch (final JsonProcessingException e) { log.info("Unable to map field: " + key, e); return null; } }); }
public void testSimpleExplicitWithBOM() throws Exception { ObjectReader r = MAPPER.reader(SIMPLE_SCHEMA); r = r.forType(FiveMinuteUser.class); FiveMinuteUser user; ByteArrayOutputStream b = new ByteArrayOutputStream(); // first, UTF-8 BOM: b.write(new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}); b.write("Bob,Robertson,MALE,AQIDBAU=,false\n".getBytes("UTF-8")); b.close(); user = r.readValue(b.toByteArray()); String fn = user.firstName; if (!fn.equals("Bob")) { fail("Expected 'Bob' (3), got '" + fn + "' (" + fn.length() + ")"); } assertEquals("Robertson", user.lastName); assertEquals(Gender.MALE, user.getGender()); assertFalse(user.isVerified()); assertArrayEquals(new byte[] {1, 2, 3, 4, 5}, user.getUserImage()); }
protected final double _testRawDeser(int reps, byte[] json, ObjectReader reader) throws IOException { long start = System.nanoTime(); final JsonFactory f = reader.getFactory(); while (--reps >= 0) { JsonParser p = f.createParser(new ByteArrayInputStream(json)); JsonToken t; while ((t = p.nextToken()) != null) { if (t == JsonToken.VALUE_STRING) { p.getText(); } else if (t.isNumeric()) { p.getNumberValue(); } ; } p.close(); } hash = f.hashCode(); return _msecsFromNanos(System.nanoTime() - start); }
@Override public StreamDefinition findOne(String id) { try { byte[] bytes = zkConnection.getClient().getData().forPath(Paths.build(Paths.STREAMS, id)); if (bytes == null) { return null; } Map<String, String> map = ZooKeeperUtils.bytesToMap(bytes); StreamDefinition streamDefinition = new StreamDefinition(id, map.get(DEFINITION_KEY)); if (map.get(MODULE_DEFINITIONS_KEY) != null) { List<ModuleDefinition> moduleDefinitions = objectReader.readValue(map.get(MODULE_DEFINITIONS_KEY)); streamDefinition.setModuleDefinitions(moduleDefinitions); } return streamDefinition; } catch (Exception e) { // NoNodeException - the definition does not exist ZooKeeperUtils.wrapAndThrowIgnoring(e, NoNodeException.class); } return null; }
public void testSimple() throws Exception { final ObjectMapper mapper = new ObjectMapper(); final ObjectReader jsonReader = mapper.reader(POJO.class); final String JSON = "{\"name\":\"Bob\", \"id\":3}"; byte[] doc = _smileDoc(JSON, true); ObjectReader detecting = jsonReader.withFormatDetection(jsonReader, jsonReader.with(new SmileFactory())); POJO pojo = detecting.readValue(doc); assertEquals(3, pojo.id); assertEquals("Bob", pojo.name); // let's verify it also works for plain JSON... pojo = detecting.readValue(JSON.getBytes("UTF-8")); assertEquals(3, pojo.id); assertEquals("Bob", pojo.name); }
public void run() { String id = null; // document id String type = null; // document type String indexName = null; // document index Map<String, Object> data = null; // document data for indexing ObjectReader dataReader = mapper.reader(new TypeReference<Map<String, Object>>() {}); int interval = (LONGPOLLING_INTERVAL < 0 || LONGPOLLING_INTERVAL > 20) ? DEFAULT_LONGPOLLING_INTERVAL : LONGPOLLING_INTERVAL; while (!closed) { // pull messages from SQS if (DEBUG) logger.info("Waiting {}s for messages...", interval); List<JsonNode> msgs = pullMessages(interval); try { BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); for (JsonNode msg : msgs) { if (msg.has("_id") && msg.has("_type")) { id = msg.get("_id").textValue(); type = msg.get("_type").textValue(); // Support for dynamic indexes indexName = msg.has("_index") ? msg.get("_index").textValue() : INDEX; JsonNode dataNode = msg.get("_data"); if (dataNode != null && dataNode.isObject()) { data = dataReader.readValue(msg.get("_data")); bulkRequestBuilder.add( client.prepareIndex(indexName, type, id).setSource(data).request()); } else { bulkRequestBuilder.add(client.prepareDelete(indexName, type, id).request()); } } } if (bulkRequestBuilder.numberOfActions() > 0) { BulkResponse response = bulkRequestBuilder.execute().actionGet(); if (response.hasFailures()) { logger.warn( "Bulk operation completed with errors: " + response.buildFailureMessage()); } idleCount = 0; } else { idleCount++; if (DEBUG) logger.info("No new messages. {}", idleCount); // no tasks in queue => throttle down pull requests if (SLEEP > 0 && idleCount >= 3) { try { if (DEBUG) logger.info("Queue is empty. Sleeping for {}s", interval); Thread.sleep(SLEEP * 1000); } catch (InterruptedException e) { if (closed) { if (DEBUG) logger.info("Done."); break; } } } } } catch (Exception e) { logger.error("Bulk index operation failed {}", e); continue; } } }
public static MyClass getObjectFromJson(final String jsonString) throws IOException { return joReader.readValue(jsonString); }
private void handleEvent(final HttpServerExchange exchange) throws Exception { try (final ChannelInputStream cis = new ChannelInputStream(exchange.getRequestChannel())) { final JsonNode payload = EVENT_PARAMETERS_READER.readTree(cis); final String generatedPageViewId = DivolteIdentifier.generate().value; final DivolteEvent.BrowserEventData browserEventData = new DivolteEvent.BrowserEventData( get(payload, "page_view_id", String.class).orElse(generatedPageViewId), get(payload, "location", String.class), get(payload, "referer", String.class), get(payload, "viewport_pixel_width", Integer.class), get(payload, "viewport_pixel_height", Integer.class), get(payload, "screen_pixel_width", Integer.class), get(payload, "screen_pixel_height", Integer.class), get(payload, "device_pixel_ratio", Integer.class)); final DivolteEvent divolteEvent = new DivolteEvent( exchange, get(payload, "corrupt", Boolean.class).orElse(false), get(payload, "party_id", String.class) .flatMap(DivolteIdentifier::tryParse) .orElse(DivolteIdentifier.generate()), get(payload, "session_id", String.class) .flatMap(DivolteIdentifier::tryParse) .orElse(DivolteIdentifier.generate()), get(payload, "event_id", String.class).orElse(generatedPageViewId + "0"), ClientSideCookieEventHandler.EVENT_SOURCE_NAME, System.currentTimeMillis(), 0L, get(payload, "new_party_id", Boolean.class).orElse(false), get(payload, "first_in_session", Boolean.class).orElse(false), get(payload, "event_type", String.class), () -> get(payload, "parameters", JsonNode.class), Optional.of(browserEventData)); get(payload, "remote_host", String.class) .ifPresent( ip -> { try { final InetAddress address = InetAddress.getByName(ip); // We have no way of knowing the port exchange.setSourceAddress(new InetSocketAddress(address, 0)); } catch (final UnknownHostException e) { log.warn("Could not parse remote host: " + ip, e); } }); exchange.putAttachment( DUPLICATE_EVENT_KEY, get(payload, "duplicate", Boolean.class).orElse(false)); exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json"); exchange .getResponseChannel() .write( ByteBuffer.wrap( mapper .newRecordFromExchange(divolteEvent) .toString() .getBytes(StandardCharsets.UTF_8))); exchange.endExchange(); } }