/// <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); } }
public static Boolean TryParseJSON(JObject JSONObject, UserProviderDelegate UserProvider, out PasswordReset PasswordReset, out String ErrorResponse, Boolean IgnoreContextMismatches = true) { try { PasswordReset = null; if (JSONObject == null) { ErrorResponse = "The given JSON object must not be null!"; return(false); } #region Parse Context [mandatory] if (!IgnoreContextMismatches) { if (!JSONObject.ParseMandatoryText("@context", "JSON-LD context", out String Context, out ErrorResponse)) { ErrorResponse = @"The JSON-LD ""@context"" information is missing!"; return(false); } if (Context != JSONLDContext) { ErrorResponse = @"The given JSON-LD ""@context"" information '" + Context + "' is not supported!"; return(false); } } #endregion #region Parse Timestamp [mandatory] if (!JSONObject.ParseMandatory("timestamp", "timestamp", out DateTime Timestamp, out ErrorResponse)) { return(false); } #endregion #region Parse Users [mandatory] if (!JSONObject.ParseMandatory("userIds", "user identifications", out JArray UserIdArray, out ErrorResponse)) { return(false); } HashSet <User_Id> userIds = null; try { userIds = UserIdArray. Where(jsonvalue => jsonvalue != null). Select(jsonvalue => User_Id.Parse(jsonvalue.Value <String>())). ToHashSet(); } catch { ErrorResponse = "The given array of users '" + UserIdArray + "' is invalid!"; return(false); } if (!userIds.Any()) { ErrorResponse = "The given array of users '" + UserIdArray + "' must not be empty!"; return(false); } HashSet <User> Users = new HashSet <User>(); foreach (var userId in userIds) { if (!UserProvider(userId, out User user)) { ErrorResponse = "The given user '" + userId + "' is unknown or invalid!"; return(false); } Users.Add(user); } #endregion #region Parse SecurityToken1 [mandatory] if (!JSONObject.ParseMandatory("securityToken1", "security token #1", SecurityToken_Id.TryParse, out SecurityToken_Id SecurityToken1, out ErrorResponse)) { return(false); } #endregion #region Parse SecurityToken2 [optional] if (JSONObject.ParseOptionalStruct("securityToken2", "security token #2", SecurityToken_Id.TryParse, out SecurityToken_Id? SecurityToken2, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion #region Parse EventTrackingId [optional] if (JSONObject.ParseOptional("eventTrackingId", "event tracking identification", EventTracking_Id.TryParse, out EventTracking_Id EventTrackingId, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion PasswordReset = new PasswordReset(Timestamp, Users, SecurityToken1, SecurityToken2, EventTrackingId); return(true); } catch (Exception e) { ErrorResponse = e.Message; PasswordReset = null; return(false); } }
public static Boolean TryParseJSON(JObject JSONObject, UserProviderDelegate UserProvider, out FAQ FAQ, out String ErrorResponse, FAQ_Id?FAQIdURL = null) { try { FAQ = null; #region Parse FAQId [optional] // Verify that a given FAQ identification // is at least valid. if (JSONObject.ParseOptionalStruct("@id", "FAQ identification", FAQ_Id.TryParse, out FAQ_Id? FAQIdBody, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } if (!FAQIdURL.HasValue && !FAQIdBody.HasValue) { ErrorResponse = "The FAQ identification is missing!"; return(false); } if (FAQIdURL.HasValue && FAQIdBody.HasValue && FAQIdURL.Value != FAQIdBody.Value) { ErrorResponse = "The optional FAQ 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-LinkedData context information", JSONLDContext.TryParse, out JSONLDContext Context, out ErrorResponse)) { ErrorResponse = @"The JSON-LD ""@context"" information is missing!"; return(false); } if (Context != DefaultJSONLDContext) { ErrorResponse = @"The given JSON-LD ""@context"" information '" + Context + "' is not supported!"; return(false); } #endregion #region Parse Question [mandatory] if (!JSONObject.ParseMandatory("question", "FAQ headline", out I18NString Question, out ErrorResponse)) { return(false); } #endregion #region Parse Answer [mandatory] if (!JSONObject.ParseMandatory("answer", "FAQ text", out I18NString Answer, out ErrorResponse)) { return(false); } #endregion #region Parse Author [mandatory] User Author = null; if (JSONObject["author"] is JObject authorJSON && authorJSON.ParseMandatory("@id", "author identification", User_Id.TryParse, out User_Id AuthorId, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } if (!UserProvider(AuthorId, out Author)) { ErrorResponse = "The given author '" + AuthorId + "' is unknown!"; return(false); } } else { return(false); } #endregion #region Parse PublicationDate [mandatory] if (!JSONObject.ParseMandatory("publicationDate", "publication date", out DateTime PublicationDate, out ErrorResponse)) { return(false); } #endregion var Tags = new TagRelevance[0]; #region Parse IsHidden [optional] if (JSONObject.ParseOptional("isHidden", "is hidden", out Boolean? IsHidden, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion var Signatures = new Signature[0]; var CustomData = JSONObject["CustomData"] as JObject; #region Get DataSource [optional] var DataSource = JSONObject.GetOptional("dataSource"); #endregion #region Get LastChange [optional] if (JSONObject.ParseOptional("lastChange", "last change", out DateTime? LastChange, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion #region Parse CryptoHash [optional] var CryptoHash = JSONObject.GetOptional("cryptoHash"); #endregion FAQ = new FAQ(FAQIdBody ?? FAQIdURL.Value, Question, Answer, Author, PublicationDate, Tags, IsHidden ?? false, Signatures, CustomData, DataSource, LastChange); ErrorResponse = null; return(true); }
public static Boolean TryParseJSON(JObject JSONObject, UserProviderDelegate UserProvider, out NewsBanner NewsBanner, out String ErrorResponse, NewsBanner_Id?NewsBannerIdURL = null) { try { NewsBanner = null; #region Parse NewsBannerId [optional] // Verify that a given news banner identification // is at least valid. if (JSONObject.ParseOptionalStruct("@id", "news banner identification", NewsBanner_Id.TryParse, out NewsBanner_Id? NewsBannerIdBody, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } if (!NewsBannerIdURL.HasValue && !NewsBannerIdBody.HasValue) { ErrorResponse = "The news banner identification is missing!"; return(false); } if (NewsBannerIdURL.HasValue && NewsBannerIdBody.HasValue && NewsBannerIdURL.Value != NewsBannerIdBody.Value) { ErrorResponse = "The optional news banner 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-LinkedData context information", JSONLDContext.TryParse, out JSONLDContext Context, out ErrorResponse)) { ErrorResponse = @"The JSON-LD ""@context"" information is missing!"; return(false); } if (Context != DefaultJSONLDContext) { ErrorResponse = @"The given JSON-LD ""@context"" information '" + Context + "' is not supported!"; return(false); } #endregion #region Parse Text [mandatory] if (!JSONObject.ParseMandatory("text", "news text", out I18NString Text, out ErrorResponse)) { return(false); } #endregion #region Parse StartTimestamp [mandatory] if (!JSONObject.ParseMandatory("startTimestamp", "start timestamp", out DateTime StartTimestamp, out ErrorResponse)) { return(false); } #endregion #region Parse EndTimestamp [mandatory] if (!JSONObject.ParseMandatory("endTimestamp", "end timestamp", out DateTime EndTimestamp, out ErrorResponse)) { return(false); } #endregion #region Parse Author [mandatory] User Author = null; if (JSONObject["author"] is JObject authorJSON && authorJSON.ParseMandatory("@id", "author identification", User_Id.TryParse, out User_Id AuthorId, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } if (!UserProvider(AuthorId, out Author)) { ErrorResponse = "The given author '" + AuthorId + "' is unknown!"; return(false); } } else { return(false); } #endregion #region Parse IsHidden [optional] if (JSONObject.ParseOptional("isHidden", "is hidden", out Boolean? IsHidden, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion var CustomData = JSONObject["CustomData"] as JObject; #region Get DataSource [optional] var DataSource = JSONObject.GetOptional("dataSource"); #endregion #region Get LastChange [optional] if (JSONObject.ParseOptional("lastChange", "last change", out DateTime? LastChange, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion #region Parse CryptoHash [optional] var CryptoHash = JSONObject.GetOptional("cryptoHash"); #endregion NewsBanner = new NewsBanner(NewsBannerIdBody ?? NewsBannerIdURL.Value, Text, StartTimestamp, EndTimestamp, Author, IsHidden ?? false, CustomData, DataSource, LastChange); ErrorResponse = null; return(true); }
public static Boolean TryParse(JObject JSON, UserProviderDelegate UserProvider, out APIKey APIKey, out String ErrorResponse, APIKey_Id?APIKeyURI = null) { try { APIKey = null; #region Parse APIKey [optional] // Verify that a given API key is at least valid. if (!JSON.ParseOptionalStruct("@id", "API key", APIKey_Id.TryParse, out APIKey_Id? APIKeyBody, out ErrorResponse)) { return(false); } if (!APIKeyURI.HasValue && !APIKeyBody.HasValue) { ErrorResponse = "The API key is missing!"; return(false); } if (APIKeyURI.HasValue && APIKeyBody.HasValue && APIKeyURI.Value != APIKeyBody.Value) { ErrorResponse = "The optional API key given within the JSON body does not match the one given in the URI!"; return(false); } #endregion #region Parse Context [mandatory] if (!JSON.ParseMandatory("@context", "JSON-LinkedData context information", JSONLDContext.TryParse, out JSONLDContext Context, out ErrorResponse)) { ErrorResponse = @"The JSON-LD ""@context"" information is missing!"; return(false); } if (Context != DefaultJSONLDContext) { ErrorResponse = @"The given JSON-LD ""@context"" information '" + Context + "' is not supported!"; return(false); } #endregion #region Parse UserId [mandatory] // Verify that a given user identification // is at least valid. if (!JSON.ParseMandatory("userId", "user identification", User_Id.TryParse, out User_Id UserId, out ErrorResponse)) { return(false); } if (!UserProvider(UserId, out User User)) { ErrorResponse = "The given user '" + UserId + "' is unknown!"; return(false); } #endregion #region Parse Description [optional] if (JSON.ParseOptional("description", "description", out I18NString Description, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion #region Parse AccessRights [mandatory] if (!JSON.ParseMandatoryEnum("accessRights", "Access Rights", // s => s.ParseMandatory_APIKeyRights(), out APIKeyRights AccessRights, out ErrorResponse)) { return(false); } #endregion #region Parse Created [mandatory] if (!JSON.ParseMandatory("created", "creation timestamp", out DateTime Created, out ErrorResponse)) { return(false); } #endregion #region Parse NotBefore [optional] if (JSON.ParseOptional("NotBefore", "'not-valid-before'-timestamp", out DateTime? NotBefore, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion #region Parse NotAfter [optional] if (JSON.ParseOptional("NotAfter", "'not-valid-after'-timestamp", out DateTime? NotAfter, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion #region Parse ValidRemoteIPAddresses [optional] if (!JSON.ParseOptionalHashSet("validRemoteIPAddresses", "valid remote IP addresses", IPAddress.TryParse, out HashSet <IIPAddress> ValidRemoteIPAddresses, out ErrorResponse)) { if (ErrorResponse != null) { return(false); } } #endregion #region Parse IsDisabled [optional] var IsDisabled = JSON["isDisabled"]?.Value <Boolean>(); #endregion #region Parse CryptoHash [optional] var CryptoHash = JSON.GetOptional("cryptoHash"); #endregion APIKey = new APIKey(APIKeyBody ?? APIKeyURI.Value, User, Description, AccessRights, Created, NotBefore, NotAfter, ValidRemoteIPAddresses, IsDisabled); ErrorResponse = null; return(true); } catch (Exception e) { ErrorResponse = e.Message; APIKey = null; return(false); } }