/** * Change encoding of uuids from <b>latin1 (ISO-8859-1)</b> to <b>base64</b>. * * @param json the json that should be processed * @return the json with changed uuids * @throws IOException the io exception */ public static JsonNode encodeUuids(JsonNode json) throws IOException { if (json.has(UUID_FIELD)) { JsonNode jsonNode = json.get(UUID_FIELD); if (jsonNode.has(UUID_VALUE)) { String value = jsonNode.get(UUID_VALUE).asText(); String encodedValue = Base64.getEncoder().encodeToString(value.getBytes("ISO-8859-1")); ((ObjectNode) jsonNode).put(UUID_VALUE, encodedValue); } } for (JsonNode node : json) { if (node.isContainerNode()) { encodeUuids(node); } } return json; }
@Override public Iterable<RecordWithMetadata<?>> convertRecord(Object outputSchema, RecordWithMetadata<byte[]> inputRecord, WorkUnitState workUnit) throws DataConversionException { try { try (JsonParser parser = jsonFactory.createJsonParser(inputRecord.getRecord())) { parser.setCodec(objectMapper); JsonNode jsonNode = parser.readValueAsTree(); // extracts required record if (!jsonNode.has(RECORD_KEY)) { throw new DataConversionException("Input data does not have record."); } String record = jsonNode.get(RECORD_KEY).getTextValue(); // Extract metadata field Metadata md = new Metadata(); if (jsonNode.has(METADATA_KEY) && jsonNode.get(METADATA_KEY).has(METADATA_RECORD_KEY)) { md.getRecordMetadata().putAll(objectMapper.readValue(jsonNode.get(METADATA_KEY).get(METADATA_RECORD_KEY), Map.class)); } return Collections.singleton(new RecordWithMetadata<>(record, md)); } } catch (IOException e) { throw new DataConversionException(e); } }
@Test public void shouldExcludeLegacyFormatIfAsked() throws Exception { // Given ExceptionRepresentation rep = new ExceptionRepresentation( new KernelException( UnknownError, "Hello" ) { }, /*legacy*/false ); // When JsonNode out = serialize( rep ); // Then assertThat(out.get("errors").get(0).get("code").asText(), equalTo("Neo.DatabaseError.General.UnknownError")); assertThat(out.get("errors").get(0).get("message").asText(), equalTo("Hello")); assertThat(out.has( "message" ), equalTo(false)); }
for (Field field : schema.getFields()) if (!isValidDefault(field.schema(), defaultValue.has(field.name()) ? defaultValue.get(field.name()) : field.defaultValue()))
/** * Removes UUIDs from a json tree. * * @param json json tree node */ public static void removeUuids(JsonNode json) { boolean containerWithId = json.isContainerNode() && json.has(UUID_FIELD); boolean isArray = json.isArray(); boolean childIsNotArray = !(json.size() == 1 && json.getElements().next().isArray()); if (containerWithId && !isArray && childIsNotArray) { ((ObjectNode) json).remove(UUID_FIELD); } for (JsonNode node : json) { if (node.isContainerNode()) { removeUuids(node); } } } }
assertEquals( "one entry", initialData + 1, data.size() ); JsonNode entry = data.get( 0 ); assertTrue( "entry has row", entry.has( "row" ) ); assertTrue( "entry has graph", entry.has( "graph" ) ); JsonNode nodes = entry.get( "graph" ).get( "nodes" ); JsonNode rels = entry.get( "graph" ).get( "relationships" );
/** * Checks if the JSON node contains the properties (not null). * * @param n JSON node to check * @param name Name of the property */ public static void validateJsonRequired(JsonNode n, String name) throws Exception { if (!n.has(name)) { throw new Exception(MessageFormat.format("{0} must be set", name)); } }
private static OpenType<?> deserializeComplexType(final JsonNode json) throws JsonProcessingException{ if(json.has(IS_PRIMITIVE_FIELD) && json.has(ARRAY_ELEMENT_FIELD) && json.has(DIMENSIONS_FIELD)) //array type return deserializeArrayType(json); else if(json.has(ITEMS_FIELD)) //composite type return deserializeCompositeType(json); else if(json.has(INDEX_FIELD) && json.has(ROW_TYPE_FIELD)) //tabular type return deserializeTabularType(json); else throw new OpenTypeProcessingException(); }
private JsonNode valueNode(JsonNode element) { if (element.has(KeyValueTokens.VALUE_TOKEN)) { return element.path(KeyValueTokens.VALUE_TOKEN); } else { throw new IllegalArgumentException("Given dataset does not contain "+KeyValueTokens.VALUE_TOKEN+" token."); } }
private String implementationKeyClass(JsonNode element) { String implementationValue = NO_IMPLEMENTATION_PROVIDED; if (element.has(KeyValueTokens.IMPLEMENTATION_KEY_TOKEN)) { implementationValue = element.path(KeyValueTokens.IMPLEMENTATION_KEY_TOKEN).getTextValue(); } return implementationValue.trim(); }
private String implementationClass(JsonNode element) { String implementationValue = NO_IMPLEMENTATION_PROVIDED; if (element.has(KeyValueTokens.IMPLEMENTATION_TOKEN)) { implementationValue = element.path(KeyValueTokens.IMPLEMENTATION_TOKEN).getTextValue(); } return implementationValue.trim(); }
@Override protected String getTicker(Pair pair) throws IOException { if(!pairs.contains(pair)) throw new IOException("Invalid pair: " + pair); JsonNode node = readJsonFromUrl("https://api.coinsecure.in/v0/noauth/lasttrade"); if(node.has("error")) throw new IOException(node.get("error").asText()); else return parseTicker(node, pair); }
private Object keyValue(JsonNode element) throws JsonParseException, JsonMappingException, IOException, ClassNotFoundException { Object keyValue; if (element.has(KeyValueTokens.KEY_TOKEN)) { String implementationKey = implementationKeyClass(element); keyValue = readElement(element.path(KeyValueTokens.KEY_TOKEN), implementationKey); } else { throw new IllegalArgumentException("Given dataset does not contain "+KeyValueTokens.KEY_TOKEN+" token."); } return keyValue; }
@Override protected String getTicker(Pair pair) throws IOException { JsonNode node = readJsonFromUrl(peer + "getTrades&lastIndex=0&asset=" + pair.getMarket()); if(node.has("errorCode")) throw new IOException(node.get("errorDescription").asText()); return parseTicker(node, pair); }
@Override protected String getTicker(Pair pair) throws IOException { // https://api.bitso.com/v2/ticker?book=btc_mxn JsonNode node = readJsonFromUrl("https://api.bitso.com/v2/ticker?book=" + pair.getCoin() + "_" + pair.getExchange()); if(node.has("error")) throw new IOException(node.get("error").get("message").asText()); return parseTicker(node, pair); }
@Override protected String getTicker(Pair pair) throws IOException { JsonNode node = readJsonFromUrl("https://coinut.com/api/tick/" + pair.getCoin() + pair.getExchange()); if(node.has("error")) { throw new IOException(node.get("error").asText()); } else { return parseTicker(node, pair); } }
@Override protected String getTicker(Pair pair) throws IOException { JsonNode node = readJsonFromUrl("https://www.galmarley.com/prices/prices.json?version=v2&interval=5&batch=Update&securityId=" + pair.getCoin() + "&valuationSecurityId=" + pair.getExchange()); if(node.has("latestPrice")) { return parseTicker(node, pair); } else { throw new IOException("Invalid pair: " + pair); } }