Exemple #1
0
        /// <summary>
        /// Deserializes an <see cref="OpenIddictMessage"/> instance.
        /// </summary>
        /// <param name="reader">The JSON reader.</param>
        /// <param name="type">The type of the deserialized instance.</param>
        /// <param name="value">The existing <see cref="OpenIddictMessage"/>, if applicable.</param>
        /// <param name="serializer">The JSON serializer.</param>
        /// <returns>The deserialized <see cref="OpenIddictMessage"/> instance.</returns>
        public override object ReadJson(
            [NotNull] JsonReader reader, [NotNull] Type type,
            [CanBeNull] object value, [CanBeNull] JsonSerializer serializer)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

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

            // Note: OpenIddict primitives are always represented as JSON objects.
            var payload = JToken.Load(reader) as JObject;

            if (payload == null)
            {
                throw new JsonSerializationException("An error occurred while reading the JSON payload.");
            }

            // If no existing value was specified, instantiate a
            // new request/response depending on the requested type.
            var message = value as OpenIddictMessage;

            if (message == null)
            {
                if (type == typeof(OpenIddictMessage))
                {
                    message = new OpenIddictMessage();
                }

                else if (type == typeof(OpenIddictRequest))
                {
                    message = new OpenIddictRequest();
                }

                else if (type == typeof(OpenIddictResponse))
                {
                    message = new OpenIddictResponse();
                }
            }

            if (message != null)
            {
                foreach (var parameter in payload.Properties())
                {
                    message.AddParameter(parameter.Name, parameter.Value);
                }

                return(message);
            }

            throw new ArgumentException("The specified type is not supported.", nameof(type));
        }
Exemple #2
0
        /// <summary>
        /// Deserializes an <see cref="OpenIddictMessage"/> instance.
        /// </summary>
        /// <param name="reader">The JSON reader.</param>
        /// <param name="type">The type of the deserialized instance.</param>
        /// <param name="options">The JSON serializer options.</param>
        /// <returns>The deserialized <see cref="OpenIddictMessage"/> instance.</returns>
        public override OpenIddictMessage Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // Note: OpenIddict primitives are always represented as JSON objects.
            var payload = JsonSerializer.Deserialize <JsonElement>(ref reader, options);

            if (payload.ValueKind != JsonValueKind.Object)
            {
                throw new JsonException("An error occurred while reading the JSON payload.");
            }

            OpenIddictMessage message;

            if (type == typeof(OpenIddictMessage))
            {
                message = new OpenIddictMessage();
            }

            else if (type == typeof(OpenIddictRequest))
            {
                message = new OpenIddictRequest();
            }

            else if (type == typeof(OpenIddictResponse))
            {
                message = new OpenIddictResponse();
            }

            else
            {
                throw new ArgumentException("The specified type is not supported.", nameof(type));
            }

            foreach (var parameter in payload.EnumerateObject())
            {
                message.AddParameter(parameter.Name, parameter.Value);
            }

            return(message);
        }