예제 #1
0
        private bool FillIfKeyValueObject(object rec, Tuple <long, JObject> pair)
        {
            IDictionary <string, object> dict = ToDynamic(pair.Item2) as IDictionary <string, object>;

            if (dict.Count == 0)
            {
                return(true);
            }

            var isKVPAttrDefined = rec.GetType().GetCustomAttribute <ChoKeyValueTypeAttribute>() != null;

            if (isKVPAttrDefined)
            {
                var kP = rec.GetType().GetProperties().Where(p => p.GetCustomAttribute <ChoKeyAttribute>() != null).FirstOrDefault();
                var vP = rec.GetType().GetProperties().Where(p => p.GetCustomAttribute <ChoValueAttribute>() != null).FirstOrDefault();

                if (kP != null && vP != null)
                {
                    kP.SetValue(rec, dict.First().Key);
                    vP.SetValue(rec, dict.First().Value);
                    return(true);
                }
            }
            if (typeof(IChoKeyValueType).IsAssignableFrom(rec.GetType()))
            {
                IChoKeyValueType kvp = rec as IChoKeyValueType;

                kvp.Key   = dict.First().Key;
                kvp.Value = dict.First().Value;
                return(true);
            }

            return(false);
        }
예제 #2
0
        public override void WriteJson(JsonWriter writer, object value,
                                       JsonSerializer serializer)
        {
            if (value == null)
            {
                return;
            }
            Type objectType = value.GetType();

            var dict = new Dictionary <string, string>();

            if (typeof(IChoKeyValueType).IsAssignableFrom(objectType))
            {
                IChoKeyValueType kvp = value as IChoKeyValueType;
                var propName         = kvp.Key.ToNString();
                var propValue        = kvp.Value.ToNString();
                if (!propName.IsNullOrWhiteSpace())
                {
                    dict.Add(propName, propValue);
                }
            }
            else
            {
                var kP        = ChoTypeDescriptor.GetProperties <ChoKeyAttribute>(objectType).FirstOrDefault();
                var vP        = ChoTypeDescriptor.GetProperties <ChoValueAttribute>(objectType).FirstOrDefault();
                var propName  = ChoType.GetPropertyValue(value, kP.Name).ToNString();
                var propValue = ChoType.GetPropertyValue(value, vP.Name).ToNString();

                if (!propName.IsNullOrWhiteSpace())
                {
                    dict.Add(propName, propValue);
                }
            }

            serializer.Serialize(writer, dict);
        }
예제 #3
0
        public override object ReadJson(JsonReader reader, Type objectType,
                                        object existingValue, JsonSerializer serializer)
        {
            var dict = serializer.Deserialize <Dictionary <string, string> >(reader);
            var item = dict.First();

            var rec = ChoActivator.CreateInstance(objectType);

            if (typeof(IChoKeyValueType).IsAssignableFrom(objectType))
            {
                IChoKeyValueType kvp = rec as IChoKeyValueType;
                kvp.Key   = item.Key;
                kvp.Value = item.Value;
            }
            else
            {
                var kP = ChoTypeDescriptor.GetProperties <ChoKeyAttribute>(objectType).FirstOrDefault();
                var vP = ChoTypeDescriptor.GetProperties <ChoValueAttribute>(objectType).FirstOrDefault();

                ChoType.SetPropertyValue(rec, kP.Name, item.Key);
                ChoType.SetPropertyValue(rec, vP.Name, item.Value);
            }
            return(rec);
        }
예제 #4
0
        private void MapToDictionaryInternal(IDictionary <string, object> dictionary, object source)
        {
            var isKVPAttrDefined = source.GetType().GetCustomAttribute <ChoKeyValueTypeAttribute>() != null;

            //check if object is KeyValuePair
            Type valueType = source.GetType();

            if (valueType.IsGenericType)
            {
                Type baseType = valueType.GetGenericTypeDefinition();
                if (baseType == typeof(KeyValuePair <,>))
                {
                    object kvpKey   = valueType.GetProperty("Key").GetValue(source, null);
                    object kvpValue = valueType.GetProperty("Value").GetValue(source, null);
                    if (kvpValue is IEnumerable)
                    {
                        dictionary[kvpKey.ToNString()] = MapToDictionary(kvpValue as IEnumerable).ToArray();
                    }
                    else if (kvpValue != null)
                    {
                        dictionary[kvpKey.ToNString()] = MapToDictionary(kvpValue);
                    }
                }
            }

            if (isKVPAttrDefined)
            {
                var kP = source.GetType().GetProperties().Where(p => p.GetCustomAttribute <ChoKeyAttribute>() != null).FirstOrDefault();
                var vP = source.GetType().GetProperties().Where(p => p.GetCustomAttribute <ChoValueAttribute>() != null).FirstOrDefault();


                if (kP != null && vP != null)
                {
                    object value = vP.GetValue(source);
                    if (value is IEnumerable)
                    {
                        dictionary[kP.GetValue(source).ToNString()] = MapToDictionary(value as IEnumerable).ToArray();
                    }
                    else if (value != null)
                    {
                        dictionary[kP.GetValue(source).ToNString()] = MapToDictionary(value);
                    }
                    return;
                }
            }
            if (typeof(IChoKeyValueType).IsAssignableFrom(source.GetType()))
            {
                IChoKeyValueType kvp   = source as IChoKeyValueType;
                object           value = kvp.Value;
                if (value.GetType().IsDynamicType())
                {
                    dictionary[kvp.Key.ToNString()] = value;
                }
                else if (value is IEnumerable && !(value is IDictionary))
                {
                    dictionary[kvp.Key.ToNString()] = MapToDictionary(value as IEnumerable).ToArray();
                }
                else if (value != null)
                {
                    dictionary[kvp.Key.ToNString()] = MapToDictionary(value);
                }
                return;
            }
            var properties = ChoType.GetProperties(source.GetType()); // source.GetType().GetProperties();

            foreach (var p in properties)
            {
                var key  = p.Name;
                var attr = p.GetCustomAttribute <JsonPropertyAttribute>();
                if (attr != null && !attr.PropertyName.IsNullOrWhiteSpace())
                {
                    key = attr.PropertyName.NTrim();
                }

                object value = p.GetValue(source, null);
                if (value == null)
                {
                    if (attr != null && attr.NullValueHandling == NullValueHandling.Ignore)
                    {
                    }
                    else
                    {
                        dictionary[key] = null;
                    }

                    continue;
                }
                valueType = value.GetType();

                if (valueType.IsSimple())
                {
                    dictionary[key] = Marshal(value);
                }
                else if (value.GetType().IsDynamicType())
                {
                    dictionary[key] = value;
                }
                else if (value is IDictionary)
                {
                    IDictionary dict = ((IDictionary)value);
                    foreach (var key1 in dict.Keys)
                    {
                        var val = dict[key];
                        dictionary[key1.ToNString()] = Marshal(value);
                    }
                    dictionary[key] = dict;
                }
                else if (value is IEnumerable)
                {
                    dictionary[key] = MapToDictionary((IEnumerable)value).ToArray();
                }
                else
                {
                    dictionary[key] = Marshal(value);
                }
            }
        }