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); }
protected override object OnValueChanging(object value) { // allow any event handler to pre-process the value. value = base.OnValueChanging(value); if (value != null) { // ensure the value is of the right type. value = OptionValueParser.ParseValue <TValue>(value, this); } return(value); }
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(); }
protected override object OnValueChanging(object value) { // allow base class to notify event handlers (if any) to pre-process the value. value = base.OnValueChanging(value); // a null value means, the value is being removed. if (value != null) { // ensure the value is of the right type. value = OptionValueParser.ParseValue <TValue>(value, this); } return(value); }
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); if (value == null) { value = jToken.Value <string>(); } // ensure the value is of the right type. value = OptionValueParser.ParseValue <TValue>(value, this); return(value); }
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); }
internal void Serialize(JsonWriter writer, JsonSerializer serializer) { if (this.CanSerialize) { // ensure the value is converted to a primitive type or a collection of primitive types // that can be safely serialized. var value = OptionValueParser.GetSerializationValue(this.Value); // notify event handlers (if any) to pre-process the JSON value. value = OnSerializing(value); serializer.Serialize(writer, value, value?.GetType()); OnSerialized(value); } }
/// <summary> /// Core method for setting the option and/or adding values to the collection. /// Setting the value has the same semantics as when the option is specified in the command line, when specified multiple times /// it is equivalent to having a collection of values not a collection of options and the operation is Add instead of SetValue. /// </summary> protected override void SetValue(object value) { if (value == null) { this.InnerList.Clear(); } else { if (value is IList <TValue> list) { this.InnerList.AddRange(list); } else { this.InnerList.Add(OptionValueParser.ParseValue <TValue>(value, this)); } } }
public static string SerializeToString(TAppOptions options, bool prettyFormat) { // Format the string similarly to the JSON format. var valueBuilder = new System.Text.StringBuilder(); var separator = prettyFormat ? Environment.NewLine : " "; foreach (var input in options.Inputs) { valueBuilder.Append($"\"{OptionValueParser.GetSerializationValue(input)}\"{separator}"); } var printOptions = options.GetOptions().Where(o => o.Name != ApplicationOptions.InputsKey && o.Name != ApplicationOptions.OptionsKey && o.Name != ApplicationOptions.ProviderIdKey && o.Name != ApplicationOptions.VersionKey).OrderBy(o => o.SerializationName); foreach (var option in printOptions) { if (option.CanSerialize) { var value = OptionValueParser.GetSerializationValue(option.Value); if (value is ICollection collection) { foreach (var listValue in collection) { valueBuilder.Append($"--{option.Name} \"{listValue}\"{separator}"); } } else { value = value is bool?string.Empty : $" \"{value}\""; valueBuilder.Append($"--{option.Name}{value}{separator}"); } } } return(valueBuilder.ToString()); }
private TValue CastValue(object value) { OptionValueParser.ThrowInvalidValueIf(value == null || value.GetType() != typeof(TValue), value, this.Owner); return((TValue)value); }
int IList.Add(object value) { this.Add(OptionValueParser.ParseValue <TValue>(value, this.Owner)); return(this.Count - 1); }