1 / 13

Serialization

Serialization. Serialization. Serialization is the process of converting an object into an intermediate format that can be stored (e.g. in a file or transmitted across a network) and "resurrected" later in the same or another computer. We will focus on machine to machine communication.

damia
Download Presentation

Serialization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Serialization

  2. Serialization • Serialization is the process of converting an object into an intermediate format that can be stored (e.g. in a file or transmitted across a network) and "resurrected" later in the same or another computer. • We will focus on machine to machine communication. • Can be used for persistence

  3. Serialization • Convert language defined types and user defined types to standard intermediate form • Primitive types: e.g. integer, short, long, double, float, boolean, char, array • Collections: ArrayList, Map, Sets, etc. • User defined typed: Classes and enums • Must solve the graph traversal problem for types that are recursive

  4. Data Interchange Format • “Binary” • XML • The standard for web applications • We will use this in our project • JSON • Can be used for inter-language communication • Becoming popular and another standard for web applications

  5. Data Interchange Format • “Binary” – Java Serialization • Only works in Java to Java Communication • Faster • Not human readable • Mechanism • For user defined types extend “Serializable” • ObjectInputStream • ObjectOutputStream

  6. Data Interchange Format • JSON • Text Based – “Human Readable” • Supposedly simpler than XML • All Objects can converted to a standard form by using a combination of the following conversions and annotations • Number (double precision) • String (double-quoted Unicode, with backslash escaping) • Boolean (true or false) • Array (an ordered sequence of values, comma-separated and enclosed in square brackets; the values do not need to be of the same type) • Object (an unordered collection of key:value pairs with the ':' character separating the key and the value, comma-separated and enclosed in curly braces; the keys must be strings and should be distinct from each other) • null (empty • JSON code for Java • GSON • FLEXJSON • XStream

  7. Data Interchange Format • XML • Text based – “Human Readable” • XML structures tend to mimic the data structures of the encoding language • Implemented techniques often only work when the source language and destination language are the same • Encoding/Decoding takes time

  8. XML Software • There are many, here are two • JAXB – built into java • Not as easy as it could be • Tutorials • From Oracle • Small and Simple • http://jaxb.java.net/tutorial/ • Xstream • Very simple – especially compared to others • The one you might want to consider • Xstream • Easy Example

  9. Get the Xstream Jar • Download the most recent jar. • Go to here. • Click on the most recent version (as of this time it is 1.4.3) • Download the main jar in the directory (currently it is xstream-1.4.3.jar) • Install it in your build path

  10. Use XstreamDeclarations • import com.thoughtworks.xstream.Xstream; • import com.toughtworks.xstream.io.xml.DomDriver • private XstreamxmlStream; //variable declaration • xmlStream = new Xstream(new DomDriver());

  11. Use XstreamClient Side • Client Side • Object result = xmlStream.fromXML(connection.getInputStream()); • Used when transforming the results of a GET request to an HTTP Server • The connection is of type HttpURLConnection returned from executing “openConnection()” on an object of type URL. Used to execute the GET request and getting the results. • xmlStream.toXML(object, connection.getOutputStream()); • Used to send an object (usually a command) to an HTTP server

  12. Use XstreamServer Side • Client Side • SomeType type = (SomeType)xmlStream.fromXML(exchange.getRequestBody()); • The exchange is of type HttpExchange which is an abstract representation of an instance of communication from a client to this server. • The “getRequestBody()” returns the information as an InputStream • xmlStream.toXML(responseObject, exchange.getResponseBody()); • Used to return an answer to the client • “getResponseBody()” returns an OutputStream

  13. Use XstreamTo And From a File orTo A String • Object object = xmlStream.fromXML(file) • The file is of type File • Converts the contents of an XML file to an object • The object is usually type cast to the appropriate type. • xmlStream.toXML(object, fileOutputStream); • Used to convert an object to an XML representation and writes it to a FileOutputStream (a file) • String string = xmlStream.toXML(object); • Converts an object to its XML representation

More Related