コード例 #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 (TypeUtil.TryGetTypeForMediaType(mediaType, out documentType))
                {
                    return((Document)jsonObject.ToObject(documentType, JsonSerializer));
                }
                var dictionary = jsonObject.ToObject <Dictionary <string, object> >(JsonSerializer);
                return(new JsonDocument(dictionary, mediaType));
            }

            if (TypeUtil.TryGetTypeForMediaType(mediaType, out documentType))
            {
                var parseFunc = TypeUtilEx.GetParseFuncForType(documentType);
                return(parseFunc(value) as Document);
            }
            return(new PlainDocument(value, mediaType));
        }
コード例 #2
0
        public Document Deserialize(string documentString, MediaType mediaType)
        {
            if (documentString == null)
            {
                throw new ArgumentNullException(nameof(documentString));
            }

            if (mediaType == null)
            {
                throw new ArgumentNullException(nameof(mediaType));
            }

            Document document = null;
            Type     type;

            if (TypeUtil.TryGetTypeForMediaType(mediaType, out type))
            {
                if (mediaType.IsJson)
                {
                    document = JsonConvert.DeserializeObject(documentString, type) as Document;
                }
                else
                {
                    var parseFunc = TypeUtilEx.GetParseFuncForType(type);
                    document = parseFunc(documentString) as Document;
                }
            }
            else
            {
                if (mediaType.IsJson)
                {
                    var json = JObject.Parse(documentString);
                    document = new JsonDocument(json.ToObject <Dictionary <string, object> >(), mediaType);
                }
                else
                {
                    document = new PlainDocument(documentString, mediaType);
                }
            }

            return(document);
        }