C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
DerivedResourceCreationConverter.cs
Go to the documentation of this file.
1using System;
2using System.Diagnostics;
5using Newtonsoft.Json.Linq;
6
8{
18 {
33 protected override T Create(Type objectType, JObject jObject)
34 {
35 Type resolvedType = null;
36
37 string typeName = jObject[APIType_Polymorphic.TypePropertyName]?.ToString();
38 if (String.IsNullOrEmpty(typeName))
39 typeName = null;
40 // See if the server is specifying a polymorphic sub-type for this resource
41 if (typeName != null)
42 {
44 // Generate a warning message if we don't recognize the server-identified type.
45 if (resolvedType == null)
46 {
48 Debug.WriteLine("Warning: The JSON returned from server for a " +
49 $"{objectType.NiceTypeName()} contained an unexpected type field: " +
50 $"\"{APIType_Polymorphic.TypePropertyName}\": \"{typeName}\", " +
51 "which seems unnecessary, since this class is not polymorphic. " +
52 "This could be a sign that the server added polymorphism to a class " +
53 "without making the appropriate adjustments in the C# Client Library.");
54 else
55 Debug.WriteLine("Warning: The JSON returned from server for a " +
56 $"{objectType.NiceTypeName()} contained an unrecognized value in the " +
57 $"type field: \"{APIType_Polymorphic.TypePropertyName}\": \"{typeName}\". " +
58 "This could be a sign that the server added a new sub-type to a class " +
59 "without making the appropriate adjustments in the C# Client Library.");
60 }
61 }
62 // If the server supplied no type (or an unrecognized type), we can still
63 // deserialize to the expected object type so long as it's not an abstract type.
64 if (resolvedType == null && !objectType.IsAbstract && !objectType.IsInterface)
66
67 // If one of the two above methods succeeded, create an object instance.
68 if (resolvedType != null)
69 return (T)Activator.CreateInstance(resolvedType);
70 // Otherwise, prepare an error describing why we couldn't deserialize this object.
71 if (typeName != null)
72 throw new InvalidOperationException("JSON references an unknown " +
73 $"{objectType.NiceTypeName()} sub-type: \"{typeName}\".");
75 "Could not create an instance of the abstract class or interface " +
76 objectType.NiceTypeName() + ", and the JSON object did not contain a " +
77 "_type property to determine what the correct sub-type is: " + jObject);
78 }
79 }
80}
Base class used by all types and resources.
Describes a collection of resources which can be listed.
Allows serializing and deserializing derived Analyze Re Resources to a target base type....
override T Create(Type objectType, JObject jObject)
Create a new instance of the requested resource type for deserialization of a JSON string.
Utility for resolving types given only the types name (Useful for parsing ambiguous JSON objects).
static bool ResolveTypeByName(string typeName, out Type resolvedType, Type requiredBaseType=null)
Clever method that checks all assemblies in the application being compiled for the type referenced by...
Interface for polymorphic types on the server.
Interface shared by all object types and resources returned by the Analyze Re server.
Definition IAPIType.cs:6