@Override public String toJson(Object value) throws Exception { return this.objectMapper.writeValueAsString(value); }
@Override public String toJson(Object value) throws Exception { return this.objectMapper.writeValueAsString(value); }
@Override public String _writeAsString(MeasurementPOJO items) throws Exception { return mapper.writeValueAsString(items); } }
@Test public void test198() { Data original = new Data(Integer.MAX_VALUE, Long.MAX_VALUE); System.out.println("original: \n" + original + "\n"); ObjectMapper boon = JsonFactory.create(); String serialized = boon.writeValueAsString(original); System.out.println("serialized: \n" + serialized + "\n"); Data deserialized = boon.readValue(serialized, Data.class); System.out.println("deserialized: \n" + deserialized + "\n"); String reserialized = boon.writeValueAsString(deserialized); System.out.println("reserialized: \n" + reserialized + "\n"); }
public static void main(String[] args) throws IOException { Builder builder = new Builder(); MetadataImpl metadataImpl = builder.getMetadata(); JsonSerializerFactory jsonSerializerFactory=new JsonSerializerFactory().usePropertyOnly(); ObjectMapper mapper = JsonFactory.create(null, jsonSerializerFactory); String json = mapper.writeValueAsString(metadataImpl); System.out.println("=============" + json); File file = new File("metadata.json"); FileWriter writer = new FileWriter(file); mapper.toJson(metadataImpl, writer); writer.close(); Path path = Paths.get(file.toString()); InputStream inputStream = Files.newInputStream(path); MetadataImpl object = JsonFactory.create().readValue(inputStream, MetadataImpl.class); inputStream.close(); System.out.println("after deserialization" + mapper.writeValueAsString(object)); }
public static void part3_1() throws Exception { ObjectMapper mapper = JsonFactory.create(); puts( mapper.writeValueAsString( user ) ); User user2 = mapper.readValue( mapper.writeValueAsString( user ), User.class ); puts( user2 ); boolean ok = user.equals ( user2 ) || die (user.toString ()); }
public static void part3_2() throws Exception { ObjectMapper mapper = JsonFactory.createUseJSONDates(); puts( mapper.writeValueAsString( user ) ); User user2 = mapper.readValue( mapper.writeValueAsString( user ), User.class ); puts( user2 ); boolean ok = user.equals ( user2 ) || die (user.toString ()); }
public static void part4IntoAMapFirst() throws Exception { ObjectMapper mapper = JsonFactory.createUseJSONDates(); puts( mapper.writeValueAsString( user ) ); //Now to write and then read this as a file. File file = File.createTempFile( "user", ".json" ); mapper.writeValue( file, user ); Object userFromFile = mapper.readValue( file, Object.class ); puts( "userFromFile", "type", userFromFile.getClass(), "value", userFromFile ); Map<String, Object> map = (Map<String, Object>) mapper.readValue( file, Map.class ); puts( "userFromFile", "type", map.getClass(), "value", map ); puts( "userFromFile.name", "type", map.get("name").getClass(), "value", map.get("name") ); puts( "userFromFile.birthDate", "type", map.get("birthDate").getClass(), "value", map.get("birthDate") ); puts( "userFromFile.gender", "type", map.get("gender").getClass(), "value", map.get("gender") ); User userFromMap = MapObjectConversion.fromMap( map, User.class); puts ( userFromMap ); boolean ok = user.equals ( userFromMap ) || die (userFromMap.toString ()); }
@Test public void test() { JsonSerializerFactory factory = new JsonSerializerFactory(); factory.usePropertyOnly(); ObjectMapper mapper = JsonFactory.create(null, factory); User user = new User(); EnumMap<Gender,User> map = new EnumMap<Gender, User>(Gender.class); map.put(Gender.FEMALE, user); puts(map); puts(mapper.writeValueAsString( map)); } }
puts( mapper.writeValueAsString( user ) );
public static void part1ReadAndWriteMyBeanToAFile() throws Exception { MyBean myBean = new MyBean(); File dst = File.createTempFile( "emp", ".json" ); ObjectMapper mapper = JsonFactory.create(); puts( "json string", mapper.writeValueAsString( myBean ) ); String str = mapper.writeValueAsString ( myBean ); boolean ok = str.contains ( "{\"name\":\"Rick\"" ) || die( str ); mapper.writeValue( dst, myBean ); // where 'dst' can be File, OutputStream or Writer File src = dst; MyBean value = mapper.readValue( src, MyBean.class ); // 'src' can be File, InputStream, Reader, String ok |= value.name.contains ( "Rick" ); puts( "mybean", value ); Object root = mapper.readValue( src, Object.class ); Map<String, Object> rootAsMap = mapper.readValue( src, Map.class ); puts( "root", root ); puts( "rootAsMap", rootAsMap ); MyBean myBean1 = new MyBean(); myBean1.name = "Diana"; MyBean myBean2 = new MyBean(); myBean2.name = "Rick"; dst = File.createTempFile( "empList", ".json" ); final List<MyBean> list = Lists.list( myBean1, myBean2 ); str = mapper.writeValueAsString ( list ); puts ( "json string", mapper.writeValueAsString ( list ) ); ok |= str.contains ( "[{\"name\":\"Diana\"},{\"name\":\"Rick\"}]" ) || die (str); mapper.writeValue( dst, list ); src = dst; List<MyBean> beans = mapper.readValue( src, List.class, MyBean.class ); puts( "mybeans", beans ); }
public static void part5WorkingWithLists() throws Exception { puts ("\n\n\n", "\npart5 WorkingWithLists"); ObjectMapper mapper = JsonFactory.createUseJSONDates(); final User diana = BeanUtils.copy( user ); final User rick = BeanUtils.copy( user ); diana.getName().setFirst( "Diana" ); rick.getName().setFirst( "Rick" ); diana.setBirthDate( Dates.getUSDate( 8, 21, 1984 ) ); File file = File.createTempFile( "userList", ".json" ); List<User> users = Lists.list( diana, rick ); mapper.writeValue( file, users ); List<User> userList = mapper.readValue( file, List.class, User.class ); puts (userList); puts ( mapper.writeValueAsString( userList ) ); boolean ok = users.toString().equals ( userList.toString () ) || die (userList.toString ()); }
public static void part7WorkingWithListFromFile() throws Exception { puts ("\n\n\n", "\npart7WorkingWithListFromFile"); ObjectMapper mapper = JsonFactory.createUseAnnotations( true ); /* Create two users. */ final User diana = BeanUtils.copy( user ); final User rick = BeanUtils.copy( user ); diana.getName().setFirst( "Diana" ); diana.setGender ( User.Gender.FEMALE ); rick.getName().setFirst ( "Rick" ); rick.ssn="IAMSET"; diana.ssn="dianaSSN"; diana.setBirthDate( Dates.getUSDate( 8, 21, 1984 ) ); File file = File.createTempFile( "userList", ".json" ); List<User> users = Lists.list( diana, rick ); /* Inspect the JSON of the users from the file. */ puts ("users", mapper.writeValueAsString( users ) ); /* Write users out to file. */ mapper.writeValue( file, users ); /* Reader Users back from file. */ List<User> userList = mapper.readValue( file, List.class, User.class ); puts ("userListBeansReadFromFile", userList); /* Inspect the JSON of the users from the file. */ puts ("usersFromFileAsJSON", mapper.writeValueAsString( userList ) ); boolean ok = userList.toString().equals ( users.toString() ) || die ( userList.toString() ); }
public static void part6WorkingWithLists() throws Exception { puts ("\n\n\n", "\npart6WorkingWithLists"); ObjectMapper mapper = JsonFactory.createUseJSONDates(); final User diana = BeanUtils.copy( user ); final User rick = BeanUtils.copy( user ); diana.getName().setFirst( "Diana" ); diana.setGender( User.Gender.FEMALE ); rick.getName().setFirst( "Rick" ); diana.setBirthDate( Dates.getUSDate( 8, 21, 1984 ) ); File file = File.createTempFile( "userList", ".json" ); List<User> users = Lists.list( diana, rick ); mapper.writeValue( file, users ); List<User> userList = mapper.readValue( file, List.class, User.class ); puts (userList); puts ( mapper.writeValueAsString( userList ) ); boolean ok = userList.toString().equals ( users.toString() ) || die ( userList.toString() ); }
List<User> userList = mapper.readValue( file, List.class, User.class ); puts (userList); puts ( mapper.writeValueAsString( userList ) );