コード例 #1
0
 private static bool CompareJsonPrimitiveTypes(JsonPrimitive objA, JsonPrimitive objB)
 {
     try
     {
         if (objA.ToString() != objB.ToString())
         {
             // Special case due to daylight saving hours change: every March on the morning of the third Sunday, we adjust the time
             // from 2am to 3am straight, so for that one hour 2:13am = 3:15am.  We must result to the UTC ticks to verify the actual
             // time is always the same, regardless of the loc/glob setup on the machine
             if (objA.ToString().StartsWith("\"\\/Date(") && objA.ToString().EndsWith(")\\/\""))
             {
                 return(GetUTCTicks(objA) == GetUTCTicks(objB));
             }
             else
             {
                 Log.Info("JsonValueVerifier (JsonPrimitiveTypes) Error: objA = " + objA.ToString());
                 Log.Info("JsonValueVerifier (JsonPrimitiveTypes) Error: objB = " + objB.ToString());
                 return(false);
             }
         }
         else
         {
             return(true);
         }
     }
     catch (Exception e)
     {
         Log.Info("JsonValueVerifier (JsonPrimitiveTypes) Error: An Exception was thrown: " + e);
         return(false);
     }
 }
コード例 #2
0
        public void TestJsonPrimitiveInteger()
        {
            dynamic number = new JsonPrimitive(1200);
            var     json   = number.ToString();
            var     prim   = JsonMapper.Parse(json);

            Assert.Equal(1200, (int)prim);
        }
コード例 #3
0
        public void TestJsonPrimitiveFloat()
        {
            dynamic number = new JsonPrimitive(45.2f);
            var     json   = number.ToString();
            var     prim   = JsonMapper.Parse(json);

            Assert.Equal(45.2f, (float)prim, 3);
        }
コード例 #4
0
        public void TestJsonPrimitiveDouble()
        {
            dynamic number = new JsonPrimitive(5.4756903);
            var     json   = number.ToString();
            var     prim   = JsonMapper.Parse(json);

            Assert.Equal(5.4756903, (double)prim, 8);
        }
コード例 #5
0
        public void TestJsonPrimitiveBool()
        {
            dynamic boolean = new JsonPrimitive(true);
            var     json    = boolean.ToString();
            var     prim    = JsonMapper.Parse(json);

            Assert.True((bool)prim);
        }
コード例 #6
0
        public void TestJsonPrimitiveLong()
        {
            dynamic number = new JsonPrimitive((long)1200000000000);
            var     json   = number.ToString();
            var     prim   = JsonMapper.Parse(json);

            Assert.Equal(1200000000000, (long)prim);
        }
コード例 #7
0
        public void TestJsonPrimitiveNull()
        {
            dynamic nill = new JsonPrimitive(null);
            var     json = nill.ToString();
            var     prim = JsonMapper.Parse(json);
            var     obj  = (object)prim;

            Assert.Null(obj);
        }
コード例 #8
0
 private static void BuildParams(string prefix, JsonValue jsonValue, List <string> results)
 {
     if (jsonValue is JsonPrimitive)
     {
         JsonPrimitive jsonPrimitive = jsonValue as JsonPrimitive;
         if (jsonPrimitive != null)
         {
             if (jsonPrimitive.JsonType == JsonType.String && string.IsNullOrEmpty(jsonPrimitive.Value.ToString()))
             {
                 results.Add(prefix + "=" + string.Empty);
             }
             else
             {
                 if (jsonPrimitive.Value is DateTime || jsonPrimitive.Value is DateTimeOffset)
                 {
                     string dateStr = jsonPrimitive.ToString();
                     if (!string.IsNullOrEmpty(dateStr) && dateStr.StartsWith("\""))
                     {
                         dateStr = dateStr.Substring(1, dateStr.Length - 2);
                     }
                     results.Add(prefix + "=" + HttpUtility.UrlEncode(dateStr));
                 }
                 else
                 {
                     results.Add(prefix + "=" + HttpUtility.UrlEncode(jsonPrimitive.Value.ToString()));
                 }
             }
         }
         else
         {
             results.Add(prefix + "=" + string.Empty);
         }
     }
     else if (jsonValue is JsonArray)
     {
         for (int i = 0; i < jsonValue.Count; i++)
         {
             if (jsonValue[i] is JsonArray || jsonValue[i] is JsonObject)
             {
                 BuildParams(prefix + "[" + i + "]", jsonValue[i], results);
             }
             else
             {
                 BuildParams(prefix + "[]", jsonValue[i], results);
             }
         }
     }
     else //jsonValue is JsonObject
     {
         foreach (KeyValuePair <string, JsonValue> item in jsonValue)
         {
             BuildParams(prefix + "[" + item.Key + "]", item.Value, results);
         }
     }
 }
