33//
44using System ;
55using System . Collections . Generic ;
6+ using System . IO ;
67using System . Linq ;
8+ using System . Reflection ;
79using System . Text ;
810using System . Text . Json ;
911using System . Text . Json . Serialization ;
12+ using Algolia . Search . Models . Common ;
1013using Algolia . Search . Serializer ;
1114
1215namespace Algolia . Search . Models . Composition ;
1316
1417/// <summary>
1518/// An object containing either an `injection` or `multifeed` behavior schema, but not both.
1619/// </summary>
17- public partial class CompositionBehavior
20+ [ JsonConverter ( typeof ( CompositionBehaviorJsonConverter ) ) ]
21+ public partial class CompositionBehavior : AbstractSchema
1822{
1923 /// <summary>
20- /// Initializes a new instance of the CompositionBehavior class.
24+ /// Initializes a new instance of the CompositionBehavior class
25+ /// with a CompositionInjectionBehavior
2126 /// </summary>
22- public CompositionBehavior ( ) { }
27+ /// <param name="actualInstance">An instance of CompositionInjectionBehavior.</param>
28+ public CompositionBehavior ( CompositionInjectionBehavior actualInstance )
29+ {
30+ ActualInstance =
31+ actualInstance ?? throw new ArgumentException ( "Invalid instance found. Must not be null." ) ;
32+ }
33+
34+ /// <summary>
35+ /// Initializes a new instance of the CompositionBehavior class
36+ /// with a CompositionMultifeedBehavior
37+ /// </summary>
38+ /// <param name="actualInstance">An instance of CompositionMultifeedBehavior.</param>
39+ public CompositionBehavior ( CompositionMultifeedBehavior actualInstance )
40+ {
41+ ActualInstance =
42+ actualInstance ?? throw new ArgumentException ( "Invalid instance found. Must not be null." ) ;
43+ }
2344
2445 /// <summary>
25- /// Gets or Sets Injection
46+ /// Gets or Sets ActualInstance
2647 /// </summary>
27- [ JsonPropertyName ( "injection" ) ]
28- public Injection Injection { get ; set ; }
48+ public sealed override object ActualInstance { get ; set ; }
2949
3050 /// <summary>
31- /// Gets or Sets Multifeed
51+ /// Get the actual instance of `CompositionInjectionBehavior`. If the actual instance is not `CompositionInjectionBehavior`,
52+ /// the InvalidClassException will be thrown
3253 /// </summary>
33- [ JsonPropertyName ( "multifeed" ) ]
34- public Multifeed Multifeed { get ; set ; }
54+ /// <returns>An instance of CompositionInjectionBehavior</returns>
55+ public CompositionInjectionBehavior AsCompositionInjectionBehavior ( )
56+ {
57+ return ( CompositionInjectionBehavior ) ActualInstance ;
58+ }
59+
60+ /// <summary>
61+ /// Get the actual instance of `CompositionMultifeedBehavior`. If the actual instance is not `CompositionMultifeedBehavior`,
62+ /// the InvalidClassException will be thrown
63+ /// </summary>
64+ /// <returns>An instance of CompositionMultifeedBehavior</returns>
65+ public CompositionMultifeedBehavior AsCompositionMultifeedBehavior ( )
66+ {
67+ return ( CompositionMultifeedBehavior ) ActualInstance ;
68+ }
69+
70+ /// <summary>
71+ /// Check if the actual instance is of `CompositionInjectionBehavior` type.
72+ /// </summary>
73+ /// <returns>Whether or not the instance is the type</returns>
74+ public bool IsCompositionInjectionBehavior ( )
75+ {
76+ return ActualInstance . GetType ( ) == typeof ( CompositionInjectionBehavior ) ;
77+ }
78+
79+ /// <summary>
80+ /// Check if the actual instance is of `CompositionMultifeedBehavior` type.
81+ /// </summary>
82+ /// <returns>Whether or not the instance is the type</returns>
83+ public bool IsCompositionMultifeedBehavior ( )
84+ {
85+ return ActualInstance . GetType ( ) == typeof ( CompositionMultifeedBehavior ) ;
86+ }
3587
3688 /// <summary>
3789 /// Returns the string presentation of the object
3890 /// </summary>
3991 /// <returns>String presentation of the object</returns>
4092 public override string ToString ( )
4193 {
42- StringBuilder sb = new StringBuilder ( ) ;
94+ var sb = new StringBuilder ( ) ;
4395 sb . Append ( "class CompositionBehavior {\n " ) ;
44- sb . Append ( " Injection: " ) . Append ( Injection ) . Append ( "\n " ) ;
45- sb . Append ( " Multifeed: " ) . Append ( Multifeed ) . Append ( "\n " ) ;
96+ sb . Append ( " ActualInstance: " ) . Append ( ActualInstance ) . Append ( "\n " ) ;
4697 sb . Append ( "}\n " ) ;
4798 return sb . ToString ( ) ;
4899 }
@@ -51,9 +102,9 @@ public override string ToString()
51102 /// Returns the JSON string presentation of the object
52103 /// </summary>
53104 /// <returns>JSON string presentation of the object</returns>
54- public virtual string ToJson ( )
105+ public override string ToJson ( )
55106 {
56- return JsonSerializer . Serialize ( this , JsonConfig . Options ) ;
107+ return JsonSerializer . Serialize ( ActualInstance , JsonConfig . Options ) ;
57108 }
58109
59110 /// <summary>
@@ -68,10 +119,7 @@ public override bool Equals(object obj)
68119 return false ;
69120 }
70121
71- return (
72- Injection == input . Injection || ( Injection != null && Injection . Equals ( input . Injection ) )
73- )
74- && ( Multifeed == input . Multifeed || ( Multifeed != null && Multifeed . Equals ( input . Multifeed ) ) ) ;
122+ return ActualInstance . Equals ( input . ActualInstance ) ;
75123 }
76124
77125 /// <summary>
@@ -83,15 +131,92 @@ public override int GetHashCode()
83131 unchecked // Overflow is fine, just wrap
84132 {
85133 int hashCode = 41 ;
86- if ( Injection != null )
134+ if ( ActualInstance != null )
135+ hashCode = hashCode * 59 + ActualInstance . GetHashCode ( ) ;
136+ return hashCode ;
137+ }
138+ }
139+ }
140+
141+ /// <summary>
142+ /// Custom JSON converter for CompositionBehavior
143+ /// </summary>
144+ public class CompositionBehaviorJsonConverter : JsonConverter < CompositionBehavior >
145+ {
146+ /// <summary>
147+ /// Check if the object can be converted
148+ /// </summary>
149+ /// <param name="objectType">Object type</param>
150+ /// <returns>True if the object can be converted</returns>
151+ public override bool CanConvert ( Type objectType )
152+ {
153+ return objectType == typeof ( CompositionBehavior ) ;
154+ }
155+
156+ /// <summary>
157+ /// To convert a JSON string into an object
158+ /// </summary>
159+ /// <param name="reader">JSON reader</param>
160+ /// <param name="typeToConvert">Object type</param>
161+ /// <param name="options">Serializer options</param>
162+ /// <returns>The object converted from the JSON string</returns>
163+ public override CompositionBehavior Read (
164+ ref Utf8JsonReader reader ,
165+ Type typeToConvert ,
166+ JsonSerializerOptions options
167+ )
168+ {
169+ var jsonDocument = JsonDocument . ParseValue ( ref reader ) ;
170+ var root = jsonDocument . RootElement ;
171+ if ( root . ValueKind == JsonValueKind . Object )
172+ {
173+ try
87174 {
88- hashCode = ( hashCode * 59 ) + Injection . GetHashCode ( ) ;
175+ return new CompositionBehavior (
176+ jsonDocument . Deserialize < CompositionInjectionBehavior > ( JsonConfig . Options )
177+ ) ;
89178 }
90- if ( Multifeed != null )
179+ catch ( Exception exception )
91180 {
92- hashCode = ( hashCode * 59 ) + Multifeed . GetHashCode ( ) ;
181+ // deserialization failed, try the next one
182+ System . Diagnostics . Debug . WriteLine (
183+ $ "Failed to deserialize into CompositionInjectionBehavior: { exception } "
184+ ) ;
93185 }
94- return hashCode ;
95186 }
187+ if ( root . ValueKind == JsonValueKind . Object )
188+ {
189+ try
190+ {
191+ return new CompositionBehavior (
192+ jsonDocument . Deserialize < CompositionMultifeedBehavior > ( JsonConfig . Options )
193+ ) ;
194+ }
195+ catch ( Exception exception )
196+ {
197+ // deserialization failed, try the next one
198+ System . Diagnostics . Debug . WriteLine (
199+ $ "Failed to deserialize into CompositionMultifeedBehavior: { exception } "
200+ ) ;
201+ }
202+ }
203+ throw new InvalidDataException (
204+ $ "The JSON string cannot be deserialized into any schema defined."
205+ ) ;
206+ }
207+
208+ /// <summary>
209+ /// To write the JSON string
210+ /// </summary>
211+ /// <param name="writer">JSON writer</param>
212+ /// <param name="value">CompositionBehavior to be converted into a JSON string</param>
213+ /// <param name="options">JSON Serializer options</param>
214+ public override void Write (
215+ Utf8JsonWriter writer ,
216+ CompositionBehavior value ,
217+ JsonSerializerOptions options
218+ )
219+ {
220+ writer . WriteRawValue ( value . ToJson ( ) ) ;
96221 }
97222}
0 commit comments