public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JObject j = JObject.Load(reader); if (j.TryGetValue("type", out var typetoken)) { if (Types.TryGetValue(typetoken.Value <string>(), out var typeInfo)) { var js = new JsonSerializer(); js.Converters.Add(new StringEnumConverter()); var pjs = new JsonSerializer(); foreach (var c in serializer.Converters) { pjs.Converters.Add(c); } Type inType = typeInfo.InnerType; object v; if (j.TryGetValue("value", out var valuetoken)) { v = js.Deserialize(valuetoken.CreateReader(), inType); } else { v = Activator.CreateInstance(inType); } IModifyValue[] p; if (j.TryGetValue("parameter", out var parametertoken)) { p = (IModifyValue[])pjs.Deserialize(parametertoken.CreateReader(), typeof(IModifyValue[])); } else { p = new IModifyValue[0]; } ModifyEntry[] entries; if (j.TryGetValue("modify", out var modifytoken)) { entries = (ModifyEntry[])pjs.Deserialize(modifytoken.CreateReader(), typeof(ModifyEntry[])); } else { entries = new ModifyEntry[0]; } IModifyValue imv = (IModifyValue)Activator.CreateInstance(typeInfo.ModifyType); imv.Parameter = p; imv.Value = v; imv.Modify = entries; return(imv); } } return(null); }
public static bool TrySet <TValue>(this IModifyValue <TValue> source, TValue value) { var res = source.TryModifyValue(value); return(res == ValueModificationResult.SuccessfullyModified); }
public static void Set <TValue>(this IModifyValue <TValue> source, TValue value) { var res = source.TryModifyValue(value); switch (res) { case ValueModificationResult.SuccessfullyModified: return; case ValueModificationResult.AlreadySet: throw new InvalidOperationException("Value is already set, can't modify the value."); case ValueModificationResult.SourceReadOnly: throw new ReadOnlyException("Can't modify value - value is readonly."); case ValueModificationResult.SourceFrozen: throw new ReadOnlyException("Can't modify value - value is frozen."); case ValueModificationResult.ValueOutOfRange: throw new ArgumentOutOfRangeException(nameof(value), value, "Can't modify value - value is out of valid range."); case ValueModificationResult.ValueUndefined: throw new ArgumentOutOfRangeException(nameof(value), value, "Can't modify value - value is undefined."); default: throw new InvalidOperationException($"Can't modify value ({res})."); } }
/// <summary> /// Parse modify value /// </summary> /// <returns></returns> public KeyValuePair <string, IModifyValue> ParseModifyValue() { if (!isParsed) { string name = string.Empty; IModifyValue value = null; if (Expression != null && Expression.NodeType == ExpressionType.MemberAccess) { name = ExpressionHelper.GetExpressionPropertyName(Expression); value = new FixedModifyValue(ExpressionHelper.GetExpressionValue(Expression)); } parsedValue = new KeyValuePair <string, IModifyValue>(name, value); isParsed = true; } return(parsedValue); }
public SetValueModifyItem(string name, IModifyValue value) { Name = name; Value = value; }
public static bool TrySet <TKey, TValue>(this IModifyValue <KeyValuePair <TKey, TValue> > source, TKey key, TValue value) { return(source.TrySet(new KeyValuePair <TKey, TValue>(key, value))); }
public static void Set <TKey, TValue>(this IModifyValue <KeyValuePair <TKey, TValue> > source, TKey key, TValue value) { source.Set(new KeyValuePair <TKey, TValue>(key, value)); }