/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace QuantConnect.Notifications { /// /// Defines a to be used when deserializing to the class. /// public class NotificationJsonConverter : JsonConverter { /// /// Use default implementation to write the json /// public override bool CanWrite => false; /// /// Writes the JSON representation of the object. /// /// The to write to.The value.The calling serializer. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(Messages.NotificationJsonConverter.WriteNotImplemented); } /// /// Reads the JSON representation of the object. /// /// The to read from.Type of the object.The existing value of object being read.The calling serializer. /// /// The object value. /// public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var jObject = JObject.Load(reader); JToken token; if (jObject.TryGetValue("PhoneNumber", StringComparison.InvariantCultureIgnoreCase, out token)) { var message = jObject.GetValue("Message", StringComparison.InvariantCultureIgnoreCase); return new NotificationSms(token.ToString(), message?.ToString()); } else if (jObject.TryGetValue("Subject", StringComparison.InvariantCultureIgnoreCase, out token)) { var data = jObject.GetValue("Data", StringComparison.InvariantCultureIgnoreCase); var message = jObject.GetValue("Message", StringComparison.InvariantCultureIgnoreCase); var address = jObject.GetValue("Address", StringComparison.InvariantCultureIgnoreCase); var headers= jObject.GetValue("Headers", StringComparison.InvariantCultureIgnoreCase); return new NotificationEmail(address?.ToString(), token.ToString(), message?.ToString(), data?.ToString(), headers?.ToObject>()); } else if (jObject.TryGetValue("Address", StringComparison.InvariantCultureIgnoreCase, out token)) { var headers = jObject.GetValue("Headers", StringComparison.InvariantCultureIgnoreCase); var data = jObject.GetValue("Data", StringComparison.InvariantCultureIgnoreCase); return new NotificationWeb(token.ToString(), data?.ToString(), headers?.ToObject>()); } else if (jObject.TryGetValue("Id", StringComparison.InvariantCultureIgnoreCase, out token)) { var message = jObject.GetValue("Message", StringComparison.InvariantCultureIgnoreCase); var botToken = jObject.GetValue("Token", StringComparison.InvariantCultureIgnoreCase); return new NotificationTelegram(token.ToString(), message?.ToString(), botToken?.ToString()); } else if (jObject.TryGetValue("host", StringComparison.InvariantCultureIgnoreCase, out token)) { // This is an FTP notification var hostname = token.ToString(); var port = jObject.GetValue("port", StringComparison.InvariantCultureIgnoreCase)?.ToObject(); var username = jObject.GetValue("username", StringComparison.InvariantCultureIgnoreCase)?.ToString(); var filePath = jObject.GetValue("fileDestinationPath", StringComparison.InvariantCultureIgnoreCase)?.ToString(); var fileContent = jObject.GetValue("fileContent", StringComparison.InvariantCultureIgnoreCase)?.ToString(); if (jObject.TryGetValue("password", StringComparison.InvariantCultureIgnoreCase, out var password)) { var secure = jObject.GetValue("secure", StringComparison.InvariantCultureIgnoreCase)?.ToObject() ?? true; return NotificationFtp.FromEncodedData(hostname, username, password.ToString(), filePath, fileContent, secure, port); } var privateKey = jObject.GetValue("privateKey", StringComparison.InvariantCultureIgnoreCase)?.ToString(); var passphrase = jObject.GetValue("passphrase", StringComparison.InvariantCultureIgnoreCase)?.ToString(); return NotificationFtp.FromEncodedData(hostname, username, privateKey, passphrase, filePath, fileContent, port); } throw new NotImplementedException(Messages.NotificationJsonConverter.UnexpectedJsonObject(jObject)); } /// /// Determines whether this instance can convert the specified object type. /// /// Type of the object. /// /// true if this instance can convert the specified object type; otherwise, false. /// public override bool CanConvert(Type objectType) { return objectType == typeof(Notification); } } }