Naive Serialization
An Introduction with Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
import java.io.*; import org.w3c.dom.*; /** * An encapsulation of a Person. * * This is an example of "naive" serialization. * * @author Prof. David Bernstein, James Madison University * @version 1.0XML */ public class Person { private Address address; private int age; private PhoneNumber[] phoneNumbers; private String firstName, lastName; /** * Construct an empty Person object. */ private Person() { } /** * Populate the attributes of this Person object from * an XML representation. * * @param source The XML representation */ public void fromXML(Element source) { NodeList nodes; firstName = source.getAttribute("firstName"); lastName = source.getAttribute("lastName"); age = Integer.parseInt(source.getAttribute("age")); nodes = source.getElementsByTagName("Address"); address = Address.parseAddress((Element)nodes.item(0)); nodes = source.getElementsByTagName("PhoneNumber"); phoneNumbers = new PhoneNumber[nodes.getLength()]; for (int i=0; i<nodes.getLength(); i++) { phoneNumbers[i] = PhoneNumber.parsePhoneNumber((Element)nodes.item(i)); } } /** * Construct a Person object from an XML representation. * * @param source The XML representation * @return The Person object */ public static Person parsePerson(Element source) { Person result = new Person(); result.fromXML(source); return result; } /** * Create an XML representation of this Person. * * @return The XML representation */ public String toXML() { String result = String.format("<Person firstName=\"%s\" lastName=\"%s\" age=\"%s\" >", firstName, lastName, age); result += "\n" + address.toXML(); for (int i=0; i<phoneNumbers.length; i++) { result += "\n" + phoneNumbers[i].toXML(); } result += "\n" + "</Person>"; return result; } }
import java.io.*; import org.w3c.dom.*; /** * An encapsulation of an Address. * * This is an example of "naive" serialization. * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class Address { private int postalCode; private String city, state, streetAddress; /** * Construct an empty Person object. */ private Address() { } /** * Populate the attributes of this Address object from * an XML representation. * * @param source The XML representation */ public void fromXML(Element source) { streetAddress = source.getAttribute("streetAddress"); city = source.getAttribute("city"); state = source.getAttribute("state"); postalCode = Integer.parseInt(source.getAttribute("postalCode")); } /** * Construct an Address object from an XML representation. * * @param source The XML representation * @return The Address object */ public static Address parseAddress(Element source) { Address result = new Address(); result.fromXML(source); return result; } /** * Create an XML representation of this Address. * * @return The XML representation */ public String toXML() { return String.format("<Address streetAddress=\"%s\" city=\"%s\" state=\"%s\" postalCode=\"%s\" />", streetAddress, city, state, postalCode); } }
import java.io.*; import org.w3c.dom.*; /** * An encapsulation of a PhoneNumber. * * This is an example of "naive" serialization. * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class PhoneNumber { private String number, type; /** * Construct an empty PhoneNumber object. */ private PhoneNumber() { } /** * Populate the attributes of this PhoneNumber object from * an XML representation. * * @param source The XML representation */ public void fromXML(Element source) { number = source.getAttribute("number"); type = source.getAttribute("type"); } /** * Construct a PhoneNumber object from an XML representation. * * @param source The XML representation * @return The Person object */ public static PhoneNumber parsePhoneNumber(Element source) { PhoneNumber result = new PhoneNumber(); result.fromXML(source); return result; } /** * Create an XML representation of this PhoneNumber. * * @return The XML representation */ public String toXML() { return String.format("<PhoneNumber type=\"%s\" number=\"%s\" />", type, number); } }
import java.io.*; import org.w3c.dom.*; import javax.xml.parsers.*; /** * A driver for an example that illustrates the use of XML * for "naive" serialization. * * @author Prof. David Bernstein, Jmes Madison University * @version 1.0 */ public class PersonDriver { /** * The entry point of the application. * * @args The command line arguments (ignored) */ public static void main(String[] args) throws Exception { File file = new File("person.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(file); Element root = document.getDocumentElement(); Person q = Person.parsePerson(root); System.out.println(q.toXML()); } }
import java.io.*; import javax.json.*; import javax.json.stream.*; /** * An encapsulation of a Person. * * This is an example of "naive" serialization. * * @author Prof. David Bernstein, James Madison University * @version 1.0JSON */ public class Person { private Address address; private int age; private PhoneNumber[] phoneNumbers; private String firstName, lastName; /** * Construct an empty Person object. */ private Person() { } /** * Populate the attributes of this Person object from * a JSON representation. * * @param source The JSON representation */ public void fromJSON(JsonObject source) { firstName = source.getString("firstName"); lastName = source.getString("lastName"); age = source.getInt("age"); address = Address.parseAddress(source.getJsonObject("address")); JsonArray pns = source.getJsonArray("phoneNumbers"); phoneNumbers = new PhoneNumber[pns.size()]; for (int i=0; i<phoneNumbers.length; i++) { JsonObject pn = pns.getJsonObject(i); phoneNumbers[i] = PhoneNumber.parsePhoneNumber(pn); } } /** * Construct a Person object from a JSON representation. * * @param source The JSON representation * @return The Person object */ public static Person parsePerson(JsonObject source) { Person result = new Person(); result.fromJSON(source); return result; } /** * Create a JSON representation of this Person. * * @return A String containing the JSON representation */ public String toJSON() { StringWriter out = new StringWriter(); JsonGenerator g = Json.createGenerator(out); toJSON(g, null); g.close(); return out.toString(); } /** * Create a JSON representation of this Person using the * given JsonGenerator. * * @param g The JsonGenerator to use * @param name The name for the JsonObject (or null for none) */ public void toJSON(JsonGenerator g, String name) { if (name == null) g.writeStartObject(); else g.writeStartObject(name); g.write("firstName", firstName); g.write("lastName", lastName); g.write("age", 25); address.toJSON(g, "address"); g.writeStartArray("phoneNumbers"); for (int i=0; i<phoneNumbers.length; i++) { phoneNumbers[i].toJSON(g, null); } g.writeEnd(); g.writeEnd(); } }
import java.io.*; import javax.json.*; import javax.json.stream.*; /** * An encapsulation of an Address. * * This is an example of "naive" serialization. * * @author Prof. David Bernstein, James Madison University * @version 1.0JSON */ public class Address { private int postalCode; private String city, state, streetAddress; /** * Construct an empty Person object. */ private Address() { } /** * Populate the attributes of this Address object from * a JSON representation. * * @param source The JSON representation */ public void fromJSON(JsonObject source) { streetAddress = source.getString("streetAddress"); city = source.getString("city"); state = source.getString("state"); postalCode = source.getInt("postalCode"); } /** * Construct an Address object from a JSON representation. * * @param source The JSON representation * @return The Address object */ public static Address parseAddress(JsonObject source) { Address result = new Address(); result.fromJSON(source); return result; } /** * Create a JSON representation of this Address. * * @return A String containing the JSON representation */ public String toJSON() { StringWriter out = new StringWriter(); JsonGenerator g = Json.createGenerator(out); toJSON(g, null); g.close(); return out.toString(); } /** * Create a JSON representation of this Person using the * given JsonGenerator. * * @param g The JsonGenerator to use * @param name The name for the JsonObject (or null for none) */ public void toJSON(JsonGenerator g, String name) { if (name == null) g.writeStartObject(); else g.writeStartObject(name); g.write("streetAddress", streetAddress); g.write("city", city); g.write("state", state); g.write("postalCode", postalCode); g.writeEnd(); } }
import java.io.*; import javax.json.*; import javax.json.stream.*; /** * An encapsulation of a PhoneNumber. * * This is an example of "naive" serialization. * * @author Prof. David Bernstein, James Madison University * @version 1.0JSON */ public class PhoneNumber { private String number, type; /** * Construct an empty PhoneNumber object. */ private PhoneNumber() { } /** * Populate the attributes of this PhoneNumber object from * a JSON representation. * * @param source The JSON representation */ public void fromJSON(JsonObject source) { type = source.getString("type"); number = source.getString("number"); } /** * Construct a PhoneNumber object from a JSON representation. * * @param source The JSON representation * @return The Person object */ public static PhoneNumber parsePhoneNumber(JsonObject source) { PhoneNumber result = new PhoneNumber(); result.fromJSON(source); return result; } /** * Create a JSON representation of this PhoneNumber. * * @return A String containing the JSON representation */ public String toJSON() { StringWriter out = new StringWriter(); JsonGenerator g = Json.createGenerator(out); toJSON(g, null); g.close(); return out.toString(); } /** * Create a JSON representation of this PhoneNumber using the * given JsonGenerator. * * @param g The JsonGenerator to use * @param name The name for the JsonObject (or null for none) */ public void toJSON(JsonGenerator g, String name) { if (name == null) g.writeStartObject(); else g.writeStartObject(name); g.write("type", type); g.write("number", number); g.writeEnd(); } }
import java.io.*; import javax.json.*; /** * An example of "naive" serialization using JSON. * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class PersonDriver { /** * The entry point of the application. * * @param args The command line arguments (ignored) */ public static void main(String[] args) throws Exception { File file = new File("person.json"); BufferedReader in = new BufferedReader(new FileReader(file)); JsonReader reader = Json.createReader(in); JsonObject person = reader.readObject(); Person p = Person.parsePerson(person); System.out.println(p.toJSON()); } }
XMLable
JSONable