@Override public int _writeItems(MeasurementPOJO items, Writer out) throws Exception { mapper.writeValue(out, items); return items.size(); }
mapper.writeValue( file, user );
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 ()); }
@Override public int _writeItems(MeasurementPOJO items, OutputStream out) throws Exception { mapper.writeValue(out, items); return items.size(); }
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 ); }
@Test public void test() { final ObjectMapper mapper = JsonFactory.createUseProperties(true); ByteArrayOutputStream stream = new ByteArrayOutputStream(); mapper.writeValue(stream, FrequentPrimitives.getArray(2)); puts(new String(stream.toByteArray())); }
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 ()); }
@Test public void test2() { final ObjectMapper mapper = JsonFactory .createUseProperties(true); ByteArrayOutputStream stream = new ByteArrayOutputStream(); //String uri, String title, int width, int height, Size size mapper.writeValue(stream, new Image("/foo", "Foo", 5, 10, Image.Size.SMALL)); puts(new String(stream.toByteArray())); }
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() ); }
@Benchmark @Override public Object boon() { ByteArrayOutputStream baos = JsonUtils.byteArrayOutputStream(); JSON_SOURCE().provider().boon().writeValue(baos, JSON_SOURCE().nextPojo()); return baos; }
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 main(String... args) throws Exception{ Player kevin = new Player("Kevin", "Cricket", 32, 221, new int[]{33, 66, 78, 21, 9, 200}); final ObjectMapper mapper = JsonFactory.create(); final File file = File.createTempFile("json", "exmaple.json"); mapper.writeValue(file, kevin); Player somePlayer = mapper.readValue(file, Player.class); puts("They are equal", somePlayer.equals(kevin)); }
mapper.writeValue( file, users ); List<User> userList = mapper.readValue( file, List.class, User.class ); puts (userList);
@Test public void test3() { final ObjectMapper mapper = JsonFactory .createUseProperties(true); ByteArrayOutputStream stream = new ByteArrayOutputStream(); MediaContent mediaContent = MediaContent.mediaContent(); //String uri, String title, int width, int height, Size size mapper.writeValue(stream, mediaContent); MediaContent mediaContent2 = mapper.readValue(stream.toByteArray(), MediaContent.class); boolean ok = mediaContent.equals(mediaContent2) || die(); }