@Test public void additionalPropertiesWorkWithAllVisibility() throws ClassNotFoundException, SecurityException, NoSuchMethodException, JsonProcessingException, IOException { mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false); mapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false); mapper.setVisibility(mapper.getVisibilityChecker().with(Visibility.ANY)); ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/additionalProperties/defaultAdditionalProperties.json", "com.example"); Class<?> classWithAdditionalProperties = resultsClassLoader.loadClass("com.example.DefaultAdditionalProperties"); String jsonWithAdditionalProperties = "{\"a\":1, \"b\":2};"; Object instanceWithAdditionalProperties = mapper.readValue(jsonWithAdditionalProperties, classWithAdditionalProperties); JsonNode jsonNode = mapper.readTree(mapper.writeValueAsString(instanceWithAdditionalProperties)); assertThat(jsonNode.path("a").asText(), is("1")); assertThat(jsonNode.path("b").asInt(), is(2)); assertThat(jsonNode.has("additionalProperties"), is(false)); }
public void testFactoryMethods() { JsonAutoDetect.Value v = JsonAutoDetect.Value.construct(PropertyAccessor.FIELD, Visibility.ANY); assertEquals(Visibility.ANY, v.getFieldVisibility()); assertEquals(Visibility.DEFAULT, v.getGetterVisibility()); assertEquals(Visibility.DEFAULT, v.getIsGetterVisibility()); assertEquals(Visibility.DEFAULT, v.getSetterVisibility()); assertEquals(Visibility.DEFAULT, v.getCreatorVisibility()); assertEquals(Visibility.DEFAULT, v.getScalarConstructorVisibility()); JsonAutoDetect.Value all = JsonAutoDetect.Value.construct(PropertyAccessor.ALL, Visibility.NONE); assertEquals(Visibility.NONE, all.getFieldVisibility()); assertEquals(Visibility.NONE, all.getGetterVisibility()); assertEquals(Visibility.NONE, all.getIsGetterVisibility()); assertEquals(Visibility.NONE, all.getSetterVisibility()); assertEquals(Visibility.NONE, all.getCreatorVisibility()); assertEquals(Visibility.NONE, all.getScalarConstructorVisibility()); }
public void testSimpleChanges() { assertSame(NO_OVERRIDES, NO_OVERRIDES.withFieldVisibility(Visibility.DEFAULT)); JsonAutoDetect.Value v = NO_OVERRIDES.withCreatorVisibility(Visibility.PUBLIC_ONLY); assertNotSame(NO_OVERRIDES, v); assertEquals(Visibility.PUBLIC_ONLY, v.getCreatorVisibility()); v = NO_OVERRIDES.withFieldVisibility(Visibility.ANY); assertEquals(Visibility.ANY, v.getFieldVisibility()); v = NO_OVERRIDES.withGetterVisibility(Visibility.NON_PRIVATE); assertEquals(Visibility.NON_PRIVATE, v.getGetterVisibility()); v = NO_OVERRIDES.withIsGetterVisibility(Visibility.PROTECTED_AND_PUBLIC); assertEquals(Visibility.PROTECTED_AND_PUBLIC, v.getIsGetterVisibility()); v = NO_OVERRIDES.withSetterVisibility(Visibility.PUBLIC_ONLY); assertEquals(Visibility.PUBLIC_ONLY, v.getSetterVisibility()); v = NO_OVERRIDES.withScalarConstructorVisibility(Visibility.PUBLIC_ONLY); assertEquals(Visibility.PUBLIC_ONLY, v.getScalarConstructorVisibility()); }
public DebugPanel(Worlds worlds) { mapper.setVisibility(mapper.getSerializationConfig() .getDefaultVisibilityChecker() .withFieldVisibility(Visibility.ANY)); this.worlds = worlds; worlds.addPropertyChangeListener(e -> { SwingUtilities.invokeLater(this::update); }); this.setSize(Toolkit.getDefaultToolkit().getScreenSize()); JScrollPane scroller = new JScrollPane(label, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); this.setLayout(new BorderLayout()); this.add(scroller, BorderLayout.CENTER); this.setDefaultCloseOperation(EXIT_ON_CLOSE); update(); }
/** * 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); }
/*** * 初始化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; }
public Std(JsonAutoDetect.Visibility paramVisibility) { if (paramVisibility == JsonAutoDetect.Visibility.DEFAULT) { this._getterMinLevel = DEFAULT._getterMinLevel; this._isGetterMinLevel = DEFAULT._isGetterMinLevel; this._setterMinLevel = DEFAULT._setterMinLevel; this._creatorMinLevel = DEFAULT._creatorMinLevel; this._fieldMinLevel = DEFAULT._fieldMinLevel; return; } this._getterMinLevel = paramVisibility; this._isGetterMinLevel = paramVisibility; this._setterMinLevel = paramVisibility; this._creatorMinLevel = paramVisibility; this._fieldMinLevel = paramVisibility; }
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); }
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()); } }
private static boolean isSetterAutoDetected( RebindConfiguration configuration, PropertyAccessors propertyAccessors, BeanInfo info ) { if ( !propertyAccessors.getSetter().isPresent() ) { return false; } for ( Class<? extends Annotation> annotation : AUTO_DISCOVERY_ANNOTATIONS ) { if ( propertyAccessors.isAnnotationPresentOnSetter( annotation ) ) { return true; } } JMethod setter = propertyAccessors.getSetter().get(); String methodName = setter.getName(); if ( !methodName.startsWith( "set" ) || methodName.length() <= 3 ) { // no annotation on method and the method does not follow naming convention return false; } JsonAutoDetect.Visibility visibility = info.getSetterVisibility(); if ( Visibility.DEFAULT == visibility ) { visibility = configuration.getDefaultSetterVisibility(); } return isAutoDetected( visibility, setter.isPrivate(), setter.isProtected(), setter.isPublic(), setter .isDefaultAccess() ); }
private static boolean isFieldAutoDetected( RebindConfiguration configuration, PropertyAccessors propertyAccessors, BeanInfo info ) { if ( !propertyAccessors.getField().isPresent() ) { return false; } for ( Class<? extends Annotation> annotation : AUTO_DISCOVERY_ANNOTATIONS ) { if ( propertyAccessors.isAnnotationPresentOnField( annotation ) ) { return true; } } JField field = propertyAccessors.getField().get(); JsonAutoDetect.Visibility visibility = info.getFieldVisibility(); if ( Visibility.DEFAULT == visibility ) { visibility = configuration.getDefaultFieldVisibility(); } return isAutoDetected( visibility, field.isPrivate(), field.isProtected(), field.isPublic(), field .isDefaultAccess() ); }
private static boolean isAutoDetected( JsonAutoDetect.Visibility visibility, boolean isPrivate, boolean isProtected, boolean isPublic, boolean isDefaultAccess ) { switch ( visibility ) { case ANY: return true; case NONE: return false; case NON_PRIVATE: return !isPrivate; case PROTECTED_AND_PUBLIC: return isProtected || isPublic; case PUBLIC_ONLY: case DEFAULT: return isPublic; default: return false; } }
@Bean public ObjectMapper jacksonObjectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility( mapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(Visibility.ANY) .withGetterVisibility(Visibility.NONE) .withSetterVisibility(Visibility.NONE) .withCreatorVisibility(Visibility.NONE) .withIsGetterVisibility(Visibility.NONE)); SimpleModule module = new SimpleModule("bandwidth-on-demand", Version.unknownVersion()); module.addSerializer(new VlanJsonSerializer()); module.addDeserializer(Vlan.class, new VlanJsonDeserializer()); module.addSerializer(new ScheduleTypeJsonSerializer()); module.addDeserializer(ScheduleType.class, new ScheduleTypeJsonDeserializer()); mapper.registerModule(module); mapper.registerModule(new Hibernate4Module()); mapper.registerModule(new JodaModule()); mapper.registerModule(new Jdk8Module()); mapper.registerModule(new JavaTimeModule()); mapper.registerSubtypes(new NamedType(LocalVirtualPort.class, "LOCAL")); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); return mapper; }
@Override public VisibilityChecker<?> getDefaultVisibilityChecker() { VisibilityChecker<?> vchecker = super.getDefaultVisibilityChecker(); if (!isEnabled(MapperFeature.AUTO_DETECT_GETTERS)) { vchecker = vchecker.withGetterVisibility(Visibility.NONE); } // then global overrides (disabling) if (!isEnabled(MapperFeature.AUTO_DETECT_IS_GETTERS)) { vchecker = vchecker.withIsGetterVisibility(Visibility.NONE); } if (!isEnabled(MapperFeature.AUTO_DETECT_FIELDS)) { vchecker = vchecker.withFieldVisibility(Visibility.NONE); } return vchecker; }
/** * Costructor that will assign given visibility value for all * properties. * * @param v level to use for all property types */ public Std(Visibility v) { // typically we shouldn't get this value; but let's handle it if we do: if (v == Visibility.DEFAULT) { _getterMinLevel = DEFAULT._getterMinLevel; _isGetterMinLevel = DEFAULT._isGetterMinLevel; _setterMinLevel = DEFAULT._setterMinLevel; _creatorMinLevel = DEFAULT._creatorMinLevel; _fieldMinLevel = DEFAULT._fieldMinLevel; } else { _getterMinLevel = v; _isGetterMinLevel = v; _setterMinLevel = v; _creatorMinLevel = v; _fieldMinLevel = v; } }
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; }
@Test public void shouldRoundTripQuotedPrintableFieldWithNoFieldVisibility() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(mapper.getVisibilityChecker().withFieldVisibility(Visibility.NONE)); roundTripAssertions( new ObjectMapper(), "anyBinaryEncoding", "\"=E3=82=A8=E3=83=B3=E3=82=B3=E3=83=BC=E3=83=89=E3=81=95=E3=82=8C=E3=81=9F=E6=96=87=E5=AD=97=E5=88=97\" is Japanese for \"encoded string\"", "\"エンコードされた文字列\" is Japanese for \"encoded string\"".getBytes("UTF-8")); }
public void testAnnotationProperties() throws Exception { Member m = Bogus.class.getField("value"); assertTrue(JsonAutoDetect.Visibility.ANY.isVisible(m)); assertFalse(JsonAutoDetect.Visibility.NONE.isVisible(m)); assertTrue(JsonAutoDetect.Visibility.NON_PRIVATE.isVisible(m)); assertTrue(JsonAutoDetect.Visibility.PUBLIC_ONLY.isVisible(m)); assertTrue(JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC.isVisible(m)); assertTrue(JsonAutoDetect.Visibility.NON_PRIVATE.isVisible(m)); // forget why DEFAULT would give false but assertFalse(JsonAutoDetect.Visibility.DEFAULT.isVisible(m)); }
public void testSimpleMerge() { JsonAutoDetect.Value base = JsonAutoDetect.Value.construct( Visibility.ANY, Visibility.PUBLIC_ONLY, Visibility.ANY, Visibility.NONE, Visibility.ANY, Visibility.PROTECTED_AND_PUBLIC); JsonAutoDetect.Value overrides = JsonAutoDetect.Value.construct( Visibility.NON_PRIVATE, Visibility.DEFAULT, Visibility.PUBLIC_ONLY, Visibility.DEFAULT, Visibility.DEFAULT, Visibility.PUBLIC_ONLY); JsonAutoDetect.Value merged = JsonAutoDetect.Value.merge(base, overrides); assertFalse(merged.equals(base)); assertFalse(merged.equals(overrides)); assertEquals(merged, merged); assertEquals(Visibility.NON_PRIVATE, merged.getFieldVisibility()); assertEquals(Visibility.PUBLIC_ONLY, merged.getGetterVisibility()); assertEquals(Visibility.PUBLIC_ONLY, merged.getIsGetterVisibility()); assertEquals(Visibility.NONE, merged.getSetterVisibility()); assertEquals(Visibility.ANY, merged.getCreatorVisibility()); assertEquals(Visibility.PUBLIC_ONLY, merged.getScalarConstructorVisibility()); // try the other way around too merged = JsonAutoDetect.Value.merge(overrides, base); assertEquals(Visibility.ANY, merged.getFieldVisibility()); assertEquals(Visibility.PUBLIC_ONLY, merged.getGetterVisibility()); assertEquals(Visibility.ANY, merged.getIsGetterVisibility()); assertEquals(Visibility.NONE, merged.getSetterVisibility()); assertEquals(Visibility.ANY, merged.getCreatorVisibility()); assertEquals(Visibility.PROTECTED_AND_PUBLIC, merged.getScalarConstructorVisibility()); // plus, special cases assertSame(overrides, JsonAutoDetect.Value.merge(null, overrides)); assertSame(overrides, JsonAutoDetect.Value.merge(overrides, null)); }
protected void init(ObjectMapper objectMapper) { objectMapper.registerModule(new DefenceModule()); objectMapper.setSerializationInclusion(Include.NON_NULL); objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY).withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.NONE) .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); objectMapper.addMixIn(Throwable.class, ThrowableMixIn.class); }
public static void exportJson(Map<Method, Set<Invocation>> data) { try { ObjectMapper mapper = new ObjectMapper(); mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY)); String formatted = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(data); LOGGER.debug("Dereferences: {}", formatted); } catch (JsonProcessingException exception) { LOGGER.warn("Export of dereferences failed", exception); } }
public String writeValueAsString(Object obj) { ObjectMapper json = new ObjectMapper(); json.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); try { return json.writeValueAsString(obj); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void configureJacksonObjectMapper(ObjectMapper objectMapper) { objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(Visibility.ANY) .withGetterVisibility(Visibility.NONE) .withIsGetterVisibility(Visibility.NONE)); }
private static void initTimeSeriesObjectMapper() { timeSeriesObjectMapper = new ObjectMapper(); timeSeriesObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); timeSeriesObjectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); timeSeriesObjectMapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); timeSeriesObjectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); }
public JsonCodec(boolean indent, boolean privateVisible){ objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); if(indent){ objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); } if(privateVisible){ objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE); } }
public FieldObjectMapper() { // only pay attention to fields setVisibility(PropertyAccessor.FIELD, Visibility.ANY); setVisibility(PropertyAccessor.GETTER, Visibility.NONE); setVisibility(PropertyAccessor.SETTER, Visibility.NONE); setVisibility(PropertyAccessor.CREATOR, Visibility.NONE); setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE); // ignore unknown fields when reading configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // ignore null fields when writing setSerializationInclusion(JsonInclude.Include.NON_NULL); }
private static ObjectMapper createDefaultMapper() { final ObjectMapper mapper = new ObjectMapper(); // Log.debug("Configuring Jackson ObjectMapper: [" + mapper + "]"); mapper.enable(SerializationFeature.INDENT_OUTPUT); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // mapper.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); mapper.registerModule(new Jdk8Module()); return mapper; }
private static ObjectMapper createDefaultMapper() { final ObjectMapper mapper = new ObjectMapper(); Log.debug("Configuring Jackson ObjectMapper: [" + mapper + "]"); mapper.enable(SerializationFeature.INDENT_OUTPUT); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // mapper.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); mapper.registerModule(new Jdk8Module()); return mapper; }
private static void initComplexDataObjectObjectMapper() { complexDataObjectObjectMapper = new ObjectMapper(); complexDataObjectObjectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); complexDataObjectObjectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true); complexDataObjectObjectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); complexDataObjectObjectMapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); complexDataObjectObjectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); }
@Test public void readsBuiltInTypes() { BuiltInTypes obj = JSONRunner.deserialize("{ " + "\"boolField\" : true," + "\"byteField\" : 1," + "\"charField\" : \"a\"," + "\"shortField\" : 2," + "\"intField\" : 3," + "\"longField\" : 4," + "\"floatField\" : 5.1," + "\"doubleField\" : 6.1," + "\"list\" : [ { \"a\" : \"q\", \"b\" : 7 } ]," + "\"set\" : [ { \"a\" : \"w\", \"b\" : 8 } ]," + "\"map\" : { \"e\" : { \"a\" : \"r\", \"b\" : 8 } }," + "\"visibility\" : \"ANY\" }", BuiltInTypes.class); assertTrue(obj.boolField); assertEquals('a', (char)obj.charField); assertEquals(2, (short)obj.shortField); assertEquals(3, (int)obj.intField); assertEquals(4, (long)obj.longField); assertEquals(5.1, obj.floatField, 0.01); assertEquals(6.1, obj.doubleField, 0.01); assertEquals(1, obj.list.size()); assertEquals("q", obj.list.get(0).a); assertEquals(1, obj.set.size()); assertEquals("w", obj.set.iterator().next().a); assertEquals(1, obj.map.size()); assertTrue(obj.map.containsKey("e")); assertEquals("r", obj.map.get("e").a); assertEquals(Visibility.ANY, obj.visibility); }
@Override public VisibilityChecker<?> findAutoDetectVisibility(AnnotatedClass annotatedClass, VisibilityChecker<?> checker) { return checker.withFieldVisibility(Visibility.PUBLIC_ONLY) .withSetterVisibility(Visibility.NONE) .withGetterVisibility(Visibility.NONE) .withIsGetterVisibility(Visibility.NONE); }
public ObjectSerializer() { objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(Visibility.ANY) .withGetterVisibility(Visibility.NONE) .withSetterVisibility(Visibility.NONE) .withIsGetterVisibility(Visibility.NONE) .withCreatorVisibility(Visibility.NONE)); }
/** * Method for configuring object mapper to write from fields * or from getters. * @param flag flag */ private void writeFromFields(final boolean flag) { if (flag) { objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); } else { objectMapper = new ObjectMapper(); } }
public JsonMapper(Include include, boolean fieldVisibility){ objectMapper.setSerializationInclusion(include); // objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false); // setDateFormat(DateUtils.DATE_TIME); objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); objectMapper.configure(Feature.ALLOW_COMMENTS, true); objectMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true); // objectMapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); if(fieldVisibility) objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); this.objectMapper.setFilters(filterProvider); this.typeFactory = this.objectMapper.getTypeFactory(); }
@Test public void clickOut_location_demo() throws JsonProcessingException { SearchQuery searchQuery = new SearchQuery("demo +auto"); searchQuery.setKeyword(operatorFactory.and(operatorFactory.contains("demo"), operatorFactory.contains("auto"))); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); String uri = clickService.clickOut("urn:LocationEntity/testEntity", objectMapper.writeValueAsString(searchQuery)); assertThat(uri, equalTo("http://example.org/real-location")); //Mockito.verify(relationshipRepository, Mockito.times(2)).getByEndPoints(Matchers.any(LocationEntity.class), Matchers.any(Tag.class)); //Mockito.verify(relationshipRepository).save(Matchers.any(WeightedRelation.class)); }