コード例 #9
0
        public void ValidJsonObjectDateTimeOffsetRoundTrip()
        {
            int seed = 1;

            Log.Info("Seed: {0}", seed);
            Random rndGen = new Random(seed);

            JsonPrimitive sourceJson = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen));
            JsonPrimitive newJson    = (JsonPrimitive)JsonValue.Parse(sourceJson.ToString());

            if (!JsonValueVerifier.Compare(sourceJson, newJson))
            {
                Assert.Fail("Test failed!  The new JsonObject DateTimeOffset value does not equal to the original one.");
            }
        }
コード例 #10
0
ファイル: JsonPrimitiveTests.cs プロジェクト: yufeih/corefx
        public void ToString_Null_WorksDependingOnOverload()
        {
            JsonPrimitive primitive = new JsonPrimitive((string)null);
            Assert.Equal("\"\"", primitive.ToString());

            using (StringWriter textWriter = new StringWriter())
            {
                primitive.Save(textWriter);
                Assert.Equal("\"\"", textWriter.ToString());
            }

            using (MemoryStream stream = new MemoryStream())
            {
                Assert.Throws<NullReferenceException>(() => primitive.Save(stream));
            }
        }
コード例 #11
0
        public void ToString_Null_WorksDependingOnOverload()
        {
            JsonPrimitive primitive = new JsonPrimitive((string)null);
            Assert.Equal("\"\"", primitive.ToString());

            using (StringWriter textWriter = new StringWriter())
            {
                primitive.Save(textWriter);
                Assert.Equal("\"\"", textWriter.ToString());
            }

            using (MemoryStream stream = new MemoryStream())
            {
                Assert.Throws<NullReferenceException>(() => primitive.Save(stream));
            }
        }
コード例 #12
0
        // the input JsonPrimitive DateTime format is "\/Date(24735422733034-0700)\/" or "\/Date(24735422733034)\/"
        // the only thing useful for us is the UTC ticks "24735422733034"
        // everything after - if present - is just an optional offset between the local time and UTC
        private static string GetUTCTicks(JsonPrimitive jprim)
        {
            string retValue = string.Empty;

            string origStr    = jprim.ToString();
            int    startIndex = origStr.IndexOf("Date(") + 5;
            int    endIndex   = origStr.IndexOf('-', startIndex + 1); // the UTC ticks can start with a '-' sign (dates prior to 1970/01/01)

            // if the optional offset is present in the data format, we want to take only the UTC ticks
            if (startIndex < endIndex)
            {
                retValue = origStr.Substring(startIndex, endIndex - startIndex);
            }
            else
            {
                // otherwise we assume the time format is without the oiptional offset, or unexpected, and use the whole string for comparison.
                retValue = origStr;
            }

            return(retValue);
        }
