130 likes | 270 Vues
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.
E N D
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
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
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
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
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
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
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
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
Use XstreamDeclarations • import com.thoughtworks.xstream.Xstream; • import com.toughtworks.xstream.io.xml.DomDriver • private XstreamxmlStream; //variable declaration • xmlStream = new Xstream(new DomDriver());
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
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
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