Skip to content

Commit 900ff65

Browse files
committed
The most significant changes were made to the Read method in the StoredValueConverter class. The previous implementation, which was not scalable and required additional code for each new property, was replaced with a new implementation that uses reflection to dynamically get the property information from the StoredValue class. This new approach is more scalable and requires less code for each new property. Additionally, error handling was added to the new implementation to throw an exception with a detailed message if an error occurs during deserialization.
Changes: 1. The System.Reflection namespace was added to the StoredValue.cs file. This namespace is used to retrieve information about assemblies, modules, members, parameters, and other entities in managed code by examining their metadata. 2. The Read method in the StoredValueConverter class was significantly refactored. The new implementation uses reflection to dynamically get the property information from the StoredValue class and deserialize the JSON data into the appropriate property. 3. Error handling was added to the Read method in the StoredValueConverter class. If an error occurs during deserialization, an exception is thrown with a message indicating that there was an error during deserialization. The original exception is also included for debugging purposes. 4. The Write method in the StoredValueConverter class was not modified. Signed-off-by: Murat Sanlisavas <[email protected]>
1 parent eb674a0 commit 900ff65

File tree

1 file changed

+28
-100
lines changed

1 file changed

+28
-100
lines changed

Casper.Network.SDK/Types/StoredValue.cs

Lines changed: 28 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Reflection;
34
using System.Text.Json;
45
using System.Text.Json.Serialization;
56

@@ -32,116 +33,43 @@ public class StoredValue
3233

3334
public class StoredValueConverter : JsonConverter<StoredValue>
3435
{
35-
public override StoredValue Read(
36-
ref Utf8JsonReader reader,
37-
Type typeToConvert,
38-
JsonSerializerOptions options)
36+
public override StoredValue Read
37+
(
38+
ref Utf8JsonReader reader,
39+
Type typeToConvert,
40+
JsonSerializerOptions options
41+
)
3942
{
40-
if (reader.TokenType != JsonTokenType.StartObject)
41-
throw new JsonException("Cannot deserialize StoredValue. StartObject expected");
43+
try
44+
{
45+
if (reader.TokenType != JsonTokenType.StartObject)
46+
throw new JsonException("Cannot deserialize StoredValue. StartObject expected.");
4247

43-
reader.Read(); // start object
48+
StoredValue storedValue = new StoredValue();
49+
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
50+
{
51+
if (reader.TokenType != JsonTokenType.PropertyName)
52+
throw new JsonException("Cannot deserialize StoredValue. PropertyName expected.");
4453

45-
if (reader.TokenType != JsonTokenType.PropertyName)
46-
throw new JsonException("Cannot deserialize StoredValue. PropertyName expected");
54+
var propertyName = reader.GetString();
55+
var propertyInfo = typeof(StoredValue).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
56+
if (propertyInfo == null)
57+
throw new JsonException($"Unknown property: {propertyName}.");
4758

48-
var propertyName = reader.GetString()?.ToLowerInvariant();
49-
reader.Read();
59+
reader.Read();
60+
var propertyValue = JsonSerializer.Deserialize(ref reader, propertyInfo.PropertyType, options);
61+
propertyInfo.SetValue(storedValue, propertyValue);
62+
}
5063

51-
if (propertyName == "contract")
52-
{
53-
var contract = JsonSerializer.Deserialize<Contract>(ref reader, options);
54-
reader.Read(); // end Contract object
55-
return new StoredValue()
56-
{
57-
Contract = contract
58-
};
59-
}
60-
else if (propertyName == "clvalue")
61-
{
62-
var clValue = JsonSerializer.Deserialize<CLValue>(ref reader, options);
63-
reader.Read(); // end CLValue object
64-
return new StoredValue()
65-
{
66-
CLValue = clValue
67-
};
68-
}
69-
else if (propertyName == "account")
70-
{
71-
var account = JsonSerializer.Deserialize<Account>(ref reader, options);
72-
reader.Read(); // end Account object
73-
return new StoredValue()
74-
{
75-
Account = account
76-
};
77-
}
78-
else if (propertyName == "contractwasm")
79-
{
80-
var wasmBytes = reader.GetString();
81-
reader.Read(); // wasm bytes
82-
return new StoredValue()
83-
{
84-
ContractWasm = wasmBytes
85-
};
86-
}
87-
else if (propertyName == "contractpackage")
88-
{
89-
var contractPackage = JsonSerializer.Deserialize<ContractPackage>(ref reader, options);
90-
reader.Read(); // end ContractPackage object
91-
return new StoredValue()
92-
{
93-
ContractPackage = contractPackage
94-
};
95-
}
96-
else if (propertyName == "transfer")
97-
{
98-
var transfer = JsonSerializer.Deserialize<Transfer>(ref reader, options);
99-
reader.Read(); // end Transfer object
100-
return new StoredValue()
101-
{
102-
Transfer = transfer
103-
};
104-
}
105-
else if (propertyName == "deployinfo")
106-
{
107-
var deployInfo = JsonSerializer.Deserialize<DeployInfo>(ref reader, options);
108-
reader.Read(); // end DeployInfo object
109-
return new StoredValue()
110-
{
111-
DeployInfo = deployInfo
112-
};
113-
}
114-
else if (propertyName == "erainfo")
115-
{
116-
var eraInfo = JsonSerializer.Deserialize<EraInfo>(ref reader, options);
117-
reader.Read(); // end EraInfo object
118-
return new StoredValue()
119-
{
120-
EraInfo = eraInfo
121-
};
122-
}
123-
else if (propertyName == "bid")
124-
{
125-
var bid = JsonSerializer.Deserialize<Bid>(ref reader, options);
126-
reader.Read(); // end Bid object
127-
return new StoredValue()
128-
{
129-
Bid = bid
130-
};
64+
return storedValue;
13165
}
132-
else if (propertyName == "withdraw")
66+
catch (Exception ex)
13367
{
134-
var withdraw = JsonSerializer.Deserialize<List<UnbondingPurse>>(ref reader, options);
135-
reader.Read(); // end Withdraw object
136-
return new StoredValue()
137-
{
138-
Withdraw = withdraw
139-
};
68+
throw new JsonException($"Cannot deserialize StoredValue. Error during deserialization.", ex);
14069
}
141-
142-
throw new JsonException("Cannot deserialize StoredValue. Inner object not yet supported");
14370
}
14471

72+
14573
public override void Write(
14674
Utf8JsonWriter writer,
14775
StoredValue value,

0 commit comments

Comments
 (0)