public void Update(Newtonsoft.Json.Linq.JArray pages, Guid userId, TakeDocModel.Version version, Guid entityId) { TakeDocModel.TypeDocument typeDoc = daoTypeDocument.GetBy(x => x.TypeDocumentId == version.Document.DocumentTypeId).First(); if (typeDoc.TypeDocumentPageNeed) { int nb = pages.Where(obj => obj.Value <string>("pageNumber") != "-1").Count(); if (nb == 0) { throw new Exception("Une photographie est obligatoire."); } } foreach (Newtonsoft.Json.Linq.JObject page in pages.OrderBy(obj => obj["pageNumber"])) { Guid id = Guid.Empty; try { id = new Guid(page.Value <string>("id")); } catch (Exception ex) { id = Guid.Empty; } int rotation = page.Value <int>("rotation"); int pageNumber = page.Value <int>("pageNumber"); string action = page.Value <string>("action"); if (string.IsNullOrEmpty(action) == false) { bool delete = action.ToUpper().Equals("DELETE"); bool add = action.ToUpper().Equals("ADD"); bool update = action.ToUpper().Equals("UPDATE"); if (delete && id != Guid.Empty) { this.Update(id, rotation, pageNumber, delete, userId, entityId); } else if (add) { string extension = page.Value <string>("base64Image").Split(';')[0].Replace("data:image/", string.Empty); this.Add(userId, entityId, version.VersionId, page.Value <string>("base64Image"), extension, rotation, pageNumber); } else if (update && id != Guid.Empty) { this.Update(id, rotation, pageNumber, delete, userId, entityId); } } } }
public static string RandomSampleJson( string json, int?seed = null, int maxNumberOfItems = 10) { Newtonsoft.Json.Linq.JToken root = (Newtonsoft.Json.Linq.JToken)Newtonsoft.Json.JsonConvert.DeserializeObject(json); Random random = seed.HasValue ? new Random(seed.Value) : new Random(); string sampledJson; if (root.Type == Newtonsoft.Json.Linq.JTokenType.Array) { Newtonsoft.Json.Linq.JArray array = (Newtonsoft.Json.Linq.JArray)root; IEnumerable <Newtonsoft.Json.Linq.JToken> tokens = array .OrderBy(x => random.Next()) .Take(random.Next(maxNumberOfItems)); Newtonsoft.Json.Linq.JArray newArray = new Newtonsoft.Json.Linq.JArray(); foreach (Newtonsoft.Json.Linq.JToken token in tokens) { newArray.Add(token); } sampledJson = newArray.ToString(); } else if (root.Type == Newtonsoft.Json.Linq.JTokenType.Object) { Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)root; IEnumerable <Newtonsoft.Json.Linq.JProperty> properties = jobject .Properties() .OrderBy(x => random.Next()) .Take(maxNumberOfItems); Newtonsoft.Json.Linq.JObject newObject = new Newtonsoft.Json.Linq.JObject(); foreach (Newtonsoft.Json.Linq.JProperty property in properties) { newObject.Add(property); } sampledJson = newObject.ToString(); } else { sampledJson = json; } return(sampledJson); }