本文整理汇总了C#中Jint.Native.JsValue.AsArray方法的典型用法代码示例。如果您正苦于以下问题:C# JsValue.AsArray方法的具体用法?C# JsValue.AsArray怎么用?C# JsValue.AsArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jint.Native.JsValue
的用法示例。
在下文中一共展示了JsValue.AsArray方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldBeAnArray
public void ShouldBeAnArray()
{
var value = new JsValue(new ArrayInstance(null));
Assert.Equal(false, value.IsBoolean());
Assert.Equal(true, value.IsArray());
Assert.Equal(false, 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.AsArray() != null);
}
示例2: ToRavenJToken
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());
}
示例3: ConvertBack
internal JToken ConvertBack(JTokenType type, JsValue value)
{
switch (type)
{
case JTokenType.Array:
if (value.IsArray())
{
var array = value.AsArray();
return new JArray(array.Properties.Where(k => ArrayInstance.IsArrayIndex(new JsValue(k.Key))).Select(kvp => ConvertBack(kvp.Value.Value ?? JsValue.Null)));
}
break;
case JTokenType.Boolean:
if (value.IsBoolean())
return new JValue(value.AsBoolean());
break;
case JTokenType.Date:
if (value.IsDate())
return new JValue(value.AsDate());
break;
case JTokenType.Float:
if (value.IsNumber())
return new JValue((float)value.AsNumber());
break;
case JTokenType.Integer:
if (value.IsNumber())
return new JValue((int)value.AsNumber());
break;
case JTokenType.String:
if (value.IsString())
return JValue.CreateString(value.AsString());
break;
}
return ConvertBack(value);
}
示例4: ToRavenJToken
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());
}