コード例 #13
0
        private void DoRoundTripCasting(JsonValue jo, Type type)
        {
            bool result = false;

            // Casting
            if (jo.JsonType == JsonType.String)
            {
                JsonValue jstr = (string)jo;
                if (type == typeof(DateTime))
                {
                    Log.Info("{0} Value:{1}", type.Name, ((DateTime)jstr).ToString(DateTimeFormatInfo.InvariantInfo));
                }
                else if (type == typeof(DateTimeOffset))
                {
                    Log.Info("{0} Value:{1}", type.Name, ((DateTimeOffset)jstr).ToString(DateTimeFormatInfo.InvariantInfo));
                }
                else if (type == typeof(Guid))
                {
                    Log.Info("{0} Value:{1}", type.Name, (Guid)jstr);
                }
                else if (type == typeof(char))
                {
                    Log.Info("{0} Value:{1}", type.Name, (char)jstr);
                }
                else if (type == typeof(Uri))
                {
                    Log.Info("{0} Value:{1}", type.Name, ((Uri)jstr).AbsoluteUri);
                }
                else
                {
                    Log.Info("{0} Value:{1}", type.Name, (string)jstr);
                }

                if (jo.ToString() == jstr.ToString())
                {
                    result = true;
                }
            }
            else if (jo.JsonType == JsonType.Object)
            {
                JsonObject jobj = new JsonObject((JsonObject)jo);

                if (jo.ToString() == jobj.ToString())
                {
                    result = true;
                }
            }
            else if (jo.JsonType == JsonType.Number)
            {
                JsonPrimitive jprim = (JsonPrimitive)jo;
                Log.Info("{0} Value:{1}", type.Name, jprim);

                if (jo.ToString() == jprim.ToString())
                {
                    result = true;
                }
            }
            else if (jo.JsonType == JsonType.Boolean)
            {
                JsonPrimitive jprim = (JsonPrimitive)jo;
                Log.Info("{0} Value:{1}", type.Name, (bool)jprim);

                if (jo.ToString() == jprim.ToString())
                {
                    result = true;
                }
            }

            if (!result)
            {
                Assert.Fail("Test Failed!");
            }
        }
