public static ObjectMapper createUseAnnotations (boolean useJsonDates) { JsonParserFactory jpf = new JsonParserFactory(); JsonSerializerFactory jsf = new JsonSerializerFactory(); jsf.useAnnotations(); if (useJsonDates) { jsf.useJsonFormatForDates(); } return new ObjectMapperImpl(jpf, jsf); }
public static ObjectMapper createUseAnnotations (boolean useJsonDates) { JsonParserFactory jpf = new JsonParserFactory(); JsonSerializerFactory jsf = new JsonSerializerFactory(); jsf.useAnnotations(); if (useJsonDates) { jsf.useJsonFormatForDates(); } return new ObjectMapperImpl(jpf, jsf); }
public static ObjectMapper createUseAnnotations (boolean useJsonDates) { JsonParserFactory jpf = new JsonParserFactory(); JsonSerializerFactory jsf = new JsonSerializerFactory(); jsf.useAnnotations(); if (useJsonDates) { jsf.useJsonFormatForDates(); } return new ObjectMapperImpl(jpf, jsf); }
@Test public void subclass() { AllTypes foo = new AllTypes (); foo.pet = new Dog (); foo.pet.name = "Mooney"; ((Dog)foo.pet).barks = true; final JsonSerializer serializer = new JsonSerializerFactory ().useAnnotations().create (); //puts ( serializer.serialize ( foo ).toString () ); Map <String, Object> map = ( Map<String, Object> ) jsonParserAndMapper.parse ( serializer.serialize ( foo ).toString () ); Map <String, Object> petMap = ( Map<String, Object> ) map.get ( "pet" ); String className = (String)petMap.get("class"); boolean ok = className.endsWith ( ".Dog" ) || die(className); AllTypes foo2 = jsonParserAndMapper.parse ( AllTypes.class, serializer.serialize ( foo ).toCharArray () ); Dog dog = (Dog)foo2.pet; ok = dog.name.equals ( "Mooney" ) || die( dog.name ); }
@Test public void interfaceTest() { AllTypes foo = new AllTypes (); foo.pet2 = new Dog(); foo.pet2.name("Mooney"); ((Dog)foo.pet2).barks = true; final JsonSerializer serializer = new JsonSerializerFactory ().useAnnotations().create (); //puts ( serializer.serialize ( foo ).toString () ); Map <String, Object> map = ( Map<String, Object> ) jsonParserAndMapper.parse ( serializer.serialize ( foo ).toString () ); Map <String, Object> petMap = ( Map<String, Object> ) map.get ( "pet2" ); String className = (String)petMap.get("class"); boolean ok = className.endsWith ( ".Dog" ) || die(className); AllTypes foo2 = jsonParserAndMapper.parse ( AllTypes.class, serializer.serialize ( foo ).toCharArray () ); Dog dog = (Dog)foo2.pet2; ok = dog.name.equals ( "Mooney" ) || die( dog.name ); }
public static void part10WorkingWithViews() throws Exception { final User rick = BeanUtils.copy( user ); rick.getName().setFirst( "Rick" ); boolean ok = true; JsonSerializer serializer = new JsonSerializerFactory().useAnnotations().setView( "public" ).create(); String str = serializer.serialize( rick ).toString(); puts (str); ok |= !str.contains( "\"empId\":" ) || die(str); serializer = new JsonSerializerFactory().useAnnotations().setView( "internal" ).create(); str = serializer.serialize( rick ).toString(); ok |= str.contains( "\"empId\":" ) || die(str); puts (str); serializer = new JsonSerializerFactory().useAnnotations().create(); str = serializer.serialize( rick ).toString(); ok |= str.contains( "\"empId\":" ) || die(str); puts (str); }
public void test3() { user = new User("red", "yellow", "green", "purple"); puts(user.getFavoriteColors()); final JsonParserFactory jsonParserFactory = new JsonParserFactory().usePropertiesFirst().useAnnotations(); final JsonSerializerFactory serializerFactory = new JsonSerializerFactory().includeNulls().useAnnotations(); final ObjectMapper objectMapper = new ObjectMapperImpl(jsonParserFactory, serializerFactory); json = objectMapper.toJson(user); puts(json); user2 = objectMapper.fromJson(json, User.class); puts(user.getFavoriteColors()); ok = user.equals(user2) || die("Users should be equal", user, user2); }
.useAnnotations () .addFilter ( new FieldFilter () { @Override