vert-x3 / vertx-mqtt

Vert.x MQTT
Apache License 2.0
185 stars 88 forks source link

The from json constructor and to json method of MqttWill class are not reversible #176

Closed ghost closed 4 years ago

ghost commented 4 years ago

MqttWill class has a from json constructor:

public MqttWill(JsonObject json) {
    this.isWillFlag = json.getBoolean("isWillFlag");
    this.willTopic = json.getString("willTopic");
    this.willMessage = json.getString("willMessage").getBytes(Charset.forName("UTF-8"));
    this.willQos = json.getInteger("willMessage");
    this.isWillRetain = json.getBoolean("isWillRetain");
  }

and a to json method:

  public JsonObject toJson() {
    JsonObject json = new JsonObject();
    json.put("isWillFlag", this.isWillFlag);
    json.put("willTopic", this.willTopic);
    json.put("willMessage", this.willMessage);
    json.put("willQos", this.willQos);
    json.put("isWillRetain", this.isWillRetain);
    return json;
  }

But these two are not reversible, which means, if I create a MqttWill instance by:

 public MqttWill(boolean isWillFlag, String willTopic, byte[] willMessage, int willQos, boolean isWillRetain) {
    this.isWillFlag = isWillFlag;
    this.willTopic = willTopic;
    this.willMessage = willMessage;
    this.willQos = willQos;
    this.isWillRetain = isWillRetain;
  }

and then get a json by toJson method, and then call MqttWill(JsonObject json) constructor will get a MqttWill instance which the message field is different!!! I have found the reason. It is because of when call toJson method, willMessage byte[] will be cast to string by Base64, and then when call MqttWill(JsonObject json) constructor, willMessage byte[] cast by getBytes(Charset.forName("UTF-8")).

In addition, because of #135 , even I can not test this problem, but I have writed a same class which has solved #135 problem and tested this problem.

Please check.