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); } }
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]"); }
public static string ToJson(this SceneManager sceneManager) { var stringStorage = new StringSerializerStorage(); var stream = new JsonStream(); sceneManager.SaveState(stringStorage, stream); return(stringStorage.Result); }
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]"); }
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); }
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); }
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(); }
public static void FromJson(this SceneManager sceneManager, string stateData) { var stringStorage = new StringSerializerStorage() { Result = stateData }; var stream = new JsonStream(); sceneManager.LoadState(stringStorage, stream); }
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()); }
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); }
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); }
/// <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)); }
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); }
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); }
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); }
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); }
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); } }
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); }
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); } }
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.")); } }
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); } }
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); }
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); } }
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); } }
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); } } }
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}]"); }
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); } }
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")); }
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}]"); }
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; }
static void ImportStream() { JsonStream jsonStream = new JsonStream(); jsonStream.DoEverythingStream(); }
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; }
public static IEnumerable <ReplayResult <TUserData> > Load <TUserData>(string jsonResultsFile) { return(JsonStream.DeserializeFromJson <ReplayResult <TUserData> >(jsonResultsFile)); }