<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									String Enum in C# for creating a JSON - C#				            </title>
            <link>https://puresourcecode.com/forum/csharp/string-enum-in-c-for-creating-a-json/</link>
            <description>PureSourceCode Discussion Board</description>
            <language>en-US</language>
            <lastBuildDate>Sat, 18 Apr 2026 09:55:12 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>String Enum in C# for creating a JSON</title>
                        <link>https://puresourcecode.com/forum/csharp/string-enum-in-c-for-creating-a-json/#post-275</link>
                        <pubDate>Tue, 08 Nov 2022 11:08:17 +0000</pubDate>
                        <description><![CDATA[I have a Blazor component for creating graphs with ChartJs. Based on the models, C# creates a Json for the Chart configuration. I use the System.Text.Json to convert the class to a Json. Som...]]></description>
                        <content:encoded><![CDATA[<p>I have a<span> </span><a href="https://github.com/erossini/BlazorChartjs">Blazor component</a><span> </span>for creating graphs with ChartJs. Based on the models, C# creates a Json for the Chart configuration. I use the<span> </span><code>System.Text.Json</code><span> </span>to convert the class to a Json. Some configurations have defined values that I can use: for example, the<span> </span><code>PointStyle</code><span> </span>accepts only few values such as<span> </span><em>circle</em><span> </span>and<span> </span><em>cross</em>.</p>
<p>For this reason, I want to have the constraint to select only the accepted values for this configuration. Then, in my project I created a class<span> </span><code>PointStyle</code><span> </span>to have a sort of enum with string.</p>
<pre contenteditable="false">public class PointStyle
{
    private PointStyle(string value) { Value = value; }

    public string Value { get; private set; }

    public static PointStyle Circle { get { return new PointStyle("circle"); } }
    public static PointStyle Cross { get { return new PointStyle("cross"); } }
}</pre>
<p><span>To obtain the correct Json configuration, in the main model I created 2 properties: </span><code>PointStyle</code><span> and </span><code>PointStyleString</code><span>.</span></p>
<pre contenteditable="false">
public PointStyle? PointStyle { 
    get =&gt; _pointStyle; 
    set
    {
        _pointStyle = value;
        PointStyleString = _pointStyle.Value;
    }
}
private PointStyle? _pointStyle;



public string? PointStyleString { get; set; }</pre>
<p><code>PointStyle</code><span> accepts only the values in the defined list but this is ignored from the Json converter. For example, I can configure this like</span></p>
<pre contenteditable="false">PointStyle = PointStyle.Cross</pre>
<p>Then, the public variable<span> </span><code>PointStyleString</code><span> </span>contains the string I have to serialize in the Json and this is updated when the<span> </span><code>PointStyle</code><span> </span>sets a new value.</p>
<p>Now, I have to read the property<span> </span><code>Value</code><span> </span>from the class<span> </span><code>PointStyle</code><span> </span>that contains the string I have to pass to the configuration. For this reason, I have a private variable _<em>pointStyle</em><span> </span>that saves the value for<span> </span><code>PointStyle</code>; so, when a new value is set, I also set the<span> </span><code>PointStyleString</code>.</p>
<p>Everything I described above is working but it seems to me quite complicated for just setting a value. Do you think I can simplify this code?</p>]]></content:encoded>
						                            <category domain="https://puresourcecode.com/forum/csharp/">C#</category>                        <dc:creator>Enrico</dc:creator>
                        <guid isPermaLink="true">https://puresourcecode.com/forum/csharp/string-enum-in-c-for-creating-a-json/#post-275</guid>
                    </item>
							        </channel>
        </rss>
		