private string JSValueToString(JavaScriptValue val) { switch (val.ValueType) { case JavaScriptValueType.Boolean: return(val.ToBoolean() ? "true" : "false"); case JavaScriptValueType.Number: try { return(val.ToInt32().ToString()); } catch (JavaScriptException) { return(val.ToDouble().ToString(CultureInfo.InvariantCulture)); } case JavaScriptValueType.Error: var keys = val.GetOwnPropertyNames(); throw new Exception("Error in js"); case JavaScriptValueType.String: return(val.ToString()); case JavaScriptValueType.Array: // int length = val.GetProperty(JavaScriptPropertyId.FromString("length")).ToInt32(); // return "Array"; case JavaScriptValueType.Object: return(val.ToJsonString()); default: return(val.ValueType.ToString()); } }
private IEnumerable <Span> GetValueSpans(JavaScriptValue value, int level = 0, bool inArray = false) { switch (value.ValueType) { case JavaScriptValueType.Null: return(new Span[] { new Span("null") }); case JavaScriptValueType.Undefined: return(new Span[] { new Span("undefined") }); case JavaScriptValueType.String: return(new Span[] { $"\"{value.ToString().Replace("\"", "\\\"")}\"".DarkGreen() }); case JavaScriptValueType.Boolean: return(new Span[] { new Span(value.ToBoolean() ? "true" : "false") }); case JavaScriptValueType.Number: try { return(new Span[] { value.ToInt32().ToString().Red() }); } catch (JavaScriptException) { return(new Span[] { value.ToDouble().ToString(CultureInfo.InvariantCulture).Red() }); } case JavaScriptValueType.Function: return(new Span[] { GetFuncRep(value).Magenta() }); case JavaScriptValueType.Error: return(new Span[] { value.ToJsonString().DarkRed() }); case JavaScriptValueType.Object: return(GetObjectSpans(value, level, inArray)); case JavaScriptValueType.Array: return(GetArraySpans(value, level)); default: return(new Span[] { new Span(value.ValueType.ToString()), }); } }