Files
i2p.i2p/apps/routerconsole/java/src/edu/internet2/ndt/JSONUtils.java
zzz e20401373c NDT changes adapted from Vuze/BiglyBT, and changes for I2P.
Vuze/Bigly is based on NDT 3.6.2b, so their changes are
manually merged here.
This rev does not compile, requires swingemu package to follow.
Changes:
- Add external start hook to Tcpbw100 from BiglyBT
- Replace all Swing/AWT classes with emulations from BiglyBT
  (included in next checkin)
- Leave classes in edu.internet2.ndt package rather than
  moving to com.vuze.plugins.mlab heirarchy as Vuze/BiglyBT did
- Change IPv6 support setting to use I2P class for detection
- Change in-progress field to an AtomicBoolean
- Change from System to I2P logging
- Change JSON library package
- Comment out some applet code
- Javadoc fixes
2018-11-13 17:08:56 +00:00

70 lines
2.1 KiB
Java

package edu.internet2.ndt;
import net.minidev.json.JSONObject;
import net.minidev.json.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();
}
}