Java Code Examples for org.codehaus.jackson.map.ObjectMapper#getSerializationConfig()

The following examples show how to use org.codehaus.jackson.map.ObjectMapper#getSerializationConfig() . 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: PropertyJsonSerializer.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(T data) throws PropertyStoreException {
  ObjectMapper mapper = new ObjectMapper();

  SerializationConfig serializationConfig = mapper.getSerializationConfig();
  serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
  serializationConfig.set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true);
  serializationConfig.set(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
  StringWriter sw = new StringWriter();

  try {
    mapper.writeValue(sw, data);

    if (sw.toString().getBytes().length > ZNRecord.SIZE_LIMIT) {
      throw new HelixException("Data size larger than 1M. Write empty string to zk.");
    }
    return sw.toString().getBytes();

  } catch (Exception e) {
    LOG.error("Error during serialization of data (first 1k): "
        + sw.toString().substring(0, 1024), e);
  }

  return new byte[] {};
}
 
Example 2
Source File: JacksonPayloadSerializer.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public <T> byte[] serialize(final T data) {
  if (data == null) {
    return null;
  }

  ObjectMapper mapper = new ObjectMapper();
  SerializationConfig serializationConfig = mapper.getSerializationConfig();
  serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
  serializationConfig.set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true);
  serializationConfig.set(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
  StringWriter sw = new StringWriter();
  try {
    mapper.writeValue(sw, data);
  } catch (Exception e) {
    logger.error("Exception during payload data serialization.", e);
    throw new ZkClientException(e);
  }
  return sw.toString().getBytes();
}
 
Example 3
Source File: TestZNRecordSerializeWriteSizeLimit.java    From helix with Apache License 2.0 6 votes vote down vote up
private byte[] serialize(Object data) {
  ObjectMapper mapper = new ObjectMapper();
  SerializationConfig serializationConfig = mapper.getSerializationConfig();
  serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
  serializationConfig.set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true);
  serializationConfig.set(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] serializedBytes = new byte[0];

  try {
    mapper.writeValue(baos, data);
    serializedBytes = baos.toByteArray();
  } catch (IOException e) {
    Assert.fail("Can not serialize data.", e);
  }

  return serializedBytes;
}
 
Example 4
Source File: ObjectMapperResolver.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public ObjectMapperResolver() throws Exception {
    mapper = new ObjectMapper();

    final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector();
    final SerializationConfig serializationConfig = mapper.getSerializationConfig();
    final DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();

    mapper.setSerializationConfig(serializationConfig.withSerializationInclusion(Inclusion.NON_NULL).withAnnotationIntrospector(jaxbIntrospector));
    mapper.setDeserializationConfig(deserializationConfig.withAnnotationIntrospector(jaxbIntrospector));
}
 
Example 5
Source File: ObjectMapperResolver.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public ObjectMapperResolver() throws Exception {
    mapper = new ObjectMapper();

    final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector();
    final SerializationConfig serializationConfig = mapper.getSerializationConfig();
    final DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();

    mapper.setSerializationConfig(serializationConfig.withSerializationInclusion(Inclusion.NON_NULL).withAnnotationIntrospector(jaxbIntrospector));
    mapper.setDeserializationConfig(deserializationConfig.withAnnotationIntrospector(jaxbIntrospector));
}
 
Example 6
Source File: ObjectMapperResolver.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public ObjectMapperResolver() throws Exception {
    mapper = new ObjectMapper();

    final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector();
    final SerializationConfig serializationConfig = mapper.getSerializationConfig();
    final DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();

    mapper.setSerializationConfig(serializationConfig.withSerializationInclusion(Inclusion.NON_NULL).withAnnotationIntrospector(jaxbIntrospector));
    mapper.setDeserializationConfig(deserializationConfig.withAnnotationIntrospector(jaxbIntrospector));
}
 
Example 7
Source File: ClusterRepresentationUtil.java    From helix with Apache License 2.0 5 votes vote down vote up
public static String ObjectToJson(Object object) throws JsonGenerationException,
    JsonMappingException, IOException {
  ObjectMapper mapper = new ObjectMapper();
  SerializationConfig serializationConfig = mapper.getSerializationConfig();
  serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);

  StringWriter sw = new StringWriter();
  mapper.writeValue(sw, object);

  return sw.toString();
}
 
