|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Reflection; |
3 | 4 | using System.Text.Json;
|
4 | 5 | using System.Text.Json.Serialization;
|
5 | 6 |
|
@@ -32,116 +33,43 @@ public class StoredValue
|
32 | 33 |
|
33 | 34 | public class StoredValueConverter : JsonConverter<StoredValue>
|
34 | 35 | {
|
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 | + ) |
39 | 42 | {
|
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."); |
42 | 47 |
|
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."); |
44 | 53 |
|
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}."); |
47 | 58 |
|
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 | + } |
50 | 63 |
|
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; |
131 | 65 | }
|
132 |
| - else if (propertyName == "withdraw") |
| 66 | + catch (Exception ex) |
133 | 67 | {
|
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); |
140 | 69 | }
|
141 |
| - |
142 |
| - throw new JsonException("Cannot deserialize StoredValue. Inner object not yet supported"); |
143 | 70 | }
|
144 | 71 |
|
| 72 | + |
145 | 73 | public override void Write(
|
146 | 74 | Utf8JsonWriter writer,
|
147 | 75 | StoredValue value,
|
|
0 commit comments