Appearance
| 1 | @@IF.dotnet() | |
| 2 | ||
| 3 | namespace Analysis.Protocol is | |
| 4 | use TYPE = System.Type | |
| 5 | ||
| 6 | use System.Reflection.BindingFlags | |
| 7 | use System.Reflection.ConstructorInfo | |
| 8 | use System.Reflection.FieldInfo | |
| 9 | use System.Reflection.MemberInfo | |
| 10 | use System.Reflection.PropertyInfo | |
| 11 | ||
| 12 | use System.Text.Json.JsonDocument | |
| 13 | use System.Text.Json.JsonElement | |
| 14 | use System.Text.Json.JsonValueKind | |
| 15 | use System.Text.Json.Nodes.JsonArray | |
| 16 | use System.Text.Json.Nodes.JsonNode | |
| 17 | use System.Text.Json.Nodes.JsonObject | |
| 18 | use System.Text.Json.Nodes.JsonValue | |
| 19 | use System.Text.Json.Serialization.JsonDerivedTypeAttribute | |
| 20 | use System.Text.Json.Serialization.JsonPolymorphicAttribute | |
| 21 | ||
| 22 | use Collections.LIST | |
| 23 | use Ghul.Pipes | |
| 24 | ||
| 25 | // Reads and writes the protocol's types by reflection over their public | |
| 26 | // members, giving the JSON that System.Text.Json gives under | |
| 27 | // JSON_OPTIONS: public properties and fields, none whose name starts | |
| 28 | // with `$`, and the discriminator first wherever the declared type is | |
| 29 | // a polymorphic base. System.Text.Json's own reflection path builds a | |
| 30 | // converter specialised to each constructor's parameter types, which a | |
| 31 | // Native AOT build has no code for. | |
| 32 | class REFLECTION_JSON is | |
| 33 | _members: System.Collections.Concurrent.ConcurrentDictionary[TYPE, MemberInfo[]] static | |
| 34 | ||
| 35 | init() static is | |
| 36 | _members = System.Collections.Concurrent.ConcurrentDictionary[TYPE, MemberInfo[]]() | |
| 37 | si | |
| 38 | ||
| 39 | // `value`, written as a value of type `declared`, on one line. | |
| 40 | write(value: object?, declared: TYPE) -> string static is | |
| 41 | if let node: JsonNode = to_node(value, declared) then | |
| 42 | return node.to_json_string(null) | |
| 43 | fi | |
| 44 | ||
| 45 | return "null" | |
| 46 | si | |
| 47 | ||
| 48 | // The value of type `type` that one line of JSON holds. | |
| 49 | read(json: string, type: TYPE) -> object? static is | |
| 50 | let document = JsonDocument.parse(json, _) | |
| 51 | ||
| 52 | try | |
| 53 | return from_element(document.root_element, type) | |
| 54 | finally | |
| 55 | document.dispose() | |
| 56 | yrt | |
| 57 | si | |
| 58 | ||
| 59 | to_node(value: object?, declared: TYPE) -> JsonNode? static is | |
| 60 | if !value? then | |
| 61 | return null | |
| 62 | fi | |
| 63 | ||
| 64 | if let text: string = value then | |
| 65 | return JsonValue.create(text, null) | |
| 66 | elif let flag: bool = value then | |
| 67 | return JsonValue.create(flag, null) | |
| 68 | elif let number: int = value then | |
| 69 | return JsonValue.create(number, null) | |
| 70 | elif let number: long = value then | |
| 71 | return JsonValue.create(number, null) | |
| 72 | elif let number: double = value then | |
| 73 | return JsonValue.create(number, null) | |
| 74 | fi | |
| 75 | ||
| 76 | if let sequence: Collections.NonGenericIterable = value then | |
| 77 | let element_type = element_type_of(declared) ?? element_type_of(value.get_type()) ?? typeof object | |
| 78 | let result = JsonArray(null) | |
| 79 | ||
| 80 | for element in System.Linq.Enumerable.cast[object](sequence) do | |
| 81 | result.add(to_node(element, element_type)) | |
| 82 | od | |
| 83 | ||
| 84 | return result | |
| 85 | fi | |
| 86 | ||
| 87 | let result = JsonObject(null) | |
| 88 | let actual = value.get_type() | |
| 89 | ||
| 90 | if let polymorphic: JsonPolymorphicAttribute = polymorphic_attribute(declared) then | |
| 91 | result.add( | |
| 92 | polymorphic.type_discriminator_property_name ?? "$type", | |
| 93 | JsonValue.create(discriminator_of(declared, actual), null)) | |
| 94 | ||
| 95 | _write_members(result, value, actual) | |
| 96 | elif declared =~ typeof object \/ declared.is_abstract \/ declared.is_interface then | |
| 97 | _write_members(result, value, actual) | |
| 98 | else | |
| 99 | _write_members(result, value, declared) | |
| 100 | fi | |
| 101 | ||
| 102 | return result | |
| 103 | si | |
| 104 | ||
| 105 | _write_members(result: JsonObject, value: object, type: TYPE) static is | |
| 106 | for member in members_of(type) do | |
| 107 | if let property: PropertyInfo = member then | |
| 108 | result.add(property.name, to_node(property.get_value(value), property.property_type)) | |
| 109 | elif let slot: FieldInfo = member then | |
| 110 | result.add(slot.name, to_node(slot.get_value(value), slot.field_type)) | |
| 111 | fi | |
| 112 | od | |
| 113 | si | |
| 114 | ||
| 115 | from_element(element: JsonElement, type: TYPE) -> object? static is | |
| 116 | if element.value_kind == JsonValueKind.NULL then | |
| 117 | return null | |
| 118 | fi | |
| 119 | ||
| 120 | let target = System.Nullable.get_underlying_type(type) ?? type | |
| 121 | ||
| 122 | if target =~ typeof string then | |
| 123 | return element.get_string() | |
| 124 | elif target =~ typeof bool then | |
| 125 | return element.get_boolean() | |
| 126 | elif target =~ typeof int then | |
| 127 | return element.get_int32() | |
| 128 | elif target =~ typeof long then | |
| 129 | return element.get_int64() | |
| 130 | elif target =~ typeof double then | |
| 131 | return element.get_double() | |
| 132 | fi | |
| 133 | ||
| 134 | if let polymorphic: JsonPolymorphicAttribute = polymorphic_attribute(target) then | |
| 135 | let name = polymorphic.type_discriminator_property_name ?? "$type" | |
| 136 | let discriminator: JsonElement mut | |
| 137 | ||
| 138 | if !element.try_get_property(name, discriminator ref) then | |
| 139 | throw System.Text.Json.JsonException("no {name} property") | |
| 140 | fi | |
| 141 | ||
| 142 | return _read_object(element, derived_type_of(target, discriminator.get_string() ?? ""), name) | |
| 143 | fi | |
| 144 | ||
| 145 | if target.is_array then | |
| 146 | let element_type = target.get_element_type()! | |
| 147 | let result = System.Array.create_instance(element_type, element.get_array_length()) | |
| 148 | let index mut = 0 | |
| 149 | ||
| 150 | for item in element.enumerate_array() do | |
| 151 | result.set_value(from_element(item, element_type), index) | |
| 152 | index = index + 1 | |
| 153 | od | |
| 154 | ||
| 155 | return result | |
| 156 | fi | |
| 157 | ||
| 158 | if let element_type: TYPE = element_type_of(target) then | |
| 159 | let list = cast DotNet.NonGeneric.Collections.IList?( | |
| 160 | System.Activator.create_instance((typeof LIST[_]).make_generic_type([element_type])))! | |
| 161 | ||
| 162 | for item in element.enumerate_array() do | |
| 163 | list.add(from_element(item, element_type)) | |
| 164 | od | |
| 165 | ||
| 166 | return list | |
| 167 | fi | |
| 168 | ||
| 169 | return _read_object(element, target, null) | |
| 170 | si | |
| 171 | ||
| 172 | _read_object(element: JsonElement, type: TYPE, discriminator: string?) -> object static is | |
| 173 | let constructors = type.get_constructors() | |
| 174 | ||
| 175 | let chosen mut = constructors |> find(c => c.get_parameters().count == 0) | |
| 176 | ||
| 177 | if !chosen? then | |
| 178 | if constructors.count != 1 then | |
| 179 | throw System.Text.Json.JsonException("{type.full_name} has no constructor to deserialize with") | |
| 180 | fi | |
| 181 | ||
| 182 | chosen = constructors[0] | |
| 183 | fi | |
| 184 | ||
| 185 | let parameters = chosen.get_parameters() | |
| 186 | let arguments = Collections.LIST[object?]() | |
| 187 | let consumed = Collections.SET[string]() | |
| 188 | ||
| 189 | for parameter in parameters do | |
| 190 | let found mut = false | |
| 191 | ||
| 192 | for property in element.enumerate_object() do | |
| 193 | if !found /\ string.equals(property.name, parameter.name, Ghul.StringComparison.ORDINAL_IGNORE_CASE) then | |
| 194 | arguments.add(from_element(property.value, parameter.parameter_type)) | |
| 195 | consumed.add(property.name) | |
| 196 | found = true | |
| 197 | fi | |
| 198 | od | |
| 199 | ||
| 200 | if !found then | |
| 201 | arguments.add( | |
| 202 | if parameter.parameter_type.is_value_type then | |
| 203 | System.Activator.create_instance(parameter.parameter_type) | |
| 204 | else | |
| 205 | null | |
| 206 | fi) | |
| 207 | fi | |
| 208 | od | |
| 209 | ||
| 210 | let result = chosen.invoke(arguments.to_array()) | |
| 211 | ||
| 212 | for property in element.enumerate_object() do | |
| 213 | if !consumed.contains(property.name) /\ property.name !~ (discriminator ?? "") then | |
| 214 | _set_member(result, type, property.name, property.value) | |
| 215 | fi | |
| 216 | od | |
| 217 | ||
| 218 | return result | |
| 219 | si | |
| 220 | ||
| 221 | _set_member(target: object, type: TYPE, name: string, value: JsonElement) static is | |
| 222 | for member in members_of(type) do | |
| 223 | if member.name =~ name then | |
| 224 | if let property: PropertyInfo = member then | |
| 225 | if property.get_set_method(false)? then | |
| 226 | property.set_value(target, from_element(value, property.property_type)) | |
| 227 | fi | |
| 228 | elif let slot: FieldInfo = member then | |
| 229 | if !slot.is_init_only then | |
| 230 | slot.set_value(target, from_element(value, slot.field_type)) | |
| 231 | fi | |
| 232 | fi | |
| 233 | ||
| 234 | return | |
| 235 | fi | |
| 236 | od | |
| 237 | si | |
| 238 | ||
| 239 | // The members System.Text.Json reads and writes on `type`, in the | |
| 240 | // order it writes them: each type from `type` down to `object`, | |
| 241 | // its properties and then its fields. | |
| 242 | members_of(type: TYPE) -> MemberInfo[] static is | |
| 243 | let cached: MemberInfo[] mut | |
| 244 | ||
| 245 | if _members.try_get_value(type, cached ref) then | |
| 246 | return cached | |
| 247 | fi | |
| 248 | ||
| 249 | let result = LIST[MemberInfo]() | |
| 250 | let seen = Collections.SET[string]() | |
| 251 | let flags = cast BindingFlags(2 + 4 + 16) | |
| 252 | ||
| 253 | let level: TYPE? mut = type | |
| 254 | ||
| 255 | while level? do | |
| 256 | for property in level.get_properties(flags) do | |
| 257 | if | |
| 258 | property.get_index_parameters().count == 0 /\ | |
| 259 | property.get_get_method(false)? /\ | |
| 260 | !property.name.starts_with('$') /\ | |
| 261 | seen.add(property.name) | |
| 262 | then | |
| 263 | result.add(property) | |
| 264 | fi | |
| 265 | od | |
| 266 | ||
| 267 | for slot in level.get_fields(flags) do | |
| 268 | if !slot.name.starts_with('$') /\ seen.add(slot.name) then | |
| 269 | result.add(slot) | |
| 270 | fi | |
| 271 | od | |
| 272 | ||
| 273 | level = level.base_type | |
| 274 | od | |
| 275 | ||
| 276 | let members = result.to_array() | |
| 277 | ||
| 278 | _members.try_add(type, members) | |
| 279 | ||
| 280 | return members | |
| 281 | si | |
| 282 | ||
| 283 | // The element type of a sequence type, or null where `type` is not | |
| 284 | // one or is a string. | |
| 285 | element_type_of(type: TYPE) -> TYPE? static is | |
| 286 | if type =~ typeof string then | |
| 287 | return null | |
| 288 | fi | |
| 289 | ||
| 290 | if type.is_array then | |
| 291 | return type.get_element_type() | |
| 292 | fi | |
| 293 | ||
| 294 | if type.is_generic_type /\ type.get_generic_type_definition() =~ typeof Collections.Iterable[_] then | |
| 295 | return type.get_generic_arguments()[0] | |
| 296 | fi | |
| 297 | ||
| 298 | for candidate in type.get_interfaces() do | |
| 299 | if candidate.is_generic_type /\ candidate.get_generic_type_definition() =~ typeof Collections.Iterable[_] then | |
| 300 | return candidate.get_generic_arguments()[0] | |
| 301 | fi | |
| 302 | od | |
| 303 | ||
| 304 | return null | |
| 305 | si | |
| 306 | ||
| 307 | polymorphic_attribute(type: TYPE) -> JsonPolymorphicAttribute? static is | |
| 308 | for attribute in type.get_custom_attributes(typeof JsonPolymorphicAttribute, false) do | |
| 309 | if let polymorphic: JsonPolymorphicAttribute = attribute then | |
| 310 | return polymorphic | |
| 311 | fi | |
| 312 | od | |
| 313 | ||
| 314 | return null | |
| 315 | si | |
| 316 | ||
| 317 | discriminator_of(base: TYPE, actual: TYPE) -> string static is | |
| 318 | for attribute in base.get_custom_attributes(typeof JsonDerivedTypeAttribute, false) do | |
| 319 | if let derived: JsonDerivedTypeAttribute = attribute /\ derived.derived_type =~ actual then | |
| 320 | if let name: string = derived.type_discriminator then | |
| 321 | return name | |
| 322 | fi | |
| 323 | fi | |
| 324 | od | |
| 325 | ||
| 326 | throw System.Text.Json.JsonException("{actual.full_name} is not a registered {base.full_name}") | |
| 327 | si | |
| 328 | ||
| 329 | derived_type_of(base: TYPE, discriminator: string) -> TYPE static is | |
| 330 | for attribute in base.get_custom_attributes(typeof JsonDerivedTypeAttribute, false) do | |
| 331 | if let derived: JsonDerivedTypeAttribute = attribute then | |
| 332 | if let name: string = derived.type_discriminator /\ name =~ discriminator then | |
| 333 | return derived.derived_type | |
| 334 | fi | |
| 335 | fi | |
| 336 | od | |
| 337 | ||
| 338 | throw System.Text.Json.JsonException("unknown {base.name} {discriminator}") | |
| 339 | si | |
| 340 | si | |
| 341 | si |