Exemplo n.º 1
0
        public void CreateFromDateTimeTest()
        {
            DateTime       dt  = DateTime.Now;
            DateTimeOffset dto = DateTimeOffset.Now;

            JsonValue jvDt1 = (JsonValue)dt;
            JsonValue jvDt2 = JsonValueExtensions.CreateFrom(dt);

            JsonValue jvDto1 = (JsonValue)dto;
            JsonValue jvDto2 = JsonValueExtensions.CreateFrom(dto);

            Assert.Equal(dt, (DateTime)jvDt1);
            Assert.Equal(dt, (DateTime)jvDt2);

            Assert.Equal(dto, (DateTimeOffset)jvDto1);
            Assert.Equal(dto, (DateTimeOffset)jvDto2);

            Assert.Equal(dt, jvDt1.ReadAs <DateTime>());
            Assert.Equal(dt, jvDt2.ReadAs <DateTime>());

            Assert.Equal(dto, jvDto1.ReadAs <DateTimeOffset>());
            Assert.Equal(dto, jvDto2.ReadAs <DateTimeOffset>());

            Assert.Equal(jvDt1.ToString(), jvDt2.ToString());
            Assert.Equal(jvDto1.ToString(), jvDto2.ToString());
        }
        private static void Flatten(List<string> pairs, JsonValue input, List<object> indices)
        {
            if (input == null)
            {
                return; // null values aren't serialized
            }

            switch (input.JsonType)
            {
                case JsonType.Array:
                    for (int i = 0; i < input.Count; i++)
                    {
                        indices.Add(i);
                        Flatten(pairs, input[i], indices);
                        indices.RemoveAt(indices.Count - 1);
                    }

                    break;
                case JsonType.Object:
                    foreach (var kvp in input)
                    {
                        indices.Add(kvp.Key);
                        Flatten(pairs, kvp.Value, indices);
                        indices.RemoveAt(indices.Count - 1);
                    }

                    break;
                default:
                    var value = input.ReadAs<string>();
                    var name = new StringBuilder();

                    for (int i = 0; i < indices.Count; i++)
                    {
                        var index = indices[i];
                        if (i > 0)
                        {
                            name.Append('[');
                        }

                        if (i < indices.Count - 1 || index is string)
                        {
                            // last array index not shown
                            name.Append(index);
                        }

                        if (i > 0)
                        {
                            name.Append(']');
                        }
                    }

                    pairs.Add(string.Format("{0}={1}", Uri.EscapeDataString(name.ToString()), Uri.EscapeDataString(value)));

                    break;
            }
        }
Exemplo n.º 3
0
        public void ReadAsTests()
        {
            JsonValue target   = AnyInstance.DefaultJsonValue;
            string    typeName = target.GetType().FullName;

            string errorMsgFormat = "Cannot read '{0}' as '{1}' type.";

            ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs(typeof(bool)); }, String.Format(errorMsgFormat, typeName, typeof(bool)));
            ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs(typeof(string)); }, String.Format(errorMsgFormat, typeName, typeof(string)));
            ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs(typeof(JsonObject)); }, String.Format(errorMsgFormat, typeName, typeof(JsonObject)));

            ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs <bool>(); }, String.Format(errorMsgFormat, typeName, typeof(bool)));
            ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs <string>(); }, String.Format(errorMsgFormat, typeName, typeof(string)));
            ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs <JsonObject>(); }, String.Format(errorMsgFormat, typeName, typeof(JsonObject)));

            bool       boolValue;
            string     stringValue;
            JsonObject objValue;

            object value;

            Assert.False(target.TryReadAs(typeof(bool), out value), "TryReadAs expected to return false");
            Assert.Null(value);

            Assert.False(target.TryReadAs(typeof(string), out value), "TryReadAs expected to return false");
            Assert.Null(value);

            Assert.False(target.TryReadAs(typeof(JsonObject), out value), "TryReadAs expected to return false");
            Assert.Null(value);

            Assert.False(target.TryReadAs <bool>(out boolValue), "TryReadAs expected to return false");
            Assert.False(boolValue);

            Assert.False(target.TryReadAs <string>(out stringValue), "TryReadAs expected to return false");
            Assert.Null(stringValue);

            Assert.False(target.TryReadAs <JsonObject>(out objValue), "TryReadAs expected to return false");
            Assert.Null(objValue);
        }
Exemplo n.º 4
0
            public static ValidationResult IsStringContainsCharSimple(JsonValue jv)
            {
                string str = jv.ReadAs<string>();

                if (str.Contains("Char"))
                {
                    return ValidationResult.Success;
                }

                return new ValidationResult("String must contain 'Char'");
            }