/// <summary> /// Generates a CKAN.META object from a string. /// Also validates that all required fields are present. /// Throws a BadMetaDataKraken if any fields are missing. /// </summary> public static CkanModule FromJson(string json) { if (!validate_json_against_schema(json)) { throw new BadMetadataKraken(null, "Validation against spec failed"); } CkanModule newModule = null; try { newModule = JsonConvert.DeserializeObject <CkanModule>(json); } catch (JsonException ex) { throw new BadMetadataKraken(null, "JSON deserialization error", ex); } // NOTE: Many of these tests may be better inour Deserialisation handler. if (!newModule.IsSpecSupported()) { throw new UnsupportedKraken( String.Format( "{0} requires CKAN {1}, we can't read it.", newModule, newModule.spec_version ) ); } // Check everything in the spec if defined. // TODO: This *can* and *should* be done with JSON attributes! foreach (string field in required_fields) { object value = newModule.GetType().GetField(field).GetValue(newModule); if (value == null) { string error = String.Format("{0} missing required field {1}", newModule.identifier, field); log.Error(error); throw new BadMetadataKraken(null, error); } } // All good! Return module return(newModule); }
/// <summary> Generates a CKAN.META object from a string. /// Also validates that all required fields are present. /// </summary> public static CkanModule from_string(string json) { CkanModule newModule = JsonConvert.DeserializeObject <CkanModule> (json); // Check everything in the spec if defined. // TODO: It would be great if this could be done with attributes. foreach (string field in required_fields) { object value = newModule.GetType().GetField(field).GetValue(newModule); if (value == null) { Console.WriteLine("Missing required field: {0}", field); throw new MissingFieldException(); // Is there a better exception choice? } } // All good! Return module return(newModule); }