Skip to content

Commit

Permalink
🐛 fix object in dictionary deserialize issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnkwlp committed Jan 23, 2024
1 parent 7ea3d82 commit 1d03b48
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 122 deletions.
46 changes: 0 additions & 46 deletions app/src/Passingwind.WorkflowApp.Web/SwaggerEnumDescriptions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private static void ConfigureNewtonsoftJsonSerializerSettings(JsonSerializerSett
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
settings.NullValueHandling = NullValueHandling.Ignore;
settings.DefaultValueHandling = DefaultValueHandling.Include;
settings.TypeNameHandling = TypeNameHandling.None; // As default, we want output the type name of json object
settings.TypeNameHandling = TypeNameHandling.None;
settings.TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple;
settings.PreserveReferencesHandling = PreserveReferencesHandling.None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS

public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
writer.WriteRawValue(Newtonsoft.Json.JsonConvert.SerializeObject(value, JsonSerializerSettings));
writer.WriteRawValue(JsonConvert.SerializeObject(value, JsonSerializerSettings));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using JsonException = System.Text.Json.JsonException;

namespace Passingwind.Abp.ElsaModule.SystemTextJson.Converters;

public class ObjectToDictionaryConverter : JsonConverter<Dictionary<string, object>>
public class ObjectToDictionaryConverter : System.Text.Json.Serialization.JsonConverter<Dictionary<string, object>>
{
public override Dictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Expand Down Expand Up @@ -81,73 +82,6 @@ private object ExtractValue(ref Utf8JsonReader reader, JsonSerializerOptions opt

public override void Write(Utf8JsonWriter writer, Dictionary<string, object> value, JsonSerializerOptions options)
{
writer.WriteStartObject();

foreach (var key in value.Keys)
{
HandleValue(writer, key, value[key]);
}

writer.WriteEndObject();
}

private static void HandleValue(Utf8JsonWriter writer, string key, object objectValue)
{
if (key != null)
{
writer.WritePropertyName(key);
}

switch (objectValue)
{
case string stringValue:
writer.WriteStringValue(stringValue);
break;
case DateTime dateTime:
writer.WriteStringValue(dateTime);
break;
case long longValue:
writer.WriteNumberValue(longValue);
break;
case int intValue:
writer.WriteNumberValue(intValue);
break;
case float floatValue:
writer.WriteNumberValue(floatValue);
break;
case double doubleValue:
writer.WriteNumberValue(doubleValue);
break;
case decimal decimalValue:
writer.WriteNumberValue(decimalValue);
break;
case bool boolValue:
writer.WriteBooleanValue(boolValue);
break;
case Dictionary<string, object> dict:
writer.WriteStartObject();
foreach (var item in dict)
{
HandleValue(writer, item.Key, item.Value);
}
writer.WriteEndObject();
break;
case object[] array:
writer.WriteStartArray();
foreach (var item in array)
{
HandleValue(writer, item);
}
writer.WriteEndArray();
break;
default:
writer.WriteNullValue();
break;
}
}

private static void HandleValue(Utf8JsonWriter writer, object value)
{
HandleValue(writer, null, value);
writer.WriteRawValue(JsonConvert.SerializeObject(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace Passingwind.Abp.ElsaModule.EntityFrameworkCore;

public class EfCoreJsonValueConverter<TModel> : ValueConverter<TModel, string>
{
public static Func<JsonSerializerSettings> Create;
public static Func<JsonSerializerSettings> CreateSettingsFunc;

static EfCoreJsonValueConverter()
{
Create = () =>
CreateSettingsFunc = () =>
{
var settings = new JsonSerializerSettings()
{
Expand Down Expand Up @@ -56,13 +56,13 @@ private static void JsonErrorHandle(object sender, ErrorEventArgs e)

private static string Serialize<T>(T value)
{
var settings = Create();
var settings = CreateSettingsFunc();
return JsonConvert.SerializeObject(value, Formatting.None, settings);
}

private static T Deserialize<T>(string json)
{
var settings = Create();
var settings = CreateSettingsFunc();
return JsonConvert.DeserializeObject<T>(json, settings);
}
}
1 change: 1 addition & 0 deletions src/Passingwind.ElsaDesigner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
},
"devDependencies": {
"@ant-design/pro-cli": "^2.1.5",
"@babel/runtime": "^7.23.8",
"@playwright/test": "^1.17.0",
"@types/classnames": "^2.3.1",
"@types/express": "^4.17.14",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const Edit: React.FC = () => {
label={intl.formatMessage({ id: 'page.globalCode.field.description' })}
name="description"
rules={[{ max: 128 }]}
width="lg"
fieldProps={{ rows: 1 }}
/>
<ProFormRadio.Group
Expand Down

0 comments on commit 1d03b48

Please sign in to comment.