Example #1
0
 /// <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);
     }
 }
Example #2
0
        /// <summary/>
        internal static object                                      ConvertToken(JToken token, Type type)
        {
            try
            {
                if (type == typeof(string))
                {
                    return(JsonFunctions.GetTokenString(token));
                }
                if (type == typeof(byte[]))
                {
                    var bytes = Encoding.UTF8.GetBytes(JsonFunctions.GetTokenString(token));

                    using (var msi = new MemoryStream(bytes))
                        using (var mso = new MemoryStream())
                        {
                            using (var gs = new GZipStream(mso, CompressionLevel.Optimal))
                            {
                                msi.CopyTo(gs);
                            }

                            return(mso.ToArray());
                        };
                }

                // 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);
            }
        }