private void mapToColumns(JObject obj, IUpdatableRow output) { var json = JsonConvert.SerializeObject(obj); var genre = JsonFunctions.JsonTuple(json, "$.data.author"); //Console.WriteLine("Genre: "+genre.Count()); /* * genre.Values.ToList().ForEach(g => { * //Console.WriteLine(g); * output.Set("contexts.data.genre", g); * } * ); */ var keys = genre.Keys; foreach (var key in keys) { Console.WriteLine($"{key}: {genre[key]}"); } //Console.WriteLine($"data: {genre["data.breadcrumb"]}"); //setting it to null for second object //work on it //create a condition and add second object if not null output.Set("contexts.data.genre", genre["data.genre"]); }
private static IEnumerable <KeyValuePair <string, T> > MapHelper <T>(JToken root, string path) { // Children var children = SelectChildren <T>(root, path); //Console.WriteLine($"Childrem count: {children.Count()}"); //children.ToList().ForEach(item => Console.WriteLine(item)); foreach (var token in children) { // Token => T var value = (T)JsonFunctions.ConvertToken(token, typeof(T)); //Console.WriteLine($"tokenPAth: {token.Path}, value: {value}"); // Tuple(path, value) yield return(new KeyValuePair <string, T>(token.Path, value)); } }
/// <summary/> internal static object ConvertToken(JToken token, Type type) { try { if (type == typeof(string)) { return(JsonFunctions.GetTokenString(token)); } // We simply delegate to Json.Net for data conversions return(token.ToObject(type)); } catch (Exception e) { // Make this easier to debug (with field and type context) // Note: We don't expose the actual value to be converted in the error message (since it might be sensitive, information disclosure) throw new JsonSerializationException( string.Format(typeof(JsonToken).Namespace + " failed to deserialize '{0}' from '{1}' to '{2}'", token.Path, token.Type.ToString(), type.FullName), e); } }
/// <summary/> protected virtual void JObjectToRow(JObject o, IUpdatableRow row) { foreach (var c in row.Schema) { JToken token = null; object value = c.DefaultValue; // All fields are represented as columns // Note: Each JSON row/payload can contain more or less columns than those specified in the row schema // We simply update the row for any column that matches (and in any order). if (o.TryGetValue(c.Name, out token) && token != null) { // Note: We simply delegate to Json.Net for all data conversions // For data conversions beyond what Json.Net supports, do an explicit projection: // ie: SELECT DateTime.Parse(datetime) AS datetime, ... // Note: Json.Net incorrectly returns null even for some non-nullable types (sbyte) // We have to correct this by using the default(T) so it can fit into a row value value = JsonFunctions.ConvertToken(token, c.Type) ?? c.DefaultValue; } // Update row.Set <object>(c.Name, value); } }