Skip to content

Commit 4a4d9b8

Browse files
committed
Add note about TimeSpan automapped as long
See #1809
1 parent c80e3fb commit 4a4d9b8

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

docs/2.0-breaking-changes/nest-breaking-changes.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Our implementation of `ITransport` can be injected with a custom `IRequestPipeli
5555

5656
Each individual moving part is further explained in the documentation
5757

58-
# Infered types
58+
# Inferred types
5959

6060
Many places that only took a string now take a more strongly typed object, `Id`, `Field`, `Fields`, `Index`, `Indices`, `Type`, `Types`, `DocumentPath<T>`.
6161
Its good to know that in most cases you can still implicitly convert to them from `string`, `long`, `Guid` where it makes sense.
@@ -102,11 +102,10 @@ In `NEST 2.0` this discrapency is gone.
102102

103103
This happens in more places e.g index settings and mappings.
104104

105-
# Aggregotor
105+
# Aggregator
106106

107107
If you are using the object initializer syntax `*Aggregator` is now `*Aggregation`
108108

109-
110109
# Attribute-based mapping
111110

112111
The single `ElasticPropertyAttribute` has been broken up into individual attributes per property type.
@@ -167,6 +166,46 @@ Aside from a simpler and cleaner API, this allows each attribute to only reflect
167166

168167
`MapFromAttributes()` has also been renamed to `AutoMap()` to better reflect that it doesn't only depend on properties being marked with attributes. It will also infer the type based on the CLR type if no attribute is present.
169168

169+
## `TimeSpan` automapped as `long` (ticks)
170+
171+
`System.TimeSpan` is now automatically mapped as a `long` representing the number of ticks within the timeSpan, allowing for range in addition to term queries. NEst 1.x automatically mapped `TimeSpan` as a string and whilst NEST 2.0 is able to deserialize strings into `TimeSpan` instances as before, it will not automatically serialize `TimeSpan` ***into*** strings when indexing. In order to achieve this, you will need to register a json converter, either by deriving from `JsonNetSerializer` and overriding `ContractConverters` or by attributing the property with `[JsonConverter(typeof(ConverterTypeName))]`. A example of a converter for serializing/deserializing string values for `TimeSpan` is
172+
173+
```c#
174+
public class StringTimeSpanConverter : JsonConverter
175+
{
176+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
177+
{
178+
if (value == null)
179+
writer.WriteNull();
180+
else
181+
{
182+
var timeSpan = (TimeSpan)value;
183+
writer.WriteValue(timeSpan.ToString());
184+
}
185+
}
186+
187+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
188+
{
189+
if (reader.TokenType == JsonToken.Null)
190+
{
191+
if (!objectType.IsGenericType || objectType.GetGenericTypeDefinition() != typeof(Nullable<>))
192+
throw new JsonSerializationException($"Cannot convert null value to {objectType}.");
193+
194+
return null;
195+
}
196+
if (reader.TokenType == JsonToken.String)
197+
{
198+
return TimeSpan.Parse((string)reader.Value);
199+
}
200+
201+
throw new JsonSerializationException($"Cannot convert token of type {reader.TokenType} to {objectType}.");
202+
}
203+
204+
public override bool CanConvert(Type objectType) => objectType == typeof(TimeSpan) || objectType == typeof(TimeSpan?);
205+
}
206+
```
207+
208+
170209
#Renamed Types
171210

172211
```diff

0 commit comments

Comments
 (0)