public void ComplexArrayProperty() { //// Arrange var data = @"{ array: [ { foo: ""bar"", bar: ""foo"" }, { foo: ""bar"", puk: ""fii"" } ] }"; //// Act var schema = JsonSchema4.FromData(data); var json = schema.ToJson(); var property = schema.Properties["array"].ActualPropertySchema; //// Assert Assert.AreEqual(JsonObjectType.Array, property.Type); Assert.AreEqual(3, property.Item.ActualSchema.Properties.Count); }
/// <summary> /// Generates the JSON Details based on the XML,this is the direction that the migration is going /// As In Another Castle is focused on XML config files .Net 4.5 and lower. /// </summary> /// <param name="xmlDetails"></param> /// <returns></returns> public JsonDetails GetJsonDetails(XmlDetails xmlDetails) { try { string json = JsonConvert.SerializeXmlNode(xmlDetails.Document, Newtonsoft.Json.Formatting.Indented, false); var schema = JsonSchema4.FromData(json); //TODO, there might be away to map between the XSD and the JSON properties... and then determine which fields are required. //This is one thing I could not seem to achieve and might be more complicated than its worth. It might be easier just to //Add the attributes visually based on your situation. return(new JsonDetails() { Schema = schema.ToJson(), RawData = json }); } catch (Exception exc) { throw new MigrationException("There was an error while migrating the JSON details", exc); } }
/// <summary> /// Read json file and return a JObject /// </summary> /// <param name="proc_guid"></param> /// <returns></returns> internal JObject readJsonFile() { try { file_name = (file_name.ToLower().EndsWith(".json")) ? file_name : String.Format("{0}.json", file_name); bool file_exist = File.Exists(file_name); canWrite = !((file_must_exists) && (!file_exist)); if (file_exist) { FileInfo fi = new FileInfo(file_name); StreamReader str = new StreamReader(fi.FullName); JsonTextReader jsr = new JsonTextReader(str); JObject JProc = null; bool errors = false; try { JsonSerializer jser = new JsonSerializer(); JProc = jser.Deserialize <JObject>(jsr); if (JProc == null) { errors = true; } } catch (Exception exc) { jErrors.Add(exc.Message); errors = true; } finally { str.Close(); } if (errors) { String filecontent = File.ReadAllText(fi.FullName); String schema = ""; if (filecontent.Contains("$schema")) { schema = filecontent.Substring(filecontent.ToLower().IndexOf("schemas")); schema = schema.Substring(0, schema.IndexOf("\"")).Replace("\\\\", "\\"); } if ((schema != "") && (File.Exists(Path.Combine(AppDataPath, schema)))) { try { String schema_file = File.ReadAllText(Path.Combine(AppDataPath, schema)); JsonSchema4 JSchema = JsonSchema4.FromData(schema_file); foreach (ValidationError ve in JSchema.Validate(filecontent)) { jErrors.Add(String.Format("Error in {0} > {1}: {2}", ve.Path, ve.Property, ve.Kind)); } errors = jErrors.Count() > 0; } catch (Exception exc) { jErrors.Add(String.Format("Error in json file '{0}': {1}", file_name, exc.Message)); canWrite = false; } } } return((!errors) ? JProc : new JObject()); } } catch (Exception exc) { if (file_must_exists) { jErrors.Add(String.Format("Error in '{0}': {1}", file_name, exc.Message)); canWrite = false; } } if (canWrite) { return(new JObject()); } else { jErrors.Add(String.Format("Program file not found: {0}", file_name)); return(null); } }