/// <summary> /// Create a JSON example for this type. /// </summary> /// <param name="type"></param> /// <returns></returns> public static string JSONExample(this Type type) { string result = string.Empty; using (JSONSerialiser js = new JSONSerialiser()) { try { // see if a default can be created if (type.IsGenericType == true) { // handle List<T> output if (type.GenericTypeArguments.Length > 0) { result = js.Serialize(Activator.CreateInstance(type.GenericTypeArguments[0], true)); result = "[" + result + "]"; } } else { result = js.Serialize(Activator.CreateInstance(type, true)); } } catch (Exception ex) { result = ex.Message; } } return(result); }
public static string ToJson(this object me) { using (JSONSerialiser serializer = new JSONSerialiser()) { return(serializer.Serialize(me)); } }
public void TestMethod1() { string peerAName = Guid.NewGuid().ToString(); string peerBName = Guid.NewGuid().ToString(); string peerAData = Guid.NewGuid().ToString(); string peerBData = Guid.NewGuid().ToString(); StringMessage message = new StringMessage() { To = new StringPeerIdentification(peerAName), From = new StringPeerIdentification(peerBName), Version = new IntMessageVersionIdentification(10), Data = peerAData }; ISerializer serializer = new JSONSerialiser(); byte[] messageBytes = serializer.Serialize(message); StringMessage newMessage = (StringMessage)serializer.Deserialize(messageBytes); //BasicUDPNetworking peerA = new BasicUDPNetworking( // StringPeerIdentification.Generate(), // new PeerManager(), // new Serializer(), // new IPEndPoint(IPAddress.Any, 4000) //); }
public void JSONSerialiser_Serialisation_DoesntThrowOnValidData() { IBaseMessage message = ISerializerTestsData.GenerateMessage(); ISerializer jsonSerialiser = new JSONSerialiser(); jsonSerialiser.Serialize(message); }
public void IntTest() { _awaitingMessage = new CountdownEvent(2); const string peerAName = "PeerA"; const int peerAPort = 4000; const string peerBName = "PeerB"; const int peerBPort = 5000; IPeerIdentification peerAIdentification = new StringPeerIdentification(peerAName); IPeerIdentification peerBIdentification = new StringPeerIdentification(peerBName); IPeerManager peerApeerManager = new PeerManager(); peerApeerManager.AddOrUpdatePeer(peerBIdentification, new IPEndPoint(IPAddress.Loopback, peerBPort)); IPeerManager peerBpeerManager = new PeerManager(); peerBpeerManager.AddOrUpdatePeer(peerAIdentification, new IPEndPoint(IPAddress.Loopback, peerAPort)); ISerializer serializer = new JSONSerialiser(); IUDPNetworking peerA = new UDPNetworking( peerAIdentification, peerApeerManager, serializer, new IPEndPoint(IPAddress.Loopback, peerAPort) ); IUDPNetworking peerB = new UDPNetworking( peerBIdentification, peerBpeerManager, serializer, new IPEndPoint(IPAddress.Loopback, peerBPort) ); peerA.OnMessageReceived += OnMessageReceived; peerB.OnMessageReceived += OnMessageReceived; peerA.SendMessageAsync(new StringMessage() { To = peerBIdentification, From = peerAIdentification, Data = "Hello B!" }); peerB.SendMessageAsync(new StringMessage() { To = peerAIdentification, From = peerBIdentification, Data = "Hello A!" }); _awaitingMessage.Wait(); peerA.Dispose(); peerB.Dispose(); }
/// <summary> /// Save an object in the session in Json Format. /// </summary> /// <param name="session"></param> /// <param name="key"></param> /// <param name="value"></param> /// <remarks>Use GetJSon to retrieve the object.></remarks> public static void SetJSon(this ISession session, string key, object value) { using (JSONSerialiser js = new JSONSerialiser()) { string sessionData = js.Serialize(value).ToBase64(); session.SetString(key, sessionData); } }
/// <summary> /// Convert a string JSON to an object. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <returns></returns> public static T JSONConvert <T>(this string value) { T result = default(T); using (JSONSerialiser js = new JSONSerialiser()) { result = js.Deserialize <T>(value); } return(result); }
/// <summary> /// Retrieve a value from the session which has been saved in JSON format. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="key"></param> /// <returns></returns> /// <remarks>Save the value using SetJson.</remarks> public static T GetJSon <T>(this ISession session, string key) { string sessionData = null; T result = default(T); using (JSONSerialiser js = new JSONSerialiser()) { sessionData = session.GetString(key).FromBase64(); result = sessionData == null ? default(T) : js.Deserialize <T>(sessionData); } return(result); }
public void JSONSerialiser_Deserialisation_DoesntThrowOnValidData() { IBaseMessage message = ISerializerTestsData.GenerateMessage(); ISerializer jsonSerialiser = new JSONSerialiser(); byte[] messageBytes = jsonSerialiser.Serialize(message); IBaseMessage messageDeserialized = jsonSerialiser.Deserialize(messageBytes); Assert.AreEqual(message.To, messageDeserialized.To); Assert.AreEqual(message.From, messageDeserialized.From); Assert.AreEqual(message.Type, messageDeserialized.Type); Assert.AreEqual(message.Version, messageDeserialized.Version); }
public override void LoadJsonConfig() { base.LoadJsonConfig(); string configPath = base.ContentRootPath + base.ConfigFileName; // In dev mode, load a different file. if (base.IsDevelopment == true) { configPath = base.ContentRootPath + "appsettings.Development.json"; } using (JSONSerialiser js = new JSONSerialiser()) { this.AppConfiguration = js.Deserialize <AppConfigurationModel>(new Uri(configPath)); } }
/// <summary> /// Create a JSON example for this type. /// </summary> /// <param name="type"></param> /// <returns></returns> public static string JSONExample(this Type type) { string result = string.Empty; using (JSONSerialiser js = new JSONSerialiser()) { try { // see if a default can be created result = js.Serialize(Activator.CreateInstance(type, true)); } catch (Exception ex) { result = ex.Message; } } return(result); }
public ActionResult <CategoryRowApiO> Patch([FromBody] CategoryRowApiO category) { try { if (category == default(CategoryRowApiO)) { // See if a partial has been sent. using (StreamReader sr = new StreamReader(Request.Body)) { string requestBody = sr.ReadToEnd(); if (requestBody != null) { if (requestBody.Length > 0) { using (JSONSerialiser serialiser = new JSONSerialiser()) { // The request may contain a partial so work around this. category = serialiser.Deserialize <CategoryRowApiO>(requestBody); } } } } } if (category != default(CategoryRowApiO)) { if (ModelState.IsValid) { CategoryRowApiO result = this.CategoryService.Update(category); this.CategoryService.Commit(); // Updated Response.StatusCode = 200; // OK return(result); } else { throw new ModelStateException(string.Format("Validation Failed, the {0} contains invalid data.", category.GetType().ToString()), ModelState); } } else { throw new ArgumentException("No json body could be found."); } } catch (ArgumentNullException e) { // expectation failed - field is missing Response.AddBody(e.Message); return(new StatusCodeResult(417)); } catch (ModelStateException e) { // not acceptable Response.AddBody(e.Message); return(new StatusCodeResult(406)); } catch (RecordNotFoundException e) { Response.AddBody(e.Message); return(new StatusCodeResult(204)); // no content } catch (Exception e) { // application error internal server error Response.AddBody(e.Message); return(new StatusCodeResult(500)); } }
public void JSONSerialiser_Serialisation_ThrowsOnNull() { ISerializer jsonSerialiser = new JSONSerialiser(); Assert.ThrowsException <ArgumentNullException>(() => jsonSerialiser.Serialize(null)); }