Example 8
Source File: TestHelixAdminScenariosRest.java    From helix with Apache License 2.0 5 votes vote down vote up
public static String ObjectToJson(Object object) throws JsonGenerationException,
    JsonMappingException, IOException {
  ObjectMapper mapper = new ObjectMapper();
  SerializationConfig serializationConfig = mapper.getSerializationConfig();
  serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);

  StringWriter sw = new StringWriter();
  mapper.writeValue(sw, object);

  return sw.toString();
}
 
Example 9
Source File: TestSchedulerMessage.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testSchedulerZeroMsg() throws Exception {
  _factory._results.clear();
  HelixManager manager = null;
  for (int i = 0; i < NODE_NR; i++) {
    _participants[i].getMessagingService()
        .registerMessageHandlerFactory(_factory.getMessageTypes(), _factory);

    manager = _participants[i]; // _startCMResultMap.get(hostDest)._manager;
  }

  Message schedulerMessage =
      new Message(MessageType.SCHEDULER_MSG + "", UUID.randomUUID().toString());
  schedulerMessage.setTgtSessionId("*");
  schedulerMessage.setTgtName("CONTROLLER");
  // TODO: change it to "ADMIN" ?
  schedulerMessage.setSrcName("CONTROLLER");

  // Template for the individual message sent to each participant
  Message msg = new Message(_factory.getMessageTypes().get(0), "Template");
  msg.setTgtSessionId("*");
  msg.setMsgState(MessageState.NEW);

  // Criteria to send individual messages
  Criteria cr = new Criteria();
  cr.setInstanceName("localhost_DOESNOTEXIST");
  cr.setRecipientInstanceType(InstanceType.PARTICIPANT);
  cr.setSessionSpecific(false);
  cr.setResource("%");
  cr.setPartition("%");

  ObjectMapper mapper = new ObjectMapper();
  SerializationConfig serializationConfig = mapper.getSerializationConfig();
  serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);

  StringWriter sw = new StringWriter();
  mapper.writeValue(sw, cr);

  String crString = sw.toString();

  schedulerMessage.getRecord().setSimpleField("Criteria", crString);
  schedulerMessage.getRecord().setMapField("MessageTemplate", msg.getRecord().getSimpleFields());
  schedulerMessage.getRecord().setSimpleField("TIMEOUT", "-1");

  HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
  Builder keyBuilder = helixDataAccessor.keyBuilder();
  PropertyKey controllerMessageKey = keyBuilder.controllerMessage(schedulerMessage.getMsgId());
  helixDataAccessor.setProperty(controllerMessageKey, schedulerMessage);

  Thread.sleep(3000);

  Assert.assertEquals(0, _factory._results.size());
  PropertyKey controllerTaskStatus = keyBuilder
      .controllerTaskStatus(MessageType.SCHEDULER_MSG.name(), schedulerMessage.getMsgId());
  for (int i = 0; i < 10; i++) {
    StatusUpdate update = helixDataAccessor.getProperty(controllerTaskStatus);
    if (update == null || update.getRecord().getMapField("SentMessageCount") == null) {
      Thread.sleep(1000);
    }
  }
  ZNRecord statusUpdate = helixDataAccessor.getProperty(controllerTaskStatus).getRecord();
  Assert.assertEquals(statusUpdate.getMapField("SentMessageCount").get("MessageCount"), "0");
  int count = 0;
  for (Set<String> val : _factory._results.values()) {
    count += val.size();
  }
  Assert.assertEquals(count, 0);
}
 
