2

I have a class called InstrumentConfigValues with properties that has type implementing an interface. Now I have an enum by name InstrumentConfig which has set of values. These values are like keys inside the json file. I want to map something like [JsonProperty(InstrumentConfig.LowDiskpace.ToString()].

For some reason its not allowing this and complains saying:

An attribute argument must be constant expression

I referred to many post specifically JsonStringEnumConverter. But how can I map each property with the enum key. I also saw this post JsonSerializationSettings but not able to correlate to my problem. Please help/

public class InstrumentConfigValues : IInstrumentConfig
{      
    public double SpaceNeededForSingleRun
    {
        get; set;
    }

    public int NumberOfInputSlots
    {
        get; set;
    }

    public int SupportedChannelCount
    {
        get; set;
    }
}

//I want this inheritance as some other class wants to access the values.
public abstract class InstrumentConfigReadWrite : InstrumentConfigValues
{
    protected ReturnCodes PopulateValuesFromJObject(JObject jObject, string path)
    {
        try
        {
            if (JsonConvert.DeserializeObject<InstrumentConfigValues>(jObject.ToString()) == null)
            {
                return ReturnCodes.ErrorReadingFile;
            }
        }
        catch (JsonSerializationException jex)
        {
            SystemDebugLogLogger.LogException(jex, "Invalid Instrument Config File Values. Data needs to be copied over.");
            return ReturnCodes.ErrorReadingFile;
        }
        return ReturnCodes.Success;
    }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
nikhil
  • 1,578
  • 3
  • 23
  • 52
  • What do you expect `[JsonProperty(InstrumentConfig.LowDiskpace.ToString()]` to do? If this did work, the property _name_ would become `LowDiskpace` (disk pace?) – ProgrammingLlama Feb 12 '19 at 02:03
  • Possible duplicate of [.NET - JSON serialization of enum as string](https://stackoverflow.com/questions/2441290/net-json-serialization-of-enum-as-string) – ESG Feb 12 '19 at 02:04
  • Possible duplicate of [An attribute argument must be a constant expression, ...- Create an attribute of type array](https://stackoverflow.com/questions/25859094/an-attribute-argument-must-be-a-constant-expression-create-an-attribute-of) – ProgrammingLlama Feb 12 '19 at 02:05
  • Just to be clear, you want to serialize instances of `InstrumentConfigValues` as a simple string, right? If so, see [Json.Net: Serialize/Deserialize property as a value, not as an object](https://stackoverflow.com/a/40480742/3744182). If not, could you please clarify your question with a [mcve] showing example(s) of your models and the JSON you want to generate? – dbc Feb 12 '19 at 02:44
  • In my situation I want to ensure that the keys in the json file maps to the names in the enum. Eg: in my enum if I have lowdiskspace And in my json I have lowdiskspace:25, then the keys both match. Otherwise if it's something like lowdisksp:25 then I want to say it's an invalid json file because the keys mismatch. – nikhil Feb 12 '19 at 04:01

1 Answers1

5

As long as you're using a current compiler, you can use nameof.

[JsonProperty(nameof(InstrumentConfig.LowDiskpace))]

If you try using this, and get an error like Compilation error: The name 'nameof' does not exist in the current context, that means you're not using a current compiler. The nameof keyword was introduced in C# 6.0/Visual Studio 2015--anything newer than that should be fine.

xander
  • 1,689
  • 10
  • 18