Download presentation
Presentation is loading. Please wait.
Published byWarren Owen Modified over 9 years ago
1
Serialization
2
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 GSON – FLEXJSON FLEXJSON – XStream 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 From Oracle – Small and Simple Small and Simple – http://jaxb.java.net/tutorial/ http://jaxb.java.net/tutorial/ – Xstream Very simple – especially compared to others The one you might want to consider Xstream – Easy Example Easy Example
9
Get the Xstream Jar Download the most recent jar. – Go to here.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 Xstream Declarations import com.thoughtworks.xstream.Xstream; import com.toughtworks.xstream.io.xml.DomDriver private Xstream xmlStream; //variable declaration xmlStream = new Xstream(new DomDriver());
11
Use Xstream Client 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 Xstream Server 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 Xstream To And From a File or To 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.