All Downloads are FREE. Search and download functionalities are using the official Maven repository.

csharp.modelEnum.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
    /// 
    /// {{description}}{{^description}}Defines {{{name}}}{{/description}}
    /// 
    {{#description}}
    /// {{.}}
    {{/description}}
    {{#vendorExtensions.x-cls-compliant}}
    [CLSCompliant({{{.}}})]
    {{/vendorExtensions.x-cls-compliant}}
    {{#vendorExtensions.x-com-visible}}
    [ComVisible({{{.}}})]
    {{/vendorExtensions.x-com-visible}}
    {{#allowableValues}}
    {{#enumVars}}
    {{#-first}}
    {{#isString}}
    {{^useGenericHost}}
    [JsonConverter(typeof(StringEnumConverter))]
    {{/useGenericHost}}
    {{/isString}}
    {{/-first}}
    {{/enumVars}}
    {{/allowableValues}}
    {{>visibility}} enum {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}{{#vendorExtensions.x-enum-byte}}: byte{{/vendorExtensions.x-enum-byte}}
    {
        {{#allowableValues}}
        {{#enumVars}}
        /// 
        /// Enum {{name}} for value: {{value}}
        /// 
        {{#isString}}
        {{^useGenericHost}}
        {{! EnumMember not currently supported in System.Text.Json, use a converter instead }}
        [EnumMember(Value = "{{{value}}}")]
        {{/useGenericHost}}
        {{/isString}}
        {{name}}{{^isString}} = {{{value}}}{{/isString}}{{#isString}}{{^vendorExtensions.x-zero-based-enum}} = {{-index}}{{/vendorExtensions.x-zero-based-enum}}{{/isString}}{{^-last}},{{/-last}}
        {{^-last}}

        {{/-last}}
        {{/enumVars}}
        {{/allowableValues}}
    }
    {{#useGenericHost}}

    /// 
    /// Converts  to and from the JSON value
    /// 
    public static class {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}ValueConverter
    {
        /// 
        /// Parses a given value to 
        /// 
        /// 
        /// 
        public static {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} FromString(string value)
        {
            {{#allowableValues}}
            {{#enumVars}}
            if (value.Equals({{^isString}}({{{value}}}).ToString(){{/isString}}{{#isString}}"{{{value}}}"{{/isString}}))
                return {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.{{name}};

            {{/enumVars}}
            {{/allowableValues}}
            throw new NotImplementedException($"Could not convert value to type {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}: '{value}'");
        }

        /// 
        /// Parses a given value to 
        /// 
        /// 
        /// 
        public static {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}? FromStringOrDefault(string value)
        {
            {{#allowableValues}}
            {{#enumVars}}
            if (value.Equals({{^isString}}({{{value}}}).ToString(){{/isString}}{{#isString}}"{{{value}}}"{{/isString}}))
                return {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.{{name}};

            {{/enumVars}}
            {{/allowableValues}}
            return null;
        }

        /// 
        /// Converts the  to the json value
        /// 
        /// 
        /// 
        /// 
        public static {{>EnumValueDataType}} ToJsonValue({{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} value)
        {
            {{^isString}}
            return ({{>EnumValueDataType}}) value;
            {{/isString}}
            {{#isString}}
            {{#allowableValues}}
            {{#enumVars}}
            if (value == {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.{{name}})
                return {{^isNumeric}}"{{/isNumeric}}{{{value}}}{{^isNumeric}}"{{/isNumeric}};

            {{/enumVars}}
            {{/allowableValues}}
            throw new NotImplementedException($"Value could not be handled: '{value}'");
            {{/isString}}
        }
    }

    /// 
    /// A Json converter for type 
    /// 
    /// 
    public class {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}JsonConverter : JsonConverter<{{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}>
    {
        /// 
        /// Returns a {{datatypeWithEnum}} from the Json object
        /// 
        /// 
        /// 
        /// 
        /// 
        public override {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            string{{nrt?}} rawValue = reader.GetString();

            {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}? result = rawValue == null
                ? null
                : {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}ValueConverter.FromStringOrDefault(rawValue);

            if (result != null)
                return result.Value;

            throw new JsonException();
        }

        /// 
        /// Writes the {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} to the json writer
        /// 
        /// 
        /// 
        /// 
        public override void Write(Utf8JsonWriter writer, {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} {{#lambda.camelcase_param}}{{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}{{/lambda.camelcase_param}}, JsonSerializerOptions options)
        {
            writer.WriteStringValue({{#lambda.camelcase_param}}{{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}{{/lambda.camelcase_param}}.ToString());
        }
    }

    /// 
    /// A Json converter for type 
    /// 
    public class {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}NullableJsonConverter : JsonConverter<{{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}?>
    {
        /// 
        /// Returns a {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} from the Json object
        /// 
        /// 
        /// 
        /// 
        /// 
        public override {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            string{{nrt?}} rawValue = reader.GetString();

            {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}? result = rawValue == null
                ? null
                : {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}ValueConverter.FromStringOrDefault(rawValue);

            if (result != null)
                return result.Value;

            throw new JsonException();
        }

        /// 
        /// Writes the DateTime to the json writer
        /// 
        /// 
        /// 
        /// 
        public override void Write(Utf8JsonWriter writer, {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}? {{#lambda.camelcase_param}}{{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}{{/lambda.camelcase_param}}, JsonSerializerOptions options)
        {
            writer.WriteStringValue({{#lambda.camelcase_param}}{{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}{{/lambda.camelcase_param}}?.ToString() ?? "null");
        }
    }
    {{/useGenericHost}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy