コード例 #1
0
        public void ReadBytes_next_document_size_equal_zero()
        {
            JObjectValidation jObject = new JObjectValidation
            {
                BooleanField = true,
                ByteField    = 1,
                CharField    = 'a',
                DateField    = "2016-08-24T18:30:32.2387069+00:00",
                DoubleField  = 10.5,
                IntField     = 10,
                ListField    = new object[2] {
                    new { }, new { }
                },
                ObjectField = new { },
                StringField = "string",
                NullField   = null
            };

            byte[] jObjectBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jObject));
            byte[] length       = Encoding.UTF8.GetBytes(jObjectBytes.Length.ToString().PadLeft(8));

            Stream stream = new MemoryStream();

            stream.Write(length, 0, 8);
            stream.Write(jObjectBytes, 0, jObjectBytes.Length);
            stream.Position = stream.Length;

            using (IJsonStream jsonStream = new JsonStream(stream))
            {
                byte[] bytes = jsonStream.ReadBytes();
                Assert.IsNull(bytes);
            }
        }
コード例 #2
0
        public void Contain_Numbers()
        {
            using var stream = JsonStream.ArrayOf(new[] { 10, 20, 40 }).Build();
            using var reader = new StreamReader(stream, Encoding.UTF8);
            var json = reader.ReadToEnd();

            json.Should().Be("[10,20,40]");
        }
コード例 #3
0
    public static string ToJson(this SceneManager sceneManager)
    {
        var stringStorage = new StringSerializerStorage();
        var stream        = new JsonStream();

        sceneManager.SaveState(stringStorage, stream);
        return(stringStorage.Result);
    }
コード例 #4
0
        public void Contain_Async()
        {
            using var stream = JsonStream.ArrayOf(new[] { 10, 20, 40 }.ToAsyncEnumerable()).Build();
            using var reader = new StreamReader(stream, Encoding.UTF8);
            var json = reader.ReadToEnd();

            json.Should().Be("[10,20,40]");
        }
コード例 #5
0
        public void Constructor_with_filePath_when_filePath_is_null()
        {
            ArgumentNullException exception = Assert.ThrowsException <ArgumentNullException>(() =>
            {
                IJsonStream jsonStream = new JsonStream(Modes.ReadAndWrite, "");
            });

            Assert.AreEqual("filePath", exception.ParamName);
        }
コード例 #6
0
 public static void DeserializeFromJSON(this ViewModel model, string json)
 {
     ISerializerStream stream = new JsonStream();
     stream.DeepSerialize = true;
     stream.DependencyContainer = uFrameKernel.Container;
     stream.TypeResolver = new ViewModelResolver() {Container = uFrameKernel.Container};
     stream.Load(Encoding.UTF8.GetBytes(json));
     model.Read(stream);
 }
コード例 #7
0
 public static string SerializeToJSON(this ViewModel model)
 {
     ISerializerStream stream = new JsonStream();
     ISerializerStorage storage = new StringSerializerStorage();
     stream.DeepSerialize = true;
     model.Write(stream);
     storage.Save(stream);
     return storage.ToString();
 }
コード例 #8
0
    public static void FromJson(this SceneManager sceneManager, string stateData)
    {
        var stringStorage = new StringSerializerStorage()
        {
            Result = stateData
        };
        var stream = new JsonStream();

        sceneManager.LoadState(stringStorage, stream);
    }
コード例 #9
0
        public static string SerializeToJSON(this ViewModel model)
        {
            ISerializerStream  stream  = new JsonStream();
            ISerializerStorage storage = new StringSerializerStorage();

            stream.DeepSerialize = true;
            model.Write(stream);
            storage.Save(stream);
            return(storage.ToString());
        }
コード例 #10
0
        public void Constructor_with_filestream_given_a_null_filestream()
        {
            ArgumentNullException exception = Assert.ThrowsException <ArgumentNullException>(() =>
            {
                FileStream fileStream  = null;
                IJsonStream jsonStream = new JsonStream(fileStream);
            });

            Assert.AreEqual("stream", exception.ParamName);
        }
コード例 #11
0
        public void DeserializesManaged(string content, bool expected)
        {
            SecretProperties properties = new SecretProperties();

            using (JsonStream json = new JsonStream(content))
            {
                properties.Deserialize(json.AsStream());
            }

            Assert.AreEqual(expected, properties.Managed);
        }
コード例 #12
0
        /// <summary>
        /// Attempts to read any numeric character from the <see cref="JsonStream"/> in sequence, until the
        /// first non-numeric character is encountered.
        /// </summary>
        /// <param name="stream">The <see cref="JsonStream"/> to read from.</param>
        /// <returns>
        /// A string representation of the internal buffer, which contains a number as defined by the JSON
        /// <a href="https://tools.ietf.org/html/rfc7159#section-6">RFC7591</a> specification.
        /// </returns>
        /// <remarks>
        /// No form of validation is carried out against the numeric sequence before returning it.
        /// </remarks>
        public string Read(JsonStream stream)
        {
            _readPosition = 0;

            while (IsNumber(stream.Peek()))
            {
                _buffer[_readPosition++] = stream.Read();
            }

            return(new string(_buffer, 0, _readPosition));
        }
コード例 #13
0
        public void DeserializesRecoverableDays(string content, int?expected)
        {
            SecretProperties properties = new SecretProperties();

            using (JsonStream json = new JsonStream(content))
            {
                properties.Deserialize(json.AsStream());
            }

            Assert.AreEqual(expected, properties.RecoverableDays);
        }
コード例 #14
0
        public void Constructor_with_filestream_given_the_size_as_zero()
        {
            ArgumentOutOfRangeException exception = Assert.ThrowsException <ArgumentOutOfRangeException>(() =>
            {
                using (FileStream stream = File.Create(GetTestFileName()))
                {
                    IJsonStream jsonStream = new JsonStream(stream, Modes.ReadAndWrite, 0);
                }
            });

            Assert.AreEqual("documentSizeLengthInBytes", exception.ParamName);
        }
コード例 #15
0
        public static void DeserializeFromJSON(this ViewModel model, string json)
        {
            ISerializerStream stream = new JsonStream();

            stream.DeepSerialize       = true;
            stream.DependencyContainer = uFrameKernel.Container;
            stream.TypeResolver        = new ViewModelResolver()
            {
                Container = uFrameKernel.Container
            };
            stream.Load(Encoding.UTF8.GetBytes(json));
            model.Read(stream);
        }
コード例 #16
0
        public void ReadBytes_using_WriteOnly_mode()
        {
            Stream stream = new MemoryStream();

            ForbiddenOperationException exception = Assert.ThrowsException <ForbiddenOperationException>(() =>
            {
                using (IJsonStream jsonStream = new JsonStream(stream, Modes.WriteOnly))
                {
                    byte[] bytes = jsonStream.ReadBytes();
                }
            });

            Assert.AreEqual("Can't read in WriteOnly mode.", exception.Message);
        }
コード例 #17
0
        public void ReadJArray_pass()
        {
            JObjectValidation jObject = new JObjectValidation
            {
                BooleanField = true,
                ByteField    = 1,
                CharField    = 'a',
                DateField    = "2016-08-24T18:30:32.2387069+00:00",
                DoubleField  = 10.5,
                IntField     = 10,
                ListField    = new object[2] {
                    new { }, new { }
                },
                ObjectField = new { },
                StringField = "string",
                NullField   = null
            };

            JObjectValidation[] list = new JObjectValidation[1] {
                jObject
            };

            byte[] jObjectBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(list));
            byte[] length       = Encoding.UTF8.GetBytes(jObjectBytes.Length.ToString().PadLeft(8));

            Stream stream = new MemoryStream();

            stream.Write(length, 0, 8);
            stream.Write(jObjectBytes, 0, jObjectBytes.Length);
            stream.Position = 0;

            using (IJsonStream jsonStream = new JsonStream(stream))
            {
                JArray readArray = jsonStream.ReadJArray();
                Assert.AreEqual(1, readArray.Count);

                JObjectValidation readJObject = readArray.First.ToObject <JObjectValidation>();

                Assert.AreEqual(jObject.BooleanField, readJObject.BooleanField);
                Assert.AreEqual(jObject.ByteField, readJObject.ByteField);
                Assert.AreEqual(jObject.CharField, readJObject.CharField);
                Assert.AreEqual(jObject.DateField, readJObject.DateField);
                Assert.AreEqual(jObject.DoubleField, readJObject.DoubleField);
                Assert.AreEqual(jObject.IntField, readJObject.IntField);
                Assert.AreEqual(jObject.ListField.Length, readJObject.ListField.Length);
                Assert.AreEqual(jObject.StringField, readJObject.StringField);
                Assert.IsNotNull(readJObject.ObjectField);
                Assert.IsNull(readJObject.NullField);
            }
        }
コード例 #18
0
        public void GetString()
        {
            Human human = new Human
            {
                Name   = "Marcus",
                Age    = 35,
                Gender = Gender.Male
            };

            string expectedString = "{\"Name\":\"Marcus\",\"Age\":35,\"Gender\":\"Male\"}";
            string humanString    = JsonStream.GetString(human);

            Assert.AreEqual(expectedString, humanString);
        }
コード例 #19
0
        public void WriteBytesAsync_using_optimized_constructor()
        {
            using (File.Create(GetTestFileName())) { }
            using (IJsonStream jsonStream = new JsonStream(Modes.ReadAndWrite, GetTestFileName()))
            {
                byte[] bytes = null;
                ForbiddenOperationException ex = Assert.ThrowsException <ForbiddenOperationException>(() =>
                {
                    jsonStream.WriteBytesAsync(bytes, false).GetAwaiter().GetResult();
                });

                Assert.AreEqual("Do not call any async method when using optimized constructor.", ex.Message);
            }
        }
コード例 #20
0
        public void WriteBytes_when_bytes_length_is_zero()
        {
            Stream stream = new MemoryStream();

            using (IJsonStream jsonStream = new JsonStream(stream))
            {
                byte[]            bytes = new byte[0];
                ArgumentException ex    = Assert.ThrowsException <ArgumentException>(() =>
                {
                    jsonStream.WriteBytes(bytes, false);
                });
                Assert.AreEqual("bytes", ex.ParamName);
                Assert.IsTrue(ex.Message.Contains("Can't write an empty byte array."));
            }
        }
コード例 #21
0
        public void WriteBytesAsync_using_ReadOnly_mode()
        {
            Stream stream = new MemoryStream();

            using (IJsonStream jsonStream = new JsonStream(stream, Modes.ReadOnly))
            {
                byte[] bytes = null;
                ForbiddenOperationException ex = Assert.ThrowsException <ForbiddenOperationException>(() =>
                {
                    jsonStream.WriteBytesAsync(bytes, false).GetAwaiter().GetResult();
                });

                Assert.AreEqual("Can't write in ReadOnly mode.", ex.Message);
            }
        }
コード例 #22
0
        public void GetBytes()
        {
            Human human = new Human
            {
                Name   = "Marcus",
                Age    = 35,
                Gender = Gender.Male
            };
            string expectedString = "{\"Name\":\"Marcus\",\"Age\":35,\"Gender\":\"Male\"}";

            byte[] expectedBytes = System.Text.Encoding.UTF8.GetBytes(expectedString);
            byte[] humanBytes    = JsonStream.GetBytes(human);
            string humanString   = System.Text.Encoding.UTF8.GetString(humanBytes);

            Assert.AreEqual(expectedString, humanString);
        }
コード例 #23
0
        public void WriteBytes_when_bytes_is_null()
        {
            Stream stream = new MemoryStream();

            using (IJsonStream jsonStream = new JsonStream(stream))
            {
                byte[] bytes = null;

                ArgumentNullException ex = Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    jsonStream.WriteBytes(bytes, false);
                });
                Assert.AreEqual("bytes", ex.ParamName);
                Assert.AreEqual(0, stream.Length);
            }
        }
コード例 #24
0
        public void WriteBytes_when_validate_is_true_not_passing()
        {
            Stream stream = new MemoryStream();

            using (IJsonStream jsonStream = new JsonStream(stream))
            {
                byte[] bytes = Encoding.UTF8.GetBytes("NOT PASS");

                JsonReaderException ex = Assert.ThrowsException <JsonReaderException>(() =>
                {
                    jsonStream.WriteBytes(bytes, true);
                });
                Assert.IsInstanceOfType(ex, typeof(JsonReaderException));
                Assert.AreEqual(0, stream.Length);
            }
        }
コード例 #25
0
ファイル: CheckStream.cs プロジェクト: finloop/WBot
 static private void read(string channel)
 {
     if (Chatters.config == null)
     {
         Chatters.config = FileIO.ReadConfigParameters("Config.json");
     }
     else
     {
         String           text;
         WebClient        web    = new WebClient();
         System.IO.Stream stream = web.OpenRead("https://api.twitch.tv/kraken/streams/" + channel + "?client_id=" + Chatters.config.clientID);
         using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
         {
             text = reader.ReadToEnd();
             json = JsonConvert.DeserializeObject <JsonStream>(text);
         }
     }
 }
コード例 #26
0
        public void Use_Serializer_Settings()
        {
            var enumerable = new[] { 72, 17, 50, 4 };

            using var stream = JsonStream
                               .Using(
                      new JsonTextWriterFactory(
                          1024,
                          new Indented()
                          )
                      )
                               .ArrayOf(enumerable)
                               .Build();
            using var reader = new StreamReader(stream, Encoding.UTF8);
            var json = reader.ReadToEnd();
            var nl   = Environment.NewLine;

            json.Should().Be($"[{nl}  72,{nl}  17,{nl}  50,{nl}  4{nl}]");
        }
