protected override object OnDeserializing(JToken jToken) { // allow base class to notify event handlers (if any) to pre-process the JSON value. var value = base.OnDeserializing(jToken); var collection = value as ICollection; if (value == null) { if (jToken.Type == JTokenType.Array) { OptionValueParser.ThrowInvalidValueIf(jToken.Any(i => i.Type != JTokenType.String), jToken, this); collection = jToken.Select(i => i.Value <string>()).ToList(); } else if (jToken.Type == JTokenType.String) { // allow for a single-value list to be represented with a string. collection = new List <string>() { jToken.Value <string>() }; } } OptionValueParser.ThrowInvalidValueIf(collection == null, jToken, this); foreach (var item in collection) { this.InnerList.Add(OptionValueParser.ParseValue <TValue>(item, this)); } // ensure the underlying value is the inner list. return(this.InnerList); }
internal void Deserialize(JToken jToken) { // Raise event for handlers to preprocess the JSON value. var value = OnDeserializing(jToken); // validate value; observe that null values are not allowed during deserialization. var stringValue = value as string; OptionValueParser.ThrowInvalidValueIf(value == null || (stringValue != null && string.IsNullOrWhiteSpace(stringValue)), jToken, this); _value = value; OnDeserialized(); }
private object ParseNamespace(string stringValue, OptionBase option) { // format namespace as a mapping key/value pair: // "Namespace": "MyServiceReference1" var parts = stringValue.Split(','); OptionValueParser.ThrowInvalidValueIf(parts.Length > 2, stringValue, option); var valueKey = parts.Length == 1 ? "*" : parts[0]; var valueVal = parts.Length == 1 ? parts[0] : parts[1]; var value = new KeyValuePair <string, string>(valueKey.Trim(), valueVal.Trim()); return(value); }
private TValue CastValue(object value) { OptionValueParser.ThrowInvalidValueIf(value == null || value.GetType() != typeof(TValue), value, this.Owner); return((TValue)value); }