public override object ReadJson(
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer)
        {
            var instance = new DeserializedInfo();
            var props    = objectType.GetTypeInfo().DeclaredProperties.ToList();

            JObject obj = JObject.Load(reader);

            foreach (JProperty jsonProperty in obj.Properties())
            {
                if (!_propertyMappings.TryGetValue(jsonProperty.Name, out var name))
                {
                    name = jsonProperty.Name;
                }

                if (name == "publication")
                {
                    instance.Publications =
                        JsonConvert.DeserializeObject <List <Publication> >(
                            jsonProperty.Value.ToString(), _publicationSerializerSettings);
                    continue;
                }
                else if (name == "topic")
                {
                    instance.CategoryRepoAssociations =
                        JsonConvert.DeserializeObject <List <CategoryRepoAssociation> >(
                            jsonProperty.Value.ToString(), _categorySerializerSettings);
                    continue;
                }

                PropertyInfo prop = props.FirstOrDefault(
                    pi => pi.CanWrite && pi.Name == name);

                prop?.SetValue(
                    instance,
                    jsonProperty.Value.ToObject(prop.PropertyType, serializer));
            }

            instance.ToolRepoAssociation =
                JsonConvert.DeserializeObject <ToolRepoAssociation>(
                    obj.ToString(), _toolRepoAssoSerializerSettings);

            instance.ToolRepoAssociation.Tool =
                JsonConvert.DeserializeObject <Tool>(
                    obj.ToString(), _toolSerializerSettings);

            return(instance);
        }
Esempio n. 2
0
 public static bool TryDeserialize(
     string json,
     JsonSerializerSettings toolSettings,
     JsonSerializerSettings assoSettings,
     JsonSerializerSettings publicationSettings,
     JsonSerializerSettings categorySettings,
     out DeserializedInfo deserializedInfo)
 {
     deserializedInfo = JsonConvert.DeserializeObject <DeserializedInfo>(
         json, GetJsonSerializerSettings(toolSettings,
                                         assoSettings,
                                         publicationSettings,
                                         categorySettings: categorySettings));
     return(true);
 }
Esempio n. 3
0
        public static bool TryDeserialize(YamlStream yamlStream, out DeserializedInfo deserializedInfo)
        {
            if (yamlStream == null)
            {
                deserializedInfo = null;
                return(false);
            }

            var mapping      = (YamlMappingNode)yamlStream.Documents[0].RootNode;
            var tool         = new Tool();
            var publications = new List <Publication>();

            foreach (var entry in mapping.Children)
            {
                switch (entry.Key.ToString())
                {
                case "package":
                    foreach (var child in ((YamlMappingNode)entry.Value).Children)
                    {
                        if (child.Key.ToString() == "name")
                        {
                            tool.Name = child.Value.ToString();
                            break;
                        }
                    }
                    break;

                case "source":
                    if (entry.Value.GetType() == typeof(YamlMappingNode))
                    {
                        foreach (var child in ((YamlMappingNode)entry.Value).Children)
                        {
                            if (child.Key.ToString() == "url")
                            {
                                tool.CodeRepo = child.Value.ToString();
                                break;
                            }
                        }
                    }
                    else if (entry.Value.GetType() == typeof(YamlSequenceNode))
                    {
                        foreach (YamlMappingNode child in ((YamlSequenceNode)entry.Value).Children)
                        {
                            foreach (var c in child.Children)
                            {
                                if (c.Key.ToString() == "url")
                                {
                                    tool.CodeRepo = c.Value.ToString();
                                    break;
                                }
                            }
                        }
                    }
                    break;

                case "extra":
                    foreach (var child in ((YamlMappingNode)entry.Value).Children)
                    {
                        if (child.Key.ToString() == "identifiers")
                        {
                            foreach (var x in ((YamlSequenceNode)child.Value).Children)
                            {
                                if (x.ToString().StartsWith("doi:", StringComparison.OrdinalIgnoreCase))
                                {
                                    publications.Add(new Publication()
                                    {
                                        DOI = x.ToString().Split(':')[1]
                                    });
                                    break;
                                }
                            }
                        }
                    }
                    break;
                }
            }

            deserializedInfo = new DeserializedInfo
            {
                ToolRepoAssociation = new ToolRepoAssociation()
                {
                    Tool = tool
                }
            };
            deserializedInfo.PopulateToolPublicationAssociations(publications);
            return(true);
        }