Skip to content

Commit

Permalink
#3556: Deserialize arrays to single components
Browse files Browse the repository at this point in the history
  • Loading branch information
md-5 committed Oct 29, 2023
1 parent e442c3d commit c92581d
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -77,13 +78,12 @@ public static BaseComponent[] parse(String json)
}

/**
* Deserialize a JSON-compliant String as a single component. The input is
* expected to be a JSON object that represents only one component.
* Deserialize a JSON-compliant String as a single component.
*
* @param json the component json to parse
* @return the deserialized component
* @throws IllegalArgumentException if anything other than a JSON object is
* passed as input
* @throws IllegalArgumentException if anything other than a valid JSON
* component string is passed as input
*/
public static BaseComponent deserialize(String json)
{
Expand All @@ -93,13 +93,12 @@ public static BaseComponent deserialize(String json)
}

/**
* Deserialize a JSON element as a single component. The input is expected
* to be a JSON object that represents only one component.
* Deserialize a JSON element as a single component.
*
* @param jsonElement the component json to parse
* @return the deserialized component
* @throws IllegalArgumentException if anything other than a JSON object is
* passed as input
* @throws IllegalArgumentException if anything other than a valid JSON
* component is passed as input
*/
public static BaseComponent deserialize(JsonElement jsonElement)
{
Expand All @@ -110,11 +109,10 @@ public static BaseComponent deserialize(JsonElement jsonElement)
{
return new TextComponent( primitive.getAsString() );
}
}

if ( !jsonElement.isJsonObject() )
} else if ( jsonElement instanceof JsonArray )
{
throw new IllegalArgumentException( "Malformatted JSON. Expected object, got array for input \"" + jsonElement + "\"." );
BaseComponent[] array = gson.fromJson( jsonElement, BaseComponent[].class );
return TextComponent.fromArray( array );
}

return gson.fromJson( jsonElement, BaseComponent.class );
Expand Down

0 comments on commit c92581d

Please sign in to comment.