mirror of
https://github.com/Akomry/sae302_applicom.git
synced 2025-12-06 11:43:53 +00:00
feat(message): Création de la classe Message, méthodes pour générer du JSON à partir de l'objet et inversement
This commit is contained in:
parent
5805e55209
commit
63c165f024
4 changed files with 222 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ module rtgre.chat {
|
|||
requires java.desktop;
|
||||
requires javafx.swing;
|
||||
requires net.synedra.validatorfx;
|
||||
requires org.json;
|
||||
|
||||
|
||||
opens rtgre.chat to javafx.fxml;
|
||||
|
|
|
|||
57
chat/src/main/java/rtgre/modeles/Message.java
Normal file
57
chat/src/main/java/rtgre/modeles/Message.java
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package rtgre.modeles;
|
||||
import org.json.JSONObject;
|
||||
|
||||
|
||||
public class Message {
|
||||
/**
|
||||
* Un message décrit sous la forme :
|
||||
* @serialField : String to: Le destinataire
|
||||
* @serialField : String body: le corps du message
|
||||
*/
|
||||
protected String to;
|
||||
protected String body;
|
||||
|
||||
public Message(String to, String body) {
|
||||
|
||||
this.to = to;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Message{" +
|
||||
"to=" + to +
|
||||
", body=" + body +
|
||||
'}';
|
||||
}
|
||||
|
||||
public JSONObject toJsonObject() {
|
||||
/**
|
||||
* Transforme le message courant en objet JSON
|
||||
*/
|
||||
return new JSONObject("{to:%s,body:%s}".formatted( this.to, this.body));
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
/**
|
||||
* Transforme l'objet courant en String JSON
|
||||
*/
|
||||
return toJsonObject().toString();
|
||||
}
|
||||
|
||||
public static Message fromJson(JSONObject json) {
|
||||
/**
|
||||
* Crée un objet message à partir d'un objet JSON
|
||||
* @param: JSONObject json: l'objet JSON à transformer
|
||||
*/
|
||||
return new Message(json.getString("to"), json.getString("body"));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue