#3556: Deserialize arrays to single components
This commit is contained in:
parent
e442c3da5c
commit
c92581d0dc
@ -2,6 +2,7 @@ package net.md_5.bungee.chat;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonDeserializationContext;
|
import com.google.gson.JsonDeserializationContext;
|
||||||
import com.google.gson.JsonDeserializer;
|
import com.google.gson.JsonDeserializer;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
@ -77,13 +78,12 @@ public class ComponentSerializer implements JsonDeserializer<BaseComponent>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserialize a JSON-compliant String as a single component. The input is
|
* Deserialize a JSON-compliant String as a single component.
|
||||||
* expected to be a JSON object that represents only one component.
|
|
||||||
*
|
*
|
||||||
* @param json the component json to parse
|
* @param json the component json to parse
|
||||||
* @return the deserialized component
|
* @return the deserialized component
|
||||||
* @throws IllegalArgumentException if anything other than a JSON object is
|
* @throws IllegalArgumentException if anything other than a valid JSON
|
||||||
* passed as input
|
* component string is passed as input
|
||||||
*/
|
*/
|
||||||
public static BaseComponent deserialize(String json)
|
public static BaseComponent deserialize(String json)
|
||||||
{
|
{
|
||||||
@ -93,13 +93,12 @@ public class ComponentSerializer implements JsonDeserializer<BaseComponent>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserialize a JSON element as a single component. The input is expected
|
* Deserialize a JSON element as a single component.
|
||||||
* to be a JSON object that represents only one component.
|
|
||||||
*
|
*
|
||||||
* @param jsonElement the component json to parse
|
* @param jsonElement the component json to parse
|
||||||
* @return the deserialized component
|
* @return the deserialized component
|
||||||
* @throws IllegalArgumentException if anything other than a JSON object is
|
* @throws IllegalArgumentException if anything other than a valid JSON
|
||||||
* passed as input
|
* component is passed as input
|
||||||
*/
|
*/
|
||||||
public static BaseComponent deserialize(JsonElement jsonElement)
|
public static BaseComponent deserialize(JsonElement jsonElement)
|
||||||
{
|
{
|
||||||
@ -110,11 +109,10 @@ public class ComponentSerializer implements JsonDeserializer<BaseComponent>
|
|||||||
{
|
{
|
||||||
return new TextComponent( primitive.getAsString() );
|
return new TextComponent( primitive.getAsString() );
|
||||||
}
|
}
|
||||||
}
|
} else if ( jsonElement instanceof JsonArray )
|
||||||
|
|
||||||
if ( !jsonElement.isJsonObject() )
|
|
||||||
{
|
{
|
||||||
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 );
|
return gson.fromJson( jsonElement, BaseComponent.class );
|
||||||
|
Loading…
Reference in New Issue
Block a user