Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#addMixInAnnotations()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#addMixInAnnotations() . 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: AttributeValueMixInTest.java    From dynamodb-import-export-tool with Apache License 2.0 6 votes vote down vote up
/**
 * Test the Mixin to make sure that it capitalizes the values, and is
 * different from an ObjectMapper without the Mixin.
 */
@Test
public void testReturnsCapitalSWithMixin() throws JsonProcessingException {
    String capitalS = "S";
    String lowercaseS = "s";
    ObjectMapper mapperWith = new ObjectMapper();
    mapperWith.setSerializationInclusion(Include.NON_NULL);

    mapperWith.addMixInAnnotations(AttributeValue.class,
            AttributeValueMixIn.class);

    String withMixIn = mapperWith.writeValueAsString(sampleScanResult()
            .get(0));

    ObjectMapper mapperWithout = new ObjectMapper();
    mapperWithout.setSerializationInclusion(Include.NON_NULL);

    String withoutMixIn = mapperWithout
            .writeValueAsString(sampleScanResult().get(0));

    assertTrue(withMixIn.contains(capitalS));
    assertTrue(withoutMixIn.contains(lowercaseS));
}
 
Example 2
Source File: PathController.java    From odo with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@RequestMapping(value = "/api/path", method = RequestMethod.GET)
@ResponseBody
public String getPathsForProfile(Model model, String profileIdentifier,
                                 @RequestParam(value = "typeFilter[]", required = false) String[] typeFilter,
                                 @RequestParam(value = "clientUUID", defaultValue = Constants.PROFILE_CLIENT_DEFAULT_ID) String clientUUID) throws Exception {
    int profileId = ControllerUtils.convertProfileIdentifier(profileIdentifier);
    List<EndpointOverride> paths = PathOverrideService.getInstance().getPaths(profileId, clientUUID, typeFilter);

    HashMap<String, Object> jqReturn = Utils.getJQGridJSON(paths, "paths");

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.addMixInAnnotations(Object.class, ViewFilters.GetPathFilter.class);
    String[] ignorableFieldNames = {"possibleEndpoints", "enabledEndpoints"};
    FilterProvider filters = new SimpleFilterProvider().addFilter("Filter properties from the PathController GET",
                                                                  SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));

    ObjectWriter writer = objectMapper.writer(filters);

    return writer.writeValueAsString(jqReturn);
}
 
Example 3
Source File: PathController.java    From odo with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/api/path/test", method = RequestMethod.GET)
@ResponseBody
public String testPath(@RequestParam String url, @RequestParam String profileIdentifier,
                       @RequestParam Integer requestType) throws Exception {
    int profileId = ControllerUtils.convertProfileIdentifier(profileIdentifier);

    List<EndpointOverride> trySelectedRequestPaths = PathOverrideService.getInstance().getSelectedPaths(Constants.OVERRIDE_TYPE_REQUEST,
                                                                                                        ClientService.getInstance().findClient("-1", profileId),
                                                                                                        ProfileService.getInstance().findProfile(profileId), url, requestType, true);

    HashMap<String, Object> jqReturn = Utils.getJQGridJSON(trySelectedRequestPaths, "paths");

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.addMixInAnnotations(Object.class, ViewFilters.GetPathFilter.class);
    String[] ignorableFieldNames = {"possibleEndpoints", "enabledEndpoints"};
    FilterProvider filters = new SimpleFilterProvider().addFilter("Filter properties from the PathController GET",
                                                                  SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));

    ObjectWriter writer = objectMapper.writer(filters);

    return writer.writeValueAsString(jqReturn);
}
 
