Exemple #1
0
 /// <summary>
 /// Parses a JSON string into a class. The class needs to be decorated with the DataContract attribute.
 /// </summary>
 /// <typeparam name="T">Class type</typeparam>
 /// <param name="json">Source JSON string</param>
 /// <param name="result">Class instance created</param>
 /// <returns>true when successful, false otherwise</returns>
 // ReSharper disable once MemberCanBeMadeStatic.Global
 public bool TryParse <T>(string json, [NotNullWhen(true)] out T?result) where T : class
 {
     try
     {
         result = LaraTools.Deserialize <T>(json);
         return(result != null);
     }
     catch (SerializationException)
     {
         result = default;
         return(false);
     }
 }
Exemple #2
0
        public async Task ReadAjaxMessage(HttpContext http)
        {
            if (!http.Request.HasFormContentType)
            {
                return;
            }
            var form = await http.Request.ReadFormAsync();  // TODO: cancellation token for shutdown

            if (form.TryGetValue(GlobalConstants.MessageKey, out var values))
            {
                Message = LaraTools.Deserialize <ClientEventMessage>(values);
            }
            Files = form.Files;
        }
Exemple #3
0
        /// <summary>
        /// Parses a JSON string. If parsing fails, throws a StatusCodeException that returns a Bad Request (400).
        /// The class needs to be decorated with the DataContract attribute.
        /// </summary>
        /// <typeparam name="T">Class type</typeparam>
        /// <param name="json">JSON source text</param>
        /// <returns>Instance of deserialized class</returns>
        public T Parse <T>(string json) where T : class
        {
            T?result;

            try
            {
                result = LaraTools.Deserialize <T>(json);
            }
            catch (Exception e)
            {
                var outer = new StatusCodeException(Resources.BadRequest, e)
                {
                    StatusCode = HttpStatusCode.BadRequest
                };
                throw outer;
            }
            if (result == null)
            {
                throw new StatusCodeException(HttpStatusCode.BadRequest, Resources.BadRequest);
            }
            return(result);
        }