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();
    }
}
