Load() public static method

public static Load ( Stream stream ) : JsonValue
stream System.IO.Stream
return JsonValue
Beispiel #1
0
        void ReadAsTest <T>(Random rndGen)
        {
            T instance = InstanceCreator.CreateInstanceOf <T>(rndGen);

            Log.Info("ReadAsTest<{0}>, instance = {1}", typeof(T).Name, instance);
            DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(T));
            JsonValue jv;

            using (MemoryStream ms = new MemoryStream())
            {
                dcjs.WriteObject(ms, instance);
                Log.Info("{0}: {1}", typeof(T).Name, Encoding.UTF8.GetString(ms.ToArray()));
                ms.Position = 0;
                jv          = JsonValue.Load(ms);
            }

            if (instance == null)
            {
                Assert.Null(jv);
            }
            else
            {
                T newInstance = jv.ReadAsType <T>();
                Assert.Equal(instance, newInstance);
            }
        }
        public void LoadTest()
        {
            string json = "{\"a\":123,\"b\":[false,null,12.34]}";

            foreach (bool useLoadTextReader in new bool[] { false, true })
            {
                JsonValue jv;
                if (useLoadTextReader)
                {
                    using (StringReader sr = new StringReader(json))
                    {
                        jv = JsonValue.Load(sr);
                    }
                }
                else
                {
                    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                    {
                        jv = JsonValue.Load(ms);
                    }
                }

                Assert.Equal(json, jv.ToString());
            }

            ExceptionHelper.Throws <ArgumentNullException>(() => JsonValue.Load((Stream)null));
            ExceptionHelper.Throws <ArgumentNullException>(() => JsonValue.Load((TextReader)null));
        }
Beispiel #3
0
 public void ZeroedStreamLoadingThrowsFormatException()
 {
     ExpectException <FormatException>(delegate
     {
         using (MemoryStream ms = new MemoryStream(new byte[10]))
         {
             JsonValue.Load(ms);
         }
     });
 }
Beispiel #4
0
 public void StreamLoading(string jsonString, bool useSeekableStream, string encodingName, Encoding encoding)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         StreamWriter sw = new StreamWriter(ms, encoding);
         sw.Write(jsonString);
         sw.Flush();
         Log.Info("[{0}] {1}: size of the json stream: {2}", useSeekableStream ? "seekable" : "non-seekable", encodingName, ms.Position);
         ms.Position = 0;
         JsonValue parsed = JsonValue.Parse(jsonString);
         JsonValue loaded = useSeekableStream ? JsonValue.Load(ms) : JsonValue.Load(new NonSeekableStream(ms));
         using (StringReader sr = new StringReader(jsonString))
         {
             JsonValue loadedFromTextReader = JsonValue.Load(sr);
             Assert.Equal(parsed.ToString(), loaded.ToString());
             Assert.Equal(parsed.ToString(), loadedFromTextReader.ToString());
         }
     }
 }
        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);
        }