Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

enum ordinal serialization #15

Open
ias-dt opened this issue Oct 8, 2013 · 0 comments
Open

enum ordinal serialization #15

ias-dt opened this issue Oct 8, 2013 · 0 comments
Assignees
Milestone

Comments

@ias-dt
Copy link

ias-dt commented Oct 8, 2013

Enums serialized via it's ordinal value will get sent in JSON notation as an integer. In piriti in the enum.vm template for the reader there is a check if the sent value is a string. So currently we cannot use enum ordinal serialization. Even if we tried to implement a converter it would not kick in, as it is only called after the string check.

We propose an extension to the enum.vm reader template like the following:

  • if it's not a string try to get the enum value via it's ordinal value

so something like this (beware, not syntax checked or tried)

if (${jsonValue}.isNull() == null) 
{
    JSONString jsonString = ${jsonValue}.isString();
    if (jsonString != null)
    {
// [..]
    } 
    else 
    {
        $valueType[] values = $valueType.values();
        if (0 <= $jsonValue && $jsonValue < values.length) 
                {
            $value = values[$jsonValue];
        }
    }
}

would ensure the reader works out of the box with enums serialized as name and as ordinal without any need for configuration.

For the writer something similar, but with some new configuration switch.

if ($value != null)
{
    #if ($property.converter)
        #createConverter()
        String $valueAsString = ${converter}.serialize($value);
    #else
        #if ($property.asOrdinal)
            String $valueAsString = $value.ordinal();
        #else
            String $valueAsString = String.valueOf($value);
        #end
    #end
    if (isNumber($valueAsString))
    {
        out.append($valueAsString);
    }
    else
    {
        out.append(JsonUtils.escapeValue($valueAsString));
    }
}
else
{
    out.append("null");
}
@ghost ghost assigned hpehl Oct 8, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants