facebook-csharp-sdk / simple-json

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

Can't deserialize GUIDs. #16

Closed danielcrenna closed 12 years ago

danielcrenna commented 12 years ago

^^^^ that.

prabirshrestha commented 12 years ago

Fixed in https://nuget.org/packages/SimpleJson/0.16.0-alpha

SimpleJson was meant to be simple as possible so we provide only few default type serialization. In the mean time we wanted to provide as much flexibility as possible. One of the core feature we wanted in SimpleJson was to customize the serialization/deserialization for your custom types. This is provided by implementing your own IJsonSerializerStrategy. By default we provide two serialization strategies, PocoJsonSerializerStrategy and DataContractJsonSerializerStrategy. Since DataContract is not available in all platforms it will be available only if you add #define SIMPLE_JSON_DATACONTRACT.

You can solve this by overriding one of the above.

public class MyJsonSerializerStrategy : PocoJsonSerializerStrategy
{
    public override object DeserializeObject(object value, Type type)
    {
        if (value is string && type == typeof(Guid))
            return new Guid((string)value);
        return base.DeserializeObject(value, type);
    }
}

Then specify the serilalization strategy.

var json = @"""bed7f4ea-1a96-11d2-8f08-00a0c9a6186d""";
var guid = SimpleJson.DeserializeObject<Guid>(json, new MyJsonSerializerStrategy());

If you don't like passing your custom json serialization strategy you can change it with the following code. Since it is not thread safe, it is recommended to set it during application start.

SimpleJson.CurrentJsonSerializerStrategy = new MyJsonSerializerStrategy();

Then you can just call SimpleJson.DeserializeObject<Guid>(json);


I need to find time to doc. But first I need to have cleaner interface methods for `IJsonSerializerStrategy`
InsidiousForce commented 12 years ago

are the strategies documented anywhere?

prabirshrestha commented 12 years ago

Currently it is not documented you will need to see the code.

Only two strategies are provided out of the box Poco and DataContract most of which should be enough for your needs. If these are not enough then you will need to create your own. If it is something like guid then we will want this to be in the core. What type are you trying to serialize/deserialize?

InsidiousForce commented 12 years ago

I'd like to for instance set up a data model for the result for say posts. And then I'd like to have the data deserialized directly into these objects instead of into a dynamic and then have to walk the dynamic and copy those into a list of objects.

prabirshrestha commented 12 years ago

You can use the following overload methods.

public static T DeserializeObject<T>(string json);
public static object DeserializeObject(string json, Type type);
public static T DeserializeObject<T>(string json, IJsonSerializerStrategy jsonSerializerStrategy);
public static object DeserializeObject(string json, Type type, IJsonSerializerStrategy jsonSerializerStrategy);
InsidiousForce commented 12 years ago

Where? How? Right now when I do a facebook call I just get back a dynamic - I don't have access to the raw json anywhere. Where do I override? I want to call Deserialize in my case, but how to do this for the one call to the FacebookSDK?

prabirshrestha commented 12 years ago

I wouldn't recommend you to ask FB C# SDK stuffs here.

https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/issues/140 https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/pull/141

We never got the updated pull requests. Feel free to send one.

InsidiousForce commented 12 years ago

thanks, I didn't realize this was an email back to post here.