public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Type dictType = value.GetType(); //Dictionary are marked as NonIntercept var dictGenericTypes = dictType.GetGenericArguments(); var keyType = dictGenericTypes[0]; var valueType = dictGenericTypes[1]; var serializerMethods = new SerializerMethods(writer, keyType, valueType); IObservableDictionaryEntries instanceEx = (IObservableDictionaryEntries)value; writer.WriteStartArray(); int caseType = instanceEx.CaseType; ValidateDictType(caseType); writer.WriteValue(caseType); var theDict = instanceEx.GetInternalDictionary(caseType); writer.WriteValue(theDict.Count); if (theDict.Count > 0) { writer.WriteStartObject(); foreach (var key in theDict.Keys) { serializerMethods.WriteKeyMethod(key); serializerMethods.WriteValueMethod(theDict[key]); } writer.WriteEndObject(); } writer.WriteEndArray(); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.StartArray) { IObservableDictionaryEntries instanceEx = (IObservableDictionaryEntries)existingValue; reader.Read(); var dictType = (long)reader.Value; ValidateDictType(dictType); var dict = instanceEx.GetInternalDictionary((int)dictType); reader.Read(); var count = (long)reader.Value; if (count == 0) { return(instanceEx); } reader.Read();//To position reader.TokenType after count var types = objectType.GetGenericArguments(); var keyType = types[0]; var valueType = types[1]; var serializerMethods = new SerializerMethods(reader, keyType, valueType); try { instanceEx.DeltaTrackerNotifications(false); reader.Read();//We assume token at this point is StartToken and need to move past it while (reader.TokenType != JsonToken.EndObject) { object key = serializerMethods.ReadKeyMethod(); object value = serializerMethods.ReadValueMethod(); dict.Add(key, value); } return(instanceEx); } finally { instanceEx.DeltaTrackerNotifications(true); } } throw new NotImplementedException("Invalid Json format for Dictionary"); }
public static string GetSerializedThreadControlChangedState() { StringBuilder sb = new StringBuilder(); sb.Append("["); List <ThreadControl> allChangedThreadControls = RecursiveFindThreadControlChangedState(Global.State.ThreadControlTree); for (int n = 0; n < allChangedThreadControls.Count; n++) { sb.Append(SerializerMethods.SerializeThreadControlState(allChangedThreadControls[n])); if (n != allChangedThreadControls.Count - 1) { sb.Append(","); } } sb.Append("]"); string result = sb.ToString(); return(allChangedThreadControls.Count == 0 ? "" : result); }
public static string GetSubscribedStateChanges() { Dictionary <string, string> items = new Dictionary <string, string>(); if (Global.State.Subscriptions["schedulerRunning"].Subscribed && Global.State.Subscriptions["schedulerRunning"].IsReady()) { bool update = (Global.State.SchedulerTC.State == ThreadControlState.Running) != (bool)Global.State.Subscriptions["schedulerRunning"].State; if (update) { bool val = (Global.State.SchedulerTC.State == ThreadControlState.Running); Global.State.Subscriptions["schedulerRunning"].State = val; items.Add("schedulerRunning", UC.BoolToJSBool(val)); } } if (Global.State.Subscriptions["applicationStats"].Subscribed && Global.State.Subscriptions["applicationStats"].IsReady()) { items.Add("applicationStats", SerializerMethods.SerializeApplicationStats()); } if (Global.State.Subscriptions["threadControlTree"].Subscribed && Global.State.Subscriptions["threadControlTree"].IsReady()) { lock (Global.State.ThreadControlTreeLock) { long change = GetThreadControlTreeChangeValue(); if (change != (long)Global.State.Subscriptions["threadControlTree"].State) { Global.State.Subscriptions["threadControlTree"].State = change; items.Add("threadControlTree", SerializerMethods.SerializeThreadControlTree()); } } } // don't do this if we already have a threadControlTree if (Global.State.Subscriptions["threadControlState"].Subscribed && !items.ContainsKey("threadControlTree") && Global.State.Subscriptions["threadControlState"].IsReady()) { lock (Global.State.ThreadControlTreeLock) { string json = GetSerializedThreadControlChangedState(); string state = UC.MD5Hash(json); if (!string.IsNullOrWhiteSpace(json) && state != (string)Global.State.Subscriptions["threadControlState"].State) { Global.State.Subscriptions["threadControlState"].State = state; items.Add("threadControlState", json); } } } if (Global.State.Subscriptions["threadControlLog"].Subscribed && Global.State.Subscriptions["threadControlLog"].IsReady()) { Tools.LogObjects.Log log = RecursiveFindLogByThreadControlId(Global.State.SelectedThreadControlId, Global.State.ThreadControlTree); if (log != null && (log.ChangedSinceLastRead(Verbosity.Verbose) || Global.State.ThreadControlIdChanged)) { items.Add("threadControlLog", "\"" + System.Web.HttpUtility.JavaScriptStringEncode(log.Read(Verbosity.Verbose)) + "\""); } } Global.State.ThreadControlIdChanged = false; return(SerializerMethods.DictionarySerializedValuesToJSON(items)); }
public static void TryBuildPayload() { if (IsSubscribed) { Current++; if (Current >= Frequency) { Current = 0; lock (LockObj) { Dictionary <string, string> items = new Dictionary <string, string>(); if (Global.State.AlgoTraderSubscriptions["main"] == true) { AlgoTraderMain main = new AlgoTraderMain(); main.CurrentTime = AlgoTraderState.CurrentTime.ToString(); main.SelectedSymbol = SelectedSymbol; main.SelectedStrategyName = AlgoTraderMethods.GetStrategyName(SelectedStrategyId); main.SelectedStrategyId = SelectedStrategyId; using (var output = new StringWriter()) { JSON.Serialize(main, output, Options.PrettyPrint); items.Add("algoTraderMain", output.ToString()); } } if (Global.State.AlgoTraderSubscriptions["log"] == true) { UIObjects.AlgoTraderLog log = new UIObjects.AlgoTraderLog(); log.Log = Global.State.AlgoTrader.TC.Log.Read(Verbosity.Verbose); //log.Log = System.Web.HttpUtility.JavaScriptStringEncode(Global.State.AlgoTrader.TC.Log.Read(Verbosity.Verbose)); using (var output = new StringWriter()) { JSON.Serialize(log, output, Options.PrettyPrint); items.Add("algoTraderLog", output.ToString()); } } if (Global.State.AlgoTraderSubscriptions["overview"] == true) { AlgoTraderOverview overview = buildOverviewObject(); using (var output = new StringWriter()) { JSON.Serialize(overview, output, Options.PrettyPrint); items.Add("algoTraderOverview", output.ToString()); } } if (Global.State.AlgoTraderSubscriptions["chart"] == true) { AlgoTraderChart chart = buildChartObject(); using (var output = new StringWriter()) { JSON.Serialize(chart, output, Options.Default); items.Add("algoTraderChart", output.ToString()); } } Payload = SerializerMethods.DictionarySerializedValuesToJSON(items); } } } }