Esempio n. 1
0
        private void AddJsonProperties(JsonWriter writer, PropertyInfo[] properties, bool inArray)
        {
            writer.WriteObject();
            foreach (PropertyInfo property in properties)
            {
                if (IsValidProperty(property))
                {
                    string jsonPropertyType = GetPropertyTypeName(property.PropertyType, TDataExchangeFormat.Json);

                    if (!inArray)
                    {
                        writer.WriteMember(property.Name);
                        writer.WriteObject();
                    }

                    writer.WriteMember("type");
                    writer.WriteValue(jsonPropertyType);

                    if (jsonPropertyType.Equals("object") || jsonPropertyType.Equals("array"))
                    {
                        bool inArray2 = false;
                        if (jsonPropertyType.Equals("object"))
                        {
                            writer.WriteMember("properties");
                            inArray2 = false;
                        }
                        else
                        {
                            writer.WriteMember("items");
                            inArray2 = true;
                        }

                        AddJsonProperties(writer, property.PropertyType.GetProperties(), inArray2);

                    }
                    if (!inArray)
                        writer.WriteEndObject();
                }
            }
            writer.WriteEndObject();
        }
Esempio n. 2
0
        private string GenerateJsonSchema()
        {
            List<string> required = new List<string>();
            MemoryStream stream = new MemoryStream();
            JsonWriter writer = new JsonWriter(stream);
            writer.WriteObject();

            writer.WriteMember("$schema");
            writer.WriteValue("http://json-schema.org/draft-04/schema");

            writer.WriteMember("title");
            writer.WriteValue(Object.Name);

            writer.WriteMember("type");
            writer.WriteValue("object");

            writer.WriteMember("properties");

            AddJsonProperties(writer, Object.GetProperties(), false);

            if (required.Count > 0)
            {
                writer.WriteMember("required");
                writer.WriteArray();
                foreach (string requirement in required)
                {
                    writer.WriteValue(requirement);
                }
                writer.WriteEndArray();
            }

            writer.WriteEndObject();

            writer.Flush();
            stream.Position = 0;

            string unformattedJsonBody = new StreamReader(stream).ReadToEnd();
            object parsedJson = JsonConvert.DeserializeObject(unformattedJsonBody);
            return JsonConvert.SerializeObject(parsedJson, Newtonsoft.Json.Formatting.Indented);
        }