/// <summary> /// Try to parse the given service ticket JSON. /// </summary> /// <param name="JSONObject">A JSON object.</param> /// <param name="ServiceTicketProvider">A delegate resolving service tickets.</param> /// <param name="UserProvider">A delegate resolving users.</param> /// <param name="OrganizationProvider">A delegate resolving organizations.</param> /// <param name="Affected">The parsed list of affected things.</param> /// <param name="ErrorResponse">An error message.</param> public static Boolean TryParseJSON(JObject JSONObject, ServiceTicketProviderDelegate ServiceTicketProvider, UserProviderDelegate UserProvider, OrganizationProviderDelegate OrganizationProvider, out Affected Affected, out String ErrorResponse) { try { Affected = null; #region Parse ServiceTickets [optional] var RelatedServiceTickets = new HashSet <MessageHolder <ServiceTicket_Id, ServiceTicket> >(); if (JSONObject.ParseOptionalHashSet("serviceTicketIds", "related service tickets", ServiceTicket_Id.TryParse, out HashSet <ServiceTicket_Id> RelatedServiceTicketIds, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } if (ServiceTicketProvider != null) { foreach (var relatedServiceTicketId in RelatedServiceTicketIds) { if (ServiceTicketProvider(relatedServiceTicketId, out ServiceTicket relatedServiceTicket)) { RelatedServiceTickets.Add(new MessageHolder <ServiceTicket_Id, ServiceTicket>(relatedServiceTicketId, relatedServiceTicket)); } } } } #endregion #region Parse Users [optional] var RelatedUsers = new HashSet <MessageHolder <User_Id, User> >(); if (JSONObject.ParseOptionalHashSet("userIds", "related users", User_Id.TryParse, out HashSet <User_Id> RelatedUserIds, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } if (UserProvider != null) { foreach (var relatedUserId in RelatedUserIds) { if (UserProvider(relatedUserId, out User relatedUser)) { RelatedUsers.Add(new MessageHolder <User_Id, User>(relatedUserId, relatedUser)); } } } } #endregion #region Parse Organizations [optional] var RelatedOrganizations = new HashSet <MessageHolder <Organization_Id, Organization> >(); if (JSONObject.ParseOptionalHashSet("organizationIds", "related organizations", Organization_Id.TryParse, out HashSet <Organization_Id> RelatedOrganizationIds, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } if (OrganizationProvider != null) { foreach (var relatedOrganizationId in RelatedOrganizationIds) { if (OrganizationProvider(relatedOrganizationId, out Organization relatedOrganization)) { RelatedOrganizations.Add(new MessageHolder <Organization_Id, Organization>(relatedOrganizationId, relatedOrganization)); } } } } #endregion Affected = new Affected(ServiceTicketLinks: RelatedServiceTickets, UserLinks: RelatedUsers, OrganizationLinks: RelatedOrganizations); ErrorResponse = null; return(true); } catch (Exception e) { ErrorResponse = e.Message; Affected = null; return(false); } }
/// <summary> /// Try to parse the given organization group JSON. /// </summary> /// <param name="JSONObject">A JSON object.</param> /// <param name="OrganizationGroupProvider">A delegate resolving organization groups.</param> /// <param name="OrganizationProvider">A delegate resolving organizations.</param> /// <param name="OrganizationGroup">The parsed organization group.</param> /// <param name="ErrorResponse">An error message.</param> /// <param name="OrganizationGroupIdURL">An optional OrganizationGroup identification, e.g. from the HTTP URL.</param> public static Boolean TryParseJSON(JObject JSONObject, OrganizationGroupProviderDelegate OrganizationGroupProvider, OrganizationProviderDelegate OrganizationProvider, out OrganizationGroup OrganizationGroup, out String ErrorResponse, OrganizationGroup_Id?OrganizationGroupIdURL = null) { try { OrganizationGroup = null; if (JSONObject?.HasValues != true) { ErrorResponse = "The given JSON object must not be null or empty!"; return(false); } #region Parse OrganizationGroupId [optional] // Verify that a given OrganizationGroup identification // is at least valid. if (JSONObject.ParseOptionalStruct("@id", "OrganizationGroup identification", OrganizationGroup_Id.TryParse, out OrganizationGroup_Id? OrganizationGroupIdBody, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } if (!OrganizationGroupIdURL.HasValue && !OrganizationGroupIdBody.HasValue) { ErrorResponse = "The OrganizationGroup identification is missing!"; return(false); } if (OrganizationGroupIdURL.HasValue && OrganizationGroupIdBody.HasValue && OrganizationGroupIdURL.Value != OrganizationGroupIdBody.Value) { ErrorResponse = "The optional OrganizationGroup identification given within the JSON body does not match the one given in the URI!"; return(false); } #endregion #region Parse Context [mandatory] if (!JSONObject.ParseMandatory("@context", "JSON-LD context", JSONLDContext.TryParse, out JSONLDContext Context, out ErrorResponse)) { ErrorResponse = @"The JSON-LD ""@context"" information is missing!"; return(false); } if (Context != OrganizationGroup.JSONLDContext) { ErrorResponse = @"The given JSON-LD ""@context"" information '" + Context + "' is not supported!"; return(false); } #endregion #region Parse Name [mandatory] if (!JSONObject.ParseMandatory("name", "name", out I18NString Name, out ErrorResponse)) { return(false); } #endregion #region Parse Description [optional] if (JSONObject.ParseOptional("description", "description", out I18NString Description, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion #region Parse ParentGroup identification [optional] if (JSONObject.ParseOptionalStruct("parentGroupId", "parentgroup identification", OrganizationGroup_Id.TryParse, out OrganizationGroup_Id? ParentGroupId, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } OrganizationGroup ParentGroup = null; if (ParentGroupId.HasValue) { OrganizationGroupProvider(ParentGroupId.Value, out ParentGroup); } #endregion #region Parse Subgroup identifications [optional] if (JSONObject.ParseOptional("SubgroupIds", "subgroup identifications", OrganizationGroup_Id.TryParse, out IEnumerable <OrganizationGroup_Id> SubgroupIds, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } List <OrganizationGroup> Subgroups = null; if (SubgroupIds?.Any() == true) { Subgroups = new List <OrganizationGroup>(); foreach (var organizationGroupId in SubgroupIds) { if (OrganizationGroupProvider(organizationGroupId, out OrganizationGroup organizationGroup)) { Subgroups.Add(organizationGroup); } } } #endregion #region Parse Organization identifications [optional] if (JSONObject.ParseOptional("organizationIds", "organization identifications", Organization_Id.TryParse, out IEnumerable <Organization_Id> OrganizationIds, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } List <Organization> Organizations = null; if (OrganizationIds?.Any() == true) { Organizations = new List <Organization>(); foreach (var organizationId in OrganizationIds) { if (OrganizationProvider(organizationId, out Organization organization)) { Organizations.Add(organization); } } } #endregion #region Get DataSource [optional] var DataSource = JSONObject.GetOptional("dataSource"); #endregion #region Parse CryptoHash [optional] var CryptoHash = JSONObject.GetOptional("cryptoHash"); #endregion OrganizationGroup = new OrganizationGroup(OrganizationGroupIdBody ?? OrganizationGroupIdURL.Value, Name, Description, Organizations, ParentGroup, Subgroups, null, null, Context, DataSource, null); ErrorResponse = null; return(true); } catch (Exception e) { ErrorResponse = e.Message; OrganizationGroup = null; return(false); } }