Пример #1
0
 internal static string PrettifyJson(this RedisValue json)
 {
     if (!json.HasValue) return json;
     if (!json.StartsWith("{") && !json.StartsWith("[")) return json;
     try
     {
         var obj = JsonConvert.DeserializeObject(json);
         return JsonConvert.SerializeObject(obj, Formatting.Indented);
     }
     catch (JsonReaderException)
     {
         return json;
     }
 }
        public void RedisValueStartsWith()
        {
            // test strings
            RedisValue x = "abc";

            Assert.True(x.StartsWith("a"), LineNumber());
            Assert.True(x.StartsWith("ab"), LineNumber());
            Assert.True(x.StartsWith("abc"), LineNumber());
            Assert.False(x.StartsWith("abd"), LineNumber());
            Assert.False(x.StartsWith("abcd"), LineNumber());
            Assert.False(x.StartsWith(123), LineNumber());
            Assert.False(x.StartsWith(false), LineNumber());

            // test binary
            x = Encoding.ASCII.GetBytes("abc");
            Assert.True(x.StartsWith("a"), LineNumber());
            Assert.True(x.StartsWith("ab"), LineNumber());
            Assert.True(x.StartsWith("abc"), LineNumber());
            Assert.False(x.StartsWith("abd"), LineNumber());
            Assert.False(x.StartsWith("abcd"), LineNumber());
            Assert.False(x.StartsWith(123), LineNumber());
            Assert.False(x.StartsWith(false), LineNumber());

            Assert.True(x.StartsWith(Encoding.ASCII.GetBytes("a")), LineNumber());
            Assert.True(x.StartsWith(Encoding.ASCII.GetBytes("ab")), LineNumber());
            Assert.True(x.StartsWith(Encoding.ASCII.GetBytes("abc")), LineNumber());
            Assert.False(x.StartsWith(Encoding.ASCII.GetBytes("abd")), LineNumber());
            Assert.False(x.StartsWith(Encoding.ASCII.GetBytes("abcd")), LineNumber());

            x = 10; // integers are effectively strings in this context
            Assert.True(x.StartsWith(1), LineNumber());
            Assert.True(x.StartsWith(10), LineNumber());
            Assert.False(x.StartsWith(100), LineNumber());
        }