Esempio n. 1
0
        public async Task <string> SerializeErrorReport(IEnumerable <ErrorCodeData> errors)
        {
            var options = TextJsonSerializerSettings.CreateJsonSerializerOptions(new HttpDataSerializerOptions(), true);

            using (var stream = new MemoryStream())
            {
                await System.Text.Json.JsonSerializer.SerializeAsync(stream, errors, options);

                stream.Position = 0;

                using (var reader = new StreamReader(stream))
                {
                    var result = await reader.ReadToEndAsync();

                    return(result);
                }
            }
        }
Esempio n. 2
0
        public async Task <string> SerializeRequest(BaseRequest request, Type requestType, HttpDataSerializerOptions options = null)
        {
            var opts = TextJsonSerializerSettings.CreateJsonSerializerOptions(options, _debugMode);

            using (var stream = new MemoryStream())
            {
                await JsonSerializer.SerializeAsync(stream, request, requestType, opts);

                stream.Position = 0;

                using (var reader = new StreamReader(stream))
                {
                    var result = await reader.ReadToEndAsync();

                    return(result);
                }
            }
        }
Esempio n. 3
0
        public async Task <ServiceResponse> DeserializeResponse(Type type, byte[] message, HttpDataSerializerOptions options = null)
        {
            var opts = TextJsonSerializerSettings.CreateJsonSerializerOptions(options, _debugMode);

            try
            {
                using (var ms = new MemoryStream(message))
                {
                    var result = await JsonSerializer.DeserializeAsync(ms, type, opts);

                    return((ServiceResponse)result);
                }
            }
            catch (JsonException ex)
            {
                _logger.LogError("Cannot deserialize request", ex);
                throw;
            }
        }
Esempio n. 4
0
        public async Task <T> DeserializeResponse <T>(byte[] message, HttpDataSerializerOptions options = null) where T : new()
        {
            var opts = TextJsonSerializerSettings.CreateJsonSerializerOptions(options, _debugMode);

            try
            {
                using (var ms = new MemoryStream(message))
                {
                    var result = await JsonSerializer.DeserializeAsync <T>(ms, opts);

                    return(result);
                }
            }
            catch (JsonException ex)
            {
                _logger.LogError("Cannot deserialize response", ex);
                throw new CodeWorksSerializationException("Cannot deserialize JSON payload", ex);
            }
        }
Esempio n. 5
0
        public async Task <BaseRequest> DeserializeRequest(
            Type type,
            byte[] message,
            HttpDataSerializerOptions options = null)
        {
            var opts = TextJsonSerializerSettings.CreateJsonSerializerOptions(options, _debugMode);

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

            try
            {
                using (var ms = new MemoryStream(message))
                {
                    var result = await JsonSerializer.DeserializeAsync(ms, type, opts);

                    return((BaseRequest)result);
                }
            }
            catch (JsonException ex)
            {
                var contents = Encoding.UTF8.GetString(message);

                if (ex.StackTrace.Contains("JsonConverterEnum"))
                {
                    // INVALID ENUM FOUND - TRY AND FIGURE IT OUT
                    var property = ex.Path.Substring(2, ex.Path.Length - 2);
                    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);
            }
        }