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

Support serializing char data. #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PeterN
Copy link

@PeterN PeterN commented May 16, 2014

Treat a char field as a 1-character string. Deserializing to a char field already works fine.

@prabirshrestha
Copy link
Member

@PeterN could you write some tests for it.

@marcrocny
Copy link

For those landing here with this need, you can try using this extension:

public class HandlesCharJsonSerializerStrategy : PocoJsonSerializerStrategy
{
    protected override bool TrySerializeKnownTypes(object input, out object output)
    {
        // handle Char parameter
        if (input is char)
        {
            output = input.ToString();
            return true;
        }
        // else
        return base.TrySerializeKnownTypes(input, out output);
    }
}

You may also want to include this override, otherwise you'll get deserialization errors if a value is omitted:

    public override object DeserializeObject(object value, Type type)
    {
        // handle empty Char
        if (type == typeof(char) && string.IsNullOrEmpty(value as string)) value = "\0"; 
        return base.DeserializeObject(value, type);
    }

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

Successfully merging this pull request may close these issues.

3 participants