All Downloads are FREE. Search and download functionalities are using the official Maven repository.

digital.toke.accessor.Data Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/*
 * SPDX-License-Identifier: Apache-2.0
 * Copyright 2019 David R. Smith All Rights Reserved 
 */
package digital.toke.accessor;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONObject;

/**
 * Use with KVv1 and KVv2 read operations
 * 
 * @author David R. Smith <[email protected]>
 *
 */
public class Data extends Accessor {
	

	public Data(Toke resp) {
		super(resp);
	}
	
	public Map map() {
		Map map = new HashMap();
		
		JSONObject top = json();
		JSONObject data = top.optJSONObject("data");
		if(data == null) {
			return map;
		}else {
			JSONObject inner = data.optJSONObject("data");
			if(inner != null) {
				data = inner;
			}
		}
		Iterator keys = data.keys();
		while(keys.hasNext()) {
			String key = keys.next();
			map.put(key, data.get(key));
		}
		
		return map;
	}
	
	/**
	 * Use on KVv1 and KVv2 reads
	 * 
	 * @return
	 */
	public Map metadata() {
		Map map = new HashMap();
		
		JSONObject top = json();
		JSONObject data = top.optJSONObject("data");
		if(data == null) {
			return map;
		}else {
			JSONObject inner = data.optJSONObject("metadata");
			if(inner != null) {
				data = inner;
			}
		}
		Iterator keys = data.keys();
		while(keys.hasNext()) {
			String key = keys.next();
			map.put(key, data.get(key));
		}
		
		return map;
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy