Skip to content

Commit c64c4f3

Browse files
committed
added an easier Deserialize extension when you do not need to deserialize to non NEST objects
1 parent f1c1967 commit c64c4f3

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Nest
8+
{
9+
public static class SerializerExtensions
10+
{
11+
/// <summary>
12+
/// This is a convenience method to deserialize to T using the registered deserializer.
13+
/// <para>NOTE:</para> If you want to deserialize to a NEST response you need to use the overload that
14+
/// takes an ElasticsearchResponse
15+
/// </summary>
16+
/// <typeparam name="T">The type to deserialize to</typeparam>
17+
/// <param name="serializer"></param>
18+
/// <param name="data">The string representation of the data to be deserialized</param>
19+
public static T Deserialize<T>(this INestSerializer serializer, string data)
20+
{
21+
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(data)))
22+
return serializer.Deserialize<T>(null, ms, null);
23+
}
24+
}
25+
}

src/Nest/ExposedInternals/NestSerializer.cs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,6 @@ public NestSerializer(IConnectionSettingsValues settings)
2424
this._serializationSettings = this.CreateSettings();
2525
}
2626

27-
///// <summary>
28-
///// Returns a response of type R based on the connection status by trying parsing status.Result into R
29-
///// </summary>
30-
///// <returns></returns>
31-
//public virtual R ToParsedResponse<R>(
32-
// ElasticsearchResponse<R> status,
33-
// bool notFoundIsAValidResponse = false,
34-
// JsonConverter piggyBackJsonConverter = null
35-
// ) where R : BaseResponse
36-
//{
37-
// var jsonSettings =piggyBackJsonConverter != null
38-
// ? this.CreateSettings(piggyBackJsonConverter)
39-
// : this._serializationSettings;
40-
41-
// var isValid =
42-
// (notFoundIsAValidResponse)
43-
// ? (status.Error == null
44-
// || status.Error.HttpStatusCode == System.Net.HttpStatusCode.NotFound)
45-
// : (status.Error == null);
46-
47-
// R r;
48-
// if (!isValid)
49-
// r = (R)typeof (R).CreateInstance();
50-
// else
51-
// r = JsonConvert.DeserializeObject<R>(status.Result, jsonSettings);
52-
53-
// var baseResponse = r as BaseResponse;
54-
// if (baseResponse == null)
55-
// return null;
56-
// baseResponse.IsValid = isValid;
57-
// baseResponse.ConnectionStatus = status;
58-
// return r;
59-
//}
60-
6127
public virtual byte[] Serialize(object data, SerializationFormatting formatting = SerializationFormatting.Indented)
6228
{
6329
var format = formatting == SerializationFormatting.None ? Formatting.None : Formatting.Indented;
@@ -68,6 +34,10 @@ public virtual byte[] Serialize(object data, SerializationFormatting formatting
6834
/// <summary>
6935
/// Deserialize an object
7036
/// </summary>
37+
/// <typeparam name="T">The type you want to deserialize too</typeparam>
38+
/// <param name="response">If the type you want is a Nest Response you have to pass a response object</param>
39+
/// <param name="stream">The stream to deserialize off</param>
40+
/// <param name="deserializationState">Optional deserialization state</param>
7141
public virtual T Deserialize<T>(IElasticsearchResponse response, Stream stream, object deserializationState = null)
7242
{
7343
var settings = this._serializationSettings;

0 commit comments

Comments
 (0)