@Bean(name="redisTemplate") public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, String> template = new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setConnectionFactory(factory); //key序列化方式 template.setKeySerializer(redisSerializer); //value序列化 template.setValueSerializer(jackson2JsonRedisSerializer); //value hashmap序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; }
@Override protected ObjectMapper initializeObjectMapper() { final ObjectMapper mapper = super.initializeObjectMapper(); final FilterProvider filters = new SimpleFilterProvider() .addFilter("beanObjectFilter", new CasSimpleBeanObjectFilter()); mapper.setFilters(filters); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false); mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.addMixIn(Object.class, CasSimpleBeanObjectFilter.class); mapper.disable(SerializationFeature.INDENT_OUTPUT); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); return mapper; }
public static ObjectMapper objectMapper() { return new ObjectMapper() // Property visibility .setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS) .setDefaultVisibility(JsonAutoDetect.Value.construct(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC)) .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true) .setSerializationInclusion(JsonInclude.Include.NON_NULL) .configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true) // Property naming and order .setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE) // Customised de/serializers // Formats, locals, encoding, binary data .setDateFormat(new SimpleDateFormat("MM/dd/yyyy")) .setDefaultPrettyPrinter(new DefaultPrettyPrinter()) .setLocale(Locale.CANADA); }
/** * RedisTemplate配置 * @param factory * @return */ @Bean @SuppressWarnings({"rawtypes", "unchecked"}) public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); //定义value的序列化方式 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; }
@Bean("redisTemplate") //新家的这个注解 10-26 12:06 @SuppressWarnings({ "rawtypes", "unchecked" }) public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisFactory){ StringRedisTemplate template = new StringRedisTemplate(redisFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; }
@Override public void execute() { Path taxonomyDefinitionImportPath = executionParameters.getTaxonomyDefinitionImportPath(); Path taxonomyDefinitionOutputPath = executionParameters.getTaxonomyDefinitionOutputPath(); assertRequiredArgs(taxonomyDefinitionImportPath, taxonomyDefinitionOutputPath); taxonomyMigrator.init(); TaxonomyTerm taxonomyDefinition = taxonomyMigrator.getTaxonomyDefinition(); // Write out the JSON files try { ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); File dir = taxonomyDefinitionOutputPath.toFile(); dir.mkdirs(); // Prefix is so taxonomy gets imported first, before any documents mapper.writeValue(new File(dir, "000000_" + "publication_taxonomy.json"), taxonomyDefinition); } catch (IOException e) { throw new RuntimeException(e); } }
public ApiFactory(String baseUrl) { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); this.okHttpClient = new OkHttpClient.Builder().readTimeout(60, TimeUnit.SECONDS) .connectTimeout(60, TimeUnit.SECONDS) .cookieJar(new SimpleCookieJar()) .addInterceptor(logging) .build(); this.retrofit = new Retrofit.Builder().baseUrl(baseUrl) .addConverterFactory(JacksonConverterFactory.create( new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE) .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY))) // 配置Json序列化的时候只使用field name,不走getter setter .client(okHttpClient) .build(); }
public static String objectToString (final Object object) { if (object == null) { return null; } try { final StringWriter stringWriter = new StringWriter(); final ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); objectMapper.writeValue(stringWriter, object); return stringWriter.toString().replaceAll(System.getProperty("line.separator"), ""); } catch (IOException ex) { STRINGUTIL_LOGGER.info("Sorry. had a error on during Object to String. ("+ex.toString()+")"); return null; } }
@Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); serializer.setObjectMapper(mapper); template.setValueSerializer(serializer); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; }
private void configureerMapper() { // Configuratie this.disable(MapperFeature.AUTO_DETECT_CREATORS); this.disable(MapperFeature.AUTO_DETECT_FIELDS); this.disable(MapperFeature.AUTO_DETECT_GETTERS); this.disable(MapperFeature.AUTO_DETECT_IS_GETTERS); this.disable(MapperFeature.AUTO_DETECT_SETTERS); // Default velden niet als JSON exposen (expliciet annoteren!) setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.NONE); this.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); this.enable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS); // serialization this.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); setSerializationInclusion(JsonInclude.Include.NON_EMPTY); this.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX); // deserialization this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); }
private static ObjectMapper initializeMapper() { return new ObjectMapper() .registerModule(new JavaTimeModule()) .disable(MapperFeature.AUTO_DETECT_GETTERS) .disable(MapperFeature.AUTO_DETECT_CREATORS) .disable(MapperFeature.AUTO_DETECT_SETTERS) .disable(MapperFeature.AUTO_DETECT_IS_GETTERS) .disable(MapperFeature.AUTO_DETECT_FIELDS) .disable(MapperFeature.DEFAULT_VIEW_INCLUSION) .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.NONE) .enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY) .enable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS) // serialization .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) .enable(SerializationFeature.USE_EQUALITY_FOR_OBJECT_ID) .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) // deserialization .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) .enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE); }
@Bean public RedisTemplate<Object, Object> redisCacheRedisTemplate() { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setKeySerializer(jackson2JsonRedisSerializer); template.setValueSerializer(jackson2JsonRedisSerializer); return template; }
@Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.findAndRegisterModules(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(mapper); template.setValueSerializer(jackson2JsonRedisSerializer); template.setKeySerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; }
@Override public void initialize(Bootstrap<BitShadowConfiguration> bootstrap) { bootstrap.addBundle(GuiceBundle.builder() .modules(new BitShadowWebModule()) .build() ); bootstrap.addBundle(new SwaggerBundle<BitShadowConfiguration>() { @Override protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(BitShadowConfiguration configuration) { return configuration.swaggerBundleConfiguration; } }); bootstrap.getObjectMapper() .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE) .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) .findAndRegisterModules(); }
/** * Register. */ public void setup() { // Prepare Environment with VertxInject Environment.create(); // Set default Json Mapper options Json.mapper.setAnnotationIntrospector(new JacksonLombokAnnotationIntrospector()) .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY).setSerializationInclusion(JsonInclude.Include.NON_NULL) .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) //Safe register VertxJsonModule on Mapper .registerModule(new Jdk8Module()) .registerModule(new JavaTimeModule()) .registerModule(new ParameterNamesModule()) .registerModule(new VertxJsonModule()); }
/** * Reads a JSON manifest file into a JSON manifest object. * * @param jsonManifestFile the JSON manifest file. * * @return the manifest object. * @throws java.io.IOException if any errors were encountered reading the JSON file. * @throws IllegalArgumentException if the manifest file has validation errors. */ public M readJsonManifest(File jsonManifestFile) throws IOException, IllegalArgumentException { // Verify that the file exists and can be read. HerdFileUtils.verifyFileExistsAndReadable(jsonManifestFile); // Deserialize the JSON manifest. BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(jsonManifestFile)); BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, Charsets.UTF_8)); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE); M manifest = getManifestFromReader(reader, objectMapper); // Validate the manifest and return it. validateManifest(manifest); return manifest; }
public JsonIO() { JsonFactory jsonFactory = new JsonFactory(); // All field names will be intern()ed jsonFactory.enable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES); jsonFactory.enable(JsonFactory.Feature.INTERN_FIELD_NAMES); jsonFactory.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); objectMapper = new ObjectMapper(jsonFactory); // Use setters and getters to be able use String.intern(). This reduces the amount of memory needed to load a State file. objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.ANY); objectMapper.setVisibility(PropertyAccessor.SETTER, Visibility.ANY); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); objectMapper.setSerializationInclusion(Include.NON_NULL); JsonPrettyPrinter prettyPrinter = new JsonPrettyPrinter(); objectWriter = objectMapper.writer(prettyPrinter); }
@Override public String toJSON() { final ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); final ByteArrayOutputStream stream = new ByteArrayOutputStream(); String json = null; try { mapper.writeValue(stream, this); json = new String(stream.toByteArray(), "UTF-8"); } catch (final IOException e) { Err.process(e); } //TODO serialize Object as JSON return json; }
/*** * 初始化ObjectMapper * * @return ObjectMapper */ public static ObjectMapper getObjectMapper() { ObjectMapper objectMapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, Visibility.ANY); // 去除不存在的属性 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 空字符串转换null对象 objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); // 去除值为null的值 objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); // 默认时间戳改成自定义日期格式 objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); objectMapper.setDateFormat(dFormat); return objectMapper; }
/*** * 保留空值的ObjectMapper * * @return ObjectMapper */ public static ObjectMapper getObjectMapperWithNull() { ObjectMapper objectMapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, Visibility.ANY); objectMapper.setSerializationInclusion(Include.ALWAYS); // 去除不存在的属性 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 空字符串转换null对象 objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); // 不去除值为null的值 objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true); // 默认时间戳改成自定义日期格式 objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); objectMapper.setDateFormat(dFormat); return objectMapper; }
@Autowired public StreamController(ClusterRegistry registry, Observable<Boolean> shutdown) { Objects.requireNonNull(registry); Objects.requireNonNull(shutdown); ObjectMapper om = new ObjectMapper(); om.enable(MapperFeature.AUTO_DETECT_FIELDS); om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); Observable<HystrixClusterMetrics> metricsObs = registry.observe(); streamObservable = metricsObs .takeUntil(shutdown) .map(d -> { try { return om.writeValueAsString(d); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }) .share(); }
@Override protected void generateComponent() { notificationSink = (NotificationSink) conf.get(StormTopologyLayoutConstants.STREAMLINE_COMPONENT_CONF_KEY); String boltId = "notificationBolt" + UUID_FOR_COMPONENTS; String boltClassName = "com.hortonworks.streamline.streams.runtime.storm.bolt.notification.NotificationBolt"; String[] constructorArgNames = {TopologyLayoutConstants.JSON_KEY_NOTIFICATION_SINK_JSON}; try { if (notificationSink == null) { throw new RuntimeException("Error generating flux, notificationSink = " + notificationSink); } ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); String notificationSinkJsonStr = objectMapper.writeValueAsString(notificationSink); conf.put(TopologyLayoutConstants.JSON_KEY_NOTIFICATION_SINK_JSON, notificationSinkJsonStr); } catch (JsonProcessingException ex) { throw new RuntimeException(ex); } List<Object> boltConstructorArgs = getConstructorArgsYaml(constructorArgNames); String[] configMethodNames = {"withNotificationStoreClass"}; String[] configKeys = {TopologyLayoutConstants.JSON_KEY_NOTIFICATION_STORE_CLASS}; List configMethods = getConfigMethodsYaml(configMethodNames, configKeys); component = createComponent(boltId, boltClassName, null, boltConstructorArgs, configMethods); addParallelismToComponent(); }
public Std withVisibility(PropertyAccessor paramPropertyAccessor, JsonAutoDetect.Visibility paramVisibility) { switch (VisibilityChecker.1.$SwitchMap$com$fasterxml$jackson$annotation$PropertyAccessor[paramPropertyAccessor.ordinal()]) { default: return this; case 1: return withGetterVisibility(paramVisibility); case 2: return withSetterVisibility(paramVisibility); case 3: return withCreatorVisibility(paramVisibility); case 4: return withFieldVisibility(paramVisibility); case 5: return withIsGetterVisibility(paramVisibility); case 6: } return with(paramVisibility); }
/** * Return the internal ObjectMapper * @return */ static ObjectMapper getMapper() { ObjectMapper mapper = new ObjectMapper(); // do not write null values mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); // set Date objects to output in readable customized format mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); mapper.setDateFormat(dateFormat); return mapper; }
private static void doGoodMapper(ArrayList<AnInterface> childs) throws JsonProcessingException { ObjectMapper theGoodMapper = new ObjectMapper(); theGoodMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); theGoodMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); theGoodMapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE); theGoodMapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE); theGoodMapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE); theGoodMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); String theJsonText = theGoodMapper.writeValueAsString(childs); System.out.println(theJsonText); try { ArrayList<AnInterface> reconstructedChilds = theGoodMapper.readValue( theJsonText, new TypeReference<ArrayList<AnInterface>>() { }); System.out.println("The good mapper works ! "); System.out.println(reconstructedChilds); } catch (IOException e) { System.err.println("Bad mapper fails " + e.getMessage()); } }
private static void doBadMapper(ArrayList<AnInterface> childs) throws JsonProcessingException { ObjectMapper theBadMapper = new ObjectMapper(); theBadMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); theBadMapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE); theBadMapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE); theBadMapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE); theBadMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); String theJsonText = theBadMapper.writeValueAsString(childs); System.out.println(theJsonText); try { ArrayList<AnInterface> reconstructedChilds = theBadMapper.readValue( theJsonText, new TypeReference<ArrayList<AnInterface>>() { }); System.out.println(reconstructedChilds); } catch (IOException e) { System.err.println("Bad mapper fails " + e.getMessage()); } }
public static <T> T fromJson(String json, Class<T> entityClass, JsonAutoDetect.Visibility visibility) { T instance = null; // Need to make this local to be thread safe ObjectMapper localMapper = new ObjectMapper(); localMapper.setVisibility(PropertyAccessor.FIELD, visibility); try { instance = localMapper.readValue(json, entityClass); } catch (IOException e) { e.printStackTrace(); } return instance; }
public static String toJson(Object object, JsonAutoDetect.Visibility visibility) { String json = null; // Need to make this local to be thread safe ObjectMapper localMapper = new ObjectMapper(); localMapper.setVisibility(PropertyAccessor.FIELD, visibility); try { json = localMapper.writeValueAsString(object); } catch (JsonProcessingException e) { if (LOG.isDebugEnabled()) { e.printStackTrace(); } LOG.error("Unable to convert object to JSON: {}", e.getMessage()); } return json; }
public Std withVisibility(PropertyAccessor method, Visibility v) { switch (method) { case GETTER: return withGetterVisibility(v); case SETTER: return withSetterVisibility(v); case CREATOR: return withCreatorVisibility(v); case FIELD: return withFieldVisibility(v); case IS_GETTER: return withIsGetterVisibility(v); case ALL: return with(v); //case NONE: // break; } return this; }
private ObjectMapper getMapper(final boolean useFields) { final ObjectMapper mapper = new ObjectMapper(); return useFields ? mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE) .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) : mapper; }
/** * Initialize object mapper. * * @return the object mapper */ protected ObjectMapper initializeObjectMapper() { final ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); return mapper; }
@SuppressWarnings({"unchecked","rawtypes"}) @Bean public RedisTemplate<String, String> redisTemplate( RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; }