コード例 #27
0
        public void WriteBytes_when_validate_is_true_pass()
        {
            Stream stream = new MemoryStream();

            using (IJsonStream jsonStream = new JsonStream(stream))
            {
                JObjectValidation jObject = new JObjectValidation
                {
                    BooleanField = true,
                    ByteField    = 1,
                    CharField    = 'a',
                    DateField    = "2016-08-24T18:30:32.2387069+00:00",
                    DoubleField  = 10.5,
                    IntField     = 10,
                    ListField    = new object[2] {
                        new { }, new { }
                    },
                    ObjectField = new { },
                    StringField = "string",
                    NullField   = null
                };

                byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jObject));

                jsonStream.WriteBytes(bytes, true);
                stream.Position = 0;
                JObjectValidation readJObject = jsonStream.ReadObject <JObjectValidation>();
                Assert.AreEqual(jObject.BooleanField, readJObject.BooleanField);
                Assert.AreEqual(jObject.ByteField, readJObject.ByteField);
                Assert.AreEqual(jObject.CharField, readJObject.CharField);
                Assert.AreEqual(jObject.DateField, readJObject.DateField);
                Assert.AreEqual(jObject.DoubleField, readJObject.DoubleField);
                Assert.AreEqual(jObject.IntField, readJObject.IntField);
                Assert.AreEqual(jObject.ListField.Length, readJObject.ListField.Length);
                Assert.AreEqual(jObject.StringField, readJObject.StringField);
                Assert.IsNotNull(readJObject.ObjectField);
                Assert.IsNull(readJObject.NullField);
            }
        }
コード例 #28
0
        public void ReadBytes_cant_read_entire_next_document()
        {
            JObjectValidation jObject = new JObjectValidation
            {
                BooleanField = true,
                ByteField    = 1,
                CharField    = 'a',
                DateField    = "2016-08-24T18:30:32.2387069+00:00",
                DoubleField  = 10.5,
                IntField     = 10,
                ListField    = new object[2] {
                    new { }, new { }
                },
                ObjectField = new { },
                StringField = "string",
                NullField   = null
            };

            byte[] jObjectBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jObject));
            byte[] length       = Encoding.UTF8.GetBytes((jObjectBytes.Length + 1).ToString().PadLeft(8));

            Stream stream = new MemoryStream();

            stream.Write(length, 0, 8);
            stream.Write(jObjectBytes, 0, jObjectBytes.Length);
            stream.Position = 0;

            InvalidJsonDocumentException exception = Assert.ThrowsException <InvalidJsonDocumentException>(() =>
            {
                using (IJsonStream jsonStream = new JsonStream(stream))
                {
                    byte[] bytes = jsonStream.ReadBytes();
                }
            });

            Assert.IsTrue(exception.Message.Contains("Cant't read all bytes of the json document at position"));
        }
コード例 #29
0
        public void Contain_Objects()
        {
            var pets = new[]
            {
                new
                {
                    Name = "Bella",
                    Race = "Cat",
                    Age  = 3
                },
                new
                {
                    Name = "Max",
                    Race = "Dog",
                    Age  = 7
                }
            };

            using var stream = JsonStream.ArrayOf(pets).Build();
            using var reader = new StreamReader(stream, Encoding.UTF8);
            var json = reader.ReadToEnd();

            json.Should().Be(@"[{""Name"":""Bella"",""Race"":""Cat"",""Age"":3},{""Name"":""Max"",""Race"":""Dog"",""Age"":7}]");
        }
