Exemplo n.º 1
0
        public List <ReusableType> Iteratereusable(CogsModel model)
        {
            List <ReusableType> res = new List <ReusableType>();

            foreach (var reuseabletype in model.ReusableDataTypes)
            {
                ReusableType define = new ReusableType();
                define.Name = reuseabletype.Name;
                foreach (var prop in reuseabletype.Properties)
                {
                    var temp = new JsonSchemaProp();
                    temp.MultiplicityElement = new Cardinality();
                    temp.Name = prop.Name;
                    if (IsReusableType(prop.DataType.Name))
                    {
                        temp.Reference = "#/definitions/" + prop.DataType.Name;
                    }
                    else if (IsSimpleType(prop.DataType.Name))
                    {
                        temp.Reference = "#/simpleType/" + prop.DataType.Name;
                    }
                    else if (IsItemType(prop.DataType.Name))
                    {
                        temp.Reference = "#/definitions/Reference";
                    }
                    else
                    {
                        if (TypeBelongToInt(prop.DataType.Name))
                        {
                            temp.Type          = "integer";
                            temp.original_type = prop.DataType.Name;
                        }
                        else if (TypeBelongToNum(prop.DataType.Name))
                        {
                            temp.Type          = "number";
                            temp.original_type = prop.DataType.Name;
                        }
                        else
                        {
                            temp.Type = prop.DataType.Name.ToLower();
                        }
                    }
                    temp.MultiplicityElement.MinCardinality = prop.MinCardinality;
                    temp.MultiplicityElement.MaxCardinality = prop.MaxCardinality;
                    temp.Description = prop.Description;
                    define.Properties.Add(temp);
                }
                res.Add(define);
            }
            return(res);
        }
Exemplo n.º 2
0
        public void Publish(CogsModel model)
        {
            //if (CogsLocation == null)
            //{
            //    throw new InvalidOperationException("Cogs location must be specified");
            //}
            if (TargetDirectory == null)
            {
                throw new InvalidOperationException("Target directory must be specified");
            }

            if (Overwrite && Directory.Exists(TargetDirectory))
            {
                Directory.Delete(TargetDirectory, true);
            }

            Directory.CreateDirectory(TargetDirectory);
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            settings.Formatting            = Formatting.Indented;
            settings.Converters.Add(new JsonSchemaConverter());
            settings.ContractResolver     = new CamelCasePropertyNamesContractResolver();
            settings.DefaultValueHandling = DefaultValueHandling.Ignore;

            ReusableStorage = model.ReusableDataTypes;
            ItemTypeStorage = model.ItemTypes;
            //create a list to store jsonschema for each itemtype
            var root = new SchemaList();
            List <JsonSchema> items = new List <JsonSchema>();

            JsonSchema   reference_node = new JsonSchema();
            ReusableType reference_def  = new ReusableType();

            reference_def.Name = "~~reference~~";

            reference_node.Title = "~~reference~~";
            items.Add(reference_node);
            List <ReusableType> define = Iteratereusable(model);

            define.Add(reference_def);

            Iterate(model, items);

            root.Schema      = "http://json-schema.org/draft-04/schema#";
            root.Id          = "#root";
            root.Properties  = items;
            root.SimpleType  = "root";
            root.definitions = define;

            if (!AdditionalProp)
            {
                foreach (var prop in root.Properties)
                {
                    prop.AddProp = false;
                }
                foreach (var prop in root.definitions)
                {
                    prop.AddProp = false;
                }
            }
            else
            {
                foreach (var prop in root.Properties)
                {
                    prop.AddProp = true;
                }
                foreach (var prop in root.definitions)
                {
                    prop.AddProp = true;
                }
            }
            //Console.WriteLine(JsonConvert.SerializeObject(root, settings));
            string res = JsonConvert.SerializeObject(root, settings);

            File.WriteAllText(Path.Combine(TargetDirectory, "jsonSchema" + ".json"), res);
        }