public EquipmentSettings(EquipmentSettings source) : this(source == null ? typeof(IConvertible) : source.PropertyType) { if (source == null) { throw new ArgumentNullException("source"); } foreach (var pair in source.decimalProps) { this.decimalProps.Add(pair.Key, pair.Value); } foreach (var pair in source.integerProps) { this.integerProps.Add(pair.Key, pair.Value); } foreach (var pair in source.textProps) { this.textProps.Add(pair.Key, pair.Value); } }
public static JsonString ToJson(EquipmentSettings source) { if (source == null) { throw new ArgumentNullException("source"); } if (!source.PropertyType.IsEnum) { throw new ArgumentException("Must have enum properties", "source"); } var result = JsonString.CreateDict(); { // TODO move type setting outside // result.AddTerminal("type", StringifyTools.StringifyString("Webcam")); string[] names = Enum.GetNames(source.PropertyType); Array props = Enum.GetValues(source.PropertyType); // go through all properties and add existing ones to JSON for (int i = 0; i < names.Length; i++) { string term = names[i].ToLowerCamelCase(); int prop = Convert.ToInt32(props.GetValue(i), JsonString.FormatProvider); decimal mProp; int iProp; string sProp; if (source.integerProps.TryGetValue(prop, out iProp)) { result.AddTerminal(term, JsonString.Stringify(iProp)); } else if (source.decimalProps.TryGetValue(prop, out mProp)) { result.AddTerminal(term, JsonString.Stringify(mProp)); } else if (source.textProps.TryGetValue(prop, out sProp)) { result.AddTerminal(term, JsonString.StringifyString(sProp)); } } } return(result); }
public static EquipmentSettings FromJson(JsonString source, Type propertyType) { if (source == null) { throw new ArgumentNullException("source"); } if (source.Type != JsonStringType.Dict) { throw new ArgumentException("Expected a JSON dictionary", "source"); } if (propertyType == null) { throw new ArgumentNullException("propertyType"); } if (!propertyType.IsEnum) { throw new ArgumentException("Must be an enum type", "propertyType"); } var result = new EquipmentSettings(propertyType); string type = JsonString.ParseString(source.GetTerminal("type", string.Empty), string.Empty); //T type = ParseProperty(sType); bool running = JsonString.ParseBool(source.GetTerminal("running", "false"), false); string name = JsonString.ParseString(source.GetTerminal("name", string.Empty), null); // get all enum entries // and convert to camelcase as used in javascript // note that this may result in unexpected results for certain edge cases string[] names = Enum.GetNames(propertyType); Array props = Enum.GetValues(propertyType); // go through all enum entries and adopt existing data for (int i = 0; i < names.Length; i++) { string term = names[i].ToLowerCamelCase(); string value = source.GetTerminal(term, string.Empty); if (value.Length == 0) { continue; } int prop = Convert.ToInt32(props.GetValue(i), JsonString.FormatProvider); decimal mProp; int iProp; bool bProp; if (JsonString.IsQuoted(value)) { string text = JsonString.ParseString(value, null); result.textProps.Add(prop, text); } else if (JsonString.TryParseInt(value, out iProp)) { result.integerProps.Add(prop, iProp); } else if (JsonString.TryParseDecimal(value, out mProp)) { result.decimalProps.Add(prop, mProp); } else if (JsonString.TryParseBool(value, out bProp)) { result.integerProps.Add(prop, bProp ? 1 : 0); } } return(result); }