forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexNameMarkerConverter.cs
46 lines (40 loc) · 1.14 KB
/
IndexNameMarkerConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace Nest.Resolvers.Converters
{
public class IndexNameMarkerConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(IndexNameMarker) == objectType;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var marker = value as IndexNameMarker;
if (marker == null)
{
writer.WriteNull();
return;
}
var contract = serializer.ContractResolver as SettingsContractResolver;
if (contract != null && contract.ConnectionSettings != null)
{
var indexName = contract.Infer.IndexName(marker);
writer.WriteValue(indexName);
}
else throw new Exception("If you use a custom contract resolver be sure to subclass from ElasticResolver");
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
string typeName = reader.Value.ToString();
return (IndexNameMarker)typeName;
}
return null;
}
}
}