Files
i2p.i2p/apps/routerconsole/java/src/edu/internet2/ndt/JSONUtils.java
zzz 54184f2889 Util: Switch users of net.minidev.json to com.json.simple
Tested with bundled 1.1.1 and Debian/Ubuntu 2.3.0
2018-11-19 14:48:21 +00:00

70 lines
2.1 KiB
Java

package edu.internet2.ndt;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import java.util.Iterator;
import java.util.Map;
/**
* Created by Sebastian Malecki on 13.05.14.
*/
public class JSONUtils {
/**
* Function that return value from json object represented by jsontext containing a single message
* which is assigned to "msg" key.
* @param jsonTxt {String} JSON object
* @return {int} obtained value from JSON object
*/
public static String getSingleMessage(String jsonTxt) {
return getValueFromJsonObj(jsonTxt, "msg");
}
/**
* Function that return value for given key from json object represented by jsontext
* @param jsonTxt {String} JSON object
* @param key {int} key by which value should be obtained from JSON map
* @return {int} obtained value from JSON map
*/
public static String getValueFromJsonObj(String jsonTxt, String key) {
JSONValue jsonParser = new JSONValue();
Map json = (Map)jsonParser.parse(new String(jsonTxt));
Iterator iter = json.entrySet().iterator();
while(iter.hasNext()){
Map.Entry entry = (Map.Entry)iter.next();
if (entry.getKey().equals(key)) {
return entry.getValue().toString();
}
}
return null;
}
/**
* Function that add new value to JSON map
* @param jsonTxt {String} JSON object
* @param key {String} key by which value should be assigned to JSON map
* @param value {String} value for given key
* @return {String} json object with added value.
*/
public static String addValueToJsonObj(String jsonTxt, String key, String value) {
JSONValue jsonParser = new JSONValue();
JSONObject json = (JSONObject)jsonParser.parse(new String(jsonTxt));
json.put(key, value);
return json.toJSONString();
}
/**
* Function that return json object represented by jsontext and included
* single message assigned to "msg" key
* @param msg {byte[]} message which should be assigned to json object
* @return {byte[]} json object represented by jsontext and encodes into a sequence of bytes
*/
public static byte[] createJsonObj(byte[] msg) {
JSONObject obj = new JSONObject();
obj.put("msg", new String(msg));
return obj.toJSONString().getBytes();
}
}