Skip to content

Commit b3d37a7

Browse files
committed
refactor: simplify JSON element conversion logic in ObjectHelper
1 parent 99f54ad commit b3d37a7

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/Weaviate.Client/ObjectHelper.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Google.Protobuf;
88
using Google.Protobuf.Reflection;
99
using Google.Protobuf.WellKnownTypes;
10-
using Weaviate.Client.Models;
1110
using Weaviate.Client.Serialization;
1211

1312
namespace Weaviate.Client;
@@ -22,37 +21,35 @@ internal class ObjectHelper
2221
}
2322

2423
var element = e.Value;
25-
2624
if (element.ValueKind == JsonValueKind.Object)
2725
{
2826
var expando = new ExpandoObject();
29-
var dictionary = (IDictionary<string, object?>)expando;
30-
31-
foreach (var property in element.EnumerateObject())
27+
var dict = (IDictionary<string, object?>)expando;
28+
foreach (var prop in element.EnumerateObject())
3229
{
33-
dictionary[property.Name] = ConvertJsonElement(property.Value);
30+
dict[prop.Name] = ConvertJsonElement(prop.Value);
3431
}
35-
3632
return expando;
3733
}
38-
3934
if (element.ValueKind == JsonValueKind.Array)
4035
{
41-
return element
42-
.EnumerateArray()
43-
.OfType<JsonElement?>()
44-
.Select(ConvertJsonElement)
45-
.ToArray();
36+
var list = new List<object?>();
37+
foreach (var item in element.EnumerateArray())
38+
{
39+
list.Add(ConvertJsonElement(item));
40+
}
41+
return list;
4642
}
47-
4843
return element.ValueKind switch
4944
{
5045
JsonValueKind.String => element.GetString(),
51-
JsonValueKind.Number => element.TryGetInt32(out int i) ? i : element.GetDouble(),
46+
JsonValueKind.Number => element.TryGetInt64(out var l) ? l
47+
: element.TryGetDouble(out var d) ? d
48+
: element.GetDecimal(),
5249
JsonValueKind.True => true,
5350
JsonValueKind.False => false,
54-
JsonValueKind.Null => null,
55-
_ => element.ToString(),
51+
JsonValueKind.Null or JsonValueKind.Undefined => null,
52+
_ => element.GetRawText(),
5653
};
5754
}
5855

0 commit comments

Comments
 (0)