com.hubspot.jackson.datatype.protobuf.ProtobufModule Java Examples

The following examples show how to use com.hubspot.jackson.datatype.protobuf.ProtobufModule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestSerialization.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Test
public void testNullValueSerialization() throws Exception {
  ObjectMapper mapper = new ObjectMapper()
      // Need to register proto module (as PhysicalPlanReader...)
      .registerModule(new ProtobufModule());

  PartitionValue value = PartitionValue.newBuilder()
      .setColumn("test")
      .setIntValue(10)
      .build();
  String serialized = mapper.writeValueAsString(value);
  assertContains("intValue", serialized);
  assertNotContains("longValue", serialized);

  value = PartitionValue.newBuilder()
      .setColumn("test")
      .build();
  serialized = mapper.writeValueAsString(value);
  assertNotContains("intValue", serialized);
}
 
Example #2
Source File: SerializationUtils.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new {@link ObjectMapper} with default modules against the provided factory.
 *
 * @param mapper the instance to register default modules with
 */
public static ObjectMapper registerDefaultModules(ObjectMapper mapper) {
  // enable support for ...
  return mapper.registerModules(
      // Optional<>s
      new Jdk8Module(),
      // Protobuf objects
      new ProtobufModule());
}
 
Example #3
Source File: SingularityRunnerBaseModule.java    From Singularity with Apache License 2.0 6 votes vote down vote up
@Provides
@Singleton
@Named(YAML)
public ObjectMapper providesYamlMapper() {
  final YAMLFactory yamlFactory = new YAMLFactory();
  yamlFactory.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);

  final ObjectMapper mapper = new ObjectMapper(yamlFactory);
  mapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  mapper.registerModule(new GuavaModule());
  mapper.registerModule(new ProtobufModule());
  mapper.registerModule(new Jdk8Module());

  return mapper;
}
 
Example #4
Source File: PluginConfig.java    From kafka-webview with MIT License 5 votes vote down vote up
/**
 * Customize the jackson object map builder.
 * @return Jackson2ObjectMapperBuilderCustomizer instance.
 */
@Bean
public Jackson2ObjectMapperBuilderCustomizer registerJacksonProtobufModule() {
    return jacksonObjectMapperBuilder -> {
        // Register custom protocol buffer serializer.
        jacksonObjectMapperBuilder.modulesToInstall(new ProtobufModule());
    };
}
 
Example #5
Source File: WebConfig.java    From kafka-webview with MIT License 5 votes vote down vote up
/**
 * Ensure that ProtobufModule is registered so protobufs can easily be serialized.
 * @param converters list of converters registered.
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.clear();

    final ObjectMapper mapper = Jackson2ObjectMapperBuilder
        .json()
        .modulesToInstall(new ProtobufModule())
        .serializerByType(GenericData.Record.class, new SimpleAvroDataSerializer(appProperties.isAvroIncludeSchema()))
        .build();

    converters.add(new MappingJackson2HttpMessageConverter(mapper));
    converters.add(new StringHttpMessageConverter());
}
 
Example #6
Source File: ProtobufUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a protobuf-ready object mapper
 *
 * Creates a protobuf-ready object mapper which follows same convention as
 * Protostuff format:
 * <ul><li>enums are represented by their index</li>/</ul>
 *
 * @return
 */
public static final ObjectMapper newMapper() {
  return new ObjectMapper()
      // Reproduce Protostuff configuration
      .enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS)
      .enable(SerializationFeature.WRITE_ENUMS_USING_INDEX)
      .disable(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS)
      .setPropertyNamingStrategy(PROTOBUF_TO_CAMEL_STRATEGY)
      .registerModule(new ProtobufModule());
}
 
Example #7
Source File: JavaUtilsTest.java    From Singularity with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingularityTaskIdSerialization() throws Exception {
  ObjectMapper om = Jackson
    .newObjectMapper()
    .setSerializationInclusion(Include.NON_ABSENT)
    .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
    .registerModule(new ProtobufModule())
    .registerModule(new Jdk8Module());

  SingularityTaskId taskId = new SingularityTaskId(
    "rid",
    "did",
    100,
    1,
    "host",
    "rack"
  );
  String id = taskId.getId();
  SingularityTaskId fromId = SingularityTaskId.valueOf(id);
  SingularityTaskId fromJson = om.readValue(
    om.writeValueAsBytes(taskId),
    SingularityTaskId.class
  );

  assertEquals(taskId, fromId);
  assertEquals(taskId, fromJson);
  assertEquals(fromId, fromJson);
}
 
Example #8
Source File: JavaUtils.java    From Singularity with Apache License 2.0 5 votes vote down vote up
public static ObjectMapper newObjectMapper() {
  final ObjectMapper mapper = new ObjectMapper();
  mapper.setSerializationInclusion(Include.NON_ABSENT);
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  mapper.registerModule(new GuavaModule());
  mapper.registerModule(new ProtobufModule());
  mapper.registerModule(new Jdk8Module());
  return mapper;
}
 
