Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Considering adding support for dynamic in a .NET 4 version of RestSharp. What do you think?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
John Sheehan  
View profile  
 More options Apr 17 2012, 8:14 pm
From: John Sheehan <johnshee...@gmail.com>
Date: Tue, 17 Apr 2012 17:14:52 -0700 (PDT)
Local: Tues, Apr 17 2012 8:14 pm
Subject: Considering adding support for dynamic in a .NET 4 version of RestSharp. What do you think?

public static partial class RestClientExtensions
{
public static RestResponse<dynamic> ExecuteDynamic(this IRestClient client,
IRestRequest request)
{
var response = client.Execute(request);

var generic = (RestResponse<dynamic>)response;
dynamic content = SimpleJson.DeserializeObject(response.Content);
generic.Data = content;

return generic;


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
prabir  
View profile  
 More options Apr 17 2012, 9:01 pm
From: prabir <prabirshres...@yahoo.com>
Date: Tue, 17 Apr 2012 18:01:22 -0700 (PDT)
Local: Tues, Apr 17 2012 9:01 pm
Subject: Re: Considering adding support for dynamic in a .NET 4 version of RestSharp. What do you think?

I don't think RestSharp needs to introduce new method. Why don't you just
make it work for client.Execute<dynamic> or client.Execute<object>

Here is how you would do for strongly typed currently.

            var client = new RestClient("https://graph.facebook.com");
            var result = client.Execute<User>(new RestRequest("4"));
            var user = result.Data;
            string name = user.name;
            string id = user.id;

So make it dynamic if T == typeof(object) and response.content-type ==
"application/json",

In simple json if you pass <T> as System.Object or dynamic, it will just
create either jsonobject/jsonarry/bool/long/string and other primitive
types. So you can use dynamic.

            string json = "{\"id\":\"4\",\"name\":\"Mark Zuckerberg\",\"first_name\":\"Mark\",\"last_name\":\"Zuckerberg\",\"link\": \"http:\\/\\/www.facebook.com\\/zuck\",\"username\":\"zuck\",\"gender\":\"male\",\"locale\":\"en_US\"}";
            var stronglyTypeObject = SimpleJson.DeserializeObject<User>(json);
            var stornglyTypedName = stronglyTypeObject.name;

            dynamic dynamicObject = SimpleJson.DeserializeObject<dynamic>(json);
            var dynamicObjectName = dynamicObject.name;

            dynamic jsonObject = SimpleJson.DeserializeObject<JsonObject>(json);
            var josnObjectName = jsonObject.name;

            dynamic obj = SimpleJson.DeserializeObject<object>(json);
            var objName = obj.name;

and .net 3.5/wp7 users where dynamic is not supported can still continue to
use Execute<object> but instead of dynamic since it returns
JsonObject/JsonArray so you will have to cast it to
IDictionary<string,object> or IList<object>

            var dict = SimpleJson.DeserializeObject<object>(json) as IDictionary<string, object>;
            var dictName = obj["name"];

Nathan Totten has a blog about this on how we solve the dynamic craziness
in various .net platforms/versions in Facebook C# SDK
http://blog.ntotten.com/2010/09/07/dynamic-objects-and-the-facebook-c...

Here is some extra points on how we deal with FB C# SDK.

By default when you install SimpleJson from nuget, JsonArray and JsonObject
classes are public but hidden from intellisense using
[EditorBrowsable(EditorBrowsableState.Never)]. These two classes are the
main feature that allows you to have dynamic. You need to
define SIMPLE_JSON_DYNAMIC conditional symbol though. If you use the
default json serializer provided by the FB C# SDK which is SimpleJson you
get dynamic support out of the box. But if you want to use other json
serializers such as json.net we provide a wrapper which converts JArray to
JsonArray and JObject to JsonObject. Full code can be found at
https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/blob/a914d...

In the end this will look better for those that support dynamic.

            var client = new RestClient("https://graph.facebook.com");
            var result = client.Execute<dynamic>(new RestRequest("4"));
            var user = result.Data;
            string name = user.name;
            string id = user.id;

And for those unlucky .net 3.5 and wp7 devs where dynamic is not supported
you still partially benefit from it.

            var client = new RestClient("https://graph.facebook.com");
            var result = client.Execute<object>(new RestRequest("4"));
            var user = (IDictionary<string, object>)result.Data;
            var name = (string)user["name"];
            var id = (string)user["user"];


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cecil  
View profile  
 More options Apr 18 2012, 2:00 am
From: Cecil <cecilphil...@gmail.com>
Date: Tue, 17 Apr 2012 23:00:08 -0700 (PDT)
Local: Wed, Apr 18 2012 2:00 am
Subject: Re: Considering adding support for dynamic in a .NET 4 version of RestSharp. What do you think?

I agree with @prabir


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Sheehan  
View profile  
 More options Apr 18 2012, 6:54 pm
From: John Sheehan <johnshee...@gmail.com>
Date: Wed, 18 Apr 2012 15:54:31 -0700
Local: Wed, Apr 18 2012 6:54 pm
Subject: Re: Considering adding support for dynamic in a .NET 4 version of RestSharp. What do you think?
I'd prefer to do this without modifying any of the current
deserializer code. That's just more #if def hell I don't want to
maintain. So while I agree with you that would be the ideal API, it's
not going to happen in the current RestSharp.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
prabir  
View profile  
 More options Apr 18 2012, 9:32 pm
From: prabir <prabirshres...@yahoo.com>
Date: Wed, 18 Apr 2012 18:32:30 -0700 (PDT)
Local: Wed, Apr 18 2012 9:32 pm
Subject: Re: Considering adding support for dynamic in a .NET 4 version of RestSharp. What do you think?

You don't need to define any #if def, all the work is already done by
SimpleJson.cs. The only thing you need is to to define SIMPLE_JSON_DYNAMIC
as conditional symbol.

Execute<dynamic> == Execute<object> so typeof(T) == typeof(object) will be
true for both Execute<dynamic> and Execute<object>. so that means no #if def

And as for json.net's JArray to JsonArray and JObject to JsonObject is also
optional. If you don't want to support conversion, then it is great too. I
think SimpleJson is more then enough. The only reason we did this in fb c#
sdk was lots of users were using strongly typed with json.net features and
json.net did not implement the dynamic we wanted it too. This was almost a
year back not sure if its fixed in json.net now.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Sheehan  
View profile  
 More options Apr 26 2012, 1:44 am
From: John Sheehan <johnshee...@gmail.com>
Date: Wed, 25 Apr 2012 22:44:06 -0700 (PDT)
Local: Thurs, Apr 26 2012 1:44 am
Subject: Re: Considering adding support for dynamic in a .NET 4 version of RestSharp. What do you think?

ExecuteDynamic(request) is in  
https://nuget.org/packages/RestSharp/103.1.0-beta

Please test and let me know what you think.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Victor Gladkikh  
View profile   Translate to Translated (View Original)
 More options Jun 26 2012, 6:50 am
From: Victor Gladkikh <victor.gladk...@gmail.com>
Date: Tue, 26 Jun 2012 03:50:52 -0700 (PDT)
Local: Tues, Jun 26 2012 6:50 am
Subject: Re: Considering adding support for dynamic in a .NET 4 version of RestSharp. What do you think?

It fails for me

Unhandled Exception: System.InvalidCastException: Unable to cast object of


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Todd Menier  
View profile  
 More options Sep 7 2012, 3:31 pm
From: Todd Menier <tmen...@gmail.com>
Date: Fri, 7 Sep 2012 12:31:10 -0700 (PDT)
Local: Fri, Sep 7 2012 3:31 pm
Subject: Re: Considering adding support for dynamic in a .NET 4 version of RestSharp. What do you think?

Same here. Others are having the same issue (here<http://www.1771.in/c-build-a-generic-helper-for-restsharp-for-crud-op...>and
here<http://stackoverflow.com/questions/12223820/build-a-generic-helper-fo...>).
Is this a bug or we doing something wrong?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Young  
View profile  
 More options Sep 8 2012, 3:12 am
From: Andrew Young <andrewdyo...@gmail.com>
Date: Sat, 8 Sep 2012 00:12:16 -0700
Local: Sat, Sep 8 2012 3:12 am
Subject: Re: Considering adding support for dynamic in a .NET 4 version of RestSharp. What do you think?

Are you looking for support for dynamic objects? The links you reference look like they can be solved with generics.

--
Andrew Young
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »