Skip to content

Commit

Permalink
Add non-generic overload for As()
Browse files Browse the repository at this point in the history
Relates #2408
  • Loading branch information
russcam committed Jun 28, 2017
1 parent dd34033 commit 637dae9
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Nest/CommonAbstractions/LazyDocument/LazyDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Nest
Expand All @@ -7,22 +8,37 @@ namespace Nest
public interface ILazyDocument
{
/// <summary>
///
/// Creates an instance of <typeparamref name="T"/> from this
/// <see cref="ILazyDocument"/> instance
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <typeparam name="T">The type</typeparam>
T As<T>() where T : class;

/// <summary>
/// Creates an instance of <paramref name="objectType"/> from this
/// <see cref="ILazyDocument"/> instance
/// </summary>
/// <typeparam name="T">The type</typeparam>
object As(Type objectType);
}

public class LazyDocument : ILazyDocument
{
internal JToken _Value { get; set; }
internal JsonSerializer _Serializer { get; set; }

/// <inheritdoc />
public T As<T>() where T : class
{
var jToken = this._Value;
return jToken?.ToObject<T>(_Serializer);
}

/// <inheritdoc />
public object As(Type objectType)
{
var jToken = this._Value;
return jToken?.ToObject(objectType, _Serializer);
}
}
}

0 comments on commit 637dae9

Please sign in to comment.