Skip to content

Commit b939669

Browse files
committed
rewrote documentation on settings custom json.net settings
1 parent dd397ec commit b939669

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

src/Nest/CommonAbstractions/SerializationBehavior/ElasticContractResolver.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ namespace Nest
1414
public class ElasticContractResolver : DefaultContractResolver
1515
{
1616
private readonly IList<Func<Type, JsonConverter>> _contractConverters;
17-
public static JsonSerializer Empty { get; } = new JsonSerializer();
18-
17+
public static JsonSerializer Empty { get; } = new JsonSerializer();
18+
1919
/// <summary>
2020
/// ConnectionSettings can be requested by JsonConverter's.
2121
/// </summary>
22-
public IConnectionSettingsValues ConnectionSettings { get; private set; }
23-
22+
public IConnectionSettingsValues ConnectionSettings { get; private set; }
23+
2424
/// <summary>
2525
/// Signals to custom converter that it can get serialization state from one of the converters. Ugly but massive performance gain
2626
/// </summary>
@@ -36,22 +36,22 @@ protected override JsonContract CreateContract(Type objectType)
3636
{
3737
// cache contracts per connection settings
3838
return this.ConnectionSettings.Inferrer.Contracts.GetOrAdd(objectType, o =>
39-
{
40-
var contract = base.CreateContract(o);
41-
39+
{
40+
var contract = base.CreateContract(o);
41+
4242
if (typeof(IDictionary).IsAssignableFrom(o) && !typeof(IIsADictionary).IsAssignableFrom(o))
4343
contract.Converter = new VerbatimDictionaryKeysJsonConverter();
4444
if (typeof(IEnumerable<QueryContainer>).IsAssignableFrom(o))
4545
contract.Converter = new QueryContainerCollectionJsonConverter();
4646
else if (o == typeof(ServerError))
4747
contract.Converter = new ServerErrorJsonConverter();
48-
else if (o == typeof(DateTime) ||
49-
o == typeof(DateTime?) ||
50-
o == typeof(DateTimeOffset) ||
51-
o == typeof(DateTimeOffset?))
48+
else if (o == typeof(DateTime) ||
49+
o == typeof(DateTime?) ||
50+
o == typeof(DateTimeOffset) ||
51+
o == typeof(DateTimeOffset?))
5252
contract.Converter = new IsoDateTimeConverter();
53-
else if (o == typeof(TimeSpan) ||
54-
o == typeof(TimeSpan?))
53+
else if (o == typeof(TimeSpan) ||
54+
o == typeof(TimeSpan?))
5555
contract.Converter = new TimeSpanConverter();
5656

5757
if (this._contractConverters.HasAny())
@@ -102,12 +102,12 @@ private IEnumerable<Type> TypeWithInterfaces(Type objectType)
102102
}
103103

104104
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
105-
{
106-
// Only serialize explicitly implemented IProperty properties on attribute types
105+
{
106+
// Only serialize explicitly implemented IProperty properties on attribute types
107107
if (typeof(ElasticsearchPropertyAttributeBase).IsAssignableFrom(type))
108-
return PropertiesOfInterface<IProperty>(type, memberSerialization);
109-
110-
// Descriptors implement properties explicitly, these are not picked up by default
108+
return PropertiesOfInterface<IProperty>(type, memberSerialization);
109+
110+
// Descriptors implement properties explicitly, these are not picked up by default
111111
if (typeof(IDescriptor).IsAssignableFrom(type))
112112
return PropertiesOfAll(type, memberSerialization);
113113

@@ -155,9 +155,9 @@ protected override string ResolvePropertyName(string fieldName)
155155

156156
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
157157
{
158-
var property = base.CreateProperty(member, memberSerialization);
159-
160-
// Skip serialization of empty collections that has DefaultValueHandling set to Ignore.
158+
var property = base.CreateProperty(member, memberSerialization);
159+
160+
// Skip serialization of empty collections that has DefaultValueHandling set to Ignore.
161161
if (property.DefaultValueHandling.HasValue
162162
&& property.DefaultValueHandling.Value == DefaultValueHandling.Ignore
163163
&& !typeof(string).IsAssignableFrom(property.PropertyType)

src/Tests/ClientConcepts/LowLevel/Connecting.doc.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,37 @@ protected override HttpClientHandler CreateHttpClientHandler(RequestData request
289289
}
290290
#endif
291291

292-
/**=== Overriding default Json.NET behavior
292+
/**=== Overriding Json.NET settings
293293
*
294294
* Overriding the default Json.NET behaviour in NEST is an expert behavior but if you need to get to the nitty gritty, this can be really useful.
295-
* First, create a subclass of the `JsonNetSerializer`
296295
*/
296+
297+
/**
298+
* The easiest way is to create an instance of `SerializerFactory` that allows you to register a modification callback
299+
* in the constructor
300+
*/
301+
public void EasyWay()
302+
{
303+
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
304+
var connectionSettings = new ConnectionSettings(
305+
pool,
306+
new HttpConnection(),
307+
new SerializerFactory((jsonSettings, nestSettings) => jsonSettings.PreserveReferencesHandling = PreserveReferencesHandling.All));
308+
309+
var client = new ElasticClient(connectionSettings);
310+
}
311+
312+
313+
/**
314+
* A more involved and explicit way would be to implement your own JsonNetSerializer subclass.
315+
*
316+
* NOTE: this is subject to change in the next major release. NEST relies heavily on stateful deserializers (that have access to the original
317+
* request) for specialized features such a covariant search results. This requirement leaks into this abstraction.
318+
*/
297319
public class MyJsonNetSerializer : JsonNetSerializer
298320
{
299-
public MyJsonNetSerializer(IConnectionSettingsValues settings) : base(settings, (s, csv) => s.PreserveReferencesHandling = PreserveReferencesHandling.All) //<1> Call this constructor if you only need access to `JsonSerializerSettings` without state
321+
public MyJsonNetSerializer(IConnectionSettingsValues settings)
322+
: base(settings, (s, csv) => s.PreserveReferencesHandling = PreserveReferencesHandling.All) //<1> Call this constructor if you only need access to `JsonSerializerSettings` without state
300323
{
301324
OverwriteDefaultSerializers((s, cvs) => ModifySerializerSettings(s)); //<2> Call OverwriteDefaultSerializers if you need access to `JsonSerializerSettings` with state
302325
}

0 commit comments

Comments
 (0)