Deserialization
This page is about how to deserialize a TextComponent from JSON or YAML.
Deserialize a TextComponent
EasyComponent allows you to deserialize a TextComponent from a JSON or a YAML file.
Common properties
TextComponent representation has common elements in both formats. They are available here:
Property
Type
Description
text
String
The text displayed by the component.
color
ChatColor
The text color of the component.
bold
boolean
Set true
to put the text of a TextComponent
in bold.
italic
boolean
Set true
to put the text of a TextComponent
in italic.
strikethrough
boolean
Set true
to put the text of a TextComponent
in strikethrough.
obsfucated
boolean
Set true
to obsfucate the text of a TextComponent
.
extra
TextComponent
Add an extra component to the current one.
From JSON
Before deserializing a TextComponent from JSON, let's see how to create it in this format. First, you have to create a JSON object which have a specific structure and specific properties.
Property
Type
Description
showText
String
The text showed when hovering over the TextComponent
suggestCommand
String
The command suggested when clicking on the TextComponent
runCommand
String
The command executed when clicking on the TextComponent
openUrl
String
The url opened when clicking on the TextComponent
Example
{
[...]
"component": {
"text": "Click here !",
"showText": "&bClick click click !",
"runCommand": "say You have clicked !",
"color": "AQUA",
"extra":{
"text": "Extra component",
"showText": "I'm an extra component"
}
}
[...]
}
Now, to deserialize a TextComponent
from JSON
, you have to retrieve the JsonObject
in which it is stored. JsonObject
is a class provided by the Gson API which is included into Spigot so you don't have to add it as dependency. After that, use this code to retrieve your TextComponent
as an EasyComponent
:
JsonObject object = ... ;
EasyComponent component = EasyComponent.getFromJson(object);
TextComponent tc = component.getAsTextComponent();
From YAML
Before deserializing a TextComponent from YAML, let's see how to create it in this format. First, you have to create a YAML section which have a specific structure and specific properties.
Property
Type
Description
show-text
String
The text showed when hovering over the TextComponent
suggest-command
String
The command suggested when clicking on the TextComponent
run-command
String
The command executed when clicking on the TextComponent
open-url
String
The url opened when clicking on the TextComponent
Example
component:
text: "Click here !"
show-text: "&bClick click click !"
run-command: "say You have clicked !"
color: "AQUA"
extra:
text: "Extra component"
show-text: "I'm an extra component"
Now, to deserialize your TextComponent
from YAML
, you must retrieve the ConfigurationSection
in which it is stored. After that, use this code to convert the ConfigurationSection
into an EasyComponent
:
ConfigurationSection section = ... ;
EasyComponent component = EasyComponent.getFromYaml(section);
TextComponent tc = component.getAsTextComponent();
Last updated
Was this helpful?