private byte[] toJsonBytes(Object obj) throws IOException { return mapper.writeValueAsBytes(obj); } }
@Override public byte[] serialize(T obj) { try { return jsonMapper.writeValueAsBytes(obj); } catch (JsonProcessingException e) { throw Throwables.propagate(e); } }
public File createSegmentDescriptorFile(final ObjectMapper jsonMapper, final DataSegment segment) throws IOException { File descriptorFile = File.createTempFile("descriptor", ".json"); try (FileOutputStream stream = new FileOutputStream(descriptorFile)) { stream.write(jsonMapper.writeValueAsBytes(segment)); } return descriptorFile; }
@Override public void write(DataOutput out) throws IOException { final byte[] segmentsBytes = HadoopDruidIndexerConfig.JSON_MAPPER.writeValueAsBytes(segments); out.writeInt(segmentsBytes.length); out.write(segmentsBytes); out.writeInt(locations.length); for (String location : locations) { out.writeUTF(location); } }
@Override public byte[] toBytes(Object o) { try { return objectMapper.writeValueAsBytes(o); } catch (JsonProcessingException e) { throw new UncheckedIOException(e); } } }
@Override public Buffer apply(OUT out) { try { return addDelimiterIfAny(new Buffer().append(mapper.writeValueAsBytes(out))); } catch (JsonProcessingException e) { throw new IllegalStateException(e); } }
private void makeMetadataBinary( final FileSmoosher v9Smoosher, final ProgressIndicator progress, final Metadata segmentMetadata ) throws IOException { if (segmentMetadata != null) { progress.startSection("make metadata.drd"); v9Smoosher.add("metadata.drd", ByteBuffer.wrap(mapper.writeValueAsBytes(segmentMetadata))); progress.stopSection("make metadata.drd"); } }
@Benchmark @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) public void serialize(Blackhole blackhole) throws Exception { for (BenchmarkUser user : userMap.values()) { byte[] serializedUser = smileMapper.writeValueAsBytes(user); blackhole.consume(serializedUser); } }
@Test public void wellKnownModules() throws JsonProcessingException, UnsupportedEncodingException { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build(); Long timestamp = 1322903730000L; DateTime dateTime = new DateTime(timestamp, DateTimeZone.UTC); assertEquals(timestamp.toString(), new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8")); Path file = Paths.get("foo"); assertTrue(new String(objectMapper.writeValueAsBytes(file), "UTF-8").endsWith("foo\"")); Optional<String> optional = Optional.of("test"); assertEquals("\"test\"", new String(objectMapper.writeValueAsBytes(optional), "UTF-8")); // Kotlin module IntRange range = new IntRange(1, 3); assertEquals("{\"start\":1,\"end\":3}", new String(objectMapper.writeValueAsBytes(range), "UTF-8")); }
@Test // SPR-12634 @SuppressWarnings("unchecked") public void customizeWellKnownModulesWithModuleClass() throws JsonProcessingException, UnsupportedEncodingException { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .modulesToInstall(CustomIntegerModule.class) .build(); DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC); assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8")); assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid")); }
@Test // SPR-12634 public void customizeDefaultModulesWithSerializer() throws JsonProcessingException, UnsupportedEncodingException { Map<Class<?>, JsonSerializer<?>> serializers = new HashMap<>(); serializers.put(Integer.class, new CustomIntegerSerializer()); this.factory.setSerializersByType(serializers); this.factory.afterPropertiesSet(); ObjectMapper objectMapper = this.factory.getObject(); DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC); assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8")); assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid")); }
@Test // SPR-12634 @SuppressWarnings("unchecked") public void customizeDefaultModulesWithModuleClass() throws JsonProcessingException, UnsupportedEncodingException { this.factory.setModulesToInstall(CustomIntegerModule.class); this.factory.afterPropertiesSet(); ObjectMapper objectMapper = this.factory.getObject(); DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC); assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8")); assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid")); }
@Test // SPR-12634 public void customizeWellKnownModulesWithSerializer() throws JsonProcessingException, UnsupportedEncodingException { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .serializerByType(Integer.class, new CustomIntegerSerializer()).build(); DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC); assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8")); assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid")); }
@Test // SPR-12634 public void customizeWellKnownModulesWithModule() throws JsonProcessingException, UnsupportedEncodingException { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .modulesToInstall(new CustomIntegerModule()) .build(); DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC); assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8")); assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid")); }
@Test public void defaultModules() throws JsonProcessingException, UnsupportedEncodingException { this.factory.afterPropertiesSet(); ObjectMapper objectMapper = this.factory.getObject(); Long timestamp = 1322903730000L; DateTime dateTime = new DateTime(timestamp, DateTimeZone.UTC); assertEquals(timestamp.toString(), new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8")); }
@Test public void testSerdeUncompressed() throws Exception { final ObjectMapper objectMapper = new DefaultObjectMapper(); final String json = "{ \"dimensionCompression\" : \"uncompressed\" }"; final IndexSpec spec = objectMapper.readValue(json, IndexSpec.class); Assert.assertEquals(CompressionStrategy.UNCOMPRESSED, spec.getDimensionCompression()); Assert.assertEquals(spec, objectMapper.readValue(objectMapper.writeValueAsBytes(spec), IndexSpec.class)); }
void disableWorker(Worker worker) throws Exception { cf.setData().forPath( joiner.join(announcementsPath, worker.getHost()), jsonMapper.writeValueAsBytes(new Worker(worker.getScheme(), worker.getHost(), worker.getIp(), worker.getCapacity(), "")) ); }
@Test public void testSerde() throws Exception { final DimFilterHavingSpec havingSpec = new DimFilterHavingSpec(new SelectorDimFilter("foo", "1", null), false); final ObjectMapper objectMapper = new DefaultObjectMapper(); Assert.assertEquals( havingSpec, objectMapper.readValue(objectMapper.writeValueAsBytes(havingSpec), HavingSpec.class) ); } }
@Test public void write() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MyBean body = new MyBean(); body.setString("Foo"); body.setNumber(42); body.setFraction(42F); body.setArray(new String[]{"Foo", "Bar"}); body.setBool(true); body.setBytes(new byte[]{0x1, 0x2}); converter.write(body, null, outputMessage); assertArrayEquals(mapper.writeValueAsBytes(body), outputMessage.getBodyAsBytes()); assertEquals("Invalid content-type", new MediaType("application", "x-jackson-smile", StandardCharsets.UTF_8), outputMessage.getHeaders().getContentType()); }
@Test public void read() throws IOException { MyBean body = new MyBean(); body.setString("Foo"); body.setNumber(42); body.setFraction(42F); body.setArray(new String[]{"Foo", "Bar"}); body.setBool(true); body.setBytes(new byte[]{0x1, 0x2}); MockHttpInputMessage inputMessage = new MockHttpInputMessage(mapper.writeValueAsBytes(body)); inputMessage.getHeaders().setContentType(new MediaType("application", "x-jackson-smile")); MyBean result = (MyBean) converter.read(MyBean.class, inputMessage); assertEquals("Foo", result.getString()); assertEquals(42, result.getNumber()); assertEquals(42F, result.getFraction(), 0F); assertArrayEquals(new String[]{"Foo", "Bar"}, result.getArray()); assertTrue(result.isBool()); assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes()); }