public override object ReadJson(JsonReader reader,
                                        Type objectType, object existingValue, JsonSerializer serializer)
        {
            var obj         = JObject.Load(reader);
            var messageType = (BroadcastMessageType)obj["type"].Value <long>();

            if (messageType == BroadcastMessageType.GamestreamState)
            {
                var stateMessageType = (GamestreamStateMessageType)obj["state"].Value <long>();
                return(obj.ToObject(GamestreamStateMessageTypeAttribute.GetTypeForMessageType(stateMessageType)
                                    ?? typeof(GamestreamStateBaseMessage)));
            }

            return(obj.ToObject(BroadcastMessageTypeAttribute.GetTypeForMessageType(messageType)
                                ?? typeof(BroadcastBaseMessage)));
        }
Example #2
0
        /// <summary>
        /// Invoked by Newtonsoft Json library.
        /// Reads the json and deserializes into Gamestream/Broadcast message.
        /// </summary>
        /// <returns>Appropriate message object.</returns>
        /// <param name="reader">Json reader.</param>
        /// <param name="objectType">Object type.</param>
        /// <param name="existingValue">Existing value.</param>
        /// <param name="serializer">Serializer.</param>
        public override object ReadJson(JsonReader reader,
                                        Type objectType, object existingValue, JsonSerializer serializer)
        {
            var obj = JObject.Load(reader);

            var messageType = (BroadcastMessageType)obj["type"].Value <int>();

            objectType = BroadcastMessageTypeAttribute.GetTypeForMessageType(messageType)
                         ?? typeof(BroadcastBaseMessage);

            if (messageType == BroadcastMessageType.GamestreamState)
            {
                var stateMessageType = (GamestreamStateMessageType)obj["state"].Value <int>();
                objectType = GamestreamStateMessageTypeAttribute.GetTypeForMessageType(stateMessageType)
                             ?? typeof(GamestreamStateBaseMessage);
            }

            var guidSerializer = new JsonSerializer();

            guidSerializer.Converters.Add(new GuidConverter());

            return(obj.ToObject(objectType, guidSerializer));
        }