{ "text": "Hello, \u00A75world!", "type": "Text", "style": { "color": "#7FFFD4", "bold": true, "italic": false, "underlined": true, "strikethrough": false, "obfuscated": false }}
{ "text": "Hello, \u00A75world!", "type": "Text", "color": "#7FFFD4", "bold": true, "italic": false, "underlined": true, "strikethrough": false, "obfuscated": false}
public class ComponentStyleConverter : JsonConverter<ComponentStyle>{ public override ComponentStyle? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException("Deserializing ComponentStyle is not supported."); } public override void Write(Utf8JsonWriter writer, ComponentStyle value, JsonSerializerOptions options) { writer.WriteStartObject(); if (value.Color != null) writer.WriteString("color", value.Color.Color); writer.WriteBoolean("bold", value.Bold); writer.WriteBoolean("italic", value.Italic); writer.WriteBoolean("underlined", value.Underlined); writer.WriteBoolean("strikethrough", value.Strikethrough); writer.WriteBoolean("obfuscated", value.Obfuscated); if (value.Insertion != null) writer.WriteString("insertion", value.Insertion); writer.WriteEndObject(); } }
style
{"componentType":"Text","children":[]}
var options = new JsonSerializerOptions{ WriteIndented = false, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, Converters = { new JsonStringEnumConverter() }, PropertyNamingPolicy = JsonNamingPolicy.CamelCase};return JsonSerializer.Serialize(component, options);
public class TextComponent : IComponent{ [JsonPropertyName("text")] public string Text { get; set; } public TextComponent(string text, IComponentStyle? style) { if (string.IsNullOrWhiteSpace(text)) throw new ArgumentException("Given text cannot be null or empty", nameof(text)); Text = text; } [JsonPropertyName("type")] public ComponentType ComponentType { get; } = ComponentType.Text; [JsonPropertyName("extra")] public List<IComponent> Children { get; } = new(); public IComponentClickEvent? ClickEvent { get; set; } public IComponentStyle Style { get; set; }}
JsonPropertyName
?