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

The following examples show how to use org.codehaus.jackson.map.ObjectMapper#enableDefaultTyping() . 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: NetworkStateSerializer.java    From zigbee4java with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes the network state.
 * <p>
 * Produces a json {@link String} containing the current state of the network.
 * 
 * @param zigBeeNetwork the {@link ZigBeeNetwork}
 * @return the serialized network state as json {@link String}.
 */
public String serialize(final ZigBeeNetwork zigBeeNetwork) {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enableDefaultTyping();
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    try {
        final HashMap<ZigBeeNode, HashMap<Integer, ZigBeeEndpoint>> devices = new HashMap<ZigBeeNode, HashMap<Integer, ZigBeeEndpoint>>(zigBeeNetwork.getDevices());

        final List<ZigBeeEndpoint> endpoints = new ArrayList<ZigBeeEndpoint>();
        for (final ZigBeeNode node : devices.keySet()) {
            for (final ZigBeeEndpoint endpoint : devices.get(node).values()) {
                endpoints.add(endpoint);
            }
        }

        return objectMapper.writeValueAsString(endpoints);
    } catch (final IOException e) {
        throw new RuntimeException("Error serializing network state.", e);
    }
}
 
Example 2
Source File: NetworkStateSerializer.java    From zigbee4java with Apache License 2.0 6 votes vote down vote up
/**
 * Deserializes the network state.
 * 
 * @param zigBeeNetworkManager the {@link ZigBeeNetworkManager ZigBee network manager}
 * @param zigBeeNetwork the {@link ZigBeeNetwork ZigBee network}
 * @param networkStateString the network state as {@link String}
 */
@SuppressWarnings("unchecked")
public void deserialize(final ZigBeeNetworkManager zigBeeNetworkManager, final ZigBeeNetwork zigBeeNetwork, final String networkStateString) {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enableDefaultTyping();
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    final List<ZigBeeEndpoint> endpoints;
    try {
        endpoints = objectMapper.readValue(networkStateString, ArrayList.class);
    } catch (final IOException e) {
        throw new RuntimeException("Error serializing network state.", e);
    }
    for (final ZigBeeEndpoint endpoint : endpoints) {
        ZigBeeNodeImpl existingNode = zigBeeNetwork.getNode(endpoint.getNode().getIeeeAddress());
        if (existingNode == null) {
            zigBeeNetwork.addNode((ZigBeeNodeImpl) endpoint.getNode());
        }
        else
        {
            ((ZigBeeEndpointImpl) endpoint).setNode(existingNode);
        }
        ((ZigBeeEndpointImpl) endpoint).setNetworkManager(zigBeeNetworkManager);
        zigBeeNetwork.addEndpoint(endpoint);
    }
}
 
Example 3
Source File: ZigBeeNetworkStateSerializer.java    From zigbee4java with Apache License 2.0 6 votes vote down vote up
/**
 * Deserializes the network state.
 * @param networkState the network state
 * @param networkStateString the network state as {@link String}
 */
public void deserialize(final ZigBeeNetworkState networkState, final String networkStateString) {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enableDefaultTyping();
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    final List<Object> devices;
    try {
        devices = objectMapper.readValue(networkStateString, ArrayList.class);
    } catch (final IOException e) {
        throw new RuntimeException("Error serializing network state.", e);
    }
    for (final Object destination : devices) {
        if (destination instanceof ZigBeeGroupAddress) {
            networkState.addGroup((ZigBeeGroupAddress) destination);
        } else {
            networkState.addDevice((ZigBeeDevice) destination);
        }
    }
}
 
Example 4
Source File: ObjectMapperFactory.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static ObjectMapper getOperatorValueSerializer()
{
  ObjectMapper returnVal = new ObjectMapper();
  returnVal.setVisibilityChecker(new VC());
  returnVal.configure(org.codehaus.jackson.map.SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
  returnVal.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, As.WRAPPER_OBJECT);
  returnVal.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
  return returnVal;
}
 
Example 5
Source File: ObjectMapperFactory.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static ObjectMapper getOperatorValueDeserializer()
{
  ObjectMapper returnVal = new ObjectMapper();
  returnVal.setVisibilityChecker(new VC());
  returnVal.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  returnVal.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, As.WRAPPER_OBJECT);
  return returnVal;
}
 
Example 6
Source File: JsonCodec.java    From multi-engine with Apache License 2.0 5 votes vote down vote up
public JsonCodec() {
    mapper = new ObjectMapper();
    // ignoring unknown properties makes us more robust to changes in the schema
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // This will allow including type information all non-final types. This allows correct
    // serialization/deserialization of generic collections, for example List<MyType>.
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
}
 
Example 7
Source File: ObjectMapperFactory.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public static ObjectMapper getOperatorValueSerializer()
{
  ObjectMapper returnVal = new ObjectMapper();
  returnVal.setVisibilityChecker(new VC());
  returnVal.configure(org.codehaus.jackson.map.SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
  returnVal.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, As.WRAPPER_OBJECT);
  returnVal.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
  return returnVal;
}
 
Example 8
Source File: ObjectMapperFactory.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public static ObjectMapper getOperatorValueDeserializer()
{
  ObjectMapper returnVal = new ObjectMapper();
  returnVal.setVisibilityChecker(new VC());
  returnVal.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  returnVal.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, As.WRAPPER_OBJECT);
  return returnVal;
}
 
