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