Skip to content

Commit 2ae4fad

Browse files
committed
Remote LocalValue.ConvertFrom(JsonNode), expand ConvertFrom(object)
1 parent 02ace40 commit 2ae4fad

File tree

2 files changed

+45
-51
lines changed

2 files changed

+45
-51
lines changed

dotnet/src/webdriver/BiDi/BiDiException.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ public class BiDiException : Exception
2626
public BiDiException(string message) : base(message)
2727
{
2828
}
29+
public BiDiException(string? message, Exception? innerException) : base(message, innerException)
30+
{
31+
}
2932
}

dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919

2020
using System;
2121
using System.Collections.Generic;
22-
using System.Diagnostics;
2322
using System.Linq;
2423
using System.Numerics;
25-
using System.Text.Json.Nodes;
2624
using System.Text.Json.Serialization;
27-
using System.Text.RegularExpressions;
2825

2926
namespace OpenQA.Selenium.BiDi.Modules.Script;
3027

@@ -75,73 +72,67 @@ public static LocalValue ConvertFrom(object? value)
7572
case string str:
7673
return new StringLocalValue(str);
7774

78-
case IEnumerable<object?> list:
79-
return new ArrayLocalValue(list.Select(ConvertFrom).ToList());
80-
81-
case object:
75+
case IDictionary<string, string?> dictionary:
8276
{
83-
var type = value.GetType();
77+
var bidiObject = new List<List<LocalValue>>(dictionary.Count);
8478

85-
var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
86-
87-
List<List<LocalValue>> values = [];
88-
89-
foreach (var property in properties)
79+
foreach (var item in dictionary)
9080
{
91-
values.Add([property.Name, ConvertFrom(property.GetValue(value))]);
81+
bidiObject.Add([new StringLocalValue(item.Key), ConvertFrom(item.Value)]);
9282
}
9383

94-
return new ObjectLocalValue(values);
84+
return new ObjectLocalValue(bidiObject);
9585
}
96-
}
97-
}
98-
99-
public static LocalValue ConvertFrom(JsonNode? node)
100-
{
101-
if (node is null)
102-
{
103-
return new NullLocalValue();
104-
}
105-
106-
switch (node.GetValueKind())
107-
{
108-
case System.Text.Json.JsonValueKind.Null:
109-
return new NullLocalValue();
11086

111-
case System.Text.Json.JsonValueKind.True:
112-
return new BooleanLocalValue(true);
87+
case IDictionary<string, object?> dictionary:
88+
{
89+
var bidiObject = new List<List<LocalValue>>(dictionary.Count);
11390

114-
case System.Text.Json.JsonValueKind.False:
115-
return new BooleanLocalValue(false);
91+
foreach (var item in dictionary)
92+
{
93+
bidiObject.Add([new StringLocalValue(item.Key), ConvertFrom(item.Value)]);
94+
}
11695

117-
case System.Text.Json.JsonValueKind.String:
118-
return new StringLocalValue(node.ToString());
96+
return new ObjectLocalValue(bidiObject);
97+
}
11998

120-
case System.Text.Json.JsonValueKind.Number:
99+
case IDictionary<int, object?> dictionary:
121100
{
122-
var numberString = node.ToString();
123-
124-
var numberAsDouble = double.Parse(numberString);
101+
var bidiObject = new List<List<LocalValue>>(dictionary.Count);
125102

126-
if (double.IsInfinity(numberAsDouble))
103+
foreach (var item in dictionary)
127104
{
128-
// Numbers outside of Int64's range will successfully parse, but become +- Infinity
129-
// We can retain the value using a BigInt
130-
return new BigIntLocalValue(numberString);
105+
bidiObject.Add([ConvertFrom(item.Key), ConvertFrom(item.Value)]);
131106
}
132107

133-
return new NumberLocalValue(numberAsDouble);
108+
return new MapLocalValue(bidiObject);
134109
}
135110

136-
case System.Text.Json.JsonValueKind.Array:
137-
return new ArrayLocalValue(node.AsArray().Select(ConvertFrom));
111+
case IEnumerable<object?> list:
112+
return new ArrayLocalValue(list.Select(ConvertFrom).ToList());
113+
114+
case object:
115+
{
116+
const System.Reflection.BindingFlags Flags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance;
117+
var properties = value.GetType().GetProperties(Flags);
138118

139-
case System.Text.Json.JsonValueKind.Object:
140-
var convertedToListForm = node.AsObject().Select(property => new LocalValue[] { new StringLocalValue(property.Key), ConvertFrom(property.Value) }).ToList();
141-
return new ObjectLocalValue(convertedToListForm);
119+
var values = new List<List<LocalValue>>(properties.Length);
120+
foreach (var property in properties)
121+
{
122+
object? propertyValue;
123+
try
124+
{
125+
propertyValue = property.GetValue(value);
126+
}
127+
catch (Exception ex)
128+
{
129+
throw new BiDiException($"Could not retrieve property {property.Name} from {property.DeclaringType}", ex);
130+
}
131+
values.Add([property.Name, ConvertFrom(propertyValue)]);
132+
}
142133

143-
default:
144-
throw new InvalidOperationException("Invalid JSON node");
134+
return new ObjectLocalValue(values);
135+
}
145136
}
146137
}
147138
}

0 commit comments

Comments
 (0)