Example 9
Source File: SamzaSqlRelRecordSerdeFactory.java    From samza with Apache License 2.0 5 votes vote down vote up
@Override
public SamzaSqlRelRecord fromBytes(byte[] bytes) {
  try {
    ObjectMapper mapper = new ObjectMapper();
    // Enable object typing to handle nested records
    mapper.enableDefaultTyping();
    return mapper.readValue(new String(bytes, "UTF-8"), new TypeReference<SamzaSqlRelRecord>() { });
  } catch (Exception e) {
    throw new SamzaException(e);
  }
}
 
Example 10
Source File: SamzaSqlRelRecordSerdeFactory.java    From samza with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] toBytes(SamzaSqlRelRecord p) {
  try {
    ObjectMapper mapper = new ObjectMapper();
    // Enable object typing to handle nested records
    mapper.enableDefaultTyping();
    return mapper.writeValueAsString(p).getBytes("UTF-8");
  } catch (Exception e) {
    throw new SamzaException(e);
  }
}
 
Example 11
Source File: SamzaSqlRelMessageSerdeFactory.java    From samza with Apache License 2.0 5 votes vote down vote up
@Override
public SamzaSqlRelMessage fromBytes(byte[] bytes) {
  try {
    ObjectMapper mapper = new ObjectMapper();
    // Enable object typing to handle nested records
    mapper.enableDefaultTyping();
    return mapper.readValue(new String(bytes, "UTF-8"), new TypeReference<SamzaSqlRelMessage>() { });
  } catch (Exception e) {
    throw new SamzaException(e);
  }
}
 
Example 12
Source File: SamzaSqlRelMessageSerdeFactory.java    From samza with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] toBytes(SamzaSqlRelMessage p) {
  try {
    ObjectMapper mapper = new ObjectMapper();
    // Enable object typing to handle nested records
    mapper.enableDefaultTyping();
    return mapper.writeValueAsString(p).getBytes("UTF-8");
  } catch (Exception e) {
    throw new SamzaException(e);
  }
}
 
Example 13
Source File: TestJackson.java    From Eagle with Apache License 2.0 5 votes vote down vote up
@Test
	public void testBase() throws JsonGenerationException, JsonMappingException, IOException {
		List<Base> objs = new ArrayList<Base>();
		ClassA a = new ClassA();
		a.setA(1);
		ClassB b = new ClassB();
		b.setB("2");
		
		objs.add(a);
		objs.add(b);
		
		ObjectMapper om = new ObjectMapper();
		om.enableDefaultTyping();
//		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		String value = om.writeValueAsString(objs);
		
		System.out.println("value = " + value);
		
		@SuppressWarnings("rawtypes")
		List result = om.readValue(value, ArrayList.class);
		System.out.println("size = " + result.size());
		Object obj1 = result.get(0);
		Object obj2 = result.get(1);
		
		Assert.assertEquals("ClassA", obj1.getClass().getSimpleName());
		Assert.assertEquals(1, ((ClassA)obj1).getA());
		Assert.assertEquals("ClassB", obj2.getClass().getSimpleName());
		Assert.assertEquals("2", ((ClassB)obj2).getB());
		
	}
 
Example 14
Source File: ZigBeeNetworkStateSerializer.java    From zigbee4java with Apache License 2.0 5 votes vote down vote up
/**
 * Serializes the network state.
 * @param networkState the network state
 * @return the serialized network state as json {@link String}.
 */
public String serialize(final ZigBeeNetworkState networkState) {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enableDefaultTyping();
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    final List<Object> destinations = new ArrayList<Object>();
    destinations.addAll(networkState.getDevices());
    destinations.addAll(networkState.getGroups());
    try {
        return objectMapper.writeValueAsString(destinations);
    } catch (final IOException e) {
        throw new RuntimeException("Error serializing network state.", e);
    }
}
 
Example 15
Source File: MetricsSnapshotSerdeV2.java    From samza with Apache License 2.0 4 votes vote down vote up
public MetricsSnapshotSerdeV2() {
  objectMapper = new ObjectMapper();
  objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE);
}
 
Example 16
Source File: ZclCommandProtocolTest.java    From zigbee4java with Apache License 2.0 4 votes vote down vote up
/**
 * Tests command serialization.
 * @param command the command
 * @throws IOException if IO exception occurs.
 */
private void testSerialization(final ZclCommand command) throws IOException {
    System.out.println(command);

    final ZclCommandMessage message1 = command.toCommandMessage();

    final byte[] payload = ZclCommandProtocol.serializePayload(message1);

    final ZclCommandMessage message2 = new ZclCommandMessage();
    message2.setType(message1.getType());

    ZclCommandProtocol.deserializePayload(payload, message2);

    final ZclCommand command2 = (ZclCommand) ZclUtil.toCommand(message2);

    Assert.assertEquals("Command equality after payload ZigBee serialization", command.toString(), command2.toString());

    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    final String json = objectMapper.writeValueAsString(message1);
    System.out.println(json);
    final ZclCommandMessage message3 = objectMapper.readValue(json, ZclCommandMessage.class);

    Assert.assertEquals("Command equality after JSON serialization", message1.toString(), message3.toString());
}