Beispiel #1
0
        /// <summary>
        /// Resolves a parameter value based on the provided object.
        /// </summary>
        /// <param name="descriptor">Parameter descriptor.</param>
        /// <param name="value">Value to resolve the parameter value from.</param>
        /// <returns>The parameter value.</returns>
        public virtual object ResolveParameter(ParameterDescriptor descriptor, IJsonValue value)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            //if(descriptor.ParameterType == typeof(HubRequestMessageBase))
            //{
            //    NodeRegistration reg = new NodeRegistration();
            //    return value.ConvertTo(reg.GetType());
            //}

            //return value;

            if (value.GetType() == descriptor.ParameterType)
            {
                return value;
            }

            return value.ConvertTo(descriptor.ParameterType);
        }
        private static void AddItemToTable(SqliteConnection db, string tableName, IJsonValue jValue)
        {
            string commandString   = "INSERT OR IGNORE INTO " + tableName + " VALUES (";
            int    parameterNumber = 0;
            string parameterName   = "";

            Type typeInput = jValue.GetType();

            if (typeInput == typeof(JsonObject))
            {
                JsonObject jObj = (JsonObject)jValue;

                using (SqliteCommand command = new SqliteCommand())
                {
                    foreach (var element in jObj)
                    {
                        switch (element.Value.ValueType)
                        {
                        case JsonValueType.Null:
                            break;

                        case JsonValueType.Boolean:
                            parameterNumber++;
                            parameterName = "@param" + parameterNumber;
                            commandString = commandString + parameterName + ", ";
                            command.Parameters.AddWithValue(parameterName, element.Value.GetBoolean());
                            break;

                        case JsonValueType.Number:
                            parameterNumber++;
                            parameterName = "@param" + parameterNumber;
                            commandString = commandString + parameterName + ", ";
                            command.Parameters.AddWithValue(parameterName, element.Value.GetNumber());
                            break;

                        case JsonValueType.String:
                            parameterNumber++;
                            parameterName = "@param" + parameterNumber;
                            commandString = commandString + parameterName + ", ";
                            command.Parameters.AddWithValue(parameterName, element.Value.GetString());
                            break;

                        case JsonValueType.Array:
                            break;

                        case JsonValueType.Object:
                            break;

                        default:
                            break;
                        }
                    }
                    commandString       = commandString.Remove(commandString.Length - 2);
                    commandString       = commandString + ");";
                    command.CommandText = commandString;
                    command.Connection  = db;
                    command.ExecuteReader();
                }
            }
        }
Beispiel #3
0
            public override object ResolveParameter(ParameterDescriptor descriptor, IJsonValue value)
            {
                if (value.GetType() == descriptor.ParameterType)
                {
                    return(value);
                }

                if (_valueField == null)
                {
                    _valueField = value.GetType().GetField("_value", BindingFlags.Instance | BindingFlags.NonPublic);
                }

                var json = (string)_valueField.GetValue(value);

                using (var reader = new StringReader(json))
                    return(_serializer.Deserialize(reader, descriptor.ParameterType));
            }
        /// <summary>
        /// Resolves a parameter value based on the provided object.
        /// </summary>
        /// <param name="descriptor">Parameter descriptor.</param>
        /// <param name="value">Value to resolve the parameter value from.</param>
        /// <returns>The parameter value.</returns>
        public virtual object ResolveParameter(ParameterDescriptor descriptor, IJsonValue value)
        {
            if (value.GetType() == descriptor.Type)
            {
                return value;
            }

            return value.ConvertTo(descriptor.Type);
        }
        /// <summary>
        /// Resolves a parameter value based on the provided object.
        /// </summary>
        /// <param name="descriptor">Parameter descriptor.</param>
        /// <param name="value">Value to resolve the parameter value from.</param>
        /// <returns>The parameter value.</returns>
        public virtual object ResolveParameter(ParameterDescriptor descriptor, IJsonValue value)
        {
            if (value.GetType() == descriptor.Type)
            {
                return(value);
            }

            return(value.ConvertTo(descriptor.Type));
        }
Beispiel #6
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.None)
            {
                reader.Read();
            }

            IJsonValue value = CreateJsonValue(reader);

            if (!objectType.IsAssignableFrom(value.GetType()))
            {
                throw JsonSerializationException.Create(reader, "Could not convert '{0}' to '{1}'.".FormatWith(CultureInfo.InvariantCulture, value.GetType(), objectType));
            }

            return(value);
        }
        /// <summary>
        /// Resolves a parameter value based on the provided object.
        /// </summary>
        /// <param name="descriptor">Parameter descriptor.</param>
        /// <param name="value">Value to resolve the parameter value from.</param>
        /// <returns>The parameter value.</returns>
        public virtual object ResolveParameter(ParameterDescriptor descriptor, IJsonValue value)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.GetType() == descriptor.ParameterType)
            {
                return(value);
            }

            return(value.ConvertTo(descriptor.ParameterType));
        }
        /// <summary>
        /// Resolves a parameter value based on the provided object.
        /// </summary>
        /// <param name="descriptor">Parameter descriptor.</param>
        /// <param name="value">Value to resolve the parameter value from.</param>
        /// <returns>The parameter value.</returns>
        public virtual object ResolveParameter(ParameterDescriptor descriptor, IJsonValue value)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.GetType() == descriptor.ParameterType)
            {
                return value;
            }

            return value.ConvertTo(descriptor.ParameterType);
        }
