diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache index 409c9014e569..276e25383ec1 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/HttpSigningConfiguration.mustache @@ -78,9 +78,10 @@ namespace {{packageName}}.Client /// /// Gets the Headers for HttpSigning /// - /// - /// - /// + /// Base path + /// HTTP method + /// Path + /// Request options /// internal Dictionary GetHttpSignedHeader(string basePath,string method, string path, RequestOptions requestOptions) { diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/model.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/model.mustache index 58778d5b59f9..339a6d2f76b7 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/model.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/model.mustache @@ -33,10 +33,15 @@ using OpenAPIClientUtils = {{packageName}}.Client.ClientUtils; using System.Reflection; {{/-first}} {{/oneOf}} +{{#aneOf}} +{{#-first}} +using System.Reflection; +{{/-first}} +{{/aneOf}} namespace {{packageName}}.{{modelPackage}} { -{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/isEnum}} +{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{#anyOf}}{{#-first}}{{>modelAnyOf}}{{/-first}}{{/anyOf}}{{^oneOf}}{{^anyOf}}{{>modelGeneric}}{{/anyOf}}{{/oneOf}}{{/isEnum}} {{/model}} {{/models}} } diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/modelAnyOf.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/modelAnyOf.mustache new file mode 100644 index 000000000000..09b745284958 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/modelAnyOf.mustache @@ -0,0 +1,218 @@ + /// + /// {{#description}}{{.}}{{/description}}{{^description}}{{classname}}{{/description}} + /// + [JsonConverter(typeof({{classname}}JsonConverter))] + [DataContract(Name = "{{{name}}}")] + {{>visibility}} partial class {{classname}} : AbstractOpenAPISchema, {{#parent}}{{{parent}}}, {{/parent}}IEquatable<{{classname}}>{{#validatable}}, IValidatableObject{{/validatable}} + { + {{#isNullable}} + /// + /// Initializes a new instance of the class. + /// + public {{classname}}() + { + this.IsNullable = true; + this.SchemaType= "anyOf"; + } + + {{/isNullable}} + {{#anyOf}} + /// + /// Initializes a new instance of the class + /// with the class + /// + /// An instance of {{{.}}}. + public {{classname}}({{{.}}} actualInstance) + { + this.IsNullable = {{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}; + this.SchemaType= "anyOf"; + this.ActualInstance = actualInstance{{^isNullable}} ?? throw new ArgumentException("Invalid instance found. Must not be null."){{/isNullable}}; + } + + {{/anyOf}} + + private Object _actualInstance; + + /// + /// Gets or Sets ActualInstance + /// + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + {{#anyOf}} + {{^-first}}else {{/-first}}if (value.GetType() == typeof({{{.}}})) + { + this._actualInstance = value; + } + {{/anyOf}} + else + { + throw new ArgumentException("Invalid instance found. Must be the following types:{{#anyOf}} {{{.}}}{{^-last}},{{/-last}}{{/anyOf}}"); + } + } + } + {{#anyOf}} + + /// + /// Get the actual instance of `{{{.}}}`. If the actual instanct is not `{{{.}}}`, + /// the InvalidClassException will be thrown + /// + /// An instance of {{{.}}} + public {{{.}}} Get{{{.}}}() + { + return ({{{.}}})this.ActualInstance; + } + {{/anyOf}} + + /// + /// Returns the string presentation of the object + /// + /// String presentation of the object + public override string ToString() + { + var sb = new StringBuilder(); + sb.Append("class {{classname}} {\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); + sb.Append("}\n"); + return sb.ToString(); + } + + /// + /// Returns the JSON string presentation of the object + /// + /// JSON string presentation of the object + public override string ToJson() + { + return JsonConvert.SerializeObject(this.ActualInstance, {{classname}}.SerializerSettings); + } + + /// + /// Converts the JSON string into an instance of {{classname}} + /// + /// JSON string + /// An instance of {{classname}} + public static {{classname}} FromJson(string jsonString) + { + {{classname}} new{{classname}} = null; + {{#anyOf}} + + try + { + new{{classname}} = new {{classname}}(JsonConvert.DeserializeObject<{{{.}}}>(jsonString, {{classname}}.SerializerSettings)); + // deserialization is considered successful at this point if no exception has been thrown. + return new{{classname}}; + } + catch (Exception exception) + { + // deserialization failed, try the next one + System.Diagnostics.Debug.WriteLine(String.Format("Failed to deserialize `{0}` into {{{.}}}: {1}", jsonString, exception.ToString())); + } + {{/anyOf}} + + // no match found, throw an exception + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); + } + + /// + /// Returns true if objects are equal + /// + /// Object to be compared + /// Boolean + public override bool Equals(object input) + { + {{#useCompareNetObjects}} + return OpenAPIClientUtils.compareLogic.Compare(this, input as {{classname}}).AreEqual; + {{/useCompareNetObjects}} + {{^useCompareNetObjects}} + return this.Equals(input as {{classname}}); + {{/useCompareNetObjects}} + } + + /// + /// Returns true if {{classname}} instances are equal + /// + /// Instance of {{classname}} to be compared + /// Boolean + public bool Equals({{classname}} input) + { + {{#useCompareNetObjects}} + return OpenAPIClientUtils.compareLogic.Compare(this, input).AreEqual; + {{/useCompareNetObjects}} + {{^useCompareNetObjects}} + if (input == null) + return false; + + return this.ActualInstance.Equals(input.ActualInstance); + {{/useCompareNetObjects}} + } + + /// + /// Gets the hash code + /// + /// Hash code + public override int GetHashCode() + { + unchecked // Overflow is fine, just wrap + { + int hashCode = 41; + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); + return hashCode; + } + } + + /// + /// To validate all properties of the instance + /// + /// Validation context + /// Validation Result + IEnumerable IValidatableObject.Validate(ValidationContext validationContext) + { + yield break; + } + } + + /// + /// Custom JSON converter for {{classname}} + /// + public class {{classname}}JsonConverter : JsonConverter + { + /// + /// To write the JSON string + /// + /// JSON writer + /// Object to be converted into a JSON string + /// JSON Serializer + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + writer.WriteRaw((String)(typeof({{classname}}).GetMethod("ToJson").Invoke(value, null))); + } + + /// + /// To convert a JSON string into an object + /// + /// JSON reader + /// Object type + /// Existing value + /// JSON Serializer + /// The object converted from the JSON string + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + return {{classname}}.FromJson(JObject.Load(reader).ToString(Formatting.None)); + } + + /// + /// Check if the object can be converted + /// + /// Object type + /// True if the object can be converted + public override bool CanConvert(Type objectType) + { + return false; + } + } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index fbee426b1104..c6d67d22ba05 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -78,9 +78,10 @@ namespace Org.OpenAPITools.Client /// /// Gets the Headers for HttpSigning /// - /// - /// - /// + /// Base path + /// HTTP method + /// Path + /// Request options /// internal Dictionary GetHttpSignedHeader(string basePath,string method, string path, RequestOptions requestOptions) { diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/GmFruit.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/GmFruit.cs index a25e108244cf..e9cacb9e86d8 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/GmFruit.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Model/GmFruit.cs @@ -29,47 +29,82 @@ namespace Org.OpenAPITools.Model /// /// GmFruit /// + [JsonConverter(typeof(GmFruitJsonConverter))] [DataContract(Name = "gmFruit")] - public partial class GmFruit : IEquatable, IValidatableObject + public partial class GmFruit : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class + /// with the class /// - /// color. - /// cultivar. - /// origin. - /// lengthCm. - public GmFruit(string color = default(string), string cultivar = default(string), string origin = default(string), decimal lengthCm = default(decimal)) + /// An instance of Apple. + public GmFruit(Apple actualInstance) { - this.Color = color; - this.Cultivar = cultivar; - this.Origin = origin; - this.LengthCm = lengthCm; + this.IsNullable = false; + this.SchemaType= "anyOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); } /// - /// Gets or Sets Color + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "color", EmitDefaultValue = false)] - public string Color { get; set; } + /// An instance of Banana. + public GmFruit(Banana actualInstance) + { + this.IsNullable = false; + this.SchemaType= "anyOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; /// - /// Gets or Sets Cultivar + /// Gets or Sets ActualInstance /// - [DataMember(Name = "cultivar", EmitDefaultValue = false)] - public string Cultivar { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Apple)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Banana)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Apple, Banana"); + } + } + } /// - /// Gets or Sets Origin + /// Get the actual instance of `Apple`. If the actual instanct is not `Apple`, + /// the InvalidClassException will be thrown /// - [DataMember(Name = "origin", EmitDefaultValue = false)] - public string Origin { get; set; } + /// An instance of Apple + public Apple GetApple() + { + return (Apple)this.ActualInstance; + } /// - /// Gets or Sets LengthCm + /// Get the actual instance of `Banana`. If the actual instanct is not `Banana`, + /// the InvalidClassException will be thrown /// - [DataMember(Name = "lengthCm", EmitDefaultValue = false)] - public decimal LengthCm { get; set; } + /// An instance of Banana + public Banana GetBanana() + { + return (Banana)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -79,10 +114,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class GmFruit {\n"); - sb.Append(" Color: ").Append(Color).Append("\n"); - sb.Append(" Cultivar: ").Append(Cultivar).Append("\n"); - sb.Append(" Origin: ").Append(Origin).Append("\n"); - sb.Append(" LengthCm: ").Append(LengthCm).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -91,9 +123,46 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, GmFruit.SerializerSettings); + } + + /// + /// Converts the JSON string into an instance of GmFruit + /// + /// JSON string + /// An instance of GmFruit + public static GmFruit FromJson(string jsonString) + { + GmFruit newGmFruit = null; + + try + { + newGmFruit = new GmFruit(JsonConvert.DeserializeObject(jsonString, GmFruit.SerializerSettings)); + // deserialization is considered successful at this point if no exception has been thrown. + return newGmFruit; + } + catch (Exception exception) + { + // deserialization failed, try the next one + System.Diagnostics.Debug.WriteLine(String.Format("Failed to deserialize `{0}` into Apple: {1}", jsonString, exception.ToString())); + } + + try + { + newGmFruit = new GmFruit(JsonConvert.DeserializeObject(jsonString, GmFruit.SerializerSettings)); + // deserialization is considered successful at this point if no exception has been thrown. + return newGmFruit; + } + catch (Exception exception) + { + // deserialization failed, try the next one + System.Diagnostics.Debug.WriteLine(String.Format("Failed to deserialize `{0}` into Banana: {1}", jsonString, exception.ToString())); + } + + // no match found, throw an exception + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); } /// @@ -125,13 +194,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.Color != null) - hashCode = hashCode * 59 + this.Color.GetHashCode(); - if (this.Cultivar != null) - hashCode = hashCode * 59 + this.Cultivar.GetHashCode(); - if (this.Origin != null) - hashCode = hashCode * 59 + this.Origin.GetHashCode(); - hashCode = hashCode * 59 + this.LengthCm.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -143,22 +207,48 @@ namespace Org.OpenAPITools.Model /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) { - // Cultivar (string) pattern - Regex regexCultivar = new Regex(@"^[a-zA-Z\\s]*$", RegexOptions.CultureInvariant); - if (false == regexCultivar.Match(this.Cultivar).Success) - { - yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Cultivar, must match a pattern of " + regexCultivar, new [] { "Cultivar" }); - } - - // Origin (string) pattern - Regex regexOrigin = new Regex(@"^[A-Z\\s]*$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); - if (false == regexOrigin.Match(this.Origin).Success) - { - yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Origin, must match a pattern of " + regexOrigin, new [] { "Origin" }); - } - yield break; } } + /// + /// Custom JSON converter for GmFruit + /// + public class GmFruitJsonConverter : JsonConverter + { + /// + /// To write the JSON string + /// + /// JSON writer + /// Object to be converted into a JSON string + /// JSON Serializer + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + writer.WriteRaw((String)(typeof(GmFruit).GetMethod("ToJson").Invoke(value, null))); + } + + /// + /// To convert a JSON string into an object + /// + /// JSON reader + /// Object type + /// Existing value + /// JSON Serializer + /// The object converted from the JSON string + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + return GmFruit.FromJson(JObject.Load(reader).ToString(Formatting.None)); + } + + /// + /// Check if the object can be converted + /// + /// Object type + /// True if the object can be converted + public override bool CanConvert(Type objectType) + { + return false; + } + } + } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index fbee426b1104..c6d67d22ba05 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -78,9 +78,10 @@ namespace Org.OpenAPITools.Client /// /// Gets the Headers for HttpSigning /// - /// - /// - /// + /// Base path + /// HTTP method + /// Path + /// Request options /// internal Dictionary GetHttpSignedHeader(string basePath,string method, string path, RequestOptions requestOptions) { diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/GmFruit.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/GmFruit.cs index a25e108244cf..e9cacb9e86d8 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/GmFruit.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Model/GmFruit.cs @@ -29,47 +29,82 @@ namespace Org.OpenAPITools.Model /// /// GmFruit /// + [JsonConverter(typeof(GmFruitJsonConverter))] [DataContract(Name = "gmFruit")] - public partial class GmFruit : IEquatable, IValidatableObject + public partial class GmFruit : AbstractOpenAPISchema, IEquatable, IValidatableObject { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class + /// with the class /// - /// color. - /// cultivar. - /// origin. - /// lengthCm. - public GmFruit(string color = default(string), string cultivar = default(string), string origin = default(string), decimal lengthCm = default(decimal)) + /// An instance of Apple. + public GmFruit(Apple actualInstance) { - this.Color = color; - this.Cultivar = cultivar; - this.Origin = origin; - this.LengthCm = lengthCm; + this.IsNullable = false; + this.SchemaType= "anyOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); } /// - /// Gets or Sets Color + /// Initializes a new instance of the class + /// with the class /// - [DataMember(Name = "color", EmitDefaultValue = false)] - public string Color { get; set; } + /// An instance of Banana. + public GmFruit(Banana actualInstance) + { + this.IsNullable = false; + this.SchemaType= "anyOf"; + this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null."); + } + + + private Object _actualInstance; /// - /// Gets or Sets Cultivar + /// Gets or Sets ActualInstance /// - [DataMember(Name = "cultivar", EmitDefaultValue = false)] - public string Cultivar { get; set; } + public override Object ActualInstance + { + get + { + return _actualInstance; + } + set + { + if (value.GetType() == typeof(Apple)) + { + this._actualInstance = value; + } + else if (value.GetType() == typeof(Banana)) + { + this._actualInstance = value; + } + else + { + throw new ArgumentException("Invalid instance found. Must be the following types: Apple, Banana"); + } + } + } /// - /// Gets or Sets Origin + /// Get the actual instance of `Apple`. If the actual instanct is not `Apple`, + /// the InvalidClassException will be thrown /// - [DataMember(Name = "origin", EmitDefaultValue = false)] - public string Origin { get; set; } + /// An instance of Apple + public Apple GetApple() + { + return (Apple)this.ActualInstance; + } /// - /// Gets or Sets LengthCm + /// Get the actual instance of `Banana`. If the actual instanct is not `Banana`, + /// the InvalidClassException will be thrown /// - [DataMember(Name = "lengthCm", EmitDefaultValue = false)] - public decimal LengthCm { get; set; } + /// An instance of Banana + public Banana GetBanana() + { + return (Banana)this.ActualInstance; + } /// /// Returns the string presentation of the object @@ -79,10 +114,7 @@ namespace Org.OpenAPITools.Model { var sb = new StringBuilder(); sb.Append("class GmFruit {\n"); - sb.Append(" Color: ").Append(Color).Append("\n"); - sb.Append(" Cultivar: ").Append(Cultivar).Append("\n"); - sb.Append(" Origin: ").Append(Origin).Append("\n"); - sb.Append(" LengthCm: ").Append(LengthCm).Append("\n"); + sb.Append(" ActualInstance: ").Append(this.ActualInstance).Append("\n"); sb.Append("}\n"); return sb.ToString(); } @@ -91,9 +123,46 @@ namespace Org.OpenAPITools.Model /// Returns the JSON string presentation of the object /// /// JSON string presentation of the object - public virtual string ToJson() + public override string ToJson() { - return JsonConvert.SerializeObject(this, Formatting.Indented); + return JsonConvert.SerializeObject(this.ActualInstance, GmFruit.SerializerSettings); + } + + /// + /// Converts the JSON string into an instance of GmFruit + /// + /// JSON string + /// An instance of GmFruit + public static GmFruit FromJson(string jsonString) + { + GmFruit newGmFruit = null; + + try + { + newGmFruit = new GmFruit(JsonConvert.DeserializeObject(jsonString, GmFruit.SerializerSettings)); + // deserialization is considered successful at this point if no exception has been thrown. + return newGmFruit; + } + catch (Exception exception) + { + // deserialization failed, try the next one + System.Diagnostics.Debug.WriteLine(String.Format("Failed to deserialize `{0}` into Apple: {1}", jsonString, exception.ToString())); + } + + try + { + newGmFruit = new GmFruit(JsonConvert.DeserializeObject(jsonString, GmFruit.SerializerSettings)); + // deserialization is considered successful at this point if no exception has been thrown. + return newGmFruit; + } + catch (Exception exception) + { + // deserialization failed, try the next one + System.Diagnostics.Debug.WriteLine(String.Format("Failed to deserialize `{0}` into Banana: {1}", jsonString, exception.ToString())); + } + + // no match found, throw an exception + throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined."); } /// @@ -125,13 +194,8 @@ namespace Org.OpenAPITools.Model unchecked // Overflow is fine, just wrap { int hashCode = 41; - if (this.Color != null) - hashCode = hashCode * 59 + this.Color.GetHashCode(); - if (this.Cultivar != null) - hashCode = hashCode * 59 + this.Cultivar.GetHashCode(); - if (this.Origin != null) - hashCode = hashCode * 59 + this.Origin.GetHashCode(); - hashCode = hashCode * 59 + this.LengthCm.GetHashCode(); + if (this.ActualInstance != null) + hashCode = hashCode * 59 + this.ActualInstance.GetHashCode(); return hashCode; } } @@ -143,22 +207,48 @@ namespace Org.OpenAPITools.Model /// Validation Result IEnumerable IValidatableObject.Validate(ValidationContext validationContext) { - // Cultivar (string) pattern - Regex regexCultivar = new Regex(@"^[a-zA-Z\\s]*$", RegexOptions.CultureInvariant); - if (false == regexCultivar.Match(this.Cultivar).Success) - { - yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Cultivar, must match a pattern of " + regexCultivar, new [] { "Cultivar" }); - } - - // Origin (string) pattern - Regex regexOrigin = new Regex(@"^[A-Z\\s]*$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); - if (false == regexOrigin.Match(this.Origin).Success) - { - yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Origin, must match a pattern of " + regexOrigin, new [] { "Origin" }); - } - yield break; } } + /// + /// Custom JSON converter for GmFruit + /// + public class GmFruitJsonConverter : JsonConverter + { + /// + /// To write the JSON string + /// + /// JSON writer + /// Object to be converted into a JSON string + /// JSON Serializer + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + writer.WriteRaw((String)(typeof(GmFruit).GetMethod("ToJson").Invoke(value, null))); + } + + /// + /// To convert a JSON string into an object + /// + /// JSON reader + /// Object type + /// Existing value + /// JSON Serializer + /// The object converted from the JSON string + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + return GmFruit.FromJson(JObject.Load(reader).ToString(Formatting.None)); + } + + /// + /// Check if the object can be converted + /// + /// Object type + /// True if the object can be converted + public override bool CanConvert(Type objectType) + { + return false; + } + } + }