In [1]:
import java.util.Date;
public class SensorReading {
private Date timestamp;
private String dimension;
private double value;
private String unity;
public SensorReading() {
/* nothing */
}
public SensorReading(Date timestamp, String dimension, double value, String unity) {
this.timestamp = timestamp;
this.dimension = dimension;
this.value = value;
this.unity = unity;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public String getDimension() {
return dimension;
}
public void setDimension(String dimension) {
this.dimension = dimension;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public String getUnity() {
return unity;
}
public void setUnity(String unity) {
this.unity = unity;
}
public String toString() {
return "time stamp: " + timestamp +
"\ndimension: " + dimension +
"\nvalue: " + value +
"\nunity: " + unity;
}
}
Out[1]:
In [2]:
import java.util.Date;
import java.util.Calendar;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Date today = Calendar.getInstance().getTime();
SensorReading sensor = new SensorReading(today, "temperature", 25, "centigrades");
try {
XMLEncoder encoder = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("temperature.xml") ) );
encoder.writeObject(sensor);
encoder.close();
System.out.println("Objeto gravado com sucesso!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Out[2]:
In [3]:
import java.util.Date;
import java.util.Calendar;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
try {
XMLDecoder decoder = new XMLDecoder(
new BufferedInputStream(
new FileInputStream("temperature.xml") ) );
SensorReading sensor = (SensorReading)decoder.readObject();
decoder.close();
System.out.println(sensor);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Out[3]:
In [4]:
%classpath add jar ../../../../../src/lib/genson-1.6.jar
In [5]:
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
import java.io.FileWriter;
import java.io.IOException;
Date today = Calendar.getInstance().getTime();
SensorReading sensor = new SensorReading(today, "temperature", 25, "centigrades");
try {
Genson genson = new GensonBuilder()
.useDateFormat(new SimpleDateFormat("yyyy-MM-dd"))
.useIndentation(true)
.useConstructorWithArguments(false)
.create();
String json = genson.serialize(sensor);
System.out.println(json);
FileWriter arquivo = new FileWriter("temperature.json");
arquivo.write(json);
arquivo.close();
} catch (IOException e) {
e.printStackTrace();
}
Out[5]:
In [6]:
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
try {
FileReader arquivo = new FileReader("temperature.json");
BufferedReader formatado = new BufferedReader(arquivo);
String json = "";
String linha = formatado.readLine();
while (linha != null) {
json += linha + "\n";
System.out.println(linha);
linha = formatado.readLine();
}
arquivo.close();
Genson genson = new GensonBuilder()
.useDateFormat(new SimpleDateFormat("yyyy-MM-dd"))
.useIndentation(true)
.useConstructorWithArguments(false)
.create();
SensorReading sensor = genson.deserialize(json, SensorReading.class);
System.out.println(sensor);
} catch (IOException erro) {
erro.printStackTrace();
}
Out[6]:
Considere três sensores de temperatura:
SensorReading sensor[] = {new SensorReading(today, "temperature", 25, "centigrades"),
new SensorReading(today, "temperature", 28, "centigrades"),
new SensorReading(today, "temperature", 26, "centigrades")};
Realize a gravação desses três sensores em um único arquivo JSON.
In [ ]:
In [ ]:
Considere uma das bases de saúde daquelas selecionadas em sala de aula.
In [ ]: