Skip to content

Commit 39eae43

Browse files
Add a converter to serialize properly DBNull value (#477)
* Add a converter to serialize properly the DBNull value according to the suggestion in dotnet/runtime#418 Move duplicate code in GxRedis.cs and GxMemcached.cs to GxClasses.csproj Adding the package reference System.Text.Json to GxClasses.csproj gets a conflict with System.Net.Http. Because of that, HttpClient used in MapFunctions.cs is replaced by HttpWebRequest for .netframework similar to the use in GXGeolocation. * Remove unused code. (cherry picked from commit fbfe50c)
1 parent 0f6b952 commit 39eae43

File tree

8 files changed

+68
-93
lines changed

8 files changed

+68
-93
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,6 @@ build
358358
/dotnet/src/dotnetcore/GxNetCoreStartup/netcoreapp3.1/GxNetCoreStartup.deps.json
359359
/dotnet/src/dotnetcore/Reor/net5.0/Reor.deps.json
360360
/dotnet/src/dotnetcore/Reor/netcoreapp3.1/Reor.deps.json
361+
/dotnet/src/dotnetcore/GxDataInitialization/net6.0/GXDataInitialization.deps.json
362+
/dotnet/src/dotnetcore/GxNetCoreStartup/net6.0/GxNetCoreStartup.deps.json
363+
/dotnet/src/dotnetcore/Reor/net6.0/Reor.deps.json

dotnet/src/dotnetframework/GxClasses/Data/GXDataCommon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4298,7 +4298,7 @@ public object GetValue(int i)
42984298
}
42994299
public virtual bool IsDBNull(int i)
43004300
{
4301-
return (block.Item(pos, i) == DBNull.Value) || (block.Item(pos, i) is DBNull);
4301+
return (block.Item(pos, i) == DBNull.Value) || (block.Item(pos, i) is DBNull || block.Item(pos, i) == null);
43024302
}
43034303
public char GetChar(int i)
43044304
{

dotnet/src/dotnetframework/GxClasses/Domain/GXGeolocation.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
using System.Globalization;
1111
using GeneXus;
1212
using GeneXus.Configuration;
13+
#if NETCORE
1314
using System.Net.Http;
15+
#endif
1416

1517
namespace GX
1618
{

dotnet/src/dotnetframework/GxClasses/GxClasses.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<PackageReference Include="Nustache" Version="1.16.0.10" />
1919
<PackageReference Include="SecurityCodeScan" PrivateAssets="all" Version="3.3.0" />
2020
<PackageReference Include="log4net" Version="2.0.11" />
21+
<PackageReference Include="System.Text.Json" Version="5.0.2" />
2122
<Reference Include="Microsoft.HostIntegration.MsDb2Client">
2223
<SpecificVersion>False</SpecificVersion>
2324
<HintPath>..\libs\Microsoft.HostIntegration.MsDb2Client.dll</HintPath>

dotnet/src/dotnetframework/GxClasses/Services/Caching/GxCache.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
using System.Collections.Generic;
1313
using GeneXus.Services;
1414
using System.Linq;
15+
using System.Security;
16+
using System.Text.Json.Serialization;
17+
using System.Text.Json;
1518
#if NETCORE
1619
using GxClasses.Helpers;
1720
using System.IO;
@@ -37,6 +40,58 @@ public interface ICacheService2 : ICacheService
3740
IDictionary<string, T> GetAll<T>(string cacheid, IEnumerable<string> keys);
3841
void SetAll<T>(string cacheid, IEnumerable<string> keys, IEnumerable<T> values, int durationMinutes = 0);
3942
}
43+
[SecurityCritical]
44+
public class DBNullConverter : JsonConverter<DBNull>
45+
{
46+
[SecurityCritical]
47+
public override DBNull Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
48+
{
49+
throw new NotSupportedException();
50+
}
51+
[SecurityCritical]
52+
public override void Write(Utf8JsonWriter writer, DBNull value, JsonSerializerOptions options)
53+
{
54+
writer.WriteNullValue();
55+
}
56+
}
57+
[SecurityCritical]
58+
public class ObjectToInferredTypesConverter : JsonConverter<object>
59+
{
60+
[SecurityCritical]
61+
public override bool CanConvert(Type typeToConvert)
62+
{
63+
return typeof(object) == typeToConvert;
64+
}
65+
[SecurityCritical]
66+
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
67+
{
68+
switch (reader.TokenType)
69+
{
70+
case JsonTokenType.True:
71+
return true;
72+
case JsonTokenType.False:
73+
return false;
74+
case JsonTokenType.Number:
75+
if (reader.TryGetInt64(out long l))
76+
return l;
77+
else return reader.GetDouble();
78+
case JsonTokenType.String:
79+
if (reader.TryGetDateTime(out DateTime datetime))
80+
return datetime;
81+
else return reader.GetString();
82+
default:
83+
using (JsonDocument document = JsonDocument.ParseValue(ref reader))
84+
{
85+
return document.RootElement.Clone().ToString();
86+
}
87+
}
88+
}
89+
[SecurityCritical]
90+
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
91+
{
92+
throw new NotImplementedException();
93+
}
94+
}
4095

4196
public class CacheFactory
4297
{

dotnet/src/dotnetframework/GxMaps/MapFunctions.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using GeneXus.Utils;
88
using System.IO;
99
using System.Globalization;
10-
using System.Net.Http;
1110

1211
namespace GeneXus.MapServices
1312
{
@@ -118,21 +117,6 @@ private static Assembly LoadAssembly(string fileName)
118117

119118
public static LocationInfo GetCurrentLocation(int minAccuracy, int timeout, bool includeHAndS, bool ignoreErrors)
120119
{
121-
if (Application.GxContext.IsHttpContext)
122-
{
123-
string ip = Application.GxContext.Current.GetRemoteAddress();
124-
using (HttpClient client = new HttpClient())
125-
{
126-
using (HttpResponseMessage response = client.GetAsync(new Uri("http://ipinfo.io/" + ip)).Result)
127-
{
128-
using (HttpContent content = response.Content)
129-
{
130-
string info = content.ReadAsStringAsync().Result;
131-
}
132-
}
133-
}
134-
return new LocationInfo { };
135-
}
136120
return new LocationInfo { };
137121
}
138122

dotnet/src/dotnetframework/Providers/Cache/GxMemcached/GxMemcached.cs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ static string Serialize(object o)
215215
{
216216
return null;
217217
}
218-
return JsonSerializer.Serialize(o);
218+
JsonSerializerOptions opts = new JsonSerializerOptions();
219+
opts.Converters.Add(new DBNullConverter());
220+
return JsonSerializer.Serialize(o, opts);
219221
}
220222
[SecurityCritical]
221223
static T Deserialize<T>(string value)
@@ -229,43 +231,4 @@ static T Deserialize<T>(string value)
229231
return JsonSerializer.Deserialize<T>(value, opts);
230232
}
231233
}
232-
[SecurityCritical]
233-
public class ObjectToInferredTypesConverter : JsonConverter<object>
234-
{
235-
[SecurityCritical]
236-
public override bool CanConvert(Type typeToConvert)
237-
{
238-
return typeof(object) == typeToConvert;
239-
}
240-
[SecurityCritical]
241-
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
242-
{
243-
switch (reader.TokenType)
244-
{
245-
case JsonTokenType.True:
246-
return true;
247-
case JsonTokenType.False:
248-
return false;
249-
case JsonTokenType.Number:
250-
if (reader.TryGetInt64(out long l))
251-
return l;
252-
else return reader.GetDouble();
253-
case JsonTokenType.String:
254-
if (reader.TryGetDateTime(out DateTime datetime))
255-
return datetime;
256-
else return reader.GetString();
257-
default:
258-
using (JsonDocument document = JsonDocument.ParseValue(ref reader))
259-
{
260-
return document.RootElement.Clone().ToString();
261-
}
262-
}
263-
}
264-
[SecurityCritical]
265-
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
266-
{
267-
throw new NotImplementedException();
268-
}
269-
}
270-
271234
}

dotnet/src/dotnetframework/Providers/Cache/GxRedis/GxRedis.cs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ static string Serialize(object o)
205205
{
206206
return null;
207207
}
208-
return JsonSerializer.Serialize(o);
208+
JsonSerializerOptions opts = new JsonSerializerOptions();
209+
opts.Converters.Add(new DBNullConverter());
210+
return JsonSerializer.Serialize(o, opts);
209211
}
210212

211213
static T Deserialize<T>(string value)
@@ -219,39 +221,4 @@ static T Deserialize<T>(string value)
219221
return JsonSerializer.Deserialize<T>(value, opts);
220222
}
221223
}
222-
public class ObjectToInferredTypesConverter: JsonConverter<object>
223-
{
224-
public override bool CanConvert(Type typeToConvert)
225-
{
226-
return typeof(object) == typeToConvert;
227-
}
228-
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
229-
{
230-
switch (reader.TokenType)
231-
{
232-
case JsonTokenType.True:
233-
return true;
234-
case JsonTokenType.False:
235-
return false;
236-
case JsonTokenType.Number:
237-
if (reader.TryGetInt64(out long l))
238-
return l;
239-
else return reader.GetDouble();
240-
case JsonTokenType.String:
241-
if (reader.TryGetDateTime(out DateTime datetime))
242-
return datetime;
243-
else return reader.GetString();
244-
default:
245-
using (JsonDocument document = JsonDocument.ParseValue(ref reader))
246-
{
247-
return document.RootElement.Clone().ToString();
248-
}
249-
}
250-
}
251-
252-
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
253-
{
254-
throw new NotImplementedException();
255-
}
256-
}
257224
}

0 commit comments

Comments
 (0)