facebook-csharp-sdk / simple-json

JSON library for .NET 2.0+/SL4+/WP7/WindowsStore with optional support for dynamic and DataContract
MIT License
382 stars 143 forks source link

Support serializing char data. #62

Open PeterN opened 10 years ago

PeterN commented 10 years ago

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

prabirshrestha commented 10 years ago

@PeterN could you write some tests for it.

marcrocny commented 6 years ago

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);
    }