Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataContract serialization of enum member to/from string #77

Open
Deepscorn opened this issue Apr 5, 2017 · 0 comments
Open

DataContract serialization of enum member to/from string #77

Deepscorn opened this issue Apr 5, 2017 · 0 comments

Comments

@Deepscorn
Copy link

What I mean:

[DataContract]
    public enum AnimalKeeper
    {
        [DataMember(Name = "no keeper")]
        None,
        [DataMember(Name = "Mary Rose")]
        Mary,
        [DataMember(Name = "Tom The Third")]
        Tom,
        [DataMember(Name = "Simply, Todd")]
        Todd
    }

[DataContract]
    public class Animal : JsonResponse
    {
        [DataMember(Name = "animal keeper")]
        public AnimalKeeper Keeper { get; private set; }

        [NotNull]
        [DataMember(Name = "animal color")]
        public string Color { get; private set; }
}

Suppose, we have following instance of Animal:
new Animal() { Keeper = "Mary Rose, Color = "red" }

I uncomment

#define SIMPLE_JSON_DATACONTRACT

to make DataContract/DataMember attributes used in serialization.

Expecting serialized result:
{ "animal keeper" : "Mary Rose", "animal color" : "red" }

Actual result
{ "animal keeper" : 1, "animal color" : "red" }

PS I came from java and there we successfully used (for example) com.google.api.client.util.Value attribute to specify [de]serialized string value for each enum member:

public enum CardStatus {
    @Value("active")
    ACTIVE,
    @Value("expired")
    EXPIRED,
    @Value("blocked")
    BLOCKED
}

public class Card {
    @Key("status")
    private CardStatus status;
    @Key("card id")
    private String id;
}

So, serialized example of Card:
{ "card id" : "101010", "status" : "expired" }

PS com.google.api.client.util.Value is in com.google.http-client:google-http-client-android:1.22.0 or com.google.http-client:google-http-client-jackson2:1.22.0

PS I tested code of merge request #64 and it gave me:
{ "animal color" : "red", "animal keeper" : "Mary" }
As you see, sadly, DataMember attribute ignored

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant