/// <summary> /// Sets the value. /// </summary> /// <param name="instance">The instance.</param> /// <param name="fieldName">Name of the field.</param> /// <param name="value">The value.</param> /// <param name="json">The json.</param> /// <exception cref="System.Exception">No Json backing object found!</exception> public void SetValue(JsonBackedObjectBase instance, string fieldName, T value, JToken json) { Dictionary <string, Tuple <JProperty, T> > dictionary = GetDictionary(instance); Tuple <JProperty, T> existing; if (dictionary.TryGetValue(fieldName, out existing)) { JProperty originalToken = existing.Item1; originalToken.Value = json; } else { if (instance.JsonValue != null) { var property = new JProperty(fieldName, json); var owner = (JObject)instance.JsonValue[r_Key]; owner.Add(property); dictionary.Add(fieldName, new Tuple <JProperty, T>(property, value)); } else { throw new Exception("No Json backing object found!"); } } }
/// <summary> /// Evals the specified instance. /// </summary> /// <param name="instance">The instance.</param> /// <returns>IReadOnlyDictionary<System.String, T>.</returns> public IReadOnlyDictionary <string, T> Eval(JsonBackedObjectBase instance) { Dictionary <string, Tuple <JProperty, T> > actual = GetDictionary(instance); r_Exposed.Clear(); foreach (KeyValuePair <string, Tuple <JProperty, T> > item in actual) { r_Exposed.Add(item.Key, item.Value.Item2); } return(r_Exposed); }
/// <summary> /// Evals the specified instance. /// </summary> /// <param name="instance">The instance.</param> /// <returns>T.</returns> public T Eval(JsonBackedObjectBase instance) { T result; if (instance.TryGetValue(this, out result)) { return(result); } result = Eval(instance.JsonValue); instance.SetValue(this, result); return(result); }
/// <summary> /// Gets the dictionary. /// </summary> /// <param name="instance">The instance.</param> /// <returns>Dictionary<System.String, Tuple<JToken, T>>.</returns> private Dictionary <string, Tuple <JProperty, T> > GetDictionary(JsonBackedObjectBase instance) { Dictionary <string, Tuple <JProperty, T> > result; if (instance.TryGetValue(this, out result)) { return(result); } result = CreateDictionary(instance.JsonValue); instance.SetValue(this, result); return(result); }
/// <summary> /// Evals the specified instance. /// </summary> /// <param name="instance">The instance.</param> /// <returns>IReadOnlyList<T>.</returns> public IReadOnlyList <T> Eval(JsonBackedObjectBase instance) { return(Eval(instance.JsonValue)); }