Skip to content

Commit dd58212

Browse files
authored
normalize ifdefs in our source included UTF8Json (#4212)
1 parent 2d12ec7 commit dd58212

35 files changed

+37
-699
lines changed

src/Elasticsearch.Net/Elasticsearch.Net.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), build.bat))\src\PublishArtifacts.build.props" />
44
<PropertyGroup>
5-
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
5+
<TargetFrameworks>netstandard2.0;netstandard2.1;net461</TargetFrameworks>
66
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8-
<!-- Here to satisy UTF8JSON which defines NETSTANDARD also for anything > NET45 -->
9-
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
108
<LangVersion>8.0</LangVersion>
119
<TieredCompilation>false</TieredCompilation>
1210
<DebugSymbols>true</DebugSymbols>

src/Elasticsearch.Net/Utf8Json/Formatters/CollectionFormatters.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,7 @@ public IGrouping<TKey, TElement> Deserialize(ref JsonReader reader, IJsonFormatt
504504
{
505505
var keyString = reader.ReadPropertyNameSegmentRaw();
506506
int key;
507-
#if NETSTANDARD
508507
CollectionFormatterHelper.groupingAutomata.TryGetValue(keyString, out key);
509-
#else
510-
CollectionFormatterHelper.groupingAutomata.TryGetValueSafe(keyString, out key);
511-
#endif
512508

513509
switch (key)
514510
{
@@ -832,7 +828,6 @@ public IList Deserialize(ref JsonReader reader, IJsonFormatterResolver formatter
832828
}
833829

834830

835-
#if NETSTANDARD
836831

837832
internal sealed class ObservableCollectionFormatter<T> : CollectionFormatterBase<T, ObservableCollection<T>>
838833
{
@@ -969,9 +964,6 @@ protected override ConcurrentStack<T> Complete(ref ArrayBuffer<T> intermediateCo
969964
}
970965
}
971966

972-
#endif
973-
974-
975967
internal static class CollectionFormatterHelper
976968
{
977969
internal static readonly byte[][] groupingName;

src/Elasticsearch.Net/Utf8Json/Formatters/DateTimeFormatter.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,6 @@ public DateTimeOffset Deserialize(ref JsonReader reader, IJsonFormatterResolver
728728

729729
internal sealed class TimeSpanFormatter : IJsonFormatter<TimeSpan>
730730
{
731-
#if NETSTANDARD
732731
readonly string formatString;
733732

734733
public TimeSpanFormatter()
@@ -740,21 +739,15 @@ public TimeSpanFormatter(string formatString)
740739
{
741740
this.formatString = formatString;
742741
}
743-
#endif
744742

745743
public void Serialize(ref JsonWriter writer, TimeSpan value, IJsonFormatterResolver formatterResolver)
746744
{
747-
#if NETSTANDARD
748745
writer.WriteString(value.ToString(formatString));
749-
#else
750-
writer.WriteString(value.ToString());
751-
#endif
752746
}
753747

754748
public TimeSpan Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
755749
{
756750
var str = reader.ReadString();
757-
#if NETSTANDARD
758751
if (formatString == null)
759752
{
760753
return TimeSpan.Parse(str, CultureInfo.InvariantCulture);
@@ -763,9 +756,6 @@ public TimeSpan Deserialize(ref JsonReader reader, IJsonFormatterResolver format
763756
{
764757
return TimeSpan.ParseExact(str, formatString, CultureInfo.InvariantCulture);
765758
}
766-
#else
767-
return TimeSpan.Parse(str);
768-
#endif
769759
}
770760
}
771761

@@ -778,12 +768,10 @@ public NullableTimeSpanFormatter()
778768
this.innerFormatter = new TimeSpanFormatter();
779769
}
780770

781-
#if NETSTANDARD
782771
public NullableTimeSpanFormatter(string formatString)
783772
{
784773
this.innerFormatter = new TimeSpanFormatter(formatString);
785774
}
786-
#endif
787775

788776
public void Serialize(ref JsonWriter writer, TimeSpan? value, IJsonFormatterResolver formatterResolver)
789777
{

src/Elasticsearch.Net/Utf8Json/Formatters/DictionaryFormatter.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ protected override TDictionary Complete(ref TDictionary intermediateCollection)
188188
return intermediateCollection;
189189
}
190190
}
191-
191+
192192
internal sealed class DictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, Dictionary<TKey, TValue>.Enumerator, Dictionary<TKey, TValue>>
193193
{
194194
protected override void Add(ref Dictionary<TKey, TValue> collection, int index, TKey key, TValue value)
@@ -280,8 +280,6 @@ protected override SortedDictionary<TKey, TValue>.Enumerator GetSourceEnumerator
280280
}
281281
}
282282

283-
#if NETSTANDARD
284-
285283
internal sealed class ReadOnlyDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, ReadOnlyDictionary<TKey, TValue>>
286284
{
287285
protected override void Add(ref Dictionary<TKey, TValue> collection, int index, TKey key, TValue value)
@@ -332,8 +330,6 @@ protected override ConcurrentDictionary<TKey, TValue> Create()
332330
}
333331
}
334332

335-
#endif
336-
337333
internal sealed class NonGenericDictionaryFormatter<T> : IJsonFormatter<T>
338334
where T : class, System.Collections.IDictionary, new()
339335
{

src/Elasticsearch.Net/Utf8Json/Formatters/EnumFormatter.cs

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public static object GetSerializeDelegate(Type type, out bool isBoxed)
3838
{
3939
var underlyingType = Enum.GetUnderlyingType(type);
4040

41-
#if NETSTANDARD
4241
isBoxed = false;
4342
var dynamicMethod = new DynamicMethod("EnumSerializeByUnderlyingValue", null, new[] { typeof(JsonWriter).MakeByRefType(), type, typeof(IJsonFormatterResolver) }, type.Module, true);
4443
var il = dynamicMethod.GetILGenerator();
@@ -47,104 +46,19 @@ public static object GetSerializeDelegate(Type type, out bool isBoxed)
4746
il.Emit(OpCodes.Call, typeof(JsonWriter).GetRuntimeMethod("Write" + underlyingType.Name, new[] { underlyingType }));
4847
il.Emit(OpCodes.Ret);
4948
return dynamicMethod.CreateDelegate(typeof(JsonSerializeAction<>).MakeGenericType(type));
50-
#else
51-
// Boxed
52-
isBoxed = true;
53-
JsonSerializeAction<object> f;
54-
if (underlyingType == typeof(byte))
55-
{
56-
f = (ref JsonWriter writer, object value, IJsonFormatterResolver _) => writer.WriteByte((byte)value);
57-
}
58-
else if (underlyingType == typeof(sbyte))
59-
{
60-
f = (ref JsonWriter writer, object value, IJsonFormatterResolver _) => writer.WriteSByte((sbyte)value);
61-
}
62-
else if (underlyingType == typeof(short))
63-
{
64-
f = (ref JsonWriter writer, object value, IJsonFormatterResolver _) => writer.WriteInt16((short)value);
65-
}
66-
else if (underlyingType == typeof(ushort))
67-
{
68-
f = (ref JsonWriter writer, object value, IJsonFormatterResolver _) => writer.WriteUInt16((ushort)value);
69-
}
70-
else if (underlyingType == typeof(int))
71-
{
72-
f = (ref JsonWriter writer, object value, IJsonFormatterResolver _) => writer.WriteInt32((int)value);
73-
}
74-
else if (underlyingType == typeof(uint))
75-
{
76-
f = (ref JsonWriter writer, object value, IJsonFormatterResolver _) => writer.WriteUInt32((uint)value);
77-
}
78-
else if (underlyingType == typeof(long))
79-
{
80-
f = (ref JsonWriter writer, object value, IJsonFormatterResolver _) => writer.WriteInt64((long)value);
81-
}
82-
else if (underlyingType == typeof(ulong))
83-
{
84-
f = (ref JsonWriter writer, object value, IJsonFormatterResolver _) => writer.WriteUInt64((ulong)value);
85-
}
86-
else
87-
{
88-
throw new InvalidOperationException("Type is not Enum. Type:" + type);
89-
}
90-
return f;
91-
#endif
9249
}
9350

9451
public static object GetDeserializeDelegate(Type type, out bool isBoxed)
9552
{
9653
var underlyingType = Enum.GetUnderlyingType(type);
9754

98-
#if NETSTANDARD
9955
isBoxed = false;
10056
var dynamicMethod = new DynamicMethod("EnumDeserializeByUnderlyingValue", type, new[] { typeof(JsonReader).MakeByRefType(), typeof(IJsonFormatterResolver) }, type.Module, true);
10157
var il = dynamicMethod.GetILGenerator();
10258
il.Emit(OpCodes.Ldarg_0); // reader
10359
il.Emit(OpCodes.Call, typeof(JsonReader).GetRuntimeMethod("Read" + underlyingType.Name, Type.EmptyTypes));
10460
il.Emit(OpCodes.Ret);
10561
return dynamicMethod.CreateDelegate(typeof(JsonDeserializeFunc<>).MakeGenericType(type));
106-
#else
107-
// Boxed
108-
isBoxed = true;
109-
JsonDeserializeFunc<object> f;
110-
if (underlyingType == typeof(byte))
111-
{
112-
f = (ref JsonReader reader, IJsonFormatterResolver _) => (object)reader.ReadByte();
113-
}
114-
else if (underlyingType == typeof(sbyte))
115-
{
116-
f = (ref JsonReader reader, IJsonFormatterResolver _) => (object)reader.ReadSByte();
117-
}
118-
else if (underlyingType == typeof(short))
119-
{
120-
f = (ref JsonReader reader, IJsonFormatterResolver _) => (object)reader.ReadInt16();
121-
}
122-
else if (underlyingType == typeof(ushort))
123-
{
124-
f = (ref JsonReader reader, IJsonFormatterResolver _) => (object)reader.ReadUInt16();
125-
}
126-
else if (underlyingType == typeof(int))
127-
{
128-
f = (ref JsonReader reader, IJsonFormatterResolver _) => (object)reader.ReadInt32();
129-
}
130-
else if (underlyingType == typeof(uint))
131-
{
132-
f = (ref JsonReader reader, IJsonFormatterResolver _) => (object)reader.ReadUInt32();
133-
}
134-
else if (underlyingType == typeof(long))
135-
{
136-
f = (ref JsonReader reader, IJsonFormatterResolver _) => (object)reader.ReadInt64();
137-
}
138-
else if (underlyingType == typeof(ulong))
139-
{
140-
f = (ref JsonReader reader, IJsonFormatterResolver _) => (object)reader.ReadUInt64();
141-
}
142-
else
143-
{
144-
throw new InvalidOperationException("Type is not Enum. Type:" + type);
145-
}
146-
return f;
147-
#endif
14862
}
14963
}
15064

src/Elasticsearch.Net/Utf8Json/Formatters/StandardClassLibraryFormatters.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,7 @@ public KeyValuePair<TKey, TValue> Deserialize(ref JsonReader reader, IJsonFormat
424424
{
425425
var keyString = reader.ReadPropertyNameSegmentRaw();
426426
int key;
427-
#if NETSTANDARD
428427
StandardClassLibraryFormatterHelper.keyValuePairAutomata.TryGetValue(keyString, out key);
429-
#else
430-
StandardClassLibraryFormatterHelper.keyValuePairAutomata.TryGetValueSafe(keyString, out key);
431-
#endif
432428

433429
switch (key)
434430
{
@@ -501,11 +497,7 @@ internal sealed class TypeFormatter : IJsonFormatter<Type>
501497
{
502498
public static readonly TypeFormatter Default = new TypeFormatter();
503499

504-
#if NETSTANDARD
505500
static readonly Regex SubtractFullNameRegex = new Regex(@", Version=\d+.\d+.\d+.\d+, Culture=\w+, PublicKeyToken=\w+", RegexOptions.Compiled);
506-
#else
507-
static readonly Regex SubtractFullNameRegex = new Regex(@", Version=\d+.\d+.\d+.\d+, Culture=\w+, PublicKeyToken=\w+");
508-
#endif
509501

510502
bool serializeAssemblyQualifiedName;
511503
bool deserializeSubtractAssemblyQualifiedName;
@@ -552,8 +544,6 @@ public Type Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterR
552544
}
553545

554546

555-
#if NETSTANDARD
556-
557547
internal sealed class BigIntegerFormatter : IJsonFormatter<BigInteger>
558548
{
559549
public static readonly IJsonFormatter<BigInteger> Default = new BigIntegerFormatter();
@@ -638,11 +628,7 @@ public Lazy<T> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatt
638628

639629
// deserialize immediately(no delay, because capture byte[] causes memory leak)
640630
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(ref reader, formatterResolver);
641-
#if NETSTANDARD
642631
return new Lazy<T>(v.AsFunc());
643-
#else
644-
return new Lazy<T>(() => v);
645-
#endif
646632
}
647633
}
648634

@@ -686,7 +672,7 @@ public Task<T> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatt
686672
}
687673
}
688674

689-
#if VALUETASK
675+
#if NETSTANDARD2_1
690676
internal sealed class ValueTaskFormatter<T> : IJsonFormatter<ValueTask<T>>
691677
{
692678
public void Serialize(ref JsonWriter writer, ValueTask<T> value, IJsonFormatterResolver formatterResolver)
@@ -703,8 +689,6 @@ public ValueTask<T> Deserialize(ref JsonReader reader, IJsonFormatterResolver fo
703689
}
704690
#endif
705691

706-
#endif
707-
708692
internal static class StandardClassLibraryFormatterHelper
709693
{
710694
internal static readonly byte[][] keyValuePairName;

0 commit comments

Comments
 (0)