/** Return a {@link JsonObject} parsed out of bytes. */ public static JsonObject fromBytes(byte[] bytes) { return JSON.parse(new ByteArrayInputStream(bytes)); }
/** Write out a JSON value - pass a JSON Object to get legal exchangeable JSON */ public static void write(JsonValue jValue) { write(IndentedWriter.stdout, jValue) ; }
public static DeltaServerConfig read(String file) { JsonObject obj = JSON.read(file); return create(obj); }
public static void main(String... args) { if ( args.length == 0 ) args = new String[] {"-"} ; try { for ( String fn : args ) { JsonValue json =null ; try { json = JSON.readAny(fn) ; } catch (JsonParseException ex) { String name = fn.equals("-") ? "<stdin>" : fn ; System.err.println(name+": "+JsonParseException.formatMessage(ex.getMessage(), ex.getLine(), ex.getColumn())) ; continue ; } JSON.write(IndentedWriter.stdout, json) ; IndentedWriter.stdout.ensureStartOfLine() ; } } finally { IndentedWriter.stdout.flush() ; } }
LOG.info(JSON.toStringFlat(obj)); try (OutputStream out = Files.newOutputStream(sourcePath.resolve(FileNames.DS_CONFIG))) { JSON.write(out, obj); } catch (IOException ex) { throw IOX.exception(ex); } return sourcePath;
/** Read any JSON value, not just an object, from a file */ public static JsonValue readAny(String filename) { try { try (InputStream in = IO.openFileEx(filename)) { return JSON.parseAny(in) ; } } catch (FileNotFoundException ex) { throw new RuntimeException("File not found: " + filename, ex) ; } catch (IOException ex) { IO.exception("IOException: " + filename, ex) ; return null ; } }
private JsonValue rpcToValue(String opName, JsonObject arg) { JsonObject argx = ( arg == null ) ? emptyObject : arg; // [NET] Network point return retry(()->DRPC.rpc(remoteServer + DeltaConst.EP_RPC, opName, argx), ()->true, ()->format("Retry : %s",opName), ()->format("Failed : %s %s",opName,JSON.toStringFlat(argx)) ); }
throw new DeltaException() ; String argStr = JSON.toString(object) ; try (TypedInputStream x = HttpOp.execHttpPostStream(url, WebContent.contentTypeJSON, argStr, WebContent.contentTypeJSON) try { String s = IO.readWholeFileAsUTF8(x) ; return JSON.parseAny(s) ; } catch (IOException ex) { throw IOX.exception(ex); } return JSON.parseAny(x) ; } catch (HttpException ex) { if ( HttpSC.BAD_REQUEST_400 == ex.getResponseCode() ) {
/** Parse a complete JSON object */ public static JsonObject parse(String string) { return _parse(new StringReader(string)) ; }
public static void main(String... args) { if ( args.length == 0 ) args = new String[] {"-"} ; try { for ( String fn : args ) { JsonValue json =null ; try { json = JSON.readAny(fn) ; } catch (JsonParseException ex) { String name = fn.equals("-") ? "<stdin>" : fn ; System.err.println(name+": "+JsonParseException.formatMessage(ex.getMessage(), ex.getLine(), ex.getColumn())) ; continue ; } JSON.write(IndentedWriter.stdout, json) ; IndentedWriter.stdout.ensureStartOfLine() ; } } finally { IndentedWriter.stdout.flush() ; } }
/** Read any JSON value, not just an object, from a file */ public static JsonValue readAny(String filename) { try { InputStream in = IO.openFileEx(filename) ; try { return JSON.parseAny(in) ; } finally { in.close() ; } } catch (FileNotFoundException ex) { throw new RuntimeException("File not found: "+filename, ex) ; } catch (IOException ex) { IO.exception("IOException: "+filename, ex); return null ; } }
public static Version create(JsonValue version) { Objects.requireNonNull(version, "version"); if ( version.isNumber() ) { long ver = JSONX.getLong(version, -2); if ( ver < -1 ) throw new DeltaException("Bad version number: '"+JSON.toStringFlat(version)+"'"); return create(ver); } if ( version.isString() ) { try { String s = version.getAsString().value(); long ver = Long.parseLong(s); return create(ver); } catch (NumberFormatException ex) { throw new DeltaException("Bad format for version: '"+JSON.toStringFlat(version)+"'"); } } throw new DeltaException("Unrecognized JSON version: '"+JSON.toStringFlat(version)+"'"); }
/** Parse a complete JSON object */ public static JsonObject parse(String string) { return _parse(new StringReader(string)) ; }
/** Set version and datasource id from a string which is JOSN */ private static void setStateFromString(DataState state, String string) { JsonObject obj = JSON.parse(string); setFromJsonObject(state, obj); }
/** Write out a JSON value to - pass a JSON Object to get legal exchangeable JSON */ public static void write(JsonValue jValue) { write(IndentedWriter.stdout, jValue) ; } }
public static void main(String... args) { if ( args.length == 0 ) args = new String[] {"-"} ; try { for ( String fn : args ) { JsonValue json =null ; try { json = JSON.readAny(fn) ; } catch (JsonParseException ex) { String name = fn.equals("-") ? "<stdin>" : fn ; System.err.println(name+": "+JsonParseException.formatMessage(ex.getMessage(), ex.getLine(), ex.getColumn())) ; continue ; } JSON.write(IndentedWriter.stdout, json) ; IndentedWriter.stdout.ensureStartOfLine() ; } } finally { IndentedWriter.stdout.flush() ; } }
/** * Look for {@link DataSource DataSources} in a disk area given by {@code location}. * <p> * Scan the given area for directories (must have a config file), check they are enabled, * and deal with {@code log_type}. */ public static List<DataSourceDescription> scanForLogs(Path location, PatchStore ps) { // PatchStore's that rely on the scan of local directories and checking the "log_type" field. Pair<List<Path>, List<Path>> pair = scanDirectory(location); List<Path> dataSourcePaths = pair.getLeft(); List<Path> disabledDataSources = pair.getRight(); //dataSourcePaths.forEach(p->LOG.info("Data source paths: "+p)); disabledDataSources.forEach(p->LOG.info("Data source: "+p+" : Disabled")); List<DataSourceDescription> descriptions = ListUtils.toList (dataSourcePaths.stream() .map(p->{ // Extract name from disk name. String dsName = p.getFileName().toString(); // read config file. JsonObject sourceObj = JSON.read(p.resolve(FileNames.DS_CONFIG).toString()); DataSourceDescription dsd = DataSourceDescription.fromJson(sourceObj); if ( ! Objects.equals(dsName, dsd.getName()) ) throw new DeltaConfigException("Names do not match: directory="+dsName+", dsd="+dsd); return dsd; }) .filter(Objects::nonNull) ); return descriptions; }
/** Read-compare */ public static void read(String string, JsonValue expected) { JsonValue v = JSON.parseAny(string) ; assertEquals(expected, v) ; }