Refine search
@RequestMapping("readjsonstring") @ResponseBody public String readJsonString() { try { String json = "{\"name\":\"mrbird\",\"age\":26}"; JsonNode node = this.mapper.readTree(json); String name = node.get("name").asText(); int age = node.get("age").asInt(); return name + " " + age; } catch (Exception e) { e.printStackTrace(); } return null; }
public static Short parseShort(String body, String field) { ObjectMapper mapper = new ObjectMapper(); JsonNode node = null; try { node = mapper.readTree(body); JsonNode leaf = node.get(field); if (leaf != null) { Integer value = leaf.asInt(); return value.shortValue(); } } catch (IOException e) { e.printStackTrace(); } return null; }
private RecordSchema createRecordSchema(final JsonNode schemaNode) throws SchemaNotFoundException { final String subject = schemaNode.get(SUBJECT_FIELD_NAME).asText(); final int version = schemaNode.get(VERSION_FIELD_NAME).asInt(); final int id = schemaNode.get(ID_FIELD_NAME).asInt(); final String schemaText = schemaNode.get(SCHEMA_TEXT_FIELD_NAME).asText(); try { final Schema avroSchema = new Schema.Parser().parse(schemaText); final SchemaIdentifier schemaId = SchemaIdentifier.builder().name(subject).id(Long.valueOf(id)).version(version).build(); final RecordSchema recordSchema = AvroTypeUtil.createSchema(avroSchema, schemaText, schemaId); return recordSchema; } catch (final SchemaParseException spe) { throw new SchemaNotFoundException("Obtained Schema with id " + id + " and name " + subject + " from Confluent Schema Registry but the Schema Text that was returned is not a valid Avro Schema"); } }
private static QueryParsingException buildQueryParsingException(Supplier<String> errorMessage, JsonNode rootCause, List<String> reasons) { final JsonNode lineJson = rootCause.path("line"); final Integer line = lineJson.isInt() ? lineJson.asInt() : null; final JsonNode columnJson = rootCause.path("col"); final Integer column = columnJson.isInt() ? columnJson.asInt() : null; final String index = rootCause.path("index").asText(null); return new QueryParsingException(errorMessage.get(), line, column, index, reasons); }
public static Byte parseByte(String body, String field) { ObjectMapper mapper = new ObjectMapper(); JsonNode node = null; try { node = mapper.readTree(body); JsonNode leaf = node.get(field); if (leaf != null) { Integer value = leaf.asInt(); return value.byteValue(); } } catch (IOException e) { e.printStackTrace(); } return null; }
@Override public RedisStateMap deserialize(final byte[] data) throws IOException { if (data == null || data.length == 0) { return null; } final RedisStateMap.Builder builder = new RedisStateMap.Builder(); try (final JsonParser jsonParser = jsonFactory.createParser(data)) { final JsonNode rootNode = jsonParser.readValueAsTree(); builder.version(rootNode.get(FIELD_VERSION).asLong()); builder.encodingVersion(rootNode.get(FIELD_ENCODING).asInt()); final JsonNode stateValuesNode = rootNode.get(FIELD_STATE_VALUES); stateValuesNode.fields().forEachRemaining(e -> builder.stateValue(e.getKey(), e.getValue().asText())); } return builder.build(); }
public BikeRentalStation makeStation(JsonNode node) { if (!node.path("activate").asText().equals("1")) { return null; } BikeRentalStation station = new BikeRentalStation(); station.id = String.format("%d", node.path("id").asInt()); station.x = node.path("longitude").asDouble(); station.y = node.path("latitude").asDouble(); station.name = new NonLocalizedString(node.path("name").asText()); station.bikesAvailable = node.path("dock_bikes").asInt(); station.spacesAvailable = node.path("free_bases").asInt(); return station; }
/** * Parse the output from the credentials process. */ private JsonNode parseProcessOutput(String processOutput) { JsonNode credentialsJson = Jackson.jsonNodeOf(processOutput); if (!credentialsJson.isObject()) { throw new IllegalStateException("Process did not return a JSON object."); } JsonNode version = credentialsJson.get("Version"); if (version == null || !version.isInt() || version.asInt() != 1) { throw new IllegalStateException("Unsupported credential version: " + version); } return credentialsJson; }
@Override public Object getField(String fieldName) { JsonNode fn = jn.get(fieldName); if (fn.isContainerNode()) { AtomicInteger idx = new AtomicInteger(0); List<Field> fields = Lists.newArrayList(fn.fieldNames()) .stream() .map(f -> new Field(f, idx.getAndIncrement())) .collect(Collectors.toList()); return new GenericJsonRecord(fields, fn); } else if (fn.isBoolean()) { return fn.asBoolean(); } else if (fn.isInt()) { return fn.asInt(); } else if (fn.isFloatingPointNumber()) { return fn.asDouble(); } else if (fn.isDouble()) { return fn.asDouble(); } else { return fn.asText(); } } }
/** Shared configuration code for all polling graph updaters. */ @Override final public void configure (Graph graph, JsonNode config) throws Exception { pollingPeriodSeconds = config.path("frequencySec").asInt(60); type = config.path("type").asText(""); // Additional configuration for the concrete subclass configurePolling(graph, config); } }
@Override public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) { if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations() && (node.has("minItems") || node.has("maxItems"))) { JAnnotationUse annotation = field.annotate(Size.class); if (node.has("minItems")) { annotation.param("min", node.get("minItems").asInt()); } if (node.has("maxItems")) { annotation.param("max", node.get("maxItems").asInt()); } } return field; }
/** * @param content Content to check. */ private JsonNode jsonResponse(String content) throws IOException { assertNotNull(content); assertFalse(content.isEmpty()); JsonNode node = JSON_MAPPER.readTree(content); assertFalse(node.get("affinityNodeId").asText().isEmpty()); assertEquals(0, node.get("successStatus").asInt()); assertTrue(node.get("error").isNull()); assertTrue(node.get("sessionToken").isNull()); return node.get("response"); }
@GET @Timed @Path("/health") @ApiOperation(value = "Get cluster and shard health overview") @RequiresPermissions(RestPermissions.INDEXERCLUSTER_READ) @Produces(MediaType.APPLICATION_JSON) public ClusterHealth clusterHealth() { final JsonNode health = cluster.health() .orElseThrow(() -> new InternalServerErrorException("Couldn't read Elasticsearch cluster health")); final ClusterHealth.ShardStatus shards = ClusterHealth.ShardStatus.create( health.path("active_shards").asInt(), health.path("initializing_shards").asInt(), health.path("relocating_shards").asInt(), health.path("unassigned_shards").asInt()); return ClusterHealth.create(health.path("status").asText().toLowerCase(Locale.ENGLISH), shards); } }
@Override public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) { if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations() && (node.has("minLength") || node.has("maxLength"))) { JAnnotationUse annotation = field.annotate(Size.class); if (node.has("minLength")) { annotation.param("min", node.get("minLength").asInt()); } if (node.has("maxLength")) { annotation.param("max", node.get("maxLength").asInt()); } } return field; }
@Test public void testJsonResponseWithoutSplitCommit() { SegmentCompletionProtocol.Response.Params params = new SegmentCompletionProtocol.Response.Params().withBuildTimeSeconds(BUILD_TIME_MILLIS).withOffset(OFFSET) .withSplitCommit(false).withStatus(SegmentCompletionProtocol.ControllerResponseStatus.COMMIT); SegmentCompletionProtocol.Response response = new SegmentCompletionProtocol.Response(params); JsonNode jsonNode = JsonUtils.objectToJsonNode(response); assertEquals(jsonNode.get("offset").asInt(), OFFSET); assertNull(jsonNode.get("segmentLocation")); assertFalse(jsonNode.get("isSplitCommitType").asBoolean()); assertEquals(jsonNode.get("status").asText(), SegmentCompletionProtocol.ControllerResponseStatus.COMMIT.toString()); assertNull(jsonNode.get("controllerVipUrl")); }