Ejemplo n.º 1
0
        public Document Deserialize(string value, MediaType mediaType)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (mediaType == null)
            {
                throw new ArgumentNullException(nameof(mediaType));
            }

            Type documentType;

            if (mediaType.IsJson)
            {
                var jsonObject = JObject.Parse(value);
                if (_documentTypeResolver.TryGetTypeForMediaType(mediaType, out documentType))
                {
                    return((Document)jsonObject.ToObject(documentType, _jsonSerializer));
                }
                var dictionary = jsonObject.ToObject <Dictionary <string, object> >(_jsonSerializer);
                return(new JsonDocument(dictionary, mediaType));
            }

            if (_documentTypeResolver.TryGetTypeForMediaType(mediaType, out documentType))
            {
                var parseFunc = TypeUtilEx.GetParseFuncForType(documentType);
                return(parseFunc(value) as Document);
            }
            return(new PlainDocument(value, mediaType));
        }
Ejemplo n.º 2
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);
        }