Exemple #1
0
        internal static Result FromContent(string content,
                                           string contentType)
        {
            if (content == null)
            {
                throw new ArgumentException("content cannot be null");
            }

            if (JS_CONTENT_TYPE.Equals(contentType) ||
                JSON_CONTENT_TYPE.Equals(contentType) ||
                // some sites return JSON with the plain text content type
                PLAIN_TEXT_CONTENT_TYPE.Equals(contentType))
            {
                try
                {
                    return(content.StartsWith("[") ?
                           new Result(new JSONArray(content)) :
                           new Result(new JSONObject(content)));
                }
                catch (Exception ex)
                {
                    throw new JSONException(ex.Message);
                }
            }

            if (TEXT_XML_CONTENT_TYPE.Equals(contentType) ||
                APPLICATION_XML_CONTENT_TYPE.Equals(contentType) ||
                // default to XML if content type is not specified
                contentType == null)
            {
                try
                {
                    return(new Result(JSONObject.FromXMLString(content)));
                }
                catch (Exception ex)
                {
                    throw new JSONException(ex.Message);
                }
            }
            throw new JSONException("Unsupported content-type: " + contentType);
        }