private void CheckValues(object[] values, JsonType expectedType) { JsonPrimitive target; bool success; foreach (object value in values) { success = JsonPrimitive.TryCreate(value, out target); Assert.True(success); Assert.NotNull(target); Assert.Equal(expectedType, target.JsonType); } }
public void ValueTest() { object[] values = { AnyInstance.AnyInt, AnyInstance.AnyString, AnyInstance.AnyGuid, AnyInstance.AnyDecimal, AnyInstance.AnyBool, AnyInstance.AnyDateTime }; foreach (object value in values) { JsonPrimitive jp; bool success = JsonPrimitive.TryCreate(value, out jp); Assert.True(success); Assert.Equal(value, jp.Value); } }
public void TryCreateInvalidTest() { bool success; JsonPrimitive target; object[] values = { AnyInstance.AnyJsonArray, AnyInstance.AnyJsonObject, AnyInstance.AnyJsonPrimitive, null, AnyInstance.DefaultJsonValue, AnyInstance.AnyDynamic, AnyInstance.AnyAddress, AnyInstance.AnyPerson }; foreach (object value in values) { success = JsonPrimitive.TryCreate(value, out target); Assert.False(success); Assert.Null(target); } }
/// <summary> /// Gets the operation support value for the specified operation and operands. /// </summary> /// <param name="operation">The operation type.</param> /// <param name="thisValue">The JsonValue instance to check operation for.</param> /// <param name="operand">The second operand instance.</param> /// <returns>An <see cref="OperationSupport"/> value.</returns> private static OperationSupport GetBinaryOperationSupport(ExpressionType operation, JsonValue thisValue, object operand) { //// Supported binary operators: +, -, *, /, %, &, |, ^, <<, >>, ==, !=, >, <, >=, <= bool isCompareOperation = false; JsonValue otherValue = operand as JsonValue; switch (operation) { //// supported binary operations case ExpressionType.Add: case ExpressionType.Subtract: case ExpressionType.Multiply: case ExpressionType.Divide: case ExpressionType.Modulo: case ExpressionType.And: case ExpressionType.Or: case ExpressionType.ExclusiveOr: case ExpressionType.LeftShift: case ExpressionType.RightShift: case ExpressionType.GreaterThan: case ExpressionType.GreaterThanOrEqual: case ExpressionType.LessThan: case ExpressionType.LessThanOrEqual: break; //// compare operations: case ExpressionType.Equal: case ExpressionType.NotEqual: isCompareOperation = true; break; default: return(OperationSupport.NotSupported); } if (operand != null) { bool thisIsPrimitive = thisValue is JsonPrimitive; if (otherValue != null) { if (!(otherValue is JsonPrimitive) || !thisIsPrimitive) { //// When either value is non-primitive it must be a compare operation. if (!isCompareOperation) { return(OperationSupport.NotSupportedOnJsonType); } } } else { //// if operand is not a JsonValue it must be a primitive CLR type and first operand must be a JsonPrimitive. if (!thisIsPrimitive) { return(OperationSupport.NotSupportedOnJsonType); } JsonPrimitive primitiveValue = null; if (!JsonPrimitive.TryCreate(operand, out primitiveValue)) { return(OperationSupport.NotSupportedOnValueType); } } } else { //// when operand is null only compare operations are valid. if (!isCompareOperation) { return(OperationSupport.NotSupportedOnOperand); } } return(OperationSupport.Supported); }