Beispiel #9
0
        public static object ToObject(this IJsonValue value, Type targetType)
        {
            if (value.GetType() == targetType)
            {
                return(value);
            }

            if (value.ValueType == JsonValueType.Null)
            {
                return(null);
            }

            if (targetType == typeof(JsonObject))
            {
                return(JsonObject.Parse(value.Stringify()));
            }

            if (typeof(IJsonValue).IsAssignableFrom(targetType))
            {
                return(value);
            }

            if (targetType == typeof(string))
            {
                return(value.GetString());
            }

            if (targetType == typeof(int) || targetType == typeof(int?))
            {
                return((int)value.GetNumber());
            }

            if (targetType == typeof(long) || targetType == typeof(long?))
            {
                return((long)value.GetNumber());
            }

            if (targetType == typeof(bool) || targetType == typeof(bool?))
            {
                return(value.GetBoolean());
            }

            if (targetType == typeof(float) || targetType == typeof(float?))
            {
                return((float)value.GetNumber());
            }

            if (targetType == typeof(double) || targetType == typeof(double?))
            {
                return(value.GetNumber());
            }

            if (targetType == typeof(decimal) || targetType == typeof(decimal?))
            {
                return((decimal)value.GetNumber());
            }

            if (targetType == typeof(DateTime) || targetType == typeof(DateTime?))
            {
                return(DateTime.Parse(value.GetString()));
            }

            if (targetType == typeof(TimeSpan) || targetType == typeof(TimeSpan?))
            {
                return(TimeSpan.Parse(value.GetString()));
            }

            throw new NotSupportedException($"Type {targetType} is not supported.");
        }
Beispiel #10
0
        /// <summary>
        /// Writes the specified Json value as string representation to the stream.
        /// </summary>
        /// <param name="value">The Json value to write.</param>
        public void Write(IJsonValue value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            switch (value.Type)
            {
            case JsonValueType.Null:
            {
                if (value is JsonValue cast)
                {
                    WriteNull(cast);
                }
                else
                {
                    throw new JsonUnexpectedValueTypeException(typeof(JsonValue), value.Type, value.GetType());
                }
                break;
            }

            case JsonValueType.Boolean:
            {
                if (value is JsonValue cast)
                {
                    WriteBoolean(cast);
                }
                else
                {
                    throw new JsonUnexpectedValueTypeException(typeof(JsonValue), value.Type, value.GetType());
                }
                break;
            }

            case JsonValueType.Number:
            {
                if (value is JsonValue cast)
                {
                    WriteNumber(cast);
                }
                else
                {
                    throw new JsonUnexpectedValueTypeException(typeof(JsonValue), value.Type, value.GetType());
                }
                break;
            }

            case JsonValueType.String:
            {
                if (value is JsonValue cast)
                {
                    WriteString(cast);
                }
                else
                {
                    throw new JsonUnexpectedValueTypeException(typeof(JsonValue), value.Type, value.GetType());
                }
                break;
            }

            case JsonValueType.Object:
            {
                if (value is JsonObject cast)
                {
                    WriteObject(cast);
                }
                else
                {
                    throw new JsonUnexpectedValueTypeException(typeof(JsonObject), value.Type, value.GetType());
                }
                break;
            }

            case JsonValueType.Array:
            {
                if (value is JsonArray cast)
                {
                    WriteArray(cast);
                }
                else
                {
                    throw new JsonUnexpectedValueTypeException(typeof(JsonArray), value.Type, value.GetType());
                }
                break;
            }

            default: throw new ArgumentOutOfRangeException(nameof(value), $"Unknown JsonValueType: '{value.Type}'.");
            }
        }
            public override object ResolveParameter(ParameterDescriptor descriptor, IJsonValue value) {
                if (value.GetType() == descriptor.ParameterType)
                    return value;

                if (_valueField == null)
                    _valueField = value.GetType().GetField("_value", BindingFlags.Instance | BindingFlags.NonPublic);

                var json = (string)_valueField.GetValue(value);
                using (var reader = new StringReader(json))
                    return _serializer.Deserialize(reader, descriptor.ParameterType);
            }