// Return true if this JSValue is strictly equal to JSValue after they are converted to the same type. // // Null is not converted to any other type before comparison. // Object and Array types are converted first to a String type using AsString() before comparing // with other types, and then we apply the same rules as for the String type. // String is converted to Double before comparing with Boolean, Int64, or Double. // Boolean is converted to 1.0 and +0.0 when comparing with String, Int64, or Double. // Int64 is converted to Double before comparing with other types. // // The behavior is similar to JavaScript == operator except for Object and Array for which // this functions does a deep structured comparison using JSEquals instead // of pointer equality. public bool JSEquals(JSValue other) { if (Type == other.Type) { switch (Type) { case JSValueType.Object: return(JSValueObject.JSEquals(ObjectValue, other.ObjectValue)); case JSValueType.Array: return(JSValueArray.JSEquals(ArrayValue, other.ArrayValue)); default: return(Equals(other)); } } else if (Type == JSValueType.Null || other.Type == JSValueType.Null) { return(false); } // If one of the types Boolean, Int64, or Double, then compare as Numbers, // otherwise compare as strings. JSValueType greatestType = Type > other.Type ? Type : other.Type; if (greatestType >= JSValueType.Boolean) { return(AsJSNumber() == other.AsJSNumber()); } else { return(AsJSString() == other.AsJSString()); } }
// Return true if this JSValue is strictly equal to JSValue. // Compared values must have the same type and value. // // The behavior is similar to JavaScript === operator except for Object and Array where // this functions does a deep structured comparison instead of pointer equality. public bool Equals(JSValue other) { if (Type != other.Type) { return(false); } switch (Type) { case JSValueType.Null: return(true); case JSValueType.Object: return(JSValueObject.Equals(ObjectValue, other.ObjectValue)); case JSValueType.Array: return(JSValueArray.Equals(ArrayValue, other.ArrayValue)); case JSValueType.String: return(StringValue == other.StringValue); case JSValueType.Boolean: return(BooleanValue == other.BooleanValue); case JSValueType.Int64: return(Int64Value == other.Int64Value); case JSValueType.Double: return(DoubleValue == other.DoubleValue); default: return(false); } }
// Create JSValueObject as a shallow copy of 'other'. public static JSValueObject CopyFrom(IReadOnlyDictionary <string, JSValue> other) { JSValueObject obj = new JSValueObject(); foreach (var keyValue in other) { obj.Add(keyValue.Key, keyValue.Value); } return(obj); }
private static JSValueObject InternalReadObjectFrom(IJSValueReader reader) { var jsObject = new JSValueObject(); while (reader.GetNextObjectProperty(out string propertyName)) { jsObject.Add(propertyName, InternalReadFrom(reader)); } return(jsObject); }
// Create JSValueObject from IJSValueReader. public static JSValueObject ReadObjectFrom(IJSValueReader reader) { if (reader.ValueType == JSValueType.Object) { if (reader is IJSValueTreeReader treeReader) { return(JSValueObject.CopyFrom(treeReader.Current.ObjectValue)); } return(InternalReadObjectFrom(reader)); } return(new JSValueObject()); }
// Return true if this JSValueObject is strictly equal to 'other'. public bool Equals(JSValueObject other) { return(Equals(this as IReadOnlyDictionary <string, JSValue>, other as IReadOnlyDictionary <string, JSValue>)); }
public static void WriteValue(this IJSValueWriter writer, JSValueObject value) { JSValue.WriteObject(writer, value); }