예제 #1
0
        public String CreateJsonFormatString(List <JsonList> list)
        {
            if (list.Count == 0)
            {
                StringValue = string.Empty; return("{}");
            }
            string result = string.Empty;

            foreach (var item in list)
            {
                result +=
                    JsonBlockExtractor.Splitters[0];
                if (!string.IsNullOrEmpty(item.StringKey))
                {
                    result += item.StringKey + _splitters[0];
                }
                if (item.Count > 0)
                {
                    result += CreateJsonFormatString(item);
                }
                else
                {
                    result += item.StringValue;
                }
            }
            if (result == string.Empty)
            {
                return(string.Empty);
            }
            return("{" + JsonBlockExtractor.SplitterPlaceHolderCommaReplacer(result.Substring(1)) + "}");
        }
예제 #2
0
파일: Adaptor.cs 프로젝트: NasimiAsl/Prop
        public IDictionary BuildDictionaryFromCom(CommunicationBase com, Type keyType, Type valType)
        {
            var DicInstance = (IDictionary)typeof(Dictionary <,>)
                              .MakeGenericType(keyType, valType)
                              .GetConstructor(Type.EmptyTypes)
                              .Invoke(null);

            foreach (var item in com)
            {
                var parts = JsonBlockExtractor.FirstRestSplitter(
                    item.Value.ToString(), new string[] { ":" });

                var key = CreateInstance(keyType);
                var val = CreateInstance(valType);

                key = BuildFromCom(new CommunicationBase()
                {
                    Value = parts[0]
                }, key);
                val = BuildFromCom(new CommunicationBase()
                {
                    Value = parts[1]
                }, val);
                DicInstance.Add(key, val);
            }

            return(DicInstance);
        }
