public static TType castTo <TType>(this JToken source)
        {
            string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(source, Newtonsoft.Json.Formatting.Indented);
            var    result     = UtilsJson.DeserializeObject <TType>(serialized);

            return(result);
        }
Example #2
0
        /// <summary>
        /// Get an associative array from a JOBJECT or JARRAY. Prepopulates the ID
        /// property of the object (in case it does not exist) with the associative array key.
        /// </summary>
        /// <returns></returns>
        public static Dictionary <string, JToken> keyedFromArrayOrObject(JToken source)
        {
            Dictionary <string, JToken> result = new Dictionary <string, JToken>();

            // Our custom deserialization already has support for propagating ID information from regular arrays to
            // associative arrays.
            return(UtilsJson.DeserializeObject <Dictionary <string, JToken> >(source.ToString()));
        }
Example #3
0
 /// <summary>
 /// Converts a YAML configuration file to a JTOKEN
 /// compatible JSON representation
 /// </summary>
 /// <typeparam name="TTokenType"></typeparam>
 /// <param name="source">Yaml or JSON</param>
 /// <returns></returns>
 public static TTokenType YamlOrJsonToKtoken <TTokenType>(string source)
     where TTokenType : JToken
 {
     source = EnsureJson(source);
     return(UtilsJson.DeserializeObject <TTokenType>(source));
 }