Ejemplo n.º 1
0
        public Task <string> SerializeResponse(ServiceResponse response, Type responseType, HttpDataSerializerOptions options = null)
        {
            var settings = JsonNetSerializerSettings.CreateJsonSerializerSettings(options, _debugMode);
            var res      = JsonConvert.SerializeObject(response, settings);

            return(Task.FromResult(res));
        }
Ejemplo n.º 2
0
        public async Task <ServiceResponse> DeserializeResponse(Type type, byte[] message, HttpDataSerializerOptions options = null)
        {
            await Task.CompletedTask;
            var settings = JsonNetSerializerSettings.CreateJsonSerializerSettings(options, _debugMode);

            if (message.Length == 0)
            {
                return((ServiceResponse)Activator.CreateInstance(type));
            }

            try
            {
                using (var ms = new MemoryStream(message))
                {
                    using (var reader = new StreamReader(ms, Encoding.UTF8))
                    {
                        var result = JsonSerializer.Create(settings).Deserialize(reader, type);
                        return((ServiceResponse)result);
                    }
                }
            }
            catch (JsonException ex)
            {
                var contents = Encoding.UTF8.GetString(message);

                if (string.IsNullOrWhiteSpace(contents))
                {
                    return((ServiceResponse)Activator.CreateInstance(type));
                }
                _logger.LogError(ex, $"Cannot deserialize request payload {contents}");
                throw new CodeWorksSerializationException($"Cannot parse payload as JSON. Payload={contents}", contents);
            }
        }
Ejemplo n.º 3
0
        public Task <string> SerializeErrorReport(IEnumerable <ErrorCodeData> errors)
        {
            var settings = JsonNetSerializerSettings.CreateJsonSerializerSettings(new HttpDataSerializerOptions(), true);

            var output = JsonConvert.SerializeObject(errors, settings);

            return(Task.FromResult(output));
        }
Ejemplo n.º 4
0
        public async Task <string> SerializeRequest(BaseRequest request, Type requestType, HttpDataSerializerOptions options = null)
        {
            var    settings = JsonNetSerializerSettings.CreateJsonSerializerSettings(options, _debugMode);
            string contents = "";

            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream, Encoding.UTF8))
                {
                    var serializer = JsonSerializer.Create(settings);
                    serializer.Serialize(writer, request, requestType);
                }

                stream.Position = 0;
                using (var reader = new StreamReader(stream, Encoding.UTF8))
                {
                    contents = await reader.ReadToEndAsync();
                }
            }

            return(contents);
        }
Ejemplo n.º 5
0
        public async Task <BaseRequest> DeserializeRequest(Type type, byte[] message, HttpDataSerializerOptions options = null)
        {
            await Task.CompletedTask;
            var settings = JsonNetSerializerSettings.CreateJsonSerializerSettings(options, _debugMode);

            if (message.Length == 0)
            {
                return((BaseRequest)Activator.CreateInstance(type));
            }

            try
            {
                using (var ms = new MemoryStream(message))
                {
                    using (var reader = new StreamReader(ms, Encoding.UTF8))
                    {
                        var result = JsonSerializer.Create(settings).Deserialize(reader, type);
                        return((BaseRequest)result);
                    }
                }
            }
            catch (JsonSerializationException ex)
            {
                var contents = Encoding.UTF8.GetString(message);
                if (ex.StackTrace.Contains("StringEnumConverter"))
                {
                    // INVALID ENUM FOUND - TRY AND FIGURE IT OUT
                    var property = ex.Path;
                    throw new ValidationErrorException("Invalid value provided", property);
                }

                throw;
            }
            catch (JsonException ex)
            {
                var t = ex.GetType();

                var contents = Encoding.UTF8.GetString(message);
                //StringEnumConverter

                if (ex.StackTrace.Contains("StringEnumConverter"))
                {
                    // INVALID ENUM FOUND - TRY AND FIGURE IT OUT
                    var property = "";
                    throw new ValidationErrorException("Invalid enum value provided", property);
                }

                if (string.IsNullOrWhiteSpace(contents))
                {
                    return((BaseRequest)Activator.CreateInstance(type));
                }

                _logger.LogError(ex, $"Cannot deserialize request payload {contents}");
                throw new CodeWorksSerializationException($"Cannot parse payload as JSON. Payload={contents}",
                                                          contents);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Cannot deserialize request payload");
                throw new CodeWorksSerializationException($"Cannot parse payload as JSON");
            }
        }