Example #9
Source File: Offers.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
@Inject
Offers(OfferManager offerManager) {
  this.offerManager = Objects.requireNonNull(offerManager);
  mapper = new ObjectMapper()
      .registerModule(new ProtobufModule())
      .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
      .setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
 
Example #10
Source File: PluginConfig.java    From kafka-webview with MIT License 4 votes vote down vote up
@Autowired(required = true)
public void configureJackson(ObjectMapper jackson2ObjectMapper) {
    jackson2ObjectMapper.registerModule(new ProtobufModule());
}
 
Example #11
Source File: PhysicalPlanReader.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public PhysicalPlanReader(
    SabotConfig config,
    ScanResult scanResult,
    LogicalPlanPersistence lpPersistance,
    final NodeEndpoint endpoint,
    final Provider<CatalogService> catalogService,
    SabotContext context) {

  this.lpPersistance = lpPersistance;

  final ObjectMapper lpMapper = lpPersistance.getMapper();

  // automatic serialization of protobuf.
  lpMapper.registerModule(new ProtobufModule());

  final SimpleModule deserModule = new SimpleModule("CustomSerializers")
      .addSerializer(MajorType.class, new MajorTypeSerDe.Se())
      .addSerializer(ByteString.class, new ByteStringSer())
      .addDeserializer(ByteString.class, new ByteStringDeser())
      .addDeserializer(MajorType.class, new MajorTypeSerDe.De());


  ProtoSerializers.registerSchema(deserModule, SourceConfig.getSchema());


  lpMapper.registerModule(deserModule);

  ConnectionConf.registerSubTypes(lpMapper, context.getConnectionReaderProvider().get());

  Set<Class<? extends PhysicalOperator>> subTypes = PhysicalOperatorUtil.getSubTypes(scanResult);
  for (Class<? extends PhysicalOperator> subType : subTypes) {
    lpMapper.registerSubtypes(subType);
  }

  // store this map so that we can use later for fragment plan reader
  this.injectables = new HashMap<>();
  this.injectables.put(CatalogService.class.getName(), catalogService.get());
  this.injectables.put(ConnectionReader.class.getName(), context.getConnectionReaderProvider().get());
  this.injectables.put(SabotContext.class.getName(), context);
  this.injectables.put(NodeEndpoint.class.getName(), endpoint);

  InjectableValues.Std injectableValues = new InjectableValues.Std(this.injectables);
  this.injectables.forEach(injectableValues::addValue);

  this.mapper = lpMapper;
  this.physicalPlanReader = mapper.readerFor(PhysicalPlan.class).with(injectableValues);
  this.optionListReader = mapper.readerFor(OptionList.class).with(injectableValues);
  this.operatorReader = mapper.readerFor(PhysicalOperator.class).with(injectableValues);
}
 
Example #12
Source File: ObjectMapperHelper.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
private static ObjectMapper create(ExtensionRegistry extensionRegistry) {
  return new ObjectMapper().registerModule(new ProtobufModule(extensionRegistry));
}
 
Example #13
Source File: ObjectMapperHelper.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
private static ObjectMapper create() {
  return new ObjectMapper().registerModule(new ProtobufModule());
}
 
Example #14
Source File: SingularityService.java    From Singularity with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(final Bootstrap<T> bootstrap) {
  if (
    !Strings.isNullOrEmpty(
      System.getProperty(SINGULARITY_DEFAULT_CONFIGURATION_PROPERTY)
    )
  ) {
    bootstrap.setConfigurationSourceProvider(
      new MergingSourceProvider(
        bootstrap.getConfigurationSourceProvider(),
        System.getProperty(SINGULARITY_DEFAULT_CONFIGURATION_PROPERTY),
        bootstrap.getObjectMapper(),
        new YAMLFactory()
      )
    );
  }

  final Iterable<? extends Module> additionalModules = checkNotNull(
    getGuiceModules(bootstrap),
    "getGuiceModules() returned null"
  );
  final Iterable<? extends Bundle> additionalBundles = checkNotNull(
    getDropwizardBundles(bootstrap),
    "getDropwizardBundles() returned null"
  );
  final Iterable<? extends ConfiguredBundle<T>> additionalConfiguredBundles = checkNotNull(
    getDropwizardConfiguredBundles(bootstrap),
    "getDropwizardConfiguredBundles() returned null"
  );

  guiceBundle =
    GuiceBundle
      .defaultBuilder(SingularityConfiguration.class)
      .modules(getServiceModule(), getObjectMapperModule(), new SingularityAuthModule())
      .modules(additionalModules)
      .enableGuiceEnforcer(false)
      .stage(getGuiceStage())
      .build();
  bootstrap.addBundle(guiceBundle);

  bootstrap.addBundle(new CorsBundle());
  bootstrap.addBundle(new ViewBundle<>());
  bootstrap.addBundle(new AssetsBundle("/assets/static/", "/static/"));
  bootstrap.addBundle(
    new MigrationsBundle<SingularityConfiguration>() {

      @Override
      public DataSourceFactory getDataSourceFactory(
        final SingularityConfiguration configuration
      ) {
        return configuration.getDatabaseConfiguration().get();
      }
    }
  );

  for (Bundle bundle : additionalBundles) {
    bootstrap.addBundle(bundle);
  }

  for (ConfiguredBundle<T> configuredBundle : additionalConfiguredBundles) {
    bootstrap.addBundle(configuredBundle);
  }

  bootstrap.getObjectMapper().registerModule(new ProtobufModule());
  bootstrap.getObjectMapper().registerModule(new GuavaModule());
  bootstrap.getObjectMapper().registerModule(new Jdk8Module());
  bootstrap.getObjectMapper().setSerializationInclusion(Include.NON_ABSENT);
  bootstrap
    .getObjectMapper()
    .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
 
Example #15
Source File: OffersTest.java    From attic-aurora with Apache License 2.0 4 votes vote down vote up
@Test
public void testOneOffer() throws Exception {
  HostOffer offer = new HostOffer(
      Protos.Offer.newBuilder()
          .setId(Protos.OfferID.newBuilder().setValue("offer_id"))
          .setFrameworkId(Protos.FrameworkID.newBuilder().setValue("framework_id"))
          .setAgentId(Protos.AgentID.newBuilder().setValue("slave_id"))
          .setHostname("host_name")
          .addResources(Protos.Resource.newBuilder()
              .setName("cpus")
              .setType(Protos.Value.Type.SCALAR)
              .setScalar(Protos.Value.Scalar.newBuilder().setValue(1.0).build())
              .setReservation(Protos.Resource.ReservationInfo.newBuilder()
                  .setLabels(Protos.Labels.newBuilder()
                      .addLabels(Protos.Label.newBuilder()
                          .setKey("key")
                          .setValue("value"))
                      .build())
                  .build())
              .build())
          .addResources(Protos.Resource.newBuilder()
              .setName("mem")
              .setType(Protos.Value.Type.SCALAR)
              .setScalar(Protos.Value.Scalar.newBuilder().setValue(128.0).build())
              .setReservation(Protos.Resource.ReservationInfo.newBuilder().build())
              .build())
          .addResources(Protos.Resource.newBuilder()
              .setName("disk")
              .setType(Protos.Value.Type.SCALAR)
              .setScalar(Protos.Value.Scalar.newBuilder().setValue(128.0).build())
              .setReservation(Protos.Resource.ReservationInfo.newBuilder()
                  .setLabels(Protos.Labels.newBuilder()
                      .addLabels(Protos.Label.newBuilder()
                          .setKey("key"))
                      .build())
                  .build())
              .setDisk(Protos.Resource.DiskInfo.newBuilder()
                  .setPersistence(Protos.Resource.DiskInfo.Persistence.newBuilder()
                      .setId("volume")
                      .build())
                  .setVolume(Protos.Volume.newBuilder()
                      .setContainerPath("path")
                      .setMode(Protos.Volume.Mode.RW)
                      .setImage(Protos.Image.newBuilder()
                          .setType(Protos.Image.Type.DOCKER)
                          .setDocker(Protos.Image.Docker.newBuilder()
                              .setName("image")
                              .build())
                          .build())
                      .build())
                  .setSource(Protos.Resource.DiskInfo.Source.newBuilder()
                      .setType(Protos.Resource.DiskInfo.Source.Type.PATH)
                      .setPath(Protos.Resource.DiskInfo.Source.Path.newBuilder()
                          .setRoot("root")
                          .build())
                      .build())
                  .build())
              .build())
          .addResources(Protos.Resource.newBuilder()
              .setName("gpus")
              .setType(Protos.Value.Type.SCALAR)
              .setScalar(Protos.Value.Scalar.newBuilder().setValue(4.0).build())
              .build())
          .addResources(Protos.Resource.newBuilder()
              .setName("ports")
              .setType(Protos.Value.Type.RANGES)
              .setRanges(Protos.Value.Ranges.newBuilder()
                  .addRange(Protos.Value.Range.newBuilder()
                      .setBegin(31000)
                      .setEnd(32000)
                      .build())
                  .build())
              .build())
          .build(),
      IHostAttributes.build(new HostAttributes().setMode(NONE)));

  expect(offerManager.getAll()).andReturn(ImmutableSet.of(offer));

  control.replay();

  Response response = offers.getOffers();
  assertEquals(HttpServletResponse.SC_OK, response.getStatus());
  ObjectMapper mapper = new ObjectMapper()
      .registerModule(new ProtobufModule())
      .setPropertyNamingStrategy(
          PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
  assertEquals(
      offer.getOffer(),
      mapper.readValue(response.getEntity().toString(), Protos.Offer.class));
}