예제 #3
0
        /// <summary>
        /// Creates a list of blocks based on our definition on what a block is
        /// </summary>
        public void CreateList(string stringValue)
        {
            // keeps

            List <String> blocksList = JsonBlockExtractor.GetBlocks(stringValue);

            this.Clear();

            if (BlockListIsNotNullOrEmpty(blocksList))
            {
                for (int i = 0; i < blocksList.Count; i++)
                {
                    String[] firstRestParts = JsonBlockExtractor.FirstRestSplitter(blocksList[i], _splitters);

                    firstRestParts[1] = JsonBlockExtractor.SplitterPlaceHolderCommaReplacer(firstRestParts[1]);

                    if (FirstPartIsNotEmpty(firstRestParts))
                    {
                        this.Add(new JsonList()
                        {
                            StringKey = firstRestParts[0], StringValue = firstRestParts[1]
                        });
                    }
                    else
                    {
                        this.Add(new JsonList()
                        {
                            StringValue = firstRestParts[1]
                        });
                    }
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Creates a dictionary of string/object key value pairs from JsonFormatString
        /// </summary>
        public void CreateDictionary()
        {
            List <String> blocksList = JsonBlockExtractor.GetBlocks(StringValue);

            if (BlockListIsNotNullOrEmpty(blocksList))
            {
                ConvertionDictionary = new Dictionary <string, object>();
                int index = 0;
                for (int i = 0; i < blocksList.Count; i++)
                {
                    String[] firstRestParts = JsonBlockExtractor.FirstRestSplitter(blocksList[i], Splitters);
                    firstRestParts[1] = JsonBlockExtractor.SplitterPlaceHolderCommaReplacer(firstRestParts[1]);
                    if (TheRestPartIsOfTypeString(firstRestParts))
                    {
                        JsonDictionaryConverter sp = new JsonDictionaryConverter(firstRestParts[1]);
                        if (sp.ConvertionDictionary == null)
                        {
                            if (FirstPartIsNotEmpty(firstRestParts))
                            {
                                ConvertionDictionary.Add(firstRestParts[0], JsonBlockExtractor.ConvertObjectValueToItsNativeType(firstRestParts[1])); // Why Do We Convert One More Time?
                            }
                            else
                            {
                                ConvertionDictionary.Add("#Key" + index++, JsonBlockExtractor.ConvertObjectValueToItsNativeType(firstRestParts[1]));// Why Do We Convert One More Time?
                            }
                        }
                        else
                        {
                            if (FirstPartIsNotEmpty(firstRestParts))
                            {
                                ConvertionDictionary.Add(firstRestParts[0], sp);
                            }
                            else
                            {
                                ConvertionDictionary.Add("#Key" + index++, sp);
                            }
                        }
                    }
                    else
                    {
                        if (FirstPartIsNotEmpty(firstRestParts))
                        {
                            ConvertionDictionary.Add(firstRestParts[0], JsonBlockExtractor.ConvertObjectValueToItsNativeType(firstRestParts[1]));
                        }
                        else
                        {
                            ConvertionDictionary.Add("#Key" + index++, JsonBlockExtractor.ConvertObjectValueToItsNativeType(firstRestParts[1]));
                        }
                    }
                }
            }
            else
            {
                ConvertionDictionary = null;
            }
        }
예제 #5
0
        public String CreateJsonFormatString(Dictionary <string, object> settingsDictionary)
        {
            if (settingsDictionary == null)
            {
                StringValue = string.Empty; return("{}");
            }
            string result = string.Empty;

            foreach (string key in settingsDictionary.Keys)
            {
                try
                {
                    Dictionary <string, object> tempDic = (Dictionary <string, object>)settingsDictionary[key];
                    result +=
                        JsonBlockExtractor.Splitters[0] +
                        JsonDictionaryConverter.GetKey(key, Splitters[0]) +
                        CreateJsonFormatString(tempDic);
                }
                catch
                {
                    try
                    {
                        object[] subData = (object[])settingsDictionary[key];
                        string   _result = "";
                        for (int i = 0; i < subData.Length; i++)
                        {
                            _result += JsonBlockExtractor.Splitters[0] + subData[i].ToString();
                        }
                        result +=
                            JsonBlockExtractor.Splitters[0] +
                            JsonDictionaryConverter.GetKey(key, Splitters[0]) +
                            "{" + JsonBlockExtractor.SplitterPlaceHolderReplacer(_result.Substring(1)) + "}";
                    }
                    catch
                    {
                        result +=
                            JsonBlockExtractor.Splitters[0] +
                            JsonDictionaryConverter.GetKey(key, Splitters[0]) +
                            JsonBlockExtractor.SplitterPlaceHolderReplacer(settingsDictionary[key].ToString());
                    }
                }
            }
            if (result == string.Empty)
            {
                return(string.Empty);
            }
            return("{" + JsonBlockExtractor.SplitterPlaceHolderCommaReplacer(result.Substring(1)) + "}");
        }
예제 #6
0
파일: Converter.cs 프로젝트: NasimiAsl/Prop
        /// <summary>
        /// populates properties of an object given as parameter
        /// </summary>
        /// <param name="ix"></param>
        /// <param name="respectiveObject"></param>
        /// <returns></returns>
        public static object PopulateRespectiveObjectValue(CommunicationBase ix, object respectiveObject)
        {
            if (respectiveObject == null)
            {
                return(null);
            }
            var respectiveObjectType = respectiveObject.GetType();

            foreach (var property in GetCommunicationFlaggedProperties(respectiveObjectType, ConversionMode.Build))
            {
                var buildOptions = property.GetCustomAttributes(typeof(BuildValueFromAttribute), true)
                                   .Select(x => (BuildValueFromAttribute)x).FirstOrDefault();

                if (buildOptions != null && buildOptions.Pattern != property.Name)
                {
                    if (buildOptions.Properties.Count == 1)
                    {
                        var firstOrDefault = ix.FirstOrDefault(x => x.Name == buildOptions.Pattern);

                        if (firstOrDefault != null)
                        {
                            property.SetValue(respectiveObject, firstOrDefault.Value, null);
                        }
                    }
                    else
                    {
                        var value = "";
                        foreach (var bProperties in buildOptions.Properties)
                        {
                            var firstOrDefault = ix.FirstOrDefault(x => x.Name == buildOptions.Pattern);
                            if (firstOrDefault != null)
                            {
                                value += CommunicationBase.PatternJoint + firstOrDefault.Value.ToString();
                            }
                        }

                        if (value != "")
                        {
                            property.SetValue(respectiveObject, value.Substring(CommunicationBase.PatternJoint.Length), null);
                        }
                    }
                }
                else
                {
                    var subIx = ix.FirstOrDefault(x => x.Name == property.Name);

                    if (subIx != null && property.CanWrite)
                    {
                        if (subIx.Value != null && IsOfExclusiveTypes(property.PropertyType))
                        {
                            try
                            {
                                property.SetValue(respectiveObject, subIx.Value.ToCurrentType(), null);
                            }
                            catch
                            {
                                if (property.PropertyType == typeof(bool))
                                {
                                    property.SetValue(respectiveObject, (subIx.Value.ToString().ToLower() != "true" ? false : true), null);
                                }
                                else if (property.PropertyType == typeof(int))
                                {
                                    property.SetValue(respectiveObject, (subIx.Value.ToString().ToLower() != "" ? subIx.Value.To <int>(0) : 0), null);
                                }
                                else
                                {
                                    property.SetValue(respectiveObject, subIx.Value.ToString(), null);
                                }
                            }
                        }
                        else if (subIx.Value != null && property.PropertyType == typeof(System.Object) && !subIx.Value.ToString().StartsWith("{"))
                        {
                            property.SetValue(respectiveObject, subIx.Value.ToCurrentType(), null);
                        }
                        else if (property.PropertyType.IsGenericType && property.PropertyType.Name.Contains("List"))
                        {
                            var gtype = property.PropertyType.GetGenericArguments()[0];

                            var listInstance = (IList)typeof(List <>)
                                               .MakeGenericType(gtype)
                                               .GetConstructor(Type.EmptyTypes)
                                               .Invoke(null);

                            foreach (var item in (CommunicationBase)subIx)
                            {
                                try
                                {
                                    if (gtype.IsEnum)
                                    {
                                        var it = Enum.Parse(gtype, item.Value.ToString());

                                        listInstance.Add(it);
                                    }
                                    else if (gtype.IsClass && !gtype.FullName.StartsWith("System"))
                                    {
                                        var it = item.Value.ToString().JsonTo(gtype.Assembly.CreateInstance(gtype.FullName));

                                        listInstance.Add(it);
                                    }
                                    else
                                    {
                                        listInstance.Add(item.Value);
                                    }
                                }
                                catch
                                {
                                }
                            }

                            property.SetValue(respectiveObject, listInstance, null);
                        }
                        else if (property.PropertyType.IsGenericType && property.PropertyType.Name.Contains("Dictionary"))
                        {
                            var keyType = property.PropertyType.GetGenericArguments()[0];
                            var valType = property.PropertyType.GetGenericArguments()[1];



                            var DicInstance = (IDictionary)typeof(Dictionary <,>)
                                              .MakeGenericType(keyType, valType)
                                              .GetConstructor(Type.EmptyTypes)
                                              .Invoke(null);

                            foreach (var item in (CommunicationBase)subIx)
                            {
                                try
                                {
                                    var parts = JsonBlockExtractor.FirstRestSplitter(
                                        item.Value.ToString(), new string[] { ":" });

                                    dynamic key = "";
                                    dynamic val = "";


                                    if (keyType.IsEnum)
                                    {
                                        key = Enum.Parse(keyType, parts[0]);
                                    }
                                    else if (keyType.IsClass && !keyType.FullName.StartsWith("System"))
                                    {
                                        key = parts[0].JsonTo(
                                            keyType.Assembly.CreateInstance(keyType.FullName));
                                    }
                                    else
                                    {
                                        key = parts[0];
                                    }

                                    if (valType.IsEnum)
                                    {
                                        val = Enum.Parse(valType, parts[1]);
                                    }
                                    else if (valType.IsClass && !valType.FullName.StartsWith("System"))
                                    {
                                        val = parts[1].JsonTo(
                                            valType.Assembly.CreateInstance(valType.FullName));
                                    }
                                    else
                                    {
                                        val = parts[1];
                                    }



                                    DicInstance.Add(key, val);
                                }
                                catch
                                {
                                }
                            }

                            property.SetValue(respectiveObject, DicInstance, null);
                        }
                        else if (property.PropertyType.IsArray)
                        {
                        }
                        else if (property.PropertyType.IsEnum)
                        {
                            if (((CommunicationBase)subIx).PropertyType == CommunicationBase.PropertyTypes.Enum)
                            {
                                try
                                {
                                    var val = Enum.Parse(property.PropertyType, ((CommunicationBase)subIx)[0].Value.ToString()
                                                         .Replace("'", "")
                                                         .Replace("\"", ""));

                                    property.SetValue(respectiveObject, val, null);
                                }
                                catch
                                {
                                    var val = Enum.Parse(property.PropertyType, ((CommunicationBase)subIx).Value.ToString()
                                                         .Replace("'", "")
                                                         .Replace("\"", ""));

                                    property.SetValue(respectiveObject, val, null);
                                }
                            }
                            else
                            {
                                var val = Enum.Parse(property.PropertyType, subIx.Value.ToString()
                                                     .Replace("'", "")
                                                     .Replace("\"", ""));

                                property.SetValue(respectiveObject, val, null);
                            }
                        }
                        else
                        {
                            if (((CommunicationBase)subIx).PropertyType == CommunicationBase.PropertyTypes.Class)
                            {
                                property.SetValue(
                                    respectiveObject,
                                    property.PropertyType.Assembly.CreateInstance(property.PropertyType.FullName)
                                    , null);
                            }

                            PopulateRespectiveObjectValue(
                                (CommunicationBase)ix.Where(x => x.Name == property.Name)
                                .FirstOrDefault(), property.GetValue(respectiveObject, null));
                        }
                    }
                }
            }

            return(respectiveObject);
        }
예제 #7
0
 private bool TheRestPartIsOfTypeString(string[] elements)
 {
     return(JsonBlockExtractor.ConvertObjectValueToItsNativeType(elements[1]).GetType() == typeof(string));
 }