/// <summary> /// Returns with the value of the given path. /// </summary> /// <param name="document"></param> /// <param name="path">E.g.: name.firstname</param> /// <returns>Framework's primitive types</returns> public static object GetValue(object document, string path) { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (string.IsNullOrEmpty(path)) { throw new ArgumentException(nameof(path)); } var token = JTokenHelper.GetPathToken(JToken.FromObject(document), path); if (token == null) { return(null); } var valueToken = token as JValue; if (valueToken != null) { return(valueToken.Value); } var arrayToken = token as JArray; if (arrayToken != null) { if (arrayToken.First?.Type == JTokenType.Integer) { return(arrayToken.Select(item => (int)item).ToArray()); } return(arrayToken.Select(item => (string)item).ToArray()); } return(token); }
public static void RemoveTagIds(object document, string fieldName, List <string> tagIdsToRemove) { if (document == null) { throw new ArgumentNullException(nameof(document)); } var token = document as JToken; if (token == null) { return; } var field = JTokenHelper.GetPathToken(token, fieldName); var valueToken = field as JValue; if (valueToken != null) { valueToken.Value = string.Empty; } var arrayToken = field as JArray; if (arrayToken != null) { var items = arrayToken.Where(tag => tagIdsToRemove.Contains((string)tag)) .Select(tag => tag) .ToList(); foreach (var item in items.Where(i => i != null)) { arrayToken.Remove(item); } } }
public static bool IsPrimitiveType(object document, string fieldName) { var token = JTokenHelper.GetPathToken(JToken.FromObject(document), fieldName); return(token != null && token is JValue); }
public static bool IsExist(object document, string fieldName) { var token = JTokenHelper.GetPathToken(JToken.FromObject(document), fieldName); return(token != null); }