public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { try { JObject obj = JObject.Load(reader); Object root = null; // TODO: Use xmlString to solve issue of empty vs null elements if (xmlString != null) { string formatXML = ""; //-- Formatting XML for deserialization XDocument maDoc = XDocument.Parse(xmlString, LoadOptions.None); var xmlWriter = new StringWriter(); maDoc.Save(xmlWriter); formatXML = xmlWriter.ToString(); string formattedJson = NIEMUtil.xmlToJson(formatXML); reader = new JsonTextReader(new StringReader(formattedJson)); } else { //-- Converting JSON back to XML string so it can be formatted for deserialization string rootJson = obj.SelectToken("maid:MutualAidDetail").ToString(); XDocument xmlDoc = JsonConvert.DeserializeXNode(rootJson, "maid--MutualAidDetail"); StringWriter xmlWriter = new StringWriter(); xmlDoc.Save(xmlWriter); string xml = xmlWriter.ToString(); string formattedJson = NIEMUtil.xmlToJson(xml); reader = new JsonTextReader(new StringReader(formattedJson)); } //-- Now starting deserialization obj = JObject.Load(reader); root = null; JToken aidReq = obj.SelectToken("maid--MutualAidDetail.maid--AidRequested"); JToken aidRes = obj.SelectToken("maid--MutualAidDetail.maid--AidResponding"); if (aidReq != null) { JsonSerializerSettings setting = new JsonSerializerSettings(); setting.Converters.Add(new deserialRequestedResourceConvert()); setting.DateFormatHandling = DateFormatHandling.IsoDateFormat; string json = aidReq.ToString(); AidRequested ar = JsonConvert.DeserializeObject <AidRequested>(json, setting); root = Activator.CreateInstance(typeof(MutualAidDetail), ar); } else if (aidRes != null) { JsonSerializerSettings setting = new JsonSerializerSettings(); setting.Converters.Add(new deserialRespondingResourceConvert()); setting.DateFormatHandling = DateFormatHandling.IsoDateFormat; string json = aidRes.ToString(); AidResponding ar = JsonConvert.DeserializeObject <AidResponding>(json, setting); root = Activator.CreateInstance(typeof(MutualAidDetail), ar); } else { // no resources, invalid throw new JsonSerializationException("Message not defined. Must be AidResponding or AidRequesting Message"); } return(root); } catch (Exception e) { return(null); } }
static void Main(string[] args) { try { //-- Creating Event with default values (check if these vary...) Event newEvent = new Event(); //set the basics newEvent.EventID = "1839-a784-199d-fe33"; newEvent.EventMessageDateTime = System.DateTime.UtcNow; newEvent.EventTypeDescriptor.CodeValue = EventTypeCodeList.Tasking; newEvent.EventValidityDateTimeRange.StartDate = System.DateTime.UtcNow; newEvent.EventValidityDateTimeRange.EndDate = System.DateTime.UtcNow.AddHours(1.0); //set the location EventLocation location = new EventLocation(); location.LocationCylinder.CodeValue = LocationCreationCodeList.MACHINE; location.LocationCylinder.LocationPoint.Point.id = "ID1"; location.LocationCylinder.LocationPoint.Point.Lat = 30.1; location.LocationCylinder.LocationPoint.Point.Lon = 30.1; location.LocationCylinder.LocationCylinderHalfHeightValue = (decimal)1.4; location.LocationCylinder.LocationCylinderRadiusValue = (decimal)1.0; newEvent.EventLocation = location; //set a comment /* EventComment comment = new EventComment(); * comment.CommentText = ""; * comment.DateTime = System.DateTime.UtcNow; * comment.OrganizationIdentification = "ARDENTMC General"; * comment.PersonHumanResourceIdentification = "Dr. Brian Wilkins"; * newEvent.EventComment = new List<EventComment>(); * newEvent.EventComment.Add(comment);*/ //-- Creating MutualAidDetail MutualAidDetail md = new MutualAidDetail(); //-- Aid Request areq1 AidRequested areq1 = new AidRequested(); GenericResource grA = new GenericResource(); grA.Quantity = 1; grA.ResourceKind.ResourceTypeCodeValue = EventTypeCodeList.ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM_EMS_AMBULANCE; areq1.Resource = new RequestedResources(grA); GenericResource grB = new GenericResource(); grB.Quantity = 1; grB.ResourceKind.ResourceTypeCodeValue = EventTypeCodeList.ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM_FIRE; areq1.Resource.ResourceList.Add(grB); areq1.location = new LocationExtension(new Address(new LocationStreet())); areq1.location.Address.LocationStreet.StreetName = "2332"; areq1.location.Address.LocationStreet.StreetNumberText = "Riverside"; areq1.location.Address.LocationStreet.StreetCategoryText = "Pkwy"; areq1.location.Address.LocationState = USStateCodeList.CA; areq1.location.Address.LocationCityName = "Sacramento"; areq1.location.Address.LocationPostalCode = "95605"; //-- Aid Request areq2 AidRequested areq2 = new AidRequested(); SpecificResource srA = new SpecificResource(); srA.ResourceIdentifier = "3-508-1028"; areq2.Resource = new RequestedResources(srA); //-- Aid Request areq3 AidRequested areq3 = new AidRequested(); MissionNeed mn1 = new MissionNeed(); mn1.Quantity = 1; mn1.ValueText = "ATOM.GRDTRK.EQT.GRDVEH.CVLVEH.EM.PUBLICWORKS.TRUCK.WATER"; // will need to change here... areq3.Resource = new RequestedResources(mn1); //-- Aid Request ares1 AidResponding ares1 = new AidResponding(); ares1.Approved = true; Equipment eq1 = new Equipment(); eq1.ID = "3-508-1023"; eq1.EstimatedArrival = DateTime.UtcNow.AddHours(1); eq1.ResourceKind.ResourceTypeCodeValue = EventTypeCodeList.ATOM_GRDTRK_EQT_GRDVEH_CVLVEH_EM_FIRE_TRUCK; eq1.EstimatedAvailability.StartDate = DateTime.UtcNow; eq1.EstimatedAvailability.EndDate = DateTime.UtcNow.AddMinutes(30); ares1.Resources = new RespondingResource(eq1); ares1.ContactInformation.Entity = "ArdentMC Fire Co."; ares1.ContactInformation.Responder = "John Smith"; //-- Aid Request ares2 AidResponding ares2 = new AidResponding(); ares2.Approved = true; Person p1 = new Person(); p1.ID = "ID2"; p1.EstimatedArrival = DateTime.UtcNow.AddMinutes(20); p1.EstimatedAvailability.StartDate = DateTime.UtcNow; p1.EstimatedAvailability.EndDate = DateTime.UtcNow.AddMinutes(50); ares2.Resources = new RespondingResource(p1); Person p2 = new Person(); p2.ID = "ID3"; p2.EstimatedArrival = DateTime.UtcNow.AddMinutes(20); p2.EstimatedAvailability.StartDate = DateTime.UtcNow; p2.EstimatedAvailability.EndDate = DateTime.UtcNow.AddMinutes(50); p2.Rank = new Rank(); p2.Rank.ValueText = "EMT-P"; // may need to change since this since this isn't on any list I know of ares2.Resources.ResourceList.Add(p2); ares2.ContactInformation.Entity = "ArdentMC Medical Facilities"; // Adding Detail to Event string xmlSample = ""; // areq1 md.Message = areq1; newEvent.Details = md; xmlSample = newEvent.ToString(); File.WriteAllText(@"C:\Sample\MutualAidReq1.xml", xmlSample); // areq2 md.Message = areq2; newEvent.Details = md; xmlSample = newEvent.ToString(); File.WriteAllText(@"C:\Sample\MutualAidReq2.xml", xmlSample); // areq3 md.Message = areq3; newEvent.Details = md; xmlSample = newEvent.ToString(); File.WriteAllText(@"C:\Sample\MutualAidReq3.xml", xmlSample); // ares1 md.Message = ares1; newEvent.Details = md; xmlSample = newEvent.ToString(); File.WriteAllText(@"C:\Sample\MutualAidRes1.xml", xmlSample); // ares2 md.Message = ares2; newEvent.Details = md; xmlSample = newEvent.ToString(); File.WriteAllText(@"C:\Sample\MutualAidRes2.xml", xmlSample); /* * //-- Serializing Event Object * * string xml = newEvent.ToString(); * File.WriteAllText(@"C:\event.xml", xml); * * * //-- Deserializing from newly created xml * * string json = NIEMEmlcUtil.xmlToJson(xml); * Event testEv = JsonConvert.DeserializeObject<Event>(json, new NIEMSHARP.NIEMEMLCLib.deSerialEventConverter(xml); * string newXML = testEv.ToString(); * File.WriteAllText(@"C:\newEvent.xml", newXML); * * * // */ //--------- Test with provided string /* * * string xmlString; * xmlString = ""; // where string goes * * Event stringEv = JsonConvert.DeserializeObject<Event>(json, new NIEMSHARP.NIEMEMLCLib.deSerialEventConverter(xmlString); * string stringXML = stringEv.ToString(); * File.WriteAllText(@"C:\stringEvent.xml", stringXML); * * // */ } catch (Exception e) { string s = e.Message + "\n"; } }