コード例 #30
0
    public override void OnInspectorGUI()
    {
        GUIHelpers.IsInsepctor = true;
        DrawTitleBar("Scene Manager");

        base.OnInspectorGUI();
        if (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)
        {

            //if (GUI.Button(GUIHelpers.GetRect(ElementDesignerStyles.ButtonStyle), "Serialize To String", ElementDesignerStyles.ButtonStyle))
            //{
            //    var sm = (target as SceneManager);

            //    sm.SaveState(new TextAssetStorage() { AssetPath = "Assets/TestData.txt" }, new JsonStream() { UseReferences = true });

            //}
            //if (GUI.Button(GUIHelpers.GetRect(ElementDesignerStyles.ButtonStyle), "Load From String", ElementDesignerStyles.ButtonStyle))
            //{
            //    var sm = (target as SceneManager);
            //    sm.LoadState(new TextAssetStorage() { AssetPath = "Assets/TestData.txt" }, new JsonStream() { UseReferences = true });

            //}
            if (GUIHelpers.DoToolbarEx("Persistable Views"))
            {
                foreach (var viewBase in GameManager.ActiveSceneManager.PersistantViews)
                {
                    if (viewBase == null) continue;
                    if (GUIHelpers.DoTriggerButton(new UFStyle(viewBase.Identifier + ": " + viewBase.name,
                        ElementDesignerStyles.EventButtonStyleSmall, null, null, null, false,
                        TextAnchor.MiddleCenter) { }))
                    {
                        //var fileStorage = new TextAssetStorage();
                        var stringStorage = new StringSerializerStorage();
                        var stream = new JsonStream();
                        viewBase.Write(stream);
                        stringStorage.Save(stream);
                        Debug.Log(stringStorage);
                    }
                }

            }
            if (GUIHelpers.DoToolbarEx("Persistable View-Models"))
            {
                foreach (var viewBase in GameManager.ActiveSceneManager.PersistantViewModels)
                {
                    if (viewBase == null) continue;
                    if (GUIHelpers.DoTriggerButton(new UFStyle(viewBase.Identifier + ": " + viewBase.GetType().Name,
                        ElementDesignerStyles.EventButtonStyleSmall, null, null, null, false,
                        TextAnchor.MiddleCenter) { }))
                    {
                        //var fileStorage = new TextAssetStorage();
                        var stringStorage = new StringSerializerStorage();
                        var stream = new JsonStream();
                        viewBase.Write(stream);
                        stringStorage.Save(stream);
                        Debug.Log(stringStorage);
                    }
                }
            }
        }

        GUIHelpers.IsInsepctor = false;
    }
コード例 #31
0
    static void ImportStream()
    {
        JsonStream jsonStream = new JsonStream();

        jsonStream.DoEverythingStream();
    }
コード例 #32
0
    public override void OnInspectorGUI()
    {
        GUIHelpers.IsInsepctor = true;
        DrawTitleBar("Scene Manager");

        base.OnInspectorGUI();
        if (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)
        {
            //if (GUI.Button(GUIHelpers.GetRect(ElementDesignerStyles.ButtonStyle), "Serialize To String", ElementDesignerStyles.ButtonStyle))
            //{
            //    var sm = (target as SceneManager);

            //    sm.SaveState(new TextAssetStorage() { AssetPath = "Assets/TestData.txt" }, new JsonStream() { UseReferences = true });

            //}
            //if (GUI.Button(GUIHelpers.GetRect(ElementDesignerStyles.ButtonStyle), "Load From String", ElementDesignerStyles.ButtonStyle))
            //{
            //    var sm = (target as SceneManager);
            //    sm.LoadState(new TextAssetStorage() { AssetPath = "Assets/TestData.txt" }, new JsonStream() { UseReferences = true });

            //}
            if (GUIHelpers.DoToolbarEx("Persistable Views"))
            {
                foreach (var viewBase in GameManager.ActiveSceneManager.PersistantViews)
                {
                    if (viewBase == null)
                    {
                        continue;
                    }
                    if (GUIHelpers.DoTriggerButton(new UFStyle(viewBase.Identifier + ": " + viewBase.name,
                                                               ElementDesignerStyles.EventButtonStyleSmall, null, null, null, false,
                                                               TextAnchor.MiddleCenter)
                    {
                    }))
                    {
                        //var fileStorage = new TextAssetStorage();
                        var stringStorage = new StringSerializerStorage();
                        var stream        = new JsonStream();
                        viewBase.Write(stream);
                        stringStorage.Save(stream);
                        Debug.Log(stringStorage);
                    }
                }
            }
            if (GUIHelpers.DoToolbarEx("Persistable View-Models"))
            {
                foreach (var viewBase in GameManager.ActiveSceneManager.PersistantViewModels)
                {
                    if (viewBase == null)
                    {
                        continue;
                    }
                    if (GUIHelpers.DoTriggerButton(new UFStyle(viewBase.Identifier + ": " + viewBase.GetType().Name,
                                                               ElementDesignerStyles.EventButtonStyleSmall, null, null, null, false,
                                                               TextAnchor.MiddleCenter)
                    {
                    }))
                    {
                        //var fileStorage = new TextAssetStorage();
                        var stringStorage = new StringSerializerStorage();
                        var stream        = new JsonStream();
                        viewBase.Write(stream);
                        stringStorage.Save(stream);
                        Debug.Log(stringStorage);
                    }
                }
            }
        }

        GUIHelpers.IsInsepctor = false;
    }
コード例 #33
0
 public static IEnumerable <ReplayResult <TUserData> > Load <TUserData>(string jsonResultsFile)
 {
     return(JsonStream.DeserializeFromJson <ReplayResult <TUserData> >(jsonResultsFile));
 }