public void ShouldBeADate() { var value = new JsValue(new DateInstance(null)); Assert.Equal(false, value.IsBoolean()); Assert.Equal(false, value.IsArray()); Assert.Equal(true, value.IsDate()); Assert.Equal(false, value.IsNull()); Assert.Equal(false, value.IsNumber()); Assert.Equal(true, value.IsObject()); Assert.Equal(false, value.IsPrimitive()); Assert.Equal(false, value.IsRegExp()); Assert.Equal(false, value.IsString()); Assert.Equal(false, value.IsUndefined()); Assert.Equal(true, value.AsDate() != null); }
private RavenJToken ToRavenJToken(JsValue v, string propertyKey) { if (v.IsBoolean()) return new RavenJValue(v.AsBoolean()); if (v.IsString()) { const string RavenDataByteArrayToBase64 = "raven-data:byte[];base64,"; var value = v.AsString(); if (value != null && value.StartsWith(RavenDataByteArrayToBase64)) { value = value.Remove(0, RavenDataByteArrayToBase64.Length); var byteArray = Convert.FromBase64String(value); return new RavenJValue(byteArray); } return new RavenJValue(value); } if (v.IsNumber()) { var num = v.AsNumber(); KeyValuePair<RavenJValue, JsValue> property; if (propertiesByValue.TryGetValue(propertyKey, out property)) { var originalValue = property.Key; if (originalValue.Type == JTokenType.Float) return new RavenJValue(num); if (originalValue.Type == JTokenType.Integer) { // If the current value is exactly as the original value, we can return the original value before we made the JS conversion, // which will convert a Int64 to jsFloat. var originalJsValue = property.Value; if (originalJsValue.IsNumber() && Math.Abs(num - originalJsValue.AsNumber()) < double.Epsilon) return originalValue; return new RavenJValue((long)num); } } // If we don't have the type, assume that if the number ending with ".0" it actually an integer. var integer = Math.Truncate(num); if (Math.Abs(num - integer) < double.Epsilon) return new RavenJValue((long)integer); return new RavenJValue(num); } if (v.IsNull()) return RavenJValue.Null; if (v.IsUndefined()) return RavenJValue.Null; if (v.IsArray()) { var jsArray = v.AsArray(); var rja = new RavenJArray(); foreach (var property in jsArray.Properties) { if (property.Key == "length") continue; var jsInstance = property.Value.Value; if (!jsInstance.HasValue) continue; var ravenJToken = ToRavenJToken(jsInstance.Value, CreatePropertyKey(property.Key, propertyKey)); if (ravenJToken == null) continue; rja.Add(ravenJToken); } return rja; } if (v.IsObject()) return ToRavenJObject(v, propertyKey); if (v.IsRegExp()) return null; throw new NotSupportedException(v.Type.ToString()); }
private RavenJToken ToRavenJToken(JsValue v, string propertyKey, bool recursiveCall) { if (v.IsBoolean()) return new RavenJValue(v.AsBoolean()); if (v.IsString()) { const string RavenDataByteArrayToBase64 = "raven-data:byte[];base64,"; var valueAsObject = v.ToObject(); var value = valueAsObject != null ? valueAsObject.ToString() : null; if (value != null && value.StartsWith(RavenDataByteArrayToBase64)) { value = value.Remove(0, RavenDataByteArrayToBase64.Length); var byteArray = Convert.FromBase64String(value); return new RavenJValue(byteArray); } return new RavenJValue(value); } if (v.IsNumber()) { var num = v.AsNumber(); KeyValuePair<RavenJValue, JsValue> property; if (propertiesByValue.TryGetValue(propertyKey, out property)) { var originalValue = property.Key; if (originalValue.Type == JTokenType.Float || originalValue.Type == JTokenType.Integer) { // If the current value is exactly as the original value, we can return the original value before we made the JS conversion, // which will convert a Int64 to jsFloat. var originalJsValue = property.Value; if (originalJsValue.IsNumber() && Math.Abs(num - originalJsValue.AsNumber()) < double.Epsilon) return originalValue; //We might have change the type of num from Integer to long in the script by design //Making sure the number isn't a real float before returning it as integer if (originalValue.Type == JTokenType.Integer && (Math.Abs(num - Math.Floor(num)) <= double.Epsilon || Math.Abs(num - Math.Ceiling(num)) <= double.Epsilon)) return new RavenJValue((long)num); return new RavenJValue(num);//float } } // If we don't have the type, assume that if the number ending with ".0" it actually an integer. var integer = Math.Truncate(num); if (Math.Abs(num - integer) < double.Epsilon) return new RavenJValue((long)integer); return new RavenJValue(num); } if (v.IsNull()) return RavenJValue.Null; if (v.IsUndefined()) return RavenJValue.Null; if (v.IsArray()) { var jsArray = v.AsArray(); var rja = new RavenJArray(); foreach (var property in jsArray.Properties) { if (InheritedProperties.Contains(property.Key)) continue; var jsInstance = property.Value.Value; if (!jsInstance.HasValue) continue; var ravenJToken = ToRavenJToken(jsInstance.Value, propertyKey + "["+property.Key +"]", recursiveCall); if (ravenJToken == null) continue; rja.Add(ravenJToken); } return rja; } if (v.IsObject()) { return ToRavenJObject(v, propertyKey, recursiveCall); } if (v.IsRegExp()) return null; throw new NotSupportedException(v.Type.ToString()); }