Ejemplo n.º 1
0
        private void OnCollectionTypeDeserializing(object sender, OptionDeserializingEventArgs e)
        {
            // Alias for CollectionTypes option, translate to collection of string ...

            var jToken = e.JToken;

            if (jToken.Type == JTokenType.Object)
            {
                // This must be an object with two properties like below: we need separate the collection type and the type assembly.
                // "CollectionTypeReference": {"Item1": "System.Collections.ArrayList", "Item2": "System.Collections.NonGeneric.dll" }
                // "CollectionType": "System.Collections.ArrayList"
                // "Reference": "System.Collections.NonGeneric, {Microsoft.NETCore.App, *}"

                var properties = jToken.Value <JObject>().Properties();
                var item1      = properties.FirstOrDefault(p => p.Name == "Item1")?.Value.Value <string>();
                if (item1 != null)
                {
                    e.Value = new List <object> {
                        item1.Trim()
                    };

                    var item2 = properties.FirstOrDefault(p => p.Name == "Item2")?.Value.Value <string>();
                    if (item2 != null)
                    {
                        _deserializedCollectionAssemblies.Add(System.IO.Path.GetFileNameWithoutExtension(item2.Trim()));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void OnNamespaceOptionDeserializing(object sender, OptionDeserializingEventArgs e)
        {
            // Alias for NamespaceMappings option, translate to list of strings that can be parsed into a key/value pairs ...

            var jToken = e.JToken;

            if (jToken.Type == JTokenType.String)
            {
                e.Value = new List <object> {
                    ParseNamespace(jToken.Value <string>(), sender as OptionBase)
                };
            }
        }
Ejemplo n.º 3
0
        private void OnCheckedReferencedAssemblyDeserializing(object sender, OptionDeserializingEventArgs e)
        {
            // Alias for References option, translate to collection of string ...

            var jToken = e.JToken;

            if (jToken.Type == JTokenType.Array)
            {
                // If this is an array of objects it should be a single-value property object like below, extract the values into a list of strings:
                // "CheckedReferencedAssemblies": [ { "Name": "Microsoft.CSharp" }, { "Name": "Microsoft.Win32.Primitives" } ],

                IEnumerable <JToken> collection = jToken.Value <JArray>();
                if (collection.All(j => j.Type == JTokenType.Object && j.Count() == 1 && ((JProperty)j.First).Name == "Name"))
                {
                    e.Value = collection.Select(o => ((JProperty)o.First).Value.Value <string>()).ToList();
                }
                else
                {
                    // We also need to handle json files written in an older format before Name was the only property. This includes a FullName (which we can ignore), and an IsChecked:
                    // "CheckedReferencedAssemblies": [ { "FullName": "Microsoft.ApplicationInsights", "IsChecked": false, "Name": "Microsoft.ApplicationInsights" } ]
                    List <string> names = new List <string>();
                    foreach (JToken j in collection)
                    {
                        if (j.Type == JTokenType.Object)
                        {
                            string name      = null;
                            bool   isChecked = true;

                            foreach (JProperty prop in j)
                            {
                                if (prop.Name == "Name")
                                {
                                    name = prop.Value.Value <string>();
                                }
                                else if (prop.Name == "IsChecked")
                                {
                                    isChecked = prop.Value.Value <bool>();
                                }
                            }

                            if (isChecked && name != null)
                            {
                                names.Add(name);
                            }
                        }
                    }

                    e.Value = names;
                }
            }
        }
Ejemplo n.º 4
0
        protected virtual object OnDeserializing(JToken jToken)
        {
            if (this.IsInitialized)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Shared.Resources.ErrorOptionAlreadyDeserializedFormat, this.SerializationName));
            }

            object value = null;

            if (Deserializing != null)
            {
                var e = new OptionDeserializingEventArgs(jToken);
                Deserializing(this, e);
                value = e.Value;
            }
            return(value);
        }
Ejemplo n.º 5
0
        private void OnSelectedAccessLevelForGeneratedClassDeserializing(object sender, OptionDeserializingEventArgs e)
        {
            // Alias for InternalTypeAccess option, translate to bool ...

            var jToken = e.JToken;

            if (jToken.Type == JTokenType.String)
            {
                var value = jToken.Value <string>();
                switch (value.ToLowerInvariant())
                {
                case "internal":
                    e.Value = true;
                    break;

                case "public":
                    e.Value = false;
                    break;
                }
            }
        }