/** Access a field of a JSON object, return an {@code int} or a default value. */ public static int getInt(JsonObject obj, String field, int dftValue) { JsonValue jv = obj.get(field); if ( jv == null ) return dftValue; return getInt(jv, dftValue); }
/** For walking structures */ public JsonObject getObj(String key) { return get(key).getAsObject() ; }
/** Access a field of a JSON object : return a {@code long}, or the default value. */ public static long getLong(JsonObject obj, String field, long dftValue) { JsonValue jv = obj.get(field); if ( jv == null ) return dftValue; return getLong(jv, dftValue); }
private static boolean isJsonObjectCompact(JsonObject jsonObject) { Set<String> x = jsonObject.keySet() ; if ( x.size() == 0 ) return true ; if ( x.size() > maxCompactObject ) return false ; for ( String k : x ) { if ( ! isCompactValue(jsonObject.get(k)) ) return false ; } return true ; } private void writeObjectCompact(JsonObject jsonObject, Set<String> x) {
private static boolean isJsonObjectCompact(JsonObject jsonObject) { Set<String> x = jsonObject.keySet() ; if ( x.size() == 0 ) return true ; if ( x.size() > maxCompactObject ) return false ; for ( String k : x ) { if ( ! isCompactValue(jsonObject.get(k)) ) return false ; } return true ; } private void writeObjectCompact(JsonObject jsonObject, Set<String> x) {
private void parseConf(BackupConfig cfg, String cfgFile) { try { JsonObject obj = JSON.read(cfgFile); cfg.port = obj.get(jPort).getAsNumber().value().intValue(); JsonArray a = obj.get(jLogs).getAsArray(); a.forEach(elt-> { BackupArea area = parseLogObject(cfg, elt); cfg.logs.add(area); }); } catch (Exception ex) { throw new CmdException("Failed to process configuration file: "+ex.getMessage()); } }
@Override public List<PatchLogInfo> listPatchLogInfo() { JsonObject obj = rpc(DeltaConst.OP_LIST_LOG_INFO, emptyObject); JsonArray array = obj.get(DeltaConst.F_ARRAY).getAsArray(); List<PatchLogInfo> x = array.stream() .map(jv->PatchLogInfo.fromJson(jv.getAsObject())) .collect(Collectors.toList()) ; return x ; }
@Override public List<DataSourceDescription> listDescriptions() { JsonObject obj = rpc(DeltaConst.OP_LIST_DSD, emptyObject); JsonArray array = obj.get(DeltaConst.F_ARRAY).getAsArray(); List<DataSourceDescription> x = array.stream() .map(jv->getDataSourceDescription(jv.getAsObject())) .collect(Collectors.toList()) ; return x ; }
private static Integer getInt(JsonObject json, String key) { if ( ! json.hasKey(key) ) throw new TDBException("StoreParamsCodec.getInt: no such key: "+key) ; Integer x = json.get(key).getAsNumber().value().intValue() ; return x ; }
public static JsonValue accessPath(JsonValue obj, String ... path) { for ( int i = 0 ; i < path.length ; i++ ) { String p = path[i] ; if ( ! obj.isObject() ) throw new JsonException("Path traverses non-object") ; obj = obj.getAsObject().get(p) ; } return obj ; }
private static Integer getInt(JsonObject json, String key) { if ( ! json.hasKey(key) ) throw new TDBException("StoreParamsCodec.getInt: no such key: "+key) ; Integer x = json.get(key).getAsNumber().value().intValue() ; return x ; }
public static JsonValue accessPath(JsonValue obj, String... path) { for ( String p : path ) { if ( !obj.isObject() ) { throw new JsonException("Path traverses non-object") ; } obj = obj.getAsObject().get(p) ; } return obj ; }
private static String stringOrNull(JsonObject obj, String key) { JsonValue v = obj.get(key); if ( v == null ) return null; if ( !v.isString() ) throw new ResultSetException("Not a string: key: " + key); return v.getAsString().value(); }
private static String getString(JsonObject json, String key) { if ( ! json.hasKey(key) ) throw new TDBException("StoreParamsCodec.getString: no such key: "+key) ; String x = json.get(key).getAsString().value() ; return x ; }
private BackupArea parseLogObject(BackupConfig cfg, JsonValue elt) { String name = elt.getAsObject().get(jName).getAsString().value(); String dir = elt.getAsObject().get(jDir).getAsString().value(); String file = elt.getAsObject().get(jFile).getAsString().value(); if ( name == null || dir == null || file == null ) throw new CmdException("Required: \""+jName+", \""+jDir+"\" and \""+jFile+"\""); return new BackupArea(name, dir, file); }
private static String stringOrNull(JsonObject obj, String key) { JsonValue v = obj.get(key) ; if ( v == null ) return null ; if ( ! v.isString() ) throw new ResultSetException("Not a string: key: "+key) ; return v.getAsString().value(); }
private static String getString(JsonObject json, String key) { if ( ! json.hasKey(key) ) throw new TDBException("StoreParamsCodec.getString: no such key: "+key) ; String x = json.get(key).getAsString().value() ; return x ; }
public static Version fromJson(JsonObject obj, String field) { long ver = JSONX.getLong(obj, field, -99) ; if ( ver < -1 ) throw new DeltaException("Bad version number: '"+JSON.toStringFlat(obj.get(field))+"'"); return create(ver); }
private static String[] getStringArray(JsonObject json, String key) { if ( ! json.hasKey(key) ) throw new TDBException("StoreParamsCodec.getStringArray: no such key: "+key) ; JsonArray a = json.get(key).getAsArray() ; String[] x = new String[a.size()] ; for ( int i = 0 ; i < a.size() ; i++ ) { x[i] = a.get(i).getAsString().value() ; } return x ; }
private static String[] getStringArray(JsonObject json, String key) { if ( ! json.hasKey(key) ) throw new TDBException("StoreParamsCodec.getStringArray: no such key: "+key) ; JsonArray a = json.get(key).getAsArray() ; String[] x = new String[a.size()] ; for ( int i = 0 ; i < a.size() ; i++ ) { x[i] = a.get(i).getAsString().value() ; } return x ; }