// Serialize / deserialize internal void ReadWrite(IReadWriteStream s) { int c = fields.Count; s.rwInt(ref c); if (s.IsWriting) { foreach (KeyValuePair <string, UniValue> f in fields) { s.wString(f.Key); f.Value.ReadWrite(s); } } else { fields = new UniFields(this, c); for (int i = 0; i < c; i++) { string t; s.rString(out t); UniValue v = new UniValue(); v.ReadWrite(s); fields.Add(t, v); } } }
// Constructor public UniValue(UniValue v) { this.type = v.type; this.value = v.value; // We have no destructor GC.SuppressFinalize(this); }
// ano - for calling from outside, makes sure to make an undo snapshot // can throw ArgumentException if the new value's type // is not int, float, string, or bool // or the value is null public void SetField(string key, object value) { if (key == null) { return; } if (IsDisposed) { return; } key = UniValue.ValidateName(key); if (key == "") { return; } if (fields.ContainsKey(key)) { // ano - we don't want to record an undo for // this element if the values aren't changing if (fields[key].Value == value) { return; } BeforeFieldsChange(); Type new_type = value.GetType(); if (!fields[key].Value.GetType().IsAssignableFrom(new_type)) { if (value is float) { fields[key].Type = (int)UniversalType.Float; } else if (value is int) { fields[key].Type = (int)UniversalType.Integer; } else if (value is bool) { fields[key].Type = (int)UniversalType.Boolean; } else if (value is string) { fields[key].Type = (int)UniversalType.String; } // ano - other cases are handled by the argument exception } // ano - ArgumentException can be thrown here fields[key].Value = value; } else { UniValue new_field = new UniValue(); // ano - ArgumentException can be thrown here new_field.Value = value; // ano - undo snapshot recorded after exception // in this case, because we can BeforeFieldsChange(); // ano - other cases are handled by the ArgumentException before if (value is float) { new_field.Type = (int)UniversalType.Float; } else if (value is int) { new_field.Type = (int)UniversalType.Integer; } else if (value is bool) { new_field.Type = (int)UniversalType.Boolean; } else if (value is string) { new_field.Type = (int)UniversalType.String; } fields.Add(key, new_field); } }