public override bool TryEvaluate(IValue lhs, IValue rhs, out IValue result) { if (!(lhs.ValueKind == JsonValueKind.Number && rhs.ValueKind == JsonValueKind.Number)) { result = JsonConstants.Null; return(false); } Decimal decVal1; double dblVal1; Decimal decVal2; double dblVal2; if (lhs.TryGetDecimal(out decVal1) && rhs.TryGetDecimal(out decVal2)) { if (decVal2 == 0) { result = JsonConstants.Null; return(false); } Decimal val = decVal1 % decVal2; result = new DecimalValue(val); return(true); } else if (lhs.TryGetDouble(out dblVal1) && rhs.TryGetDouble(out dblVal2)) { if (dblVal2 == 0) { result = JsonConstants.Null; return(false); } double val = dblVal1 % dblVal2; result = new DoubleValue(val); return(true); } else { result = JsonConstants.Null; return(false); } }
public override bool TryEvaluate(IList <IValue> args, out IValue result) { Debug.Assert(this.Arity.HasValue && args.Count == this.Arity !.Value); var arg0 = args[0]; switch (arg0.ValueKind) { case JsonValueKind.Number: result = arg0; return(true); case JsonValueKind.String: { var s = arg0.GetString(); Decimal dec; double dbl; if (Decimal.TryParse(s, out dec)) { result = new DecimalValue(dec); return(true); } else if (Double.TryParse(s, out dbl)) { result = new DoubleValue(dbl); return(true); } else { result = JsonConstants.Null; return(false); } } default: result = JsonConstants.Null; return(false); } }
public override bool TryEvaluate(IList <IValue> args, out IValue result) { Debug.Assert(this.Arity.HasValue && args.Count == this.Arity !.Value); var arg0 = args[0]; if (arg0.ValueKind != JsonValueKind.Array || arg0.GetArrayLength() == 0) { result = JsonConstants.Null; return(false); } IValue sum; if (!SumFunction.Instance.TryEvaluate(args, out sum)) { result = JsonConstants.Null; return(false); } Decimal decVal; double dblVal; if (sum.TryGetDecimal(out decVal)) { result = new DecimalValue(decVal / arg0.GetArrayLength()); return(true); } else if (sum.TryGetDouble(out dblVal)) { result = new DoubleValue(dblVal / arg0.GetArrayLength()); return(true); } else { result = JsonConstants.Null; return(false); } }
public override bool TryEvaluate(DynamicResources resources, IValue root, JsonLocationNode lastNode, IValue current, ProcessingFlags options, out IValue value) { if (current.ValueKind == JsonValueKind.Object) { IValue element; if (current.TryGetProperty(_identifier, out element)) { return(this.TryEvaluateTail(resources, root, PathGenerator.Generate(lastNode, _identifier, options), element, options, out value)); } else { value = JsonConstants.Null; return(true); } } else if (current.ValueKind == JsonValueKind.Array && _identifier == "length") { value = new DecimalValue(new Decimal(current.GetArrayLength())); return(true); } else if (current.ValueKind == JsonValueKind.String && _identifier == "length") { byte[] bytes = Encoding.UTF32.GetBytes(current.GetString().ToCharArray()); value = new DecimalValue(new Decimal(current.GetString().Length)); return(true); } else { value = JsonConstants.Null; return(true); } }
public override bool TryEvaluate(IList <IValue> args, out IValue result) { Debug.Assert(this.Arity.HasValue && args.Count == this.Arity !.Value); var arg0 = args[0]; switch (arg0.ValueKind) { case JsonValueKind.Object: { int count = 0; foreach (var item in arg0.EnumerateObject()) { ++count; } result = new DecimalValue(new Decimal(count)); return(true); } case JsonValueKind.Array: result = new DecimalValue(new Decimal(arg0.GetArrayLength())); return(true); case JsonValueKind.String: { byte[] bytes = Encoding.UTF32.GetBytes(arg0.GetString().ToCharArray()); result = new DecimalValue(new Decimal(bytes.Length / 4)); return(true); } default: { result = JsonConstants.Null; return(false); } } }