1

I'm having the following class:

[DataContract]
class ExampleClass
{
    //Properties
    [DataMember(Name = "method")]
    private const string method = "Example Constant"; 
    ...
    ...

The DataContractJsonSerializer don't seem to include the constant "method" in the JSON output.

How can I keep the member constant, and cause the class to serialize it?

Thank you.

3
  • 2
    Does it still fail if you make the constant public, rather than private? Another trouble, though, may be that the value of a constant -- and this is to do with what a constant IS -- can't really be set anywhere but in the initializer. Commented Aug 7, 2016 at 15:57
  • 3
    I really don't think you can do that. One thing is serializing that, that could maybe be done, but deserializing that would be impossible. DTO should contain getters and setters. A const have no setter. Commented Aug 7, 2016 at 15:57
  • @EdPlunkett Yes, it still doesn't serialize the constant even if it's public. If I remove the "const" it does get serialized. In addition, serialization works for non-constant private members. Commented Aug 7, 2016 at 16:07

3 Answers 3

4

For the JSON-output the const is not accessable at all as it´s private. However serializing a const makes no sense at all, as a const is a static member and belongs to the class itself, not to a specific instance. Therefor the serializer can´t set it on an instance as there is nothing to set on that instance.

Having said this you may wrap your constant in a property:

[DataContract]
class ExampleClass
{
    private const string method = "Example Constant"; 

    //Properties
    [DataMember(Name = "method")]
    public string Method { get; set; }

    public ExampleClass(this.Method = method; }
}

PS.: I´m not familiar with contract-serializers, but this is what you would do with an Xml-serializer. Maybe with contract-ser you can also have a get-only property thus you won´t need the extra constructor, but I´m not sure about this.

Sign up to request clarification or add additional context in comments.

Comments

3

const is a special keyword in .NET. If you specify field as constant, it would not exist in compiled code, but all the references of constant would be replaced with values of constant at compile time. Therefore this constant does not exist in compiled code and so it is not serialized as well.

The easiest way to fix it is to change constant to regular field (or property) and assign the value in constructor:

[DataContract]
class ExampleClass
{
    public ExampleClass()
    {
        method =  "Example Constant"; 
    }

    [DataMember(Name = "method")]
    private string method;

Alternatively, you can use property:

    [DataMember(Name = "method")]
    private string Method { get; set; }

1 Comment

"but all the references of constant would be replaced with values of constant at compile time" Good to know...
0

Constants are like normal variables. The only difference is that they will replace with the value on compile time.

JSON is a format for transporting data, therefore it does not have any reason for doing something special about constants. But if you have special types like classes or even enums where the name make a difference in the meaning, then there will be additional properties in JSON

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.