Lean  $LEAN_TAG$
NotificationJsonConverter.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 using System.Collections.Generic;
19 using Newtonsoft.Json;
20 using Newtonsoft.Json.Linq;
21 
23 {
24  /// <summary>
25  /// Defines a <see cref="JsonConverter"/> to be used when deserializing to the <see cref="Notification"/> class.
26  /// </summary>
27  public class NotificationJsonConverter : JsonConverter
28  {
29  /// <summary>
30  /// Use default implementation to write the json
31  /// </summary>
32  public override bool CanWrite => false;
33 
34  /// <summary>
35  /// Writes the JSON representation of the object.
36  /// </summary>
37  /// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param><param name="value">The value.</param><param name="serializer">The calling serializer.</param>
38  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
39  {
40  throw new NotImplementedException(Messages.NotificationJsonConverter.WriteNotImplemented);
41  }
42 
43  /// <summary>
44  /// Reads the JSON representation of the object.
45  /// </summary>
46  /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param><param name="objectType">Type of the object.</param><param name="existingValue">The existing value of object being read.</param><param name="serializer">The calling serializer.</param>
47  /// <returns>
48  /// The object value.
49  /// </returns>
50  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
51  {
52  var jObject = JObject.Load(reader);
53 
54  JToken token;
55  if (jObject.TryGetValue("PhoneNumber", StringComparison.InvariantCultureIgnoreCase, out token))
56  {
57  var message = jObject.GetValue("Message", StringComparison.InvariantCultureIgnoreCase);
58 
59  return new NotificationSms(token.ToString(), message?.ToString());
60  }
61  else if (jObject.TryGetValue("Subject", StringComparison.InvariantCultureIgnoreCase, out token))
62  {
63  var data = jObject.GetValue("Data", StringComparison.InvariantCultureIgnoreCase);
64  var message = jObject.GetValue("Message", StringComparison.InvariantCultureIgnoreCase);
65  var address = jObject.GetValue("Address", StringComparison.InvariantCultureIgnoreCase);
66  var headers= jObject.GetValue("Headers", StringComparison.InvariantCultureIgnoreCase);
67 
68  return new NotificationEmail(address?.ToString(), token.ToString(), message?.ToString(), data?.ToString(), headers?.ToObject<Dictionary<string, string>>());
69  }
70  else if (jObject.TryGetValue("Address", StringComparison.InvariantCultureIgnoreCase, out token))
71  {
72  var headers = jObject.GetValue("Headers", StringComparison.InvariantCultureIgnoreCase);
73  var data = jObject.GetValue("Data", StringComparison.InvariantCultureIgnoreCase);
74 
75  return new NotificationWeb(token.ToString(), data?.ToString(), headers?.ToObject<Dictionary<string, string>>());
76  }
77  else if (jObject.TryGetValue("Id", StringComparison.InvariantCultureIgnoreCase, out token))
78  {
79  var message = jObject.GetValue("Message", StringComparison.InvariantCultureIgnoreCase);
80  var botToken = jObject.GetValue("Token", StringComparison.InvariantCultureIgnoreCase);
81  return new NotificationTelegram(token.ToString(), message?.ToString(), botToken?.ToString());
82  }
83  else if (jObject.TryGetValue("host", StringComparison.InvariantCultureIgnoreCase, out token))
84  {
85  // This is an FTP notification
86  var hostname = token.ToString();
87  var port = jObject.GetValue("port", StringComparison.InvariantCultureIgnoreCase)?.ToObject<int?>();
88  var username = jObject.GetValue("username", StringComparison.InvariantCultureIgnoreCase)?.ToString();
89  var filePath = jObject.GetValue("fileDestinationPath", StringComparison.InvariantCultureIgnoreCase)?.ToString();
90  var fileContent = jObject.GetValue("fileContent", StringComparison.InvariantCultureIgnoreCase)?.ToString();
91 
92  if (jObject.TryGetValue("password", StringComparison.InvariantCultureIgnoreCase, out var password))
93  {
94  var secure = jObject.GetValue("secure", StringComparison.InvariantCultureIgnoreCase)?.ToObject<bool>() ?? true;
95  return NotificationFtp.FromEncodedData(hostname, username, password.ToString(), filePath, fileContent, secure, port);
96  }
97 
98  var privateKey = jObject.GetValue("privateKey", StringComparison.InvariantCultureIgnoreCase)?.ToString();
99  var passphrase = jObject.GetValue("passphrase", StringComparison.InvariantCultureIgnoreCase)?.ToString();
100  return NotificationFtp.FromEncodedData(hostname, username, privateKey, passphrase, filePath, fileContent, port);
101  }
102 
103  throw new NotImplementedException(Messages.NotificationJsonConverter.UnexpectedJsonObject(jObject));
104  }
105 
106  /// <summary>
107  /// Determines whether this instance can convert the specified object type.
108  /// </summary>
109  /// <param name="objectType">Type of the object.</param>
110  /// <returns>
111  /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
112  /// </returns>
113  public override bool CanConvert(Type objectType)
114  {
115  return objectType == typeof(Notification);
116  }
117  }
118 }