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:
bouclyma 2024-12-11 10:36:30 +01:00
parent 5805e55209
commit 63c165f024
4 changed files with 222 additions and 0 deletions

View 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"));
}
}