From javax.json.JsonObject To byte[] And Back : Adam Bien's Weblog

From javax.json.JsonObject To byte[] And Back : Adam Bien's Weblog Adam Bien's Weblog « Trouble-Free Shoppin... | Main | More Questions Than... » From javax.json.JsonObject To byte[] And Back Enjoy self-explanatory, Java EE 7 code :-): import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonReader; import javax.json.JsonWriter; //... JsonObject deserialize(byte[] content) throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream(content); JsonReader reader = Json.createReader(bais)) { return reader.readObject(); } } byte[] serialize(JsonObject object) throws IOException { try (ByteArrayOutputStream oos = new ByteArrayOutputStream(); JsonWriter writer = Json.createWriter(oos)) { writer.writeObject(object); writer.close(); oos.flush(); return oos.toByteArray(); } } //... @Test public void reserialize() throws IOException { JsonObject expected = Json.createObjectBuilder(). add("name", "duke"). build(); byte[] serialized = serialize(expected); JsonObject actual = deserialize(serialized); assertNotSame(actual, expected); assertThat(actual, is(expected)); } See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting Posted at 12:24PM Jul 02, 2015 by Adam Bien , Comments[0]  | Views/Hits: 451 NEW workshop: HTML 5 and JavaScript Essentials , Three days in April 2017: from Java EE 7 Architectures over Microservices to Performance, Troubleshooting and Monitoring On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming. Newsletter: airhacks.news A book about rethinking Java EE Patterns Comments: Post a Comment: Name: E-Mail: URL: Notify me by email of new comments Remember Information? Your Comment: HTML Syntax: NOT allowed Number of posts: 1619 Number of comments: 5897 Yesterday's hits: 26476 Today's hits: 22049 Post reads / hour: 1018 Top posts: Adam Bien 430078 Adam Bien's Weblog 107617 Java 8: Reading A File Into A String : Adam Bien's Weblog 75913 Java 7+: Writing A String To File--A One Liner : Adam Bien's Weblog 48221 Simplest Possible EJB 3.1 / REST (JSR-311) Component : Adam Bien's Weblog 24328 Search Results for '' : Adam Bien's Weblog 22487 Client-Side HTTP Basic Access Authentication With JAX-RS 2.0 : Adam Bien's [...] 22052 Trending (last hour): JMS Redelivery, HA Singletons, Message Ordering, Test Coverage, JSON-P vs.[...] 16 Java 8 FlatMap Example : Adam Bien's Weblog 4 Java 8: Reading A File Into A String : Adam Bien's Weblog 4 What are the options of remote debugging like:-Xrunjdwp, server, suspend? :[...] 3 Singleton Pattern in ES6 and ES7 : Adam Bien's Weblog 3 about.adam-bien.com blog archives realworldpatterns.com Real World Java EE Patterns Rethinking Best Practices Order from amazon.de Order from amazon.com Online Workshops ...the last 150 posts JMS Redelivery, HA Singletons, Message Ordering, Test Coverage, JSON-P vs. JSON-B, UI Strategies or 36th airhacks.tv Episode Is Available Java 8: An Endless Stream Of Random Numbers HA Singletons, Clouds vs. On-Prem, Messaging and Message Ordering, UI-Stack Options, Swagger, Code Coverage or 36th airhacks.tv Pushing Full Profile Java EE Applications To Amazon EC2 Container Service 20+ Topics in 45 mins -- 35th airhacks.tv Questions and Answers JPA and Microservices, RxJava, CRUD and TX, Gradle, Cross Field Validation, Licensing, MDA or Topics For The 35th airhacks.tv JSON Is The New Data Transfer Object (DTO) Asynchronous WAR To WAR Communication With WebSockets Creating A CustomElement (WebComponent) From Scratch 60k eBills per Hour, Tiny WARs -- Or Java EE 7 In Mass Billing and Public Services Market in Slovenia Launching Java EE Thin WARs In The Clouds Upcoming Cloud, Microservice, Web and Java EE Events Importance of Java EE, JCP, Java EE Guardians and Microprofile--Interview With Reza Rahman Java 8 Base64 Encoding / Decoding Java 8 partitioningBy Example Java 8 groupingBy Example Java Is #1 In January 2017 Spring Boot vs. Wordpress, SOAP Rocks, CDI CMT, Reactive vs. Blocking IO, or 34th airhacks.tv is available 2017 Predictions Java EE vs. Spring, State of Microprofile, Is SOAP nice? Oracle targets non-payers or the topics for the 1.2017 / 34th airhacks.tv My Blog is a Personal Notepad Java 8: Converting An Array To A Stream Scalable, Productive, Decoupled, Resilient and Hacky New 2017! How To Maximize Fun In Enterprise Projects URL Templates With JAX-RS GlassFish on Steroids, Micro, Full, Clouds and Commercial Support--An Interview With Dominika Tasarz Load Balancing Microservices With Docker, nginx and TomEE Accidental Spring Evangelism, Rolling Updates, Log Management, Exception Factories, Docker, Queuing Choices or 33rd airhacks.tv Java EE 8 News, Central Logs, Layering, Util, ExceptionFactories, JDBC in Docker, DTOs, Lombok--Or 33rd airhacks.tv TomEE 7.0.2 Full--Docker Smoke Test Slicing The Monolith--With Java EE