public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (objectType.Equals(typeof(ChatComponent)))
        {
            var obj = JObject.Load(reader);
            return(ChatComponent.FromJson(obj));
        }
        else if (objectType.Equals(typeof(ChatComponent[])))
        {
            var objArr         = JArray.Load(reader);
            var chatComponents = new ChatComponent[objArr.Count];
            for (int i = 0; i < chatComponents.Length; i++)
            {
                if (objArr[i].Type == JTokenType.Object)                    // object type can be any component
                {
                    chatComponents[i] = ChatComponent.FromJson((JObject)objArr[i]);
                }
                else if (objArr[i].Type == JTokenType.String)                   // string components can be single json primitives
                {
                    chatComponents[i] = new StringComponent()
                    {
                        text = (string)objArr[i]
                    }
                }
                ;
            }

            return(chatComponents);
        }

        throw new JsonReaderException($"ChatComponentJsonConverter cannot deserialize to {objectType.ToString()}");
    }
Exemple #2
0
    public ChatMessage(ChatMessagePacket pkt)
    {
        var chatComponent = ChatComponent.FromJson(pkt.Json);

        // use .ToString() for plaintext and components will output text with no formatting
        PlaintextMessage = chatComponent.ToString();

        HtmlFormattedMessage = ChatComponent.GetFormattedString(chatComponent, null);
    }