5
5
using System ;
6
6
using System . Text . Json ;
7
7
using System . Text . Json . Serialization ;
8
- using System . Threading ;
9
- using System . Threading . Tasks ;
10
- using Elastic . Transport ;
11
8
12
9
namespace Elastic . Clients . Elasticsearch
13
10
{
14
11
/// <summary>
15
- /// A lazily deserialized document.
12
+ /// <para>A lazily deserialized document.</para>
13
+ /// <para>Holds raw JSON bytes which can be lazily converted to a specific <see cref="Type"/> at a later time.</para>
16
14
/// </summary>
17
15
[ JsonConverter ( typeof ( LazyDocumentConverter ) ) ]
18
- public class LazyDocument
16
+ public readonly struct LazyDocument
19
17
{
20
- private readonly Serializer _sourceSerializer ;
21
- private readonly Serializer _requestResponseSerializer ;
22
- private readonly IMemoryStreamFactory _memoryStreamFactory ;
23
-
24
18
internal LazyDocument ( byte [ ] bytes , IElasticsearchClientSettings settings )
25
19
{
26
20
Bytes = bytes ;
27
-
28
- _sourceSerializer = settings . SourceSerializer ;
29
- _requestResponseSerializer = settings . RequestResponseSerializer ;
30
- _memoryStreamFactory = settings . MemoryStreamFactory ;
21
+ Settings = settings ;
31
22
}
32
23
33
- internal byte [ ] Bytes { get ; }
34
-
35
- internal T AsUsingRequestResponseSerializer < T > ( )
36
- {
37
- using var ms = _memoryStreamFactory . Create ( Bytes ) ;
38
- return _requestResponseSerializer . Deserialize < T > ( ms ) ;
39
- }
24
+ internal byte [ ] ? Bytes { get ; }
25
+ internal IElasticsearchClientSettings ? Settings { get ; }
40
26
41
27
/// <summary>
42
28
/// Creates an instance of <typeparamref name="T" /> from this
43
29
/// <see cref="LazyDocument" /> instance.
44
30
/// </summary>
45
31
/// <typeparam name="T">The type</typeparam>
46
- public T As < T > ( )
47
- {
48
- using var ms = _memoryStreamFactory . Create ( Bytes ) ;
49
- return _sourceSerializer . Deserialize < T > ( ms ) ;
50
- }
51
-
52
- /// <summary>
53
- /// Creates an instance of <paramref name="objectType" /> from this
54
- /// <see cref="LazyDocument" /> instance.
55
- /// </summary>
56
- /// <param name="objectType">The type</param>
57
- public object As ( Type objectType )
32
+ public T ? As < T > ( )
58
33
{
59
- using var ms = _memoryStreamFactory . Create ( Bytes ) ;
60
- return _sourceSerializer . Deserialize ( objectType , ms ) ;
61
- }
34
+ if ( Bytes is null || Settings is null || Bytes . Length == 0 )
35
+ return default ;
62
36
63
- /// <summary>
64
- /// Creates an instance of <typeparamref name="T" /> from this
65
- /// <see cref="LazyDocument" /> instance.
66
- /// </summary>
67
- /// <typeparam name="T">The type</typeparam>
68
- public ValueTask < T > AsAsync < T > ( CancellationToken ct = default )
69
- {
70
- using var ms = _memoryStreamFactory . Create ( Bytes ) ;
71
- return _sourceSerializer . DeserializeAsync < T > ( ms , ct ) ;
72
- }
73
-
74
- /// <summary>
75
- /// Creates an instance of <paramref name="objectType" /> from this
76
- /// <see cref="LazyDocument" /> instance.
77
- /// </summary>
78
- /// <param name="objectType">The type</param>
79
- public ValueTask < object > AsAsync ( Type objectType , CancellationToken ct = default )
80
- {
81
- using var ms = _memoryStreamFactory . Create ( Bytes ) ;
82
- return _sourceSerializer . DeserializeAsync ( objectType , ms , ct ) ;
37
+ using var ms = Settings . MemoryStreamFactory . Create ( Bytes ) ;
38
+ return Settings . SourceSerializer . Deserialize < T > ( ms ) ;
83
39
}
84
40
}
85
41
@@ -101,6 +57,6 @@ public override LazyDocument Read(ref Utf8JsonReader reader, Type typeToConvert,
101
57
return new LazyDocument ( stream . ToArray ( ) , _settings ) ;
102
58
}
103
59
104
- public override void Write ( Utf8JsonWriter writer , LazyDocument value , JsonSerializerOptions options ) => throw new NotImplementedException ( ) ;
60
+ public override void Write ( Utf8JsonWriter writer , LazyDocument value , JsonSerializerOptions options ) => throw new NotImplementedException ( "We only ever expect to deserialize a LazyDocument on responses." ) ;
105
61
}
106
62
}
0 commit comments