protected void assertNodeNumbers(JsonNode n, int expInt, double expDouble) { assertEquals(expInt, n.asInt()); assertEquals(expInt, n.asInt(-42)); assertEquals((long) expInt, n.asLong()); assertEquals((long) expInt, n.asLong(19L)); assertEquals(expDouble, n.asDouble()); assertEquals(expDouble, n.asDouble(-19.25)); }
protected void assertNodeNumbersForNonNumeric(JsonNode n) { assertFalse(n.isNumber()); assertEquals(0, n.asInt()); assertEquals(-42, n.asInt(-42)); assertEquals(0, n.asLong()); assertEquals(12345678901L, n.asLong(12345678901L)); assertEquals(0.0, n.asDouble()); assertEquals(-19.25, n.asDouble(-19.25)); }
public static void secondSituation(String ok) throws IOException { byte[] jsonData = ok.getBytes(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(jsonData); JsonNode idNode = rootNode.path("id"); System.out.println("id = " + idNode.asInt()); JsonNode phoneNosNode = rootNode.path("cardNo"); System.out.println("cardNo = " + phoneNosNode.asInt()); }
@Override public void create(Record record, JsonNode json, Class clazz, String dir) throws Exception { for (Field f : clazz.getFields()) { if (!json.has(f.getName())) return; JsonNode j = json.get(f.getName()); JsonField field = f.getAnnotation(JsonField.class); JsonStruc struc = f.getAnnotation(JsonStruc.class); if (field != null) { SubRecordData sub = record.get(field.value()[0]); if (f.getType() == String.class) ((SubZString) sub).value = j.asText(); else if (f.getType() == JsonFile.class) ((SubZString) sub).value = (j.asText().startsWith("/") ? j.asText().substring(1) : dir + "/" + j.asText()) .replace("/", "\\"); else if (f.getType() == int[].class) for (int i = 0; i < sub.size(); i++) ((SubIntArray) sub).value[i] = j.get(i).intValue(); else if (f.getType() == JsonFormID[].class) { if (field.value()[1] != null) record.get(field.value()[1]); ((SubFormIDList) sub).value.clear(); for (int i = 0; i < sub.size(); i++) ((SubFormIDList) sub).value.add(getFormId(j.get(i), field.value(), 2)); } } if (struc != null) { SubRecordData sub = record.get(struc.value()[0]); Field subf = sub.getClass().getField(struc.value()[1]); if (f.getType() == int.class) subf.setInt(sub, j.asInt()); else if (f.getType() == float.class) subf.setFloat(sub, (float) j.asDouble()); } } }
public Object unwrap(Object o) { if (o == null) { return null; } if (!(o instanceof JsonNode)) { return o; } JsonNode e = (JsonNode) o; if (e.isValueNode()) { if (e.isTextual()) { return e.asText(); } else if (e.isBoolean()) { return e.asBoolean(); } else if (e.isInt()) { return e.asInt(); } else if (e.isLong()) { return e.asLong(); } else if (e.isBigDecimal()) { return e.decimalValue(); } else if (e.isDouble()) { return e.doubleValue(); } else if (e.isFloat()) { return e.floatValue(); } else if (e.isBigDecimal()) { return e.decimalValue(); } else if (e.isNull()) { return null; } } return o; }
/** * Accepts an JSON String and turns the data object within this JSON String into an object of type * Study. It can handle different versions of the study model. The version is determined by the * version field in the JSON string. Each supported study version has its own model which is used * for unmarshaling. */ @Override protected Study concreteUnmarshaling(String jsonStr) throws IOException { JsonNode node = JsonUtils.OBJECTMAPPER.readTree(jsonStr).findValue(JsonUtils.VERSION); int version = node.asInt(); if (version > Study.SERIAL_VERSION) { throw new IOException(MessagesStrings.TOO_NEW_STUDY_VERSION); } switch (version) { case 0: case 2: // Version 2 node = JsonUtils.OBJECTMAPPER.readTree(jsonStr).findValue(JsonUtils.DATA); StudyV2 studyV2 = JsonUtils.OBJECTMAPPER.treeToValue(node, StudyV2.class); study = bindV2(studyV2); break; case 3: // Current version node = JsonUtils.OBJECTMAPPER.readTree(jsonStr).findValue(JsonUtils.DATA); study = JsonUtils.OBJECTMAPPER.treeToValue(node, Study.class); break; default: throw new IOException(MessagesStrings.UNSUPPORTED_STUDY_VERSION); } return study; }
/** * Matches the contents of a push header instruction. * * @param instructionJson JSON instruction to match * @param description Description object used for recording errors * @return true if contents match, false otherwise */ private boolean matchModMplsHeaderInstruction(JsonNode instructionJson, Description description) { ModMplsHeaderInstruction instructionToMatch = (ModMplsHeaderInstruction) instruction; final String jsonSubtype = instructionJson.get("subtype").textValue(); if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { description.appendText("subtype was " + jsonSubtype); return false; } final String jsonType = instructionJson.get("type").textValue(); if (!instructionToMatch.type().name().equals(jsonType)) { description.appendText("type was " + jsonType); return false; } final JsonNode ethJson = instructionJson.get("ethernetType"); if (ethJson == null) { description.appendText("ethernetType was not null"); return false; } if (instructionToMatch.ethernetType().toShort() != ethJson.asInt()) { description.appendText("ethernetType was " + ethJson); return false; } return true; }
/** * Parses an ID. * * @param node * @return */ protected Object parseId(JsonNode node) { if (node == null || node.isNull()) { return null; } else if (node.isDouble()) { return node.asDouble(); } else if (node.isFloatingPointNumber()) { return node.asDouble(); } else if (node.isInt()) { return node.asInt(); } else if (node.isIntegralNumber()) { return node.asInt(); } else if (node.isLong()) { return node.asLong(); } else if (node.isTextual()) { return node.asText(); } throw new IllegalArgumentException("Unknown id type"); }
@Override public Criterion decodeCriterion(ObjectNode json) { JsonNode ethTypeNode = nullIsIllegal( json.get(CriterionCodec.ETH_TYPE), CriterionCodec.ETH_TYPE + MISSING_MEMBER_MESSAGE); int ethType; if (ethTypeNode.isInt()) { ethType = ethTypeNode.asInt(); } else { ethType = Integer.decode(ethTypeNode.textValue()); } return Criteria.matchEthType(ethType); }
public static void firstSituation(String ok) throws IOException { byte[] jsonData = ok.getBytes(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(jsonData); JsonNode idNode = rootNode.path("id"); System.out.println("id = " + idNode.asInt()); JsonNode phoneNosNode = rootNode.path("phoneNumbers"); Iterator<JsonNode> elements = phoneNosNode.elements(); while (elements.hasNext()) { JsonNode phone = elements.next(); System.out.println("phoneNumbers = " + phone.asLong()); } }
public Object extension(JsonNode jsonNode) { if (jsonNode.getNodeType().equals(JsonNodeType.BOOLEAN)) { return jsonNode.asBoolean(); } if (jsonNode.getNodeType().equals(JsonNodeType.STRING)) { return jsonNode.asText(); } if (jsonNode.getNodeType().equals(JsonNodeType.NUMBER)) { NumericNode n = (NumericNode) jsonNode; if (n.isLong()) { return jsonNode.asLong(); } if (n.isInt()) { return jsonNode.asInt(); } if (n.isBigDecimal()) { return jsonNode.textValue(); } if (n.isBoolean()) { return jsonNode.asBoolean(); } if (n.isFloat()) { return jsonNode.floatValue(); } if (n.isDouble()) { return jsonNode.doubleValue(); } if (n.isShort()) { return jsonNode.intValue(); } return jsonNode.asText(); } if (jsonNode.getNodeType().equals(JsonNodeType.ARRAY)) { ArrayNode an = (ArrayNode) jsonNode; List<Object> o = new ArrayList<Object>(); for (JsonNode i : an) { Object obj = extension(i); if (obj != null) { o.add(obj); } } return o; } return jsonNode; }
// Searching for Id as Number in JSON using range query @Test(expected = ClassCastException.class) public void testPOJOcombinedSearchforNumberWithStringHandle() throws JsonProcessingException, IOException { PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class); PojoPage<Artifact> p; this.loadSimplePojos(products); QueryManager queryMgr = client.newQueryManager(); String queryAsString = "{\"search\":{\"query\":{" + "\"range-constraint-query\":{\"constraint-name\":\"id\", \"value\":[5,10,15,20,25,30]}}," + "\"options\":{\"return-metrics\":false, \"constraint\":{\"name\":\"id\", \"range\":{\"type\": \"xs:long\",\"json-property\":\"id\"}}}" + "}}"; PojoQueryDefinition qd = (PojoQueryDefinition) queryMgr.newRawCombinedQueryDefinition( new StringHandle(queryAsString).withFormat(Format.JSON)); StringHandle results = new StringHandle(); JacksonHandle jh = new JacksonHandle(); p = products.search(qd, 1, jh); long pageNo = 1, count = 0; do { count = 0; p = products.search(qd, pageNo, results.withFormat(Format.JSON)); while (p.iterator().hasNext()) { Artifact a = p.iterator().next(); validateArtifact(a); count++; } assertEquals("Page total results", count, p.getTotalSize()); pageNo = pageNo + p.getPageSize(); System.out.println(results.get().toString()); } while (!p.isLastPage() && pageNo < p.getTotalSize()); assertFalse("String handle is not empty", results.get().isEmpty()); assertTrue("String handle contains results", results.get().contains("results")); assertTrue("String handle contains format", results.get().contains("\"format\":\"json\"")); ObjectMapper mapper = new ObjectMapper(); JsonNode actNode = mapper.readTree(results.get()).get("total"); assertEquals("Total search results resulted are ", 6, actNode.asInt()); }
// Searching for Id as Number in JSON using string should not return any results @Test public void testPOJOSearchWithStringHandle() throws JsonProcessingException, IOException { PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class); PojoPage<Artifact> p; this.loadSimplePojos(products); QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("5"); StringHandle results = new StringHandle(); JacksonHandle jh = new JacksonHandle(); p = products.search(qd, 1, jh); long pageNo = 1, count = 0; do { count = 0; p = products.search(qd, pageNo, results.withFormat(Format.JSON)); while (p.iterator().hasNext()) { Artifact a = p.iterator().next(); validateArtifact(a); count++; // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); } assertEquals("Page total results", 0, p.getTotalSize()); pageNo = pageNo + p.getPageSize(); // System.out.println(results.get().toString()); } while (!p.isLastPage() && pageNo < p.getTotalSize()); assertFalse("String handle is not empty", results.get().isEmpty()); assertTrue("String handle contains results", results.get().contains("results")); assertFalse("String handle contains format", results.get().contains("\"format\":\"json\"")); ObjectMapper mapper = new ObjectMapper(); JsonNode actNode = mapper.readTree(results.get()).get("total"); // System.out.println(expNode.equals(actNode)+"\n"+ // expNode.toString()+"\n"+actNode.toString()); assertEquals("Total search results resulted are ", actNode.asInt(), 0); }
/** * This method reads the JSON object array representation of employees from input file and then * navigates the employee objects to find objects that are not having email attribute. This * example update those object with dummy email and writes the modified contents in to * emp-array-modified.json file * * @param jsonFileName * @throws IOException */ public void findAndUpdateNode(String jsonFileName) throws IOException { // read json file InputStream inputStream = getClass().getResourceAsStream(jsonFileName); // create ObjectMapper instance ObjectMapper objectMapper = new ObjectMapper(); // read JSON like DOM Parser JsonNode rootNode = objectMapper.readTree(inputStream); if (rootNode.isArray()) { for (JsonNode objNode : rootNode) { System.out.println(objNode); JsonNode idNode = objNode.path("employeeId"); logger.log(Level.INFO, "id = " + idNode.asInt()); JsonNode emailNode = objNode.path("email"); logger.log(Level.INFO, "email = " + emailNode.textValue()); if (emailNode.textValue() == null) { ((ObjectNode) objNode).put("email", "unknown"); } objectMapper.writeValue(new File("emp-array-modified.json"), rootNode); } } }
@Override JsonNode computeNode(JsonPathContext context, JsonNode[] childValues) { JsonNode o = childValues[0]; JsonNode i = index.eval(context).asNode(); if (i.isTextual()) { if (!o.isObject()) { throw new JsonPathRuntimeException( "field selector must apply on an object, not a " + o.getNodeType().toString().toLowerCase(), position); } return o.path(asString(i, "index of selector")); } if (i.isNumber()) { if (!o.isArray()) { throw new JsonPathRuntimeException( "field selector must apply on an array, not a " + o.getNodeType().toString().toLowerCase(), position); } int n = i.asInt(); if (n >= o.size()) { throw new JsonPathRuntimeException( "index out of bound " + n + " > " + (o.size() - 1), position); } if (n < -o.size()) { throw new JsonPathRuntimeException( "index out of bound " + n + " < " + (-o.size()), position); } if (n < 0) { return o.get(o.size() + n); } return o.get(n); } throw new TypeMismatchException(position, JsonNodeType.NUMBER, o, "the index of the array"); }
@Test public void testLatestBackref() throws Exception { ModelPath a = ModelPath.builder("Container.parent.latset_backref.StatusUpdate|Document|Blog.name.value") .addPathMember( new ModelPathStep( true, newHashSet("StatusUpdate", "Document", "Blog"), "parent", latest_backRef, newHashSet("Container"), null)) .addPathMember( new ModelPathStep( false, newHashSet("StatusUpdate", "Document", "Blog"), null, value, null, Arrays.asList("instanceId"))) .build(); Set<Id> permissions = new HashSet<>(); ViewResponse viewResponse = viewFieldsCollector.getView(permissions); ObjectNode view = viewResponse.getViewBody(); Assert.assertNull(view); viewFieldsCollector.add( viewDescriptor, a, new Id[] {new Id(1), new Id(2)}, new String[] {"Container", "Document"}, new ViewValue(new long[] {1, 2}, "{\"instanceId\":11}".getBytes()), 1L); viewFieldsCollector.add( viewDescriptor, a, new Id[] {new Id(1), new Id(3)}, new String[] {"Container", "StatusUpdate"}, new ViewValue(new long[] {1, 3}, "{\"instanceId\":12}".getBytes()), 2L); viewFieldsCollector.add( viewDescriptor, a, new Id[] {new Id(1), new Id(4)}, new String[] {"Container", "Document"}, new ViewValue(new long[] {1, 5}, "{\"instanceId\":13}".getBytes()), 4L); viewFieldsCollector.add( viewDescriptor, a, new Id[] {new Id(1), new Id(5)}, new String[] {"Container", "Document"}, new ViewValue(new long[] {1, 4}, "{\"instanceId\":14}".getBytes()), 3L); viewFieldsCollector.done(); viewResponse = viewFieldsCollector.getView(permissions); view = viewResponse.getViewBody(); Assert.assertNull(view); permissions.add(new Id(1L)); permissions.add(new Id(2L)); permissions.add(new Id(3L)); permissions.add(new Id(4L)); permissions.add(new Id(5L)); viewResponse = viewFieldsCollector.getView(permissions); view = viewResponse.getViewBody(); System.out.println("view=" + view); JsonNode got = view.get("latest_parent"); Assert.assertTrue(got.isObject()); JsonNode field = got.get("instanceId"); Assert.assertTrue(field.isInt()); Assert.assertEquals(field.asInt(), 13); // added at the highest timestamp }
private JsonNode processEditEntityResponse( final String entityIdentifier, final Observable<Response> createEntityResponse, final String type) throws IOException { final Response response = createEntityResponse.toBlocking().firstOrDefault(null); if (response == null) { final String message = String.format("could not create new %s for '%s'", type, entityIdentifier); LOG.error(message); throw new WikidataImporterError(new WikidataImporterException(message)); } final int status = response.getStatus(); LOG.debug("response status = {}", status); if (status != 200) { final String message = String.format( "could not create new %s for '%s'; response status != 200 (was '%d').", type, entityIdentifier, status); LOG.error(message); throw new WikidataImporterError(new WikidataImporterException(message)); } final String responseBody = response.readEntity(String.class); LOG.debug("response body = {}", responseBody); final ObjectNode responseJSON = MAPPER.readValue(responseBody, ObjectNode.class); if (responseJSON == null) { final String message = String.format( "could not create new %s for '%s'; could not deserialize response.", type, entityIdentifier); LOG.error(message); throw new WikidataImporterError(new WikidataImporterException(message)); } final JsonNode errorNode = responseJSON.get(MEDIAWIKI_ERROR_IDENTIFIER); if (errorNode != null) { final String message = String.format( "could not create new %s for '%s'; an error occurred ('%s').", type, entityIdentifier, responseBody); LOG.debug(message); // return error so that it can be handled at the client return responseJSON; } final JsonNode successNode = responseJSON.get(MEDIAWIKI_SUCCESS_IDENTIFIER); if (successNode == null) { final String message = String.format( "could not create new %s for '%s'; no 'success' node in response ('%s')", type, entityIdentifier, responseBody); LOG.error(message); throw new WikidataImporterError(new WikidataImporterException(message)); } final int success = successNode.asInt(); if (success != 1) { final String message = String.format( "could not create new %s for '%s'; 'success' = '%d'", type, entityIdentifier, success); LOG.error(message); throw new WikidataImporterError(new WikidataImporterException(message)); } final JsonNode entityNode = responseJSON.get(MEDIAWIKI_ENTITY_IDENTIFIER); if (entityNode == null) { final String message = String.format( "could not create new %s for '%s'; no 'entity' node in response ('%s')", type, entityIdentifier, responseBody); LOG.error(message); throw new WikidataImporterError(new WikidataImporterException(message)); } return entityNode; }
@Override public NextObjective decode(ObjectNode json, CodecContext context) { if (json == null || !json.isObject()) { return null; } CoreService coreService = context.getService(CoreService.class); final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class); final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class); ObjectiveCodecHelper och = new ObjectiveCodecHelper(); DefaultNextObjective.Builder baseBuilder = DefaultNextObjective.builder(); final DefaultNextObjective.Builder builder = (DefaultNextObjective.Builder) och.decode(json, baseBuilder, context); // decode id JsonNode idJson = json.get(ID); checkNotNull(idJson); builder.withId(idJson.asInt()); // decode application id ApplicationId appId = coreService.registerApplication(REST_APP_ID); builder.fromApp(appId); // decode type String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText(); switch (typeStr) { case "HASHED": builder.withType(NextObjective.Type.HASHED); break; case "BROADCAST": builder.withType(NextObjective.Type.BROADCAST); break; case "FAILOVER": builder.withType(NextObjective.Type.FAILOVER); break; case "SIMPLE": builder.withType(NextObjective.Type.SIMPLE); break; default: log.warn(INVALID_TYPE_MESSAGE, typeStr); return null; } // decode treatments JsonNode treatmentsJson = json.get(TREATMENTS); checkNotNull(treatmentsJson); if (treatmentsJson != null) { IntStream.range(0, treatmentsJson.size()) .forEach( i -> { ObjectNode treatmentJson = get(treatmentsJson, i); builder.addTreatment(trafficTreatmentCodec.decode(treatmentJson, context)); }); } // decode meta JsonNode metaJson = json.get(META); if (metaJson != null) { TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) metaJson, context); builder.withMeta(trafficSelector); } // decode operation String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText(); NextObjective nextObjective; switch (opStr) { case "ADD": nextObjective = builder.add(); break; case "REMOVE": nextObjective = builder.remove(); break; default: log.warn(INVALID_OP_MESSAGE, opStr); return null; } return nextObjective; }
public SolverSimpleQuery(String query) { try { season = numberOfSeasons = teamID = numberOfSeasons = leagueID = -1; superEff = false; solver = selectedMethod = leagueName = teamName = null; selectedInputs = new ArrayList<>(); selectedInputsNames = new ArrayList<>(); selectedOutputs = new ArrayList<>(); selectedOutputsNames = new ArrayList<>(); ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readTree(query); if (rootNode.has("superEff")) superEff = rootNode.path("superEff").asBoolean(true); if (rootNode.has("inputOriented")) inputOriented = rootNode.path("inputOriented").asBoolean(); if (rootNode.has("season")) season = rootNode.path("season").asInt(); if (rootNode.has("numberOfTeams")) numberOfTeams = rootNode.path("numberOfTeams").asInt(); if (rootNode.has("numberOfSeasons")) numberOfSeasons = rootNode.path("numberOfSeasons").asInt(1); if (rootNode.has("teamID")) teamID = rootNode.path("teamID").asInt(); if (rootNode.has("leagueID")) leagueID = rootNode.path("leagueID").asInt(); if (rootNode.has("solver")) solver = rootNode.path("solver").asText(); if (rootNode.has("selectedMethod")) selectedMethod = rootNode.path("selectedMethod").asText(); if (rootNode.has("leagueName")) leagueName = rootNode.path("leagueName").asText(); if (rootNode.has("teamName")) teamName = rootNode.path("teamName").asText(); if (rootNode.has("selectedInputs")) { JsonNode selectedInputs = rootNode.path("selectedInputs"); Iterator<JsonNode> iterator = selectedInputs.iterator(); while (iterator.hasNext()) { JsonNode input = iterator.next(); this.selectedInputs.add(input.asInt()); } } if (rootNode.has("selectedInputsNames")) { JsonNode selectedInputsNames = rootNode.path("selectedInputsNames"); Iterator<JsonNode> iterator = selectedInputsNames.iterator(); while (iterator.hasNext()) { JsonNode input = iterator.next(); this.selectedInputsNames.add(input.asText()); } } if (rootNode.has("selectedOutputs")) { JsonNode selectedOutputs = rootNode.path("selectedOutputs"); Iterator<JsonNode> iterator = selectedOutputs.iterator(); while (iterator.hasNext()) { JsonNode input = iterator.next(); this.selectedOutputs.add(input.asInt()); } } if (rootNode.has("selectedOutputsNames")) { JsonNode selectedOutputsNames = rootNode.path("selectedOutputsNames"); Iterator<JsonNode> iterator = selectedOutputsNames.iterator(); while (iterator.hasNext()) { JsonNode input = iterator.next(); this.selectedOutputsNames.add(input.asText()); } } // System.out.println("]"); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
private int parsePort(JsonNode node) { return node.isInt() ? node.asInt() : ServerEnvironment.parsePortValue("config", node.asText()); }