public void Delete() { if (jToken != null) { jToken.Remove(); } }
/// <inheritdoc/> public override object?ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Newtonsoft.Json.Linq.JObject jObject = serializer.Deserialize <Newtonsoft.Json.Linq.JObject>(reader); if (jObject == null) { return(null); } var discriminator = Newtonsoft.Json.Linq.Extensions.Value <string>(jObject.GetValue(this.discriminator, StringComparison.OrdinalIgnoreCase)); Type subtype = GetObjectSubtype(objectType, discriminator); var objectContract = serializer.ContractResolver.ResolveContract(subtype) as Newtonsoft.Json.Serialization.JsonObjectContract; if (objectContract == null || System.Linq.Enumerable.All(objectContract.Properties, p => p.PropertyName != this.discriminator)) { jObject.Remove(this.discriminator); } try { isReading = true; return(serializer.Deserialize(jObject.CreateReader(), subtype)); } finally { isReading = false; } }
private Response CreateResponse(Newtonsoft.Json.Linq.JObject jObject) { //preserve metadata values into variables int total_records = jObject["total_records"].ToObject <int>(); var returned_records = jObject["returned_records"].ToObject <string>(); var returned_count = jObject["returned_count"].ToObject <int>(); //remove the unwanted keys jObject.Remove("total_records"); jObject.Remove("returned_records"); jObject.Remove("returned_count"); //once, the metadata keys are removed, json.net will be able to deserialize without problem var response = jObject.ToObject <Response>(); //Assign back the metadata to response object response.total_records = total_records; response.returned_count = returned_count; response.returned_records = returned_records; //.. now person can be accessed like response['6112'], and // metadata can be accessed like response.total_records return(response); }
static void ConvertAttachStringToObject(Newtonsoft.Json.Linq.JObject token) { //处理"msg_attach"值,该json原始值是一个string,需要转换为json object var attachmentToken = token.SelectToken(NIMIMMessage.AttachmentPath); if (attachmentToken == null) { return; } if (attachmentToken.Type == Newtonsoft.Json.Linq.JTokenType.String) { var attachValue = attachmentToken.ToObject <string>(); if (string.IsNullOrEmpty(attachValue)) { token.Remove(NIMIMMessage.AttachmentPath); return; } var newAttachToken = Newtonsoft.Json.Linq.JToken.Parse(attachValue); attachmentToken.Replace(newAttachToken); } }
public async Task Invoke(HttpContext httpContext) { httpContext.Request.EnableRewind(); var strtmp = httpContext.Request.Path.ToString().ToUpper(); if (_options.enableSwagger && strtmp.Contains(_options.swaggerUrl.ToUpper())) { await _next.Invoke(httpContext); return; } var context = new RouteContext(httpContext); context.RouteData.Routers.Clear(); context.RouteData.Routers.Add(_router); RouteCollection routec = _router as RouteCollection; await _router.RouteAsync(context); RouteData routeData = context.RouteData; if (context.Handler != null) { httpContext.Features[typeof(IRoutingFeature)] = new RoutingFeature() { RouteData = context.RouteData, }; } Console.WriteLine(httpContext.Request.Method.ToUpper()); Newtonsoft.Json.Linq.JObject paramjson = new Newtonsoft.Json.Linq.JObject(); foreach (var onekey in routeData.Values.Keys) { object tmp; routeData.Values.TryGetValue(onekey, out tmp); if (tmp != null) { paramjson.Add(onekey, tmp.ToString()); } } string area, controler, action; if (routeData.Values.Keys.Contains("area")) { area = routeData.Values["area"].ToString().ToLower(); paramjson.Remove("area"); } else { area = "-1"; } if (routeData.Values.Keys.Contains("controller")) { controler = routeData.Values["controller"].ToString().ToLower(); paramjson.Remove("controller"); } else { controler = ""; } if (routeData.Values.Keys.Contains("action")) { action = routeData.Values["action"].ToString().ToLower(); paramjson.Remove("action"); } else { action = ""; } if (area == "-1" && string.IsNullOrEmpty(controler)) { await ReturnSystemError(httpContext); return; } string rolestr = ""; string userid = ""; bool isdevice = false; Microsoft.Extensions.Primitives.StringValues dtoken = ""; if (context.HttpContext.Request.Headers.TryGetValue("DeviceToken", out dtoken)) { if (!string.IsNullOrEmpty(dtoken)) { isdevice = true; } } if (httpContext.User != null && httpContext.User.Identity.IsAuthenticated) { ClaimsIdentity ci = httpContext.User.Identity as ClaimsIdentity; var userPrincipal = new ClaimsPrincipal(ci); System.Threading.Thread.CurrentPrincipal = userPrincipal; var clm = (from x in ci.Claims where x.Type == "RoleId" select x).FirstOrDefault(); rolestr = clm == null ? "" : clm.Value; clm = (from x in ci.Claims where x.Type == "UserId" select x).FirstOrDefault(); userid = clm == null ? "" : clm.Value; if (!context.HttpContext.Request.Path.ToString().Contains("home/getUnReadMessage")) { } } else { if (httpContext.Request.Headers.ContainsKey("token")) { } } string paramstr = paramjson.ToString(); if (!this._options.authorizefilter.AnonymousAllowed(controler, action, httpContext.Request.Method, paramstr, area, isdevice)) { if (string.IsNullOrEmpty(rolestr)) { if (_options.isWeb) { await ReturnRedirect(httpContext, _options.loginUrl); } else { await ReturnNeedLogin(httpContext); } return; } if (!this._options.authorizefilter.Isvalidtoken(userid)) { await ReturnNeedLogin(httpContext); return; } if (!this._options.authorizefilter.IsAllowed(rolestr, controler, action, httpContext.Request.Method, paramstr, area, isdevice)) { await ReturnNoAuthorized(httpContext); return; } } await _next.Invoke(httpContext); }