private string ParseObject(string key, Newtonsoft.JsonV4.Schema.JsonSchema schema, IDictionary <string, ApiObject> objects, IDictionary <string, ApiEnum> enums, Newtonsoft.JsonV4.Schema.JsonSchema parentSchema, string baseClass = null) { var propertiesSchemas = schema.Properties; var obj = new ApiObject { Name = NetNamingMapper.GetObjectName(key), Properties = ParseSchema(propertiesSchemas, objects, enums, parentSchema), BaseClass = baseClass }; AdditionalProperties(obj.Properties, schema); if (!obj.Properties.Any()) { return(null); } // Avoid duplicated keys and names if (objects.ContainsKey(key) || objects.Any(o => o.Value.Name == obj.Name) || otherObjects.ContainsKey(key) || otherObjects.Any(o => o.Value.Name == obj.Name) || schemaObjects.ContainsKey(key) || schemaObjects.Any(o => o.Value.Name == obj.Name)) { if (UniquenessHelper.HasSameProperties(obj, objects, key, otherObjects, schemaObjects)) { return(key); } obj.Name = UniquenessHelper.GetUniqueName(objects, obj.Name, otherObjects, schemaObjects); key = UniquenessHelper.GetUniqueKey(objects, key, otherObjects); } objects.Add(key, obj); return(key); }
private void ParseComplexTypes(IDictionary <string, ApiObject> objects, Newtonsoft.JsonV4.Schema.JsonSchema schema, Property prop, KeyValuePair <string, Newtonsoft.JsonV4.Schema.JsonSchema> property) { if (schema.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.Object && schema.Properties != null) { ParseObject(property.Key, schema.Properties, objects); prop.Type = NetNamingMapper.GetObjectName(property.Key); } if (schema.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.Array) { ParseArray(objects, schema, prop, property); } }
private static Newtonsoft.JsonV4.Schema.JsonSchema ParseV4Schema(string key, string jsonSchema, IDictionary <string, string> warnings, IDictionary <string, ApiObject> objects) { Newtonsoft.JsonV4.Schema.JsonSchema v4Schema = null; try { v4Schema = Newtonsoft.JsonV4.Schema.JsonSchema.Parse(jsonSchema, new JsonSchemaCustomV4Resolver(objects)); } catch (Exception exv4) { if (!warnings.ContainsKey(key)) { warnings.Add(key, "Could not parse JSON Schema. " + exv4.Message.Replace("\r\n", string.Empty).Replace("\n", string.Empty)); } } return(v4Schema); }
public ApiObject Parse(string key, string jsonSchema, IDictionary <string, ApiObject> objects, IDictionary <string, string> warnings) { var obj = new ApiObject { Name = NetNamingMapper.GetObjectName(key), Properties = new List <Property>() }; JsonSchema schema = null; Newtonsoft.JsonV4.Schema.JsonSchema v4Schema = null; try { schema = JsonSchema.Parse(jsonSchema); } catch (Exception exv3) // NewtonJson does not support Json Schema v4 { try { schema = null; v4Schema = Newtonsoft.JsonV4.Schema.JsonSchema.Parse(jsonSchema); } catch (Exception exv4) { //dynamic dynObj = JsonConvert.DeserializeObject(jsonSchema); //foreach (var kv in dynObj) //{ // //TODO: manual parse schema ? or parse using example ? //} if (!warnings.ContainsKey(key)) { warnings.Add(key, "Could not parse JSON Schema. v3 parser message: " + exv3.Message.Replace("\r\n", string.Empty).Replace("\n", string.Empty) + ". v4 parser message: " + exv4.Message.Replace("\r\n", string.Empty).Replace("\n", string.Empty)); } } } if (schema == null && v4Schema == null) { return(obj); } if (schema != null) { if (schema.Type == JsonSchemaType.Array) { obj.IsArray = true; if (schema.Items != null && schema.Items.Any()) { ParseProperties(objects, obj.Properties, schema.Items.First().Properties); } } else { ParseProperties(objects, obj.Properties, schema.Properties); } } else { if (v4Schema.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.Array) { obj.IsArray = true; if (v4Schema.Items != null && v4Schema.Items.Any()) { ParseProperties(objects, obj.Properties, v4Schema.Items.First().Properties); } } else { ParseProperties(objects, obj.Properties, v4Schema.Properties); } } return(obj); }