|
| 1 | +package org.jlab.utils; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.BufferedWriter; |
| 6 | +import java.io.OutputStreamWriter; |
| 7 | +import org.jlab.io.base.DataBank; |
| 8 | +import org.jlab.io.base.DataEvent; |
| 9 | +import org.jlab.jnp.utils.json.JsonArray; |
| 10 | +import org.jlab.jnp.utils.json.JsonObject; |
| 11 | +import org.jlab.jnp.utils.json.JsonValue; |
| 12 | +import org.jlab.jnp.utils.json.PrettyPrint; |
| 13 | +import org.json.JSONObject; |
| 14 | + |
| 15 | +/** |
| 16 | + * Stuff to read and manipulate HIPO banks with JSON objects. |
| 17 | + * @author baltzell |
| 18 | + */ |
| 19 | +public class JsonUtils { |
| 20 | + |
| 21 | + /** |
| 22 | + * Just print it to the screen, nicely. |
| 23 | + * @param json |
| 24 | + */ |
| 25 | + public static void show(JsonObject json) { |
| 26 | + BufferedWriter buffWriter = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 27 | + try { |
| 28 | + json.writeTo(buffWriter,PrettyPrint.indentWithSpaces(4)); |
| 29 | + buffWriter.newLine(); |
| 30 | + buffWriter.flush(); |
| 31 | + } |
| 32 | + catch (IOException e) {} |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Just print it to the screen, nicely. |
| 37 | + * @param bank |
| 38 | + * @param varName |
| 39 | + */ |
| 40 | + public static void show(DataBank bank, String varName) { |
| 41 | + show(read(bank,varName)); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Convenience method to get a JsonObject from a bank. |
| 46 | + * @param bank bank to read |
| 47 | + * @param varName name of byte variable to read |
| 48 | + * @return JSON generated from array of bytes |
| 49 | + */ |
| 50 | + public static JsonObject read(DataBank bank, String varName) { |
| 51 | + return JsonObject.readFrom(new String(bank.getByte(varName))); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * This won't be useful once DataBank.setByte(String,byte[]) is implemented |
| 56 | + * @param bank bank to modify |
| 57 | + * @param varName name of bank byte variable to modify |
| 58 | + * @param contents contents of the variable, one byte per row |
| 59 | + * @return the input bank after modification |
| 60 | + */ |
| 61 | + public static DataBank fill(DataBank bank, String varName, String contents) { |
| 62 | + byte[] bytes = contents.getBytes(); |
| 63 | + for (int row=0; row<bank.rows(); row++) { |
| 64 | + if (row >= bytes.length) { |
| 65 | + break; |
| 66 | + } |
| 67 | + bank.setByte(varName, row, bytes[row]); |
| 68 | + } |
| 69 | + return bank; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Convert a map to JNP's JsonObject |
| 74 | + * WARNING: presumably not generic to extended JSONs, but sufficient for |
| 75 | + * configuration sections of CLARA YAMLs. |
| 76 | + * @param map |
| 77 | + * @return |
| 78 | + */ |
| 79 | + public static JsonObject Map2Json(Map<String,Object> map) { |
| 80 | + JsonObject ret = new JsonObject(); |
| 81 | + for (Map.Entry<String,Object> entry : map.entrySet()) { |
| 82 | + String topKey = entry.getKey(); |
| 83 | + if (entry.getValue() instanceof String) { |
| 84 | + ret.add(topKey, (String)entry.getValue()); |
| 85 | + } |
| 86 | + else if (entry.getValue() instanceof Map) { |
| 87 | + ret.add(topKey,Map2Json((Map)entry.getValue())); |
| 88 | + } |
| 89 | + else { |
| 90 | + throw new RuntimeException("non-String non-Supported."); |
| 91 | + } |
| 92 | + } |
| 93 | + return ret; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Convert from Map to JNP's JsonObject |
| 98 | + * @param json an org.json.JSONObject |
| 99 | + * @return the corresponding org.jlab.jnp.utils.json.JsonObject |
| 100 | + */ |
| 101 | + public static JsonObject JSON2Json(JSONObject json) { |
| 102 | + return Map2Json(json.toMap()); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Convenience method to create a bank containing a JsonObject. |
| 107 | + * @param event event used to generate the bank |
| 108 | + * @param bankName name of bank to create |
| 109 | + * @param varName name of variable to put the JSON object in |
| 110 | + * @param json contents for the variable |
| 111 | + * @return new bank |
| 112 | + */ |
| 113 | + public static DataBank create(DataEvent event, String bankName, String varName, JsonObject json) { |
| 114 | + DataBank bank = event.createBank(bankName, json.toString().length()); |
| 115 | + fill(bank, varName, json.toString()); |
| 116 | + return bank; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Convenience method to create a bank containing a JsonObject. |
| 121 | + * @param event event used to generate the bank |
| 122 | + * @param bankName name of bank to create |
| 123 | + * @param varName name of variable to put the JSON object in |
| 124 | + * @param json contents for the variable |
| 125 | + * @return new bank |
| 126 | + */ |
| 127 | + public static DataBank create(DataEvent event, String bankName, String varName, JSONObject json) { |
| 128 | + return create(event, bankName, varName, JSON2Json(json)); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Convenience method to create a bank containing a JsonObject. |
| 133 | + * @param event event used to generate the bank |
| 134 | + * @param bankName name of bank to create |
| 135 | + * @param varName name of variable to put the JSON object in |
| 136 | + * @param json contents for the variable |
| 137 | + * @return new bank |
| 138 | + */ |
| 139 | + public static DataBank create(DataEvent event, String bankName, String varName, Map json) { |
| 140 | + return create(event, bankName, varName, Map2Json(json)); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * JsonObject's merge method overwrites keys of the same name, this extends. |
| 145 | + * WARNING: presumably not generic to extended JSONs, but sufficient for |
| 146 | + * configuration sections of CLARA YAMLs. |
| 147 | + * @param a |
| 148 | + * @param b |
| 149 | + * @return a+b |
| 150 | + */ |
| 151 | + public static JsonObject add(JsonObject a, JsonObject b) { |
| 152 | + JsonObject ret = new JsonObject(); |
| 153 | + for (String key : a.names()) { |
| 154 | + if (b.names().contains(key)) { |
| 155 | + JsonValue aval = a.get(key); |
| 156 | + JsonValue bval = b.get(key); |
| 157 | + if (aval instanceof JsonObject) { |
| 158 | + JsonObject obj = new JsonObject(); |
| 159 | + JsonObject aobj = new JsonObject((JsonObject)aval); |
| 160 | + JsonObject bobj = new JsonObject((JsonObject)bval); |
| 161 | + for (String anam : aobj.names()) { |
| 162 | + obj.set(anam, aobj.get(anam)); |
| 163 | + } |
| 164 | + for (String bnam : bobj.names()) { |
| 165 | + obj.set(bnam, bobj.get(bnam)); |
| 166 | + } |
| 167 | + ret.set(key, obj); |
| 168 | + } |
| 169 | + else { |
| 170 | + JsonArray arr = new JsonArray(); |
| 171 | + arr.add(a.get(key)); |
| 172 | + arr.add(b.get(key)); |
| 173 | + ret.set(key,arr); |
| 174 | + } |
| 175 | + } |
| 176 | + else { |
| 177 | + ret.set(key, a.get(key)); |
| 178 | + } |
| 179 | + } |
| 180 | + for (String key : b.names()) { |
| 181 | + if (!a.names().contains(key)) { |
| 182 | + ret.set(key, b.get(key)); |
| 183 | + } |
| 184 | + } |
| 185 | + return ret; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Modify event by extending bank by merging new JSON data to existing. |
| 190 | + * If bank doesn't exist, create it. |
| 191 | + * @param event event to get the bank from and put it back into |
| 192 | + * @param bankName name of bank |
| 193 | + * @param varName name of variable within bank |
| 194 | + * @param extension JSON object to extend with |
| 195 | + * @return |
| 196 | + */ |
| 197 | + public static DataBank extend(DataEvent event, String bankName, String varName, JsonObject extension) { |
| 198 | + JsonObject contents; |
| 199 | + if (event.hasBank(bankName)) { |
| 200 | + contents = add(read(event.getBank(bankName), varName),extension); |
| 201 | + event.removeBank(bankName); |
| 202 | + } |
| 203 | + else { |
| 204 | + contents = new JsonObject(extension); |
| 205 | + } |
| 206 | + DataBank bank = create(event, bankName, varName, contents); |
| 207 | + event.appendBank(bank); |
| 208 | + return bank; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Modify event by extending bank by merging new JSON data to existing. |
| 213 | + * If bank doesn't exist, create it. |
| 214 | + * @param event event to get the bank from and put it back into |
| 215 | + * @param bankName name of bank |
| 216 | + * @param varName name of variable within bank |
| 217 | + * @param extension JSON object to extend with |
| 218 | + * @return |
| 219 | + */ |
| 220 | + public static DataBank extend(DataEvent event, String bankName, String varName, JSONObject extension) { |
| 221 | + return extend(event, bankName, varName, JSON2Json(extension)); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Modify event by extending bank by merging new JSON data to existing. |
| 226 | + * If bank doesn't exist, create it. |
| 227 | + * @param event event to get the bank from and put it back into |
| 228 | + * @param bankName name of bank |
| 229 | + * @param varName name of variable within bank |
| 230 | + * @param extension JSON object to extend with |
| 231 | + * @return |
| 232 | + */ |
| 233 | + public static DataBank extend(DataEvent event, String bankName, String varName, Map<String,Object> extension) { |
| 234 | + return extend(event, bankName, varName, Map2Json(extension)); |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Emulate the way CLARA parses the full YAML and presents it in EngineData. |
| 239 | + * The "global" and "service" subsections in the "configuration" section get |
| 240 | + * squashed into one namespace, and service-specific keys override any |
| 241 | + * globals of the same name. |
| 242 | + * |
| 243 | + * @param claraJson the full CLARA YAML contents |
| 244 | + * @param serviceName the name of the service in CLARA YAML (not class name) |
| 245 | + * @return data in the format the given CLARA service would see |
| 246 | + */ |
| 247 | + public static JSONObject filterClaraYaml(JSONObject claraJson, String serviceName) { |
| 248 | + JSONObject ret = new JSONObject(); |
| 249 | + if (claraJson.has("configuration")) { |
| 250 | + JSONObject config = claraJson.getJSONObject("configuration"); |
| 251 | + if (config.has("global")) { |
| 252 | + JSONObject globals = config.getJSONObject("global"); |
| 253 | + for (String key : globals.keySet()) { |
| 254 | + ret.accumulate(key, globals.getString(key)); |
| 255 | + } |
| 256 | + } |
| 257 | + if (config.has("services")) { |
| 258 | + if (config.getJSONObject("services").has(serviceName)) { |
| 259 | + JSONObject service = config.getJSONObject("services").getJSONObject(serviceName); |
| 260 | + for (String key : service.keySet()) { |
| 261 | + ret.put(key, service.getString(key)); |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | + return ret; |
| 267 | + } |
| 268 | + |
| 269 | +} |
0 commit comments