public object Combine(object value) { if (!(value is IJSONDocument)) { throw new Exception("At MaxDataCombiner: Document needs to be in IJSONDocument format"); } IJSONDocument document = (IJSONDocument)value; IJSONDocument updateDoc = new JSONDocument(); foreach (var attribute in _attributes) { object value1 = document[_userDefinedName]; object value2 = _document[_userDefinedName]; int comparer; if (value1 is IComparable) { comparer = JSONComparer.Compare((IComparable)value1, value2); } else { comparer = JsonWrapper.Wrap(value1).CompareTo(JsonWrapper.Wrap(value2)); } if (comparer > 0) { updateDoc.Add(_userDefinedName, value1); } } JsonDocumentUtil.Update(_document, updateDoc); //_document.Update(updateDoc); return(_document); }
private void button1_Click(object sender, EventArgs e) { JSONComparer c = new JSONComparer(); string master = "{a:1,b:2,c:2,d:[{e:1,f:2},{e:3,f:5}]}"; string toCompare = "{a:1,b:3,c:2,d:[{e:1,f:2},{e:3,f:5}]}"; c.Compare<String>(master, toCompare); }
public new bool Equals(object x, object y) { var c = new JSONComparer(); var xc = x as ICollection; var yc = y as ICollection; if (xc.Count != yc.Count) { return(false); } var ans = true; foreach (var xyv in xc.Cast <object>().Zip(yc.Cast <object>(), (xv, yv) => (xv, yv))) { ans = ans && c.Equals(xyv.xv, xyv.yv); if (!ans) { break; } } return(ans); }
public override int CompareTo(object obj) { var attributeValue = obj as AttributeValue; if (attributeValue != null) { switch (attributeValue.Type) { case AttributeValueType.Single: return(JSONComparer.Compare(_value, _type, _actualType, attributeValue[0], attributeValue.DataType, attributeValue.ActualType, _order)); case AttributeValueType.Mask: return(0 - attributeValue.CompareTo(this)); case AttributeValueType.All: return(0); case AttributeValueType.None: return(-1); case AttributeValueType.Null: return(attributeValue.Order == SortOrder.ASC ? int.MaxValue : int.MinValue); default: throw new IndexException(ErrorCodes.Indexes.ATTRIBUTEVALUE_TYPE_MISMATCH, new[] { this.ValueInString, attributeValue.ValueInString }); } } throw new IndexException(ErrorCodes.Indexes.ATTRIBUTEVALUE_TYPE_MISMATCH, new[] { this.ValueInString, obj.ToString() }); }
public new bool Equals(object x, object y) { JSONComparer jc = null; JSONCollectionComparer jac = null; var ans = true; var members = x.GetType().GetMembers().Where(m => m is PropertyInfo || m is FieldInfo); foreach (var m in members) { var mType = m.GetMemberType(); var xv = m.GetValue(x); var yv = m.GetValue(y); if (xv != null && yv != null) { if (xv.HasDeclaredEquals()) { ans = ans && xv.Equals(yv); } else { switch (xv) { case ICollection xc: jac = jac ?? new JSONCollectionComparer(); ans = ans && jac.Equals(xv, yv); break; default: jc = jc ?? new JSONComparer(); ans = ans && jc.Equals(xv, yv);; break; } } } if (!ans) { break; } } return(ans); }
public void ApplyValue(params object[] values) { if (valueCount == 0) { _min = values[0]; valueCount++; return; } if (values != null && values.Length > 0) { if (values[0] != null) { if (JSONComparer.Compare((IComparable)_min, values[0]) > 0) { _min = values[0]; } } } }
public int Compare(object x, object y) { if (x == null && y == null) { return(0); } if (x == null) { return(-1); } if (y == null) { return(1); } IJSONDocument xDoc = (IJSONDocument)x; IJSONDocument yDoc = (IJSONDocument)y; foreach (OrderedAttribute attr in _attributesOrderBy) { int result = 0; IComparable first; object second; var attribute = attr.ToString(); IJsonValue firstValue; IJsonValue secondValue; if (attr.Evaluate(out firstValue, xDoc)) { if (attr.Evaluate(out secondValue, yDoc)) { result = JSONComparer.Compare((IComparable)firstValue.Value, secondValue.Value); } else { result = 1; } } else if (xDoc.TryGet(attribute, out first)) { if (yDoc.TryGet(attribute, out second)) { result = JSONComparer.Compare(first, second); } else { result = 1; } } else { result = -1; } //object fieldObj = xDoc[attr.ToString()]; //if (fieldObj is double) //{ // double field1 = xDoc.Get<double>(attr.ToString()); // double field2 = yDoc.Get<double>(attr.ToString()); // result = field1.CompareTo(field2); //} //else if (fieldObj is string) //{ // string field1 = xDoc.Get<string>(attr.ToString()); // string field2 = yDoc.Get<string>(attr.ToString()); // result = field1.CompareTo(field2); //} //else if (fieldObj is bool) //{ // bool field1 = xDoc.Get<bool>(attr.ToString()); // bool field2 = yDoc.Get<bool>(attr.ToString()); // result = field1.CompareTo(field2); //} //else if (fieldObj is long) //{ // long field1 = xDoc.Get<long>(attr.ToString()); // long field2 = yDoc.Get<long>(attr.ToString()); // result = field1.CompareTo(field2); //} //else //{ // throw new Exception("Invalid type for comparison"); //} if (result != 0) { if (attr.Order.SortOrder == Alachisoft.NosDB.Common.Enum.SortOrder.DESC) { if (result < 0) { result = 1; } else { result = -1; } } return(result); } } return(0); }