Exemplo n.º 1
0
        // Specifying TableName on the attribute overrides any table name inferred by the object's type. So if a
        // TableName has been set, we have to do some private reflection to update the internal tableNameCache.
        // We will address this in the Mobile Services code to make this scenario supported and then remove this
        // private reflection.
        public static void AddToTableNameCache(this IMobileServiceClient client, Type type, string tableName)
        {
            string clientTypeName = client.GetType().Name;

            // Get Serializer
            PropertyInfo serializerProperty = client.GetType().GetProperty(SerializerPropertyName, BindingFlags.NonPublic | BindingFlags.Instance);

            ThrowIfNullMemberInfo(serializerProperty, clientTypeName, clientTypeName, SerializerPropertyName);

            object serializer = serializerProperty.GetValue(client);

            ThrowIfNullValue(serializer, clientTypeName, clientTypeName, SerializerPropertyName);

            // Get SerializerSettings
            PropertyInfo settingsProperty = serializer.GetType().GetProperty(SerializerSettingsPropertyName, BindingFlags.Public | BindingFlags.Instance);

            ThrowIfNullMemberInfo(settingsProperty, clientTypeName, serializer.GetType().Name, SerializerSettingsPropertyName);

            MobileServiceJsonSerializerSettings settings = settingsProperty.GetValue(serializer) as MobileServiceJsonSerializerSettings;

            ThrowIfNullValue(settings, clientTypeName, serializer.GetType().Name, SerializerSettingsPropertyName);

            // Get cache
            FieldInfo tableNameCacheField = settings.ContractResolver.GetType().GetField(TableNameCacheFieldName, BindingFlags.NonPublic | BindingFlags.Instance);

            ThrowIfNullMemberInfo(tableNameCacheField, clientTypeName, settings.ContractResolver.GetType().Name, TableNameCacheFieldName);

            Dictionary <Type, string> tableNameCache = tableNameCacheField.GetValue(settings.ContractResolver) as Dictionary <Type, string>;

            ThrowIfNullValue(tableNameCache, clientTypeName, settings.ContractResolver.GetType().Name, TableNameCacheFieldName);

            // Update cache
            tableNameCache[type] = tableName;
        }
        /// <summary>
        /// Parses the response into a JToken.
        /// If the response is null or empty, null will be returned.
        /// </summary>
        /// <param name="response">The response to parse.</param>
        /// <param name="settings">The serializer settings used for parsing the response.</param>
        /// <returns>A JToken containing the response or null.</returns>
        public static JToken ParseToJToken(this string response, MobileServiceJsonSerializerSettings settings)
        {
            if (String.IsNullOrEmpty(response))
            {
                return null;
            }
            using (var reader = new JsonTextReader(new StringReader(response)))
            {
                reader.DateParseHandling = settings.DateParseHandling;
                reader.DateTimeZoneHandling = settings.DateTimeZoneHandling;
                reader.FloatParseHandling = settings.FloatParseHandling;
                reader.Culture = settings.Culture;

                return JToken.Load(reader);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parses the response into a JToken.
        /// If the response is null or empty, null will be returned.
        /// </summary>
        /// <param name="response">The response to parse.</param>
        /// <param name="settings">The serializer settings used for parsing the response.</param>
        /// <returns>A JToken containing the response or null.</returns>
        public static JToken ParseToJToken(this string response, MobileServiceJsonSerializerSettings settings)
        {
            if (String.IsNullOrEmpty(response))
            {
                return(null);
            }
            using (var reader = new JsonTextReader(new StringReader(response)))
            {
                reader.DateParseHandling    = settings.DateParseHandling;
                reader.DateTimeZoneHandling = settings.DateTimeZoneHandling;
                reader.FloatParseHandling   = settings.FloatParseHandling;
                reader.Culture = settings.Culture;

                return(JToken.Load(reader));
            }
        }
 internal static async Task<Tuple<string, JToken>> ParseContent(HttpResponseMessage response, MobileServiceJsonSerializerSettings serializerSettings)
 {
     string content = null;
     JToken value = null;
     try
     {
         if (response.Content != null)
         {
             content = await response.Content.ReadAsStringAsync();
             value = content.ParseToJToken(serializerSettings);
         }
     }
     catch { }
     return Tuple.Create(content, value);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a new instance of the <see cref="MobileServiceSerializer"/>
 /// class.
 /// </summary>
 public MobileServiceSerializer()
 {
     this.SerializerSettings = new MobileServiceJsonSerializerSettings();
 }
Exemplo n.º 6
0
        internal static async Task <Tuple <string, JToken> > ParseContent(HttpResponseMessage response, MobileServiceJsonSerializerSettings serializerSettings)
        {
            string content = null;
            JToken value   = null;

            try
            {
                if (response.Content != null)
                {
                    content = await response.Content.ReadAsStringAsync();

                    value = content.ParseToJToken(serializerSettings);
                }
            }
            catch { }
            return(Tuple.Create(content, value));
        }