public void GetFormatterParseFunc_DoubleSpecificCulture_ReturnValidParser()
        {
            // Act
            var parseFunc = TypeUtilEx.GetFormattedParseFuncForType(typeof(double));
            var result    = parseFunc("100.000", CultureInfo.GetCultureInfo("pt-BR"));

            // Assert
            result.ShouldBe(100_000);
        }
        public void GetGenericFormatterParseFunc_DoubleInvariantCulture_ReturnValidParser()
        {
            // Act
            var parseFunc = TypeUtilEx.GetFormattedParseFunc <double>();
            var result    = parseFunc("100.000", CultureInfo.InvariantCulture);

            // Assert
            result.ShouldBe(100);
        }
        public void GetStringValue_EmptyStringToEmptyArray_ReturnEmptyArray()
        {
            //Act
            var actual = TypeUtilEx.TryParseString(string.Empty, typeof(string[]), out var result, CultureInfo.InvariantCulture, StringSplitOptions.RemoveEmptyEntries);

            //Assert
            actual.ShouldBeTrue();
            var output = result.ShouldBeOfType <string[]>();

            output.ShouldBeEmpty();
        }
Esempio n. 4
0
        public static Document ToDocument(
            this JToken jToken,
            MediaType mediaType,
            JsonSerializer serializer,
            IDocumentTypeResolver documentTypeResolver)
        {
            Document document;

            if (documentTypeResolver.TryGetTypeForMediaType(mediaType, out var documentType))
            {
                try
                {
                    if (mediaType.IsJson)
                    {
                        document = (Document)serializer.Deserialize(jToken.CreateReader(), documentType);
                    }
                    else if (jToken != null)
                    {
                        var parseFunc = TypeUtilEx.GetParseFuncForType(documentType);
                        document = (Document)parseFunc(jToken.ToString());
                    }
                    else
                    {
                        document = (Document)Activator.CreateInstance(documentType);
                    }

                    return(document);
                }
                catch (JsonException) { }
                catch (ArgumentException) { }
                catch (TypeLoadException)
                {
                    // Ignore deserialization exceptions and return a Plain/Json document instead
                }
            }

            if (mediaType.IsJson)
            {
                if (jToken is IDictionary <string, JToken> contentJsonObject)
                {
                    var contentDictionary = contentJsonObject.ToDictionary(k => k.Key, v => v.Value.GetTokenValue());
                    document = new JsonDocument(contentDictionary, mediaType);
                }
                else
                {
                    throw new ArgumentException("The property is not a JSON");
                }
            }
            else
            {
                document = new PlainDocument(jToken.ToString(), mediaType);
            }
            return(document);
        }
Esempio n. 5
0
        public static T GetMessageContent <T>(this IRequestContext context) where T : Document
        {
            var content   = context.GetVariable <string>(CONTENT_VARIABLE_NAME);
            var mediaType = context.GetVariable <MediaType>(TYPE_VARIABLE_NAME);

            if (mediaType.IsJson)
            {
                return(JsonConvert.DeserializeObject <T>(content, JsonNetSerializer.Settings));
            }
            else
            {
                object document;
                if (TypeUtilEx.TryParseString(content, typeof(T), out document))
                {
                    return((T)document);
                }
                return(null);
            }
        }
Esempio n. 6
0
        public static Document ToDocument(this JToken jToken, MediaType mediaType, global::Newtonsoft.Json.JsonSerializer serializer)
        {
            Type     documentType;
            Document document;

            if (TypeUtil.TryGetTypeForMediaType(mediaType, out documentType))
            {
                if (mediaType.IsJson)
                {
                    document = (Document)serializer.Deserialize(jToken.CreateReader(), documentType);
                }
                else if (jToken != null)
                {
                    var parseFunc = TypeUtilEx.GetParseFuncForType(documentType);
                    document = (Document)parseFunc(jToken.ToString());
                }
                else
                {
                    document = (Document)Activator.CreateInstance(documentType);
                }
            }
            else
            {
                if (mediaType.IsJson)
                {
                    var contentJsonObject = jToken as IDictionary <string, JToken>;
                    if (contentJsonObject != null)
                    {
                        var contentDictionary = contentJsonObject.ToDictionary(k => k.Key, v => v.Value.GetTokenValue());
                        document = new JsonDocument(contentDictionary, mediaType);
                    }
                    else
                    {
                        throw new ArgumentException("The property is not a JSON");
                    }
                }
                else
                {
                    document = new PlainDocument(jToken.ToString(), mediaType);
                }
            }
            return(document);
        }