Example 10
Source File: TestSchedulerMsgUsingQueue.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test()
public void testSchedulerMsgUsingQueue() throws Exception {
  // Logger.getRootLogger().setLevel(Level.INFO);
  _factory._results.clear();
  Thread.sleep(2000);
  HelixManager manager = null;
  for (int i = 0; i < NODE_NR; i++) {
    _participants[i].getMessagingService().registerMessageHandlerFactory(
        _factory.getMessageType(), _factory);

    manager = _participants[i]; // _startCMResultMap.get(hostDest)._manager;
  }

  Message schedulerMessage =
      new Message(MessageType.SCHEDULER_MSG + "", UUID.randomUUID().toString());
  schedulerMessage.setTgtSessionId("*");
  schedulerMessage.setTgtName("CONTROLLER");
  // TODO: change it to "ADMIN" ?
  schedulerMessage.setSrcName("CONTROLLER");
  schedulerMessage.getRecord().setSimpleField(
      DefaultSchedulerMessageHandlerFactory.SCHEDULER_TASK_QUEUE, "TestSchedulerMsg");
  // Template for the individual message sent to each participant
  Message msg = new Message(_factory.getMessageType(), "Template");
  msg.setTgtSessionId("*");
  msg.setMsgState(MessageState.NEW);

  // Criteria to send individual messages
  Criteria cr = new Criteria();
  cr.setInstanceName("localhost_%");
  cr.setRecipientInstanceType(InstanceType.PARTICIPANT);
  cr.setSessionSpecific(false);
  cr.setResource("%");
  cr.setPartition("%");

  ObjectMapper mapper = new ObjectMapper();
  SerializationConfig serializationConfig = mapper.getSerializationConfig();
  serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);

  StringWriter sw = new StringWriter();
  mapper.writeValue(sw, cr);

  String crString = sw.toString();

  schedulerMessage.getRecord().setSimpleField("Criteria", crString);
  schedulerMessage.getRecord().setMapField("MessageTemplate", msg.getRecord().getSimpleFields());
  schedulerMessage.getRecord().setSimpleField("TIMEOUT", "-1");

  HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
  PropertyKey.Builder keyBuilder = helixDataAccessor.keyBuilder();
  helixDataAccessor.createControllerMessage(schedulerMessage);

  for (int i = 0; i < 30; i++) {
    Thread.sleep(2000);
    if (_PARTITIONS == _factory._results.size()) {
      break;
    }
  }

  Assert.assertEquals(_PARTITIONS, _factory._results.size());
  PropertyKey controllerTaskStatus =
      keyBuilder.controllerTaskStatus(MessageType.SCHEDULER_MSG.name(),
          schedulerMessage.getMsgId());

  int messageResultCount = 0;
  for (int i = 0; i < 10; i++) {
    ZNRecord statusUpdate = helixDataAccessor.getProperty(controllerTaskStatus).getRecord();
    Assert.assertTrue(statusUpdate.getMapField("SentMessageCount").get("MessageCount")
        .equals("" + (_PARTITIONS * 3)));
    for (String key : statusUpdate.getMapFields().keySet()) {
      if (key.startsWith("MessageResult ")) {
        messageResultCount++;
      }
    }
    if (messageResultCount == _PARTITIONS * 3) {
      break;
    } else {
      Thread.sleep(2000);
    }
  }
  Assert.assertEquals(messageResultCount, _PARTITIONS * 3);
  int count = 0;
  for (Set<String> val : _factory._results.values()) {
    count += val.size();
  }
  Assert.assertEquals(count, _PARTITIONS * 3);

}
 
Example 11
Source File: ZNRecordSerializer.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] serialize(Object data) {
  if (!(data instanceof ZNRecord)) {
    // null is NOT an instance of any class
    LOG.error("Input object must be of type ZNRecord but it is " + data
        + ". Will not write to zk");
    throw new ZkMarshallingError("Input object is not of type ZNRecord (was " + data + ")");
  }

  ZNRecord record = (ZNRecord) data;

  // apply retention policy
  int max = getListFieldBound(record);
  if (max < Integer.MAX_VALUE) {
    Map<String, List<String>> listMap = record.getListFields();
    for (String key : listMap.keySet()) {
      List<String> list = listMap.get(key);
      if (list.size() > max) {
        listMap.put(key, list.subList(0, max));
      }
    }
  }

  // do serialization
  ObjectMapper mapper = new ObjectMapper();
  SerializationConfig serializationConfig = mapper.getSerializationConfig();
  serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
  serializationConfig.set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true);
  serializationConfig.set(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] serializedBytes;
  boolean isCompressed = false;

  try {
    mapper.writeValue(baos, data);
    serializedBytes = baos.toByteArray();
    // apply compression if needed
    if (ZNRecordUtil.shouldCompress(record, serializedBytes.length)) {
      serializedBytes = GZipCompressionUtil.compress(serializedBytes);
      isCompressed = true;
    }
  } catch (Exception e) {
    LOG.error(
        "Exception during data serialization. ZNRecord ID: {} will not be written to zk.",
        record.getId(), e);
    throw new ZkMarshallingError(e);
  }

  int writeSizeLimit = ZNRecordUtil.getSerializerWriteSizeLimit();
  if (serializedBytes.length > writeSizeLimit) {
    LOG.error("Data size: {} is greater than {} bytes, is compressed: {}, ZNRecord.id: {}."
            + " Data will not be written to Zookeeper.", serializedBytes.length, writeSizeLimit,
        isCompressed, record.getId());
    throw new ZkMarshallingError(
        "Data size: " + serializedBytes.length + " is greater than " + writeSizeLimit
            + " bytes, is compressed: " + isCompressed + ", ZNRecord.id: " + record.getId());
  }

  return serializedBytes;
}