コード例 #1
0
        private PatchResult <JObject> TryPrivatePatch(JObject JSON,
                                                      JObject Patch)
        {
            foreach (var property in Patch)
            {
                if (property.Key == "id")
                {
                    return(PatchResult <JObject> .Failed(JSON,
                                                         "Patching the 'unique identification' of a token is not allowed!"));
                }

                else if (property.Value is null)
                {
                    JSON.Remove(property.Key);
                }

                else if (property.Value is JObject subObject)
                {
                    if (JSON.ContainsKey(property.Key))
                    {
                        if (JSON[property.Key] is JObject oldSubObject)
                        {
                            //ToDo: Perhaps use a more generic JSON patch here!
                            // PatchObject.Apply(ToJSON(), EVSEPatch),
                            var patchResult = TryPrivatePatch(oldSubObject, subObject);

                            if (patchResult.IsSuccess)
                            {
                                JSON[property.Key] = patchResult.PatchedData;
                            }
                        }

                        else
                        {
                            JSON[property.Key] = subObject;
                        }
                    }

                    else
                    {
                        JSON.Add(property.Key, subObject);
                    }
                }

                //else if (property.Value is JArray subArray)
                //{
                //}

                else
                {
                    JSON[property.Key] = property.Value;
                }
            }

            return(PatchResult <JObject> .Success(JSON));
        }
コード例 #2
0
        /// <summary>
        /// Try to patch the JSON representaion of this token.
        /// </summary>
        /// <param name="TokenPatch">The JSON merge patch.</param>
        /// <param name="AllowDowngrades">Allow to set the 'lastUpdated' timestamp to an earlier value.</param>
        public PatchResult <Token> TryPatch(JObject TokenPatch,
                                            Boolean AllowDowngrades = false)
        {
            if (TokenPatch == null)
            {
                return(PatchResult <Token> .Failed(this,
                                                   "The given token patch must not be null!"));
            }

            lock (patchLock)
            {
                if (TokenPatch["last_updated"] is null)
                {
                    TokenPatch["last_updated"] = DateTime.UtcNow.ToIso8601();
                }

                else if (AllowDowngrades == false &&
                         TokenPatch["last_updated"].Type == JTokenType.Date &&
                         (TokenPatch["last_updated"].Value <DateTime>().ToIso8601().CompareTo(LastUpdated.ToIso8601()) < 1))
                {
                    return(PatchResult <Token> .Failed(this,
                                                       "The 'lastUpdated' timestamp of the token patch must be newer then the timestamp of the existing token!"));
                }


                var patchResult = TryPrivatePatch(ToJSON(), TokenPatch);


                if (patchResult.IsFailed)
                {
                    return(PatchResult <Token> .Failed(this,
                                                       patchResult.ErrorResponse));
                }

                if (TryParse(patchResult.PatchedData,
                             out Token PatchedToken,
                             out String ErrorResponse))
                {
                    return(PatchResult <Token> .Success(PatchedToken,
                                                        ErrorResponse));
                }