コード例 #14
0
        private bool TestPrimitiveType(string typeName)
        {
            bool retValue    = true;
            bool specialCase = false;

            int seed = 1;

            Log.Info("Seed: {0}", seed);
            Random rndGen = new Random(seed);

            JsonPrimitive sourceJson = null;
            JsonPrimitive sourceJson2;
            object        tempValue = null;

            switch (typeName.ToLower())
            {
            case "boolean":
                tempValue   = PrimitiveCreator.CreateInstanceOfBoolean(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString().ToLower());
                sourceJson2 = new JsonPrimitive((bool)tempValue);
                break;

            case "byte":
                tempValue   = PrimitiveCreator.CreateInstanceOfByte(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((byte)tempValue);
                break;

            case "char":
                sourceJson2 = new JsonPrimitive((char)PrimitiveCreator.CreateInstanceOfChar(rndGen));
                specialCase = true;
                break;

            case "datetime":
                tempValue   = PrimitiveCreator.CreateInstanceOfDateTime(rndGen);
                sourceJson2 = new JsonPrimitive((DateTime)tempValue);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(sourceJson2.ToString());
                break;

            case "decimal":
                tempValue   = PrimitiveCreator.CreateInstanceOfDecimal(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(((decimal)tempValue).ToString(NumberFormatInfo.InvariantInfo));
                sourceJson2 = new JsonPrimitive((decimal)tempValue);
                break;

            case "double":
                double tempDouble = PrimitiveCreator.CreateInstanceOfDouble(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempDouble.ToString("R", NumberFormatInfo.InvariantInfo));
                sourceJson2 = new JsonPrimitive(tempDouble);
                break;

            case "guid":
                sourceJson2 = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfGuid(rndGen));
                specialCase = true;
                break;

            case "int16":
                tempValue   = PrimitiveCreator.CreateInstanceOfInt16(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((short)tempValue);
                break;

            case "int32":
                tempValue   = PrimitiveCreator.CreateInstanceOfInt32(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((int)tempValue);
                break;

            case "int64":
                tempValue   = PrimitiveCreator.CreateInstanceOfInt64(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((long)tempValue);
                break;

            case "sbyte":
                tempValue   = PrimitiveCreator.CreateInstanceOfSByte(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((sbyte)tempValue);
                break;

            case "single":
                float fltValue = PrimitiveCreator.CreateInstanceOfSingle(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(fltValue.ToString("R", NumberFormatInfo.InvariantInfo));
                sourceJson2 = new JsonPrimitive(fltValue);
                break;

            case "string":
                do
                {
                    tempValue = PrimitiveCreator.CreateInstanceOfString(rndGen);
                } while (tempValue == null);

                sourceJson2 = new JsonPrimitive((string)tempValue);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(sourceJson2.ToString());
                break;

            case "uint16":
                tempValue   = PrimitiveCreator.CreateInstanceOfUInt16(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((ushort)tempValue);
                break;

            case "uint32":
                tempValue   = PrimitiveCreator.CreateInstanceOfUInt32(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((uint)tempValue);
                break;

            case "uint64":
                tempValue   = PrimitiveCreator.CreateInstanceOfUInt64(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((ulong)tempValue);
                break;

            case "uri":
                Uri uri = null;
                do
                {
                    try
                    {
                        uri = PrimitiveCreator.CreateInstanceOfUri(rndGen);
                    }
                    catch (UriFormatException)
                    {
                    }
                } while (uri == null);

                sourceJson2 = new JsonPrimitive(uri);
                specialCase = true;
                break;

            case "null":
                sourceJson  = (JsonPrimitive)JsonValue.Parse("null");
                sourceJson2 = null;
                break;

            default:
                sourceJson  = null;
                sourceJson2 = null;
                break;
            }

            if (!specialCase)
            {
                // comparison between two constructors
                if (!JsonValueVerifier.Compare(sourceJson, sourceJson2))
                {
                    Log.Info("(JsonPrimitive)JsonValue.Parse(string) failed to match the results from default JsonPrimitive(obj)constructor for type {0}", typeName);
                    retValue = false;
                }

                if (sourceJson != null)
                {
                    // test JsonValue.Load(TextReader)
                    JsonPrimitive newJson = null;
                    using (StringReader sr = new StringReader(sourceJson.ToString()))
                    {
                        newJson = (JsonPrimitive)JsonValue.Load(sr);
                    }

                    if (!JsonValueVerifier.Compare(sourceJson, newJson))
                    {
                        Log.Info("JsonValue.Load(TextReader) failed to function properly for type {0}", typeName);
                        retValue = false;
                    }

                    // test JsonValue.Load(Stream) is located in the JObjectFromGenoTypeLib test case

                    // test JsonValue.Parse(string)
                    newJson = null;
                    newJson = (JsonPrimitive)JsonValue.Parse(sourceJson.ToString());
                    if (!JsonValueVerifier.Compare(sourceJson, newJson))
                    {
                        Log.Info("JsonValue.Parse(string) failed to function properly for type {0}", typeName);
                        retValue = false;
                    }
                }
            }
            else
            {
                // test JsonValue.Load(TextReader)
                JsonPrimitive newJson2 = null;
                using (StringReader sr = new StringReader(sourceJson2.ToString()))
                {
                    newJson2 = (JsonPrimitive)JsonValue.Load(sr);
                }

                if (!JsonValueVerifier.Compare(sourceJson2, newJson2))
                {
                    Log.Info("JsonValue.Load(TextReader) failed to function properly for type {0}", typeName);
                    retValue = false;
                }

                // test JsonValue.Load(Stream) is located in the JObjectFromGenoTypeLib test case

                // test JsonValue.Parse(string)
                newJson2 = null;
                newJson2 = (JsonPrimitive)JsonValue.Parse(sourceJson2.ToString());
                if (!JsonValueVerifier.Compare(sourceJson2, newJson2))
                {
                    Log.Info("JsonValue.Parse(string) failed to function properly for type {0}", typeName);
                    retValue = false;
                }
            }

            return(retValue);
        }
コード例 #15
0
 private static void UnparseJson3(JsonPrimitive jp, StringBuilder sb)
 {
     sb.Append(jp.ToString());
 }