Example 4
Source File: PrintingController.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@PostConstruct
public void postConstruct() {
	objectMapper = new ObjectMapper();
	objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	// abstract info classes
	objectMapper.addMixInAnnotations(AbstractAttributeInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(ClientLayerInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(ClientUserDataInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(ClientWidgetInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(ConstraintInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(LayerExtraInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(PrintComponentInfo.class, TypeInfoMixin.class);
	// abstract SLD classes
	objectMapper.addMixInAnnotations(ExpressionInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(ExpressionTypeInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(ComparisonOpsTypeInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(LogicOpsTypeInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(SpatialOpsTypeInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(AbstractGeometryCollectionInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(AbstractGeometryInfo.class, TypeInfoMixin.class);
	objectMapper.addMixInAnnotations(SymbolizerTypeInfo.class, TypeInfoMixin.class);
}
 
Example 5
Source File: JacksonRepresentationImpl.java    From ontopia with Apache License 2.0 5 votes vote down vote up
@Override
protected ObjectMapper createObjectMapper() {
	ObjectMapper mapper = super.createObjectMapper();
	mapper.addMixInAnnotations(LocatorIF.class, MLocator.class);
	mapper.addMixInAnnotations(TopicIF.class, MTopic.class);
	mapper.addMixInAnnotations(TopicNameIF.class, MTopicName.class);
	mapper.addMixInAnnotations(VariantNameIF.class, MVariantName.class);
	mapper.addMixInAnnotations(OccurrenceIF.class, MOccurrence.class);
	mapper.addMixInAnnotations(AssociationIF.class, MAssociation.class);
	mapper.addMixInAnnotations(AssociationRoleIF.class, MAssociationRole.class);
	mapper.addMixInAnnotations(TopicMapIF.class, MTopicMapAsValue.class);
	mapper.addMixInAnnotations(TopicMapReferenceIF.class, MTopicMapReference.class);
	mapper.addMixInAnnotations(TopicMapSourceIF.class, MTopicMapSource.class);
	
	@SuppressWarnings("unchecked")
	Map<Class<?>, Class<?>> additional = (Map<Class<?>, Class<?>>) Response.getCurrent().getAttributes().get(ADDITIONAL_MIXINS_ATTRIBUTE);

	if ((additional != null) && (!additional.isEmpty())) {
		for (Map.Entry<Class<?>, Class<?>> entry : additional.entrySet()) {
			mapper.addMixInAnnotations(entry.getKey(), entry.getValue());
		}
	}
	
	for (JsonParser.Feature feature : JsonParser.Feature.values()) {
		Parameter parameter = ContextUtils.getParameter(ContextUtils.getCurrentApplicationContext(), JSONPARSER_FEATURE + feature.name());
		if (parameter != null) {
			mapper.configure(feature, ContextUtils.getParameterAsBoolean(parameter, feature.enabledByDefault() || DEFAULT_PARSER_FEATURES.contains(feature)));
		}
	}
	
	return mapper;
}
 
Example 6
Source File: Application.java    From evernote-rest-webapp with Apache License 2.0 5 votes vote down vote up
/**
 * override spring-boot default ObjectMapper to configure output(serialization) json.
 *
 * @see org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration.ObjectMappers#jacksonObjectMapper()
 */
@Bean
public ObjectMapper jacksonObjectMapper() {

	// use different visibility for serialization(output json)
	// I want to ONLY change the visibility for serialization, but couldn't find nice way to do it.
	// maybe related to this issue: https://github.com/FasterXML/jackson-databind/issues/352
	// for now, override ObjectMapper and set new SerializationConfig in instance initializer.
	// TODO: find correct way to do this.
	final ObjectMapper mapper = new ObjectMapper() {
		{
			// use instance fields for output json
			_serializationConfig = _serializationConfig.with(
					_serializationConfig.getDefaultVisibilityChecker()
							.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
							.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
							.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
							.withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
							.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
			);
		}

	};

	// mix-in to ignore thrift specific fields for serialization.
	mapper.addMixInAnnotations(Object.class, ThriftPropertyJacksonFilter.class);

	return mapper;
}
 
Example 7
Source File: JsonSerializer.java    From gtfs-validator with MIT License 5 votes vote down vote up
/**
 * Create a JSON serializer for these validation results.
 * @param results
 */
public JsonSerializer (FeedValidationResultSet results) {
	super(results);
	mapper = new ObjectMapper();
	mapper.addMixInAnnotations(Rectangle2D.class, Rectangle2DMixIn.class);
	SimpleModule deser = new SimpleModule();
	deser.addDeserializer(Rectangle2D.class, new Rectangle2DDeserializer());
	mapper.registerModule(deser);
	SimpleFilterProvider filters = new SimpleFilterProvider();
	filters.addFilter("bbox", SimpleBeanPropertyFilter.filterOutAllExcept("west", "east", "south", "north"));
	writer = mapper.writer(filters);
}
 
Example 8
Source File: GMailMessageActivitySerializer.java    From streams with Apache License 2.0 5 votes vote down vote up
public GMailMessageActivitySerializer(GMailProvider provider) {

    this.provider = provider;

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, Boolean.FALSE);

    mapper.addMixInAnnotations(IMAPSSLStore.class, MessageMixIn.class);
    mapper.addMixInAnnotations(IMAPFolder.class, MessageMixIn.class);
    mapper.addMixInAnnotations(IMAPMessage.class, MessageMixIn.class);
    mapper.addMixInAnnotations(MimeMultipart.class, MessageMixIn.class);
    mapper.addMixInAnnotations(JavaMailGmailMessage.class, MessageMixIn.class);

  }