public void CastTests() { int value = 10; JsonValue target = new JsonPrimitive(value); int v1 = JsonValue.CastValue <int>(target); Assert.Equal <int>(value, v1); v1 = (int)target; Assert.Equal <int>(value, v1); long v2 = JsonValue.CastValue <long>(target); Assert.Equal <long>(value, v2); v2 = (long)target; Assert.Equal <long>(value, v2); string s = JsonValue.CastValue <string>(target); Assert.Equal <string>(value.ToString(), s); s = (string)target; Assert.Equal <string>(value.ToString(), s); object obj = JsonValue.CastValue <object>(target); Assert.Equal(target, obj); obj = (object)target; Assert.Equal(target, obj); object nill = JsonValue.CastValue <object>(null); Assert.Null(nill); dynamic dyn = target; JsonValue defaultJv = dyn.IamDefault; nill = JsonValue.CastValue <string>(defaultJv); Assert.Null(nill); nill = (string)defaultJv; Assert.Null(nill); obj = JsonValue.CastValue <object>(defaultJv); Assert.Same(defaultJv, obj); obj = (object)defaultJv; Assert.Same(defaultJv, obj); JsonValue jv = JsonValue.CastValue <JsonValue>(target); Assert.Equal <JsonValue>(target, jv); jv = JsonValue.CastValue <JsonValue>(defaultJv); Assert.Equal <JsonValue>(defaultJv, jv); jv = JsonValue.CastValue <JsonPrimitive>(target); Assert.Equal <JsonValue>(target, jv); ExceptionHelper.Throws <InvalidCastException>(delegate { int i = JsonValue.CastValue <int>(null); }); ExceptionHelper.Throws <InvalidCastException>(delegate { int i = JsonValue.CastValue <int>(defaultJv); }); ExceptionHelper.Throws <InvalidCastException>(delegate { int i = JsonValue.CastValue <char>(target); }); }