Lean  $LEAN_TAG$
SymbolValueJsonConverter.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 Newtonsoft.Json;
19 
20 namespace QuantConnect
21 {
22  /// <summary>
23  /// Defines a <see cref="JsonConverter"/> to be used when you only want to serialize
24  /// the <see cref="Symbol.Value"/> property instead of the full <see cref="Symbol"/>
25  /// instance
26  /// </summary>
27  public class SymbolValueJsonConverter : JsonConverter
28  {
29  /// <summary>
30  /// Writes the JSON representation of the object.
31  /// </summary>
32  /// <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>
33  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
34  {
35  var symbol = value as Symbol;
36  if (symbol != null)
37  {
38  writer.WriteValue(symbol.Value);
39  }
40  else
41  {
42  serializer.Serialize(writer, value);
43  }
44  }
45 
46  /// <summary>
47  /// Reads the JSON representation of the object.
48  /// </summary>
49  /// <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>
50  /// <returns>
51  /// The object value.
52  /// </returns>
53  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
54  {
55  throw new NotImplementedException(Messages.SymbolValueJsonConverter.ConverterIsWriteOnly);
56  }
57 
58  /// <summary>
59  /// Determines whether this instance can convert the specified object type.
60  /// </summary>
61  /// <param name="objectType">Type of the object.</param>
62  /// <returns>
63  /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
64  /// </returns>
65  public override bool CanConvert(Type objectType)
66  {
67  throw new NotImplementedException(Messages.SymbolValueJsonConverter.ConverterIsIntendedToBeDirectlyDecoratedInMember);
68  }
69  }
70 }