33import com .google .gson .*;
44import com .google .protobuf .*;
55import com .google .protobuf .util .JsonFormat ;
6+ import org .jspecify .annotations .Nullable ;
67
78import java .lang .reflect .InvocationTargetException ;
89import java .util .Optional ;
910
1011/**
1112 * Utility class for serializing ModAPI messages to JSON format.
1213 */
14+ @ SuppressWarnings ("unused" )
1315public final class ModAPIUtility {
1416
1517 private static final JsonFormat .Parser JSON_PARSER = JsonFormat .parser ().ignoringUnknownFields ();
@@ -27,13 +29,29 @@ private ModAPIUtility() {
2729 * @throws InvalidProtocolBufferException if serialization fails
2830 */
2931 public static String serializeMessage (final Message message ) throws InvalidProtocolBufferException {
30- final String json = JsonFormat .printer ().print (message );
31- final JsonObject jsonObject = JsonParser .parseString (json ).getAsJsonObject ();
32- final Optional <String > messageId = ModAPIMessages .getMessageId (message .getClass ());
33- if (messageId .isEmpty ()) {
34- throw new IllegalArgumentException ("Message class not registered: " + message .getClass ().getName ());
32+ return serializeMessage (message , null );
33+ }
34+
35+ /**
36+ * Serializes a Protocol Message to a JSON string,
37+ * including the packet ID and a request ID to correlate requests and responses.
38+ *
39+ * @param message the Protocol Message to serialize
40+ * @param requestId the request ID to include in the serialized message
41+ * @return the serialized JSON string with its packet ID and request ID included
42+ * @throws InvalidProtocolBufferException if serialization fails
43+ */
44+ public static String serializeMessage (final Message message , final @ Nullable Integer requestId ) throws InvalidProtocolBufferException {
45+ final JsonObject jsonObject = JsonParser .parseString (JsonFormat .printer ().print (message )).getAsJsonObject ();
46+
47+ final String messageId = ModAPIMessages .getMessageId (message .getClass ())
48+ .orElseThrow (() -> new IllegalArgumentException ("Message class not registered: " + message .getClass ().getName ()));
49+
50+ jsonObject .add ("packet_id" , new JsonPrimitive (messageId ));
51+ if (requestId != null ) {
52+ jsonObject .add ("request_id" , new JsonPrimitive (requestId ));
3553 }
36- jsonObject . add ( "packet_id" , new JsonPrimitive ( messageId . get ()));
54+
3755 return jsonObject .toString ();
3856 }
3957
@@ -51,16 +69,16 @@ public static String serializeMessage(final Message message) throws InvalidProto
5169 public static ModAPIMessage deserializeMessage (final String json ) throws InvalidProtocolBufferException {
5270 try {
5371 final JsonObject jsonObject = JsonParser .parseString (json ).getAsJsonObject ();
54- final String packetId = jsonObject .remove ("packet_id" ).getAsString ();
55- final String cleanedJson = jsonObject .toString () ;
72+ final String packetId = jsonObject .get ("packet_id" ).getAsString ();
73+ final Integer requestId = jsonObject .has ( "request_id" ) ? jsonObject . get ( "request_id" ). getAsInt () : null ;
5674
5775 final Optional <Class <? extends Message >> clazz = ModAPIMessages .getMessageClass (packetId );
5876 if (clazz .isEmpty ()) {
5977 throw new IllegalStateException ("No message class registered for packet ID: " + packetId );
6078 }
6179 final Message .Builder builder = (Message .Builder ) clazz .get ().getMethod ("newBuilder" ).invoke (null );
62- JSON_PARSER .merge (cleanedJson , builder );
63- return new ModAPIMessage (packetId , builder .build ());
80+ JSON_PARSER .merge (json , builder );
81+ return new ModAPIMessage (packetId , builder .build (), requestId );
6482 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
6583 throw new RuntimeException ("Failed to deserialize message." , e );
6684 }
0 commit comments