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>
        /// Initializes a new instance of the <see cref="StaticContent"/> class.
        /// </summary>
        /// <param name="bytes">The byte array.</param>
        public StaticContent(byte[] bytes)
        {
            bytes = bytes ?? throw new ArgumentNullException(nameof(bytes));
            var  compressed = LaraTools.Compress(bytes);
            long required   = (int)Math.Floor(bytes.LongLength * RequiredCompressionFactor);

            if (compressed.Length > required)
            {
                _bytes     = bytes;
                Compressed = false;
            }
            else
            {
                _bytes     = compressed;
                Compressed = true;
            }
            ETag = ComputeETag(bytes);
        }
Exemple #4
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);
        }
Exemple #5
0
 // ReSharper disable once InconsistentNaming
 public string ToJSON() => LaraTools.Serialize(this);
Exemple #6
0
 // ReSharper disable once InconsistentNaming
 public static string GetFirstURL(IWebHost host)
 {
     host = host ?? throw new ArgumentNullException(nameof(host));
     return(LaraTools.GetFirstUrl(host));
 }
Exemple #7
0
 /// <summary>
 /// Launches the user's default web browser on the first address of the host passed in parameters.
 /// </summary>
 /// <param name="host">The host.</param>
 public static void LaunchBrowser(IWebHost host)
 => LaraTools.LaunchBrowser(host);
Exemple #8
0
 /// <summary>
 /// Launches the user's default web browser on the specified address.
 /// </summary>
 /// <param name="address">The address.</param>
 public static void LaunchBrowser(string address)
 => LaraTools.LaunchBrowser(address);
Exemple #9
0
 /// <summary>
 /// Serializes a class decorated with DataContract to a JSON string
 /// </summary>
 /// <param name="instance">Instance to serialize</param>
 /// <param name="type">Type of class</param>
 /// <returns>JSON string</returns>
 public string Stringify(object instance, Type type) => LaraTools.Serialize(instance, type);
Exemple #10
0
 /// <summary>
 /// Serializes a class decorated with DataContract to a JSON string
 /// </summary>
 /// <typeparam name="T">Type of class</typeparam>
 /// <param name="instance">Instance to serialize</param>
 /// <returns>JSON string</returns>
 public string Stringify <T>(T instance) => LaraTools.Serialize(instance);
Exemple #11
0
 // ReSharper disable once InconsistentNaming
 public string ToJSON()
 {
     return(LaraTools.Serialize(this));
 }