@Override public T fromJson(String v) throws IOException { return ProtostuffUtil.fromJSON(v, schema, false); } }
/** * Convert a JSON string into a Java object * * @param data the JSON data * @param the object to update * @param schema the Protostuff schema for the object * @param numeric if true, use field id as key */ public static <T> T fromJSON(String data, Schema<T> schema, boolean numeric) throws IOException { T message = schema.newMessage(); fromJSON(data.getBytes(UTF_8), message, schema, numeric); return message; }
private static Map<String, BackupFileInfo> scanInfoFiles(FileSystem fs, Path backupDir) throws IOException { final Map<String, BackupFileInfo> tableToInfo = Maps.newHashMap(); final FileStatus[] backupFiles = fs.listStatus(backupDir, BACKUP_INFO_FILES_GLOB); for (FileStatus backupFile : backupFiles) { final String tableName = getTableName(backupFile.getPath().getName(), BACKUP_INFO_FILE_SUFFIX); // read backup info file final byte[] headerBytes = new byte[(int) backupFile.getLen()]; IOUtils.readFully(fs.open(backupFile.getPath()), headerBytes, 0, headerBytes.length); final BackupFileInfo backupFileInfo = new BackupFileInfo(); ProtostuffUtil.fromJSON(headerBytes, backupFileInfo, BackupFileInfo.getSchema(), false); tableToInfo.put(tableName, backupFileInfo); } return tableToInfo; }
@Override public T deserialize(final byte[] raw) throws IOException { final B builder = readSchema.newMessage(); ProtostuffUtil.fromJSON(raw, builder, readSchema, false); return (T)builder.build(); }
/** * Scan dbDirectory to read kvstore definitions and load all stores in memory. */ public void scan() throws Exception { final FileSystem fs = FileSystem.getLocal(new Configuration()); final FileStatus[] metaDataFiles = fs.listStatus(new Path(metaDataFilesDir.getPath()), METADATA_FILES_GLOB); for (FileStatus fileStatus : metaDataFiles) { final byte[] headerBytes = new byte[(int) fileStatus.getLen()]; IOUtils.readFully(fs.open(fileStatus.getPath()), headerBytes, 0, headerBytes.length); final KVStoreInfo metadata = new KVStoreInfo(); ProtostuffUtil.fromJSON(headerBytes, metadata, KVStoreInfo.getSchema(), false); final StoreBuilderConfig storeBuilderConfig = DataStoreUtils.toBuilderConfig(metadata); getOrCreateStore(storeBuilderConfig); } }
@Test public void testNanDeserialization() throws IOException { Foo foo = ProtostuffUtil.fromJSON("{ \"bar\": NaN }", SCHEMA, false); assertEquals(Double.NaN, foo.bar, 0); }
@Test public void testDoubleDeserialization() throws IOException { Foo foo = ProtostuffUtil.fromJSON("{ \"bar\": 42.0 }", SCHEMA, false); assertEquals(42.0d, foo.bar, 0.0001); }
@Test public void testNanSerDe() throws IOException { Foo foo = new Foo(); foo.bar = Double.NaN; assertEquals(Double.NaN, ProtostuffUtil.fromJSON(ProtostuffUtil.toJSON(foo, SCHEMA, false), SCHEMA, false).bar, 0); } }