Skip to content

Commit

Permalink
Add a converter to serialize properly DBNull value (#477)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
claudiamurialdo committed Oct 26, 2021
1 parent 0f6b952 commit 39eae43
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,6 @@ build
/dotnet/src/dotnetcore/GxNetCoreStartup/netcoreapp3.1/GxNetCoreStartup.deps.json
/dotnet/src/dotnetcore/Reor/net5.0/Reor.deps.json
/dotnet/src/dotnetcore/Reor/netcoreapp3.1/Reor.deps.json
/dotnet/src/dotnetcore/GxDataInitialization/net6.0/GXDataInitialization.deps.json
/dotnet/src/dotnetcore/GxNetCoreStartup/net6.0/GxNetCoreStartup.deps.json
/dotnet/src/dotnetcore/Reor/net6.0/Reor.deps.json
2 changes: 1 addition & 1 deletion dotnet/src/dotnetframework/GxClasses/Data/GXDataCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4298,7 +4298,7 @@ public object GetValue(int i)
}
public virtual bool IsDBNull(int i)
{
return (block.Item(pos, i) == DBNull.Value) || (block.Item(pos, i) is DBNull);
return (block.Item(pos, i) == DBNull.Value) || (block.Item(pos, i) is DBNull || block.Item(pos, i) == null);
}
public char GetChar(int i)
{
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/dotnetframework/GxClasses/Domain/GXGeolocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using System.Globalization;
using GeneXus;
using GeneXus.Configuration;
#if NETCORE
using System.Net.Http;
#endif

namespace GX
{
Expand Down
1 change: 1 addition & 0 deletions dotnet/src/dotnetframework/GxClasses/GxClasses.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="Nustache" Version="1.16.0.10" />
<PackageReference Include="SecurityCodeScan" PrivateAssets="all" Version="3.3.0" />
<PackageReference Include="log4net" Version="2.0.11" />
<PackageReference Include="System.Text.Json" Version="5.0.2" />
<Reference Include="Microsoft.HostIntegration.MsDb2Client">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\libs\Microsoft.HostIntegration.MsDb2Client.dll</HintPath>
Expand Down
55 changes: 55 additions & 0 deletions dotnet/src/dotnetframework/GxClasses/Services/Caching/GxCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
using System.Collections.Generic;
using GeneXus.Services;
using System.Linq;
using System.Security;
using System.Text.Json.Serialization;
using System.Text.Json;
#if NETCORE
using GxClasses.Helpers;
using System.IO;
Expand All @@ -37,6 +40,58 @@ public interface ICacheService2 : ICacheService
IDictionary<string, T> GetAll<T>(string cacheid, IEnumerable<string> keys);
void SetAll<T>(string cacheid, IEnumerable<string> keys, IEnumerable<T> values, int durationMinutes = 0);
}
[SecurityCritical]
public class DBNullConverter : JsonConverter<DBNull>
{
[SecurityCritical]
public override DBNull Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotSupportedException();
}
[SecurityCritical]
public override void Write(Utf8JsonWriter writer, DBNull value, JsonSerializerOptions options)
{
writer.WriteNullValue();
}
}
[SecurityCritical]
public class ObjectToInferredTypesConverter : JsonConverter<object>
{
[SecurityCritical]
public override bool CanConvert(Type typeToConvert)
{
return typeof(object) == typeToConvert;
}
[SecurityCritical]
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.True:
return true;
case JsonTokenType.False:
return false;
case JsonTokenType.Number:
if (reader.TryGetInt64(out long l))
return l;
else return reader.GetDouble();
case JsonTokenType.String:
if (reader.TryGetDateTime(out DateTime datetime))
return datetime;
else return reader.GetString();
default:
using (JsonDocument document = JsonDocument.ParseValue(ref reader))
{
return document.RootElement.Clone().ToString();
}
}
}
[SecurityCritical]
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}

public class CacheFactory
{
Expand Down
16 changes: 0 additions & 16 deletions dotnet/src/dotnetframework/GxMaps/MapFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using GeneXus.Utils;
using System.IO;
using System.Globalization;
using System.Net.Http;

namespace GeneXus.MapServices
{
Expand Down Expand Up @@ -118,21 +117,6 @@ private static Assembly LoadAssembly(string fileName)

public static LocationInfo GetCurrentLocation(int minAccuracy, int timeout, bool includeHAndS, bool ignoreErrors)
{
if (Application.GxContext.IsHttpContext)
{
string ip = Application.GxContext.Current.GetRemoteAddress();
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = client.GetAsync(new Uri("http://ipinfo.io/" + ip)).Result)
{
using (HttpContent content = response.Content)
{
string info = content.ReadAsStringAsync().Result;
}
}
}
return new LocationInfo { };
}
return new LocationInfo { };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ static string Serialize(object o)
{
return null;
}
return JsonSerializer.Serialize(o);
JsonSerializerOptions opts = new JsonSerializerOptions();
opts.Converters.Add(new DBNullConverter());
return JsonSerializer.Serialize(o, opts);
}
[SecurityCritical]
static T Deserialize<T>(string value)
Expand All @@ -229,43 +231,4 @@ static T Deserialize<T>(string value)
return JsonSerializer.Deserialize<T>(value, opts);
}
}
[SecurityCritical]
public class ObjectToInferredTypesConverter : JsonConverter<object>
{
[SecurityCritical]
public override bool CanConvert(Type typeToConvert)
{
return typeof(object) == typeToConvert;
}
[SecurityCritical]
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.True:
return true;
case JsonTokenType.False:
return false;
case JsonTokenType.Number:
if (reader.TryGetInt64(out long l))
return l;
else return reader.GetDouble();
case JsonTokenType.String:
if (reader.TryGetDateTime(out DateTime datetime))
return datetime;
else return reader.GetString();
default:
using (JsonDocument document = JsonDocument.ParseValue(ref reader))
{
return document.RootElement.Clone().ToString();
}
}
}
[SecurityCritical]
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}

}
39 changes: 3 additions & 36 deletions dotnet/src/dotnetframework/Providers/Cache/GxRedis/GxRedis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ static string Serialize(object o)
{
return null;
}
return JsonSerializer.Serialize(o);
JsonSerializerOptions opts = new JsonSerializerOptions();
opts.Converters.Add(new DBNullConverter());
return JsonSerializer.Serialize(o, opts);
}

static T Deserialize<T>(string value)
Expand All @@ -219,39 +221,4 @@ static T Deserialize<T>(string value)
return JsonSerializer.Deserialize<T>(value, opts);
}
}
public class ObjectToInferredTypesConverter: JsonConverter<object>
{
public override bool CanConvert(Type typeToConvert)
{
return typeof(object) == typeToConvert;
}
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.True:
return true;
case JsonTokenType.False:
return false;
case JsonTokenType.Number:
if (reader.TryGetInt64(out long l))
return l;
else return reader.GetDouble();
case JsonTokenType.String:
if (reader.TryGetDateTime(out DateTime datetime))
return datetime;
else return reader.GetString();
default:
using (JsonDocument document = JsonDocument.ParseValue(ref reader))
{
return document.RootElement.Clone().ToString();
}
}
}

public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}

0 comments on commit 39eae43

Please sign in to comment.