コード例 #1
0
        /// <summary>
        ///		Deserialise the response message's content into the specified CLR data type using the most appropriate formatter.
        /// </summary>
        /// <typeparam name="TBody">
        ///		The CLR data type into which the body will be deserialised.
        /// </typeparam>
        /// <param name="responseMessage">
        ///		The response message.
        /// </param>
        /// <param name="formatters">
        ///		The collection of content formatters from which to select an appropriate formatter.
        /// </param>
        /// <returns>
        ///		The deserialised message body.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        ///		An appropriate formatter could not be found in the request's list of formatters.
        /// </exception>
        public static async Task <TBody> ReadContentAsAsync <TBody>(this HttpResponseMessage responseMessage, IFormatterCollection formatters)
        {
            if (responseMessage == null)
            {
                throw new ArgumentNullException(nameof(responseMessage));
            }

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

            responseMessage.EnsureHasBody();

            InputFormatterContext readContext   = responseMessage.Content.CreateInputFormatterContext <TBody>();
            IInputFormatter       readFormatter = formatters.FindInputFormatter(readContext);

            if (readFormatter == null)
            {
                throw new InvalidOperationException($"None of the supplied formatters can read data of type '{readContext.DataType.FullName}' from media type '{readContext.MediaType}'.");
            }

            return(await responseMessage.ReadContentAsAsync <TBody>(readFormatter, readContext).ConfigureAwait(false));
        }
コード例 #2
0
        /// <inheritdoc />
        protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0)
        {
            ScriptValue value = (ScriptValue)token;

            if (value.Value is string stringvalue)
            {
                resulttext.Append('"');
                foreach (char character in stringvalue)
                {
                    switch (character)
                    {
                    case '"':
                        resulttext.Append("\\\"");
                        break;

                    case '\t':
                        resulttext.Append("\\t");
                        break;

                    case '\r':
                        resulttext.Append("\\r");
                        break;

                    case '\n':
                        resulttext.Append("\\n");
                        break;

                    case '\\':
                        resulttext.Append("\\\\");
                        break;

                    default:
                        resulttext.Append(character);
                        break;
                    }
                }
                resulttext.Append('"');
            }
            else if (value.Value is bool boolean)
            {
                resulttext.Append(boolean ? "true" : "false");
            }
            else if (value.Value is decimal decimalvalue)
            {
                resulttext.Append(decimalvalue.ToString(CultureInfo.InvariantCulture)).Append('d');
            }
            else if (value.Value is double doublevalue)
            {
                resulttext.Append(doublevalue.ToString(CultureInfo.InvariantCulture));
            }
            else if (value.Value is float floatvalue)
            {
                resulttext.Append(floatvalue.ToString(CultureInfo.InvariantCulture)).Append('f');
            }
            else if (value.Value is byte bytevalue)
            {
                resulttext.Append(bytevalue.ToString(CultureInfo.InvariantCulture)).Append('b');
            }
            else if (value.Value is sbyte sbytevalue)
            {
                resulttext.Append(sbytevalue.ToString(CultureInfo.InvariantCulture)).Append("sb");
            }
            else if (value.Value is short shortvalue)
            {
                resulttext.Append(shortvalue.ToString(CultureInfo.InvariantCulture)).Append('s');
            }
            else if (value.Value is ushort ushortvalue)
            {
                resulttext.Append(ushortvalue.ToString(CultureInfo.InvariantCulture)).Append("us");
            }
            else if (value.Value is int intvalue)
            {
                resulttext.Append(intvalue.ToString(CultureInfo.InvariantCulture));
            }
            else if (value.Value is uint uintvalue)
            {
                resulttext.Append(uintvalue.ToString(CultureInfo.InvariantCulture)).Append("u");
            }
            else if (value.Value is long longvalue)
            {
                resulttext.Append(longvalue.ToString(CultureInfo.InvariantCulture)).Append('l');
            }
            else if (value.Value is ulong ulongvalue)
            {
                resulttext.Append(ulongvalue.ToString(CultureInfo.InvariantCulture)).Append("ul");
            }
            else if (value.Value is null)
            {
                resulttext.Append("null");
            }
        }
コード例 #3
0
 /// <summary>
 /// formats a token
 /// </summary>
 /// <param name="token">token to format</param>
 /// <param name="resulttext">text to append formatted token to</param>
 /// <param name="formatters">formatter collection to retrieve token formatters from</param>
 /// <param name="depth">current indentation depth</param>
 protected abstract void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0);
コード例 #4
0
 /// <inheritdoc />
 public void FormatToken(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0, bool mustindent = false)
 {
 }
コード例 #5
0
        /// <inheritdoc />
        protected override void Format(IScriptToken token, StringBuilder resulttext, IFormatterCollection formatters, int depth = 0)
        {
            ScriptVariable value = (ScriptVariable)token;

            resulttext.Append($"${value.Name}");
        }