protected static void fromJson( String value, Set<String> fields, Map<String, ByteIterator> result) throws IOException { JsonNode json = MAPPER.readTree(value); boolean checkFields = fields != null && !fields.isEmpty(); for (Iterator<Map.Entry<String, JsonNode>> jsonFields = json.getFields(); jsonFields.hasNext(); /* increment in loop body */) { Map.Entry<String, JsonNode> jsonField = jsonFields.next(); String name = jsonField.getKey(); if (checkFields && !fields.contains(name)) { continue; } JsonNode jsonValue = jsonField.getValue(); if (jsonValue != null && !jsonValue.isNull()) { result.put(name, new StringByteIterator(jsonValue.asText())); } } }
/** * Instantiate a new keystore using the file at the provided path */ public JsonCredentialStore(Path path, KeyToStringCodec codec) throws IOException { credentials = new HashMap<>(); FileSystem fs = path.getFileSystem(new Configuration()); try (InputStream in = fs.open(path)) { ObjectMapper jsonParser = defaultMapper; JsonNode tree = jsonParser.readTree(in); if (!tree.isObject()) { throw new IllegalArgumentException("Json in " + path.toString() + " is not an object!"); } Iterator<Map.Entry<String, JsonNode>> it = tree.getFields(); while (it.hasNext()) { Map.Entry<String, JsonNode> field = it.next(); String keyId = field.getKey(); byte[] key = codec.decodeKey(field.getValue().getTextValue()); credentials.put(keyId, key); } } log.info("Initialized keystore from {} with {} keys", path.toString(), credentials.size()); }
public static void exportToMap(final JsonNode node, final Map<String, Object> output, final Predicate<String> fieldFilter) { node.getFields().forEachRemaining(field -> { if (fieldFilter.test(field.getKey())) output.put(field.getKey(), field.getValue()); }); } }
public Iterator<Map.Entry<String, JsonRepresentation>> mapIterator() { ensureIsAMap(); return Iterators.transform(jsonNode.getFields(), MAP_ENTRY_JSON_NODE_TO_JSON_REPRESENTATION); }
public Iterator<Map.Entry<String, JsonRepresentation>> mapIterator() { ensureIsAMap(); return Iterators.transform(jsonNode.getFields(), MAP_ENTRY_JSON_NODE_TO_JSON_REPRESENTATION); }
private static Map<String, String> readMetadata(File metadataPath, ObjectMapper mapper) throws IOException { if (metadataPath.exists()) { Map<String, String> ret = Maps.newLinkedHashMap(); JsonNode node = mapper.readTree(Files.toString(metadataPath, Charsets.UTF_8)); Iterator<Map.Entry<String,JsonNode>> iterator = node.getFields(); while (iterator.hasNext()) { Map.Entry<String, JsonNode> entry = iterator.next(); ret.put(entry.getKey(), entry.getValue().getTextValue()); } return ret; } return null; }
private static Map<String, String> readMetadata(File metadataPath, ObjectMapper mapper) throws IOException { if (metadataPath.exists()) { Map<String, String> ret = Maps.newLinkedHashMap(); JsonNode node = mapper.readTree(Files.toString(metadataPath, Charsets.UTF_8)); Iterator<Map.Entry<String,JsonNode>> iterator = node.getFields(); while (iterator.hasNext()) { Map.Entry<String, JsonNode> entry = iterator.next(); ret.put(entry.getKey(), entry.getValue().getTextValue()); } return ret; } return null; }
private Strings(String jsonLocation) { map = new HashMap<String, Map<String, String>>(); String json = Tools.readFile(jsonLocation); JsonNode node = Tools.jsonToNode(json); Map<String, String> innerMap = new HashMap<String, String>(); // Iterate over all the string fields JsonNode s = node.get("strings"); Iterator<Entry<String, JsonNode>> sIt = s.getFields(); while (sIt.hasNext()) { Entry<String, JsonNode> e = sIt.next(); innerMap.put(e.getKey(), e.getValue().asText()); } map.put("strings", innerMap); }
private Map<String, Set<String>> extractTextProperties(JsonNode node) { Map<String, Set<String>> resultMap = new HashMap<String, Set<String>>(); // get an iterator to the fields Iterator<Entry<String, JsonNode>> nodeIterator = node.getFields(); while ( nodeIterator.hasNext() ) { Entry<String, JsonNode> oneEntry = nodeIterator.next(); String key = oneEntry.getKey(); Set<String> valueSet = new HashSet<String>(); JsonNode valueNodes = oneEntry.getValue(); if ( valueNodes != null ) { for ( JsonNode oneValue : valueNodes ) { if ( oneValue != null ) { valueSet.add( extractTextNode( oneValue ) ); } } } resultMap.put( key, valueSet ); } return resultMap; }
@Override protected List<Pair> getPairsFromAPI() throws IOException { List<Pair> pairs = new ArrayList<Pair>(); Iterator<Map.Entry<String,JsonNode>> elements = readJsonFromUrl(API_URL).getFields(); String[] split; for(Map.Entry<String,JsonNode> entry; elements.hasNext();) { entry = elements.next(); if(!entry.getValue().get("last_price").asText().isEmpty()) { split = entry.getKey().split("_"); pairs.add(new Pair(split[0], split[1])); } } return pairs; }
/** * Instantiate a new keystore using the file at the provided path */ public JsonCredentialStore(Path path, KeyToStringCodec codec) throws IOException { credentials = new HashMap<>(); FileSystem fs = path.getFileSystem(new Configuration()); try (InputStream in = fs.open(path)) { ObjectMapper jsonParser = defaultMapper; JsonNode tree = jsonParser.readTree(in); if (!tree.isObject()) { throw new IllegalArgumentException("Json in " + path.toString() + " is not an object!"); } Iterator<Map.Entry<String, JsonNode>> it = tree.getFields(); while (it.hasNext()) { Map.Entry<String, JsonNode> field = it.next(); String keyId = field.getKey(); byte[] key = codec.decodeKey(field.getValue().getTextValue()); credentials.put(keyId, key); } } log.info("Initialized keystore from {} with {} keys", path.toString(), credentials.size()); }
/** * Instantiate a new keystore using the file at the provided path */ public JsonCredentialStore(Path path, KeyToStringCodec codec) throws IOException { credentials = new HashMap<>(); FileSystem fs = path.getFileSystem(new Configuration()); try (InputStream in = fs.open(path)) { ObjectMapper jsonParser = defaultMapper; JsonNode tree = jsonParser.readTree(in); if (!tree.isObject()) { throw new IllegalArgumentException("Json in " + path.toString() + " is not an object!"); } Iterator<Map.Entry<String, JsonNode>> it = tree.getFields(); while (it.hasNext()) { Map.Entry<String, JsonNode> field = it.next(); String keyId = field.getKey(); byte[] key = codec.decodeKey(field.getValue().getTextValue()); credentials.put(keyId, key); } } log.info("Initialized keystore from {} with {} keys", path.toString(), credentials.size()); }
JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(obj); JSONArray lang = (JSONArray) jsonObject.get("Headlines"); for (int i = 0; i < lang.size(); i++) { JSONObject jsonobject = (JSONObject) lang.get(i); Object subArray = jsonobject.get("Tags"); ObjectMapper mapperNew = new ObjectMapper(); JsonFactory factoryNew = mapperNew.getJsonFactory(); JsonParser jpNew; System.out.println("sub Array " + subArray.toString()); jpNew = factoryNew.createJsonParser(subArray.toString()); JsonNode inputNew = mapperNew.readTree(jpNew); for (final JsonNode elementNew : inputNew) { Iterator<Map.Entry<String, JsonNode>> nodeIterator3 = elementNew.getFields(); while (nodeIterator3.hasNext()) { Map.Entry<String, JsonNode> entry3 = (Map.Entry<String, JsonNode>) nodeIterator3.next(); if (entry3.getKey() != null && entry3.getKey().equals("TagType")) { System.out.println("TagType " + entry3.getValue()); } if (entry3.getKey() != null && entry3.getKey().equals("TagValues")) { System.out.println("TagValues " + entry3.getValue()); } } }
private static Map<String, Object> extractMeasuresFromComplexType(final String parentMeasureName, final JsonNode jsonNode) { final Map<String, Object> unwrappedValues = new HashMap<String, Object>(); if (jsonNode.isContainerNode()) { final Iterator<Entry<String, JsonNode>> i = jsonNode.getFields(); while (i.hasNext()) { final Entry<String, JsonNode> childEntry = i.next(); unwrappedValues.putAll(extractMeasuresFromComplexType(parentMeasureName + "." + childEntry.getKey(), childEntry.getValue())); } } else { final String measureName = parentMeasureName; try { final Object value = getSimpleValue(jsonNode.asText()); unwrappedValues.put(measureName, value); } catch (final ParseException pe) { // probably String or some non-numeric value. Pass } } return unwrappedValues; }
private static CompositeType deserializeCompositeType(final JsonNode json) throws JsonProcessingException { final String typeName = json.get(TYPE_NAME_FIELD).asText(); final String description = json.get(DESCRIPTION_FIELD).asText(); final List<String> itemNames = new LinkedList<>(); final List<String> itemDescriptions = new LinkedList<>(); final List<OpenType<?>> itemTypes = new LinkedList<>(); final Iterator<Map.Entry<String, JsonNode>> items = json.get(ITEMS_FIELD).getFields(); while (items.hasNext()) { final Map.Entry<String, JsonNode> itemEntry = items.next(); itemNames.add(itemEntry.getKey()); itemDescriptions.add(itemEntry.getValue().get(DESCRIPTION_FIELD).asText()); itemTypes.add(deserialize(itemEntry.getValue().get(TYPE_FIELD))); } final String[] EMPTY_STRING_ARRAY = new String[0]; final OpenType<?>[] EMPTY_TYPE_ARRAY = new OpenType<?>[0]; try { return new CompositeType(typeName, description, itemNames.toArray(EMPTY_STRING_ARRAY), itemDescriptions.toArray(EMPTY_STRING_ARRAY), itemTypes.toArray(EMPTY_TYPE_ARRAY)); } catch (final OpenDataException e) { throw new OpenTypeProcessingException(e); } }
/** Very simple way to "erase" passwords - * replaces all field values whose names contains {@code 'pass'} by {@code 'xxxxx'}. */ private static JsonNode secure(JsonNode node) { if (node.isObject()) { ObjectNode objectNode = (ObjectNode)node; Iterator<Map.Entry<String, JsonNode>> fields = node.getFields(); while (fields.hasNext()) { Map.Entry<String, JsonNode> field = fields.next(); if (field.getValue().isTextual() && field.getKey().toLowerCase().contains("pass")) { objectNode.put(field.getKey(), "xxxxx"); } else { secure(field.getValue()); } } return objectNode; } else if (node.isArray()) { ArrayNode arrayNode = (ArrayNode)node; Iterator<JsonNode> elements = arrayNode.getElements(); while (elements.hasNext()) { secure(elements.next()); } return arrayNode; } else { return node; } }
@Override VariableQueryNode parse() throws ParseException { final VariableQueryNode queryNode = new VariableQueryNode(); final JsonNode value = node.path(this.getProperty()); if (value.getFields().hasNext()) { throw new ParseException("Node variable must contain an empty object as value."); } return queryNode; }
Iterator<Entry<String, JsonNode>> nodeIterator = node.getFields(); while( nodeIterator.hasNext() ) { Entry<String, JsonNode> oneEntry = nodeIterator.next();
private static Object jsonNodeAsSimpleObject(JsonNode value) { Object o = null; if (value.isTextual()) { o = value.asText(); } else if (value.isArray()) { ArrayList<Object> arrayList = new ArrayList<>(); for (Iterator<JsonNode> iter = ((ArrayNode) value).getElements(); iter .hasNext();) { arrayList.add(jsonNodeAsSimpleObject(iter.next())); } o = arrayList.toArray(); } else if (value.isNull()) { o = null; } else if (value.isObject()) { HashMap<String, Object> map = new HashMap<String, Object>(); final Iterator<Entry<String, JsonNode>> fields = value.getFields(); while (fields.hasNext()) { final Entry<String, JsonNode> next = fields.next(); map.put(next.getKey(), jsonNodeAsSimpleObject(next.getValue())); } o = map; } else throw new RuntimeException("Unknown type: " + value); return o; }
private void readProperties(ObjectNode node) throws JsonSchemaValidationException { JsonNode propsNode = node.get("properties"); if (propsNode == null) { return; } if (!propsNode.isObject()) { throw new JsonSchemaValidationException("Invalid properties property - not object type", "", propsNode, null); } Iterator<Map.Entry<String,JsonNode>> fields = propsNode.getFields(); while (fields.hasNext()) { Map.Entry<String, JsonNode> field = fields.next(); if (propertySchemas.containsKey(field.getKey())) { throw new JsonSchemaValidationException("Invalid properties property - property specified twice: " + field, "", propsNode, null); } JsonNode value = field.getValue(); if (!value.isObject()) { throw new JsonSchemaValidationException("Invalid properties property - value type not object: " + value, "", propsNode, null); } propertySchemas.put(field.getKey(), new JsonSchemaObjectNode((ObjectNode) value)); } }