Exemple #1
0
        /// <summary>
        /// Fetches a field value from the document, throwing an exception if the field does not exist.
        /// </summary>
        /// <param name="path">The field path to fetch. Must not be null.</param>
        /// <exception cref="InvalidOperationException">The field does not exist in the document data.</exception>
        /// <returns>The deserialized value.</returns>
        public T GetValue <T>(FieldPath path)
        {
            var raw = ExtractValue(path);

            GaxPreconditions.CheckState(raw != null, $"Field {path} not found in document");
            return((T)ValueDeserializer.Deserialize(Database, raw, typeof(T)));
        }
Exemple #2
0
 /// <summary>
 /// Deserializes the document data as the specified type.
 /// </summary>
 /// <typeparam name="T">The type to deserialize the document data as.</typeparam>
 /// <returns>The deserialized data, or null if this object represents a missing document.</returns>
 public T ConvertTo <T>() where T : class
 {
     if (!Exists)
     {
         return(null);
     }
     return((T)ValueDeserializer.DeserializeMap(Database, Document.Fields, typeof(T)));
 }
 /// <summary>
 /// Deserializes the document data as the specified type.
 /// </summary>
 /// <typeparam name="T">The type to deserialize the document data as.</typeparam>
 /// <returns>The deserialized data, or null if this object represents a missing document.</returns>
 public T ConvertTo <T>()
 {
     if (!Exists)
     {
         return(default(T));
     }
     return((T)ValueDeserializer.DeserializeMap(Database, Document.Fields, typeof(T)));
 }
Exemple #4
0
        /// <summary>
        /// Attempts to fetch the given field path from the document, returning whether or not it was found, and deserializing
        /// it if it was found.
        /// </summary>
        /// <remarks>
        /// This method does not throw an exception if the field is not found, but does throw an exception if the field was found
        /// but cannot be deserialized.
        /// </remarks>
        /// <typeparam name="T">The type to deserialize the value to, if it is found.</typeparam>
        /// <param name="path">The field path to fetch. Must not be null.</param>
        /// <param name="value">When this method returns, contains the deserialized value if the field was found, or the default value
        /// of <typeparamref name="T"/> otherwise.</param>
        /// <returns>true if the field was found; false otherwise.</returns>
        public bool TryGetValue <T>(FieldPath path, out T value)
        {
            var raw = ExtractValue(path);

            if (raw == null)
            {
                value = default(T);
                return(false);
            }
            value = (T)ValueDeserializer.Deserialize(Database, raw, typeof(T));
            return(true);
        }