@Override public String toJson(T v) { return ProtostuffUtil.toJSON(v, schema, false); }
@Override public byte[] serialize(final T instance) throws IOException { try(final ByteArrayOutputStream out = new ByteArrayOutputStream()) { ProtostuffUtil.toJSON(out, instance, writeSchema, false); return out.toByteArray(); } }
/** * Convert a message into a JSON string * * @param the message to write * @param schema the protostuff schema for the message * @param numeric if true, use field id as keys * @return a JSON string */ public static <T> String toJSON(T message, Schema<T> schema, boolean numeric) { try(java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream()) { toJSON(baos, message, schema, numeric); return new String(baos.toByteArray(), UTF_8); } catch (IOException e) { throw new AssertionError("IOException not expected with ByteArrayOutputStream", e); } } }
private QueryProfile recordProfile(OutputStream out, JobId id, int attempt) throws IOException, JobNotFoundException { QueryProfile profile = jobsService.get().getProfile(id, attempt); ProtostuffUtil.toJSON(out, profile, SchemaUserBitShared.QueryProfile.WRITE, false); return profile; }
private void createMetaDataFile(StoreBuilderConfig builderConfig) throws IOException { final KVStoreInfo kvStoreInfo = DataStoreUtils.toInfo(builderConfig); final Path metadataFile = new Path(metaDataFilesDir.getAbsolutePath(), format("%s%s", builderConfig.getName(), METADATA_FILE_SUFFIX)); final FileSystem fs = FileSystem.getLocal(new Configuration()); try (FSDataOutputStream metaDataOut = fs.create(metadataFile, true)) { ProtostuffUtil.toJSON(metaDataOut, kvStoreInfo, KVStoreInfo.getSchema(), false); } }
final Path backupInfoFile = new Path(backupRootDir, format("%s%s", backupFileInfo.getKvstoreInfo().getTablename(), BACKUP_INFO_FILE_SUFFIX)); try (FSDataOutputStream backupInfoOut = fs.create(backupInfoFile, true)) { ProtostuffUtil.toJSON(backupInfoOut, backupFileInfo, BackupFileInfo.getSchema(), false);
@Test public void testNanSerialization() throws IOException { Foo foo = new Foo(); foo.bar = Double.NaN; assertThat(ProtostuffUtil.toJSON(foo, SCHEMA, false), CoreMatchers.containsString("NaN")); assertThat(ProtostuffUtil.toJSON(foo, SCHEMA, false), CoreMatchers.not(CoreMatchers.containsString("\"NaN\""))); }
@Test public void testDoubleSerialization() throws IOException { Foo foo = new Foo(); foo.bar = 42.0d; assertThat(ProtostuffUtil.toJSON(foo, SCHEMA, false), CoreMatchers.containsString("42")); }
@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); } }
private boolean recordHeader(OutputStream output, JobId id, User user, String submissionId) throws UserNotFoundException, IOException, JobNotFoundException { SupportHeader header = new SupportHeader(); header.setClusterInfo(getClusterInfo()); header.setJob(jobsService.get().getJob(id).getJobAttempt()); Submission submission = new Submission() .setSubmissionId(submissionId) .setDate(System.currentTimeMillis()) .setEmail(user.getEmail()) .setFirst(user.getFirstName()) .setLast(user.getLastName()); header.setSubmission(submission); // record the dremio version that was used to run the query in the header header.setDremioVersion(jobsService.get().getProfile(id, 0).getDremioVersion()); ProtostuffUtil.toJSON(output, header, SupportHeader.getSchema(), false); return true; }