Deserializing generically typed classes with Newtonsoft - c#

I have classes that look like this, based on the json being returned by Slack's api:
public class Response<T>
{
public bool ok { get; set; }
public string error { get; set; }
}
public class PostMessage : Response<PostMessage>
{
public string ts { get; set; }
public string channel { get; set; }
public Message message { get; set; }
}
public class ChannelsHistory : Response<ChannelsHistory>
{
public string latest { get; set; }
public List<Message> messages { get; set; }
public bool has_more { get; set; }
}
And I want to write a single method that can bottleneck the call to JsonConvert.DeserializeObject<T>. I don't know too much about the details of the implementation behind that method, but I thought that this would work:
internal static Response<T> GetSlackResponse<T>(List<KeyValuePair<string, string>> parameters = null)
{
Uri slackUri = BuidSlackUri(typeof(T), parameters);
String jsonResponse = GetJson(slackUri);
Response<T> response = JsonConvert.DeserializeObject<Response<T>>(jsonResponse);
if (!response.ok)
{
Aesthetic.Catch("The Slack API failed to respond successfully. " + response.error);
}
return response;
}
Nothing is failing, but not all of the properties I need are being deserialized. For example, a call to GetSlackResponse() will return a Response that has ok set to true, but I won't have access to the Message property of the PostMessage class. I've tried casting (both explicitly and with as), to no avail.
I'm sure I'm missing something simple here, can someone point it out?

Having my various Response class extend a generically typed class was a red herring; it introduced nothing but making the problem more confusing. I only needed the method to be generic, not the type itself.
Changing the relevant line in GetSlackResponse<T>() method from
Response<T> response = JsonConvert.DeserializeObject<Response<T>>(jsonResponse);
to
T response = JsonConvert.DeserializeObject<T>(jsonResponse);
fixed everything. Now my various response types only need to extend my base Response class, and will be properly deserialized by JSON.NET. There is no need for the response to be a generically typed object.

Related

SOAP Response to Tree etc

I have a Question About Soap Responses and how to handle them.
I get a xmlstring from my Response, but have to convert it into this Class to Output into
a specific Window with a view of a tree.
public ExampleTree() { }
public ExampleTree(string nodeContent, List<ExampleTree> subColl)
{
DisplayContent = displayContent;
SubCollection = subCollection;
}
public string DisplayContent { get; set; }
public List<ExampleTree> SubCollection { get; set; }
Does anyone knows which way is the smartest? The hard Thing is that it has to be
generic so I can not use Service References. I just found hardcoded usage, but what
about this case(with subcollections etc.)?

How can I deserialize JSON to a custom type which has a property of type IEnumerable<dynamic>?

I'm writing a console app to retrieve JSON data from a 3rd party API. I have no control over the API data structures or functionality.
Several of the calls I make will return multiple 'pages' of data. The data is a collection of objects of a certain type e.g. User.
I have created classes in my app to match the various data types from the API.
public class User
{
[JsonProperty("id")]
public int ID { get; set; }
[JsonProperty("first_name")]
public string FirstName { get; set; }
[JsonProperty("last_name")]
public string LastName { get; set; }
}
public class FooBar
{
[JsonProperty("foo")]
public string Foo { get; set; }
[JsonProperty("bar")]
public string Bar { get; set; }
}
The API response is always in the same format for these calls. While the actual object types in the "data" array will differ depending on what call has been made.
{
"paging":{"page":1},
"data":[{<object>}, {<object>}, {<object>},...]
}
I have created a class to try to deserialize these. The dynamic[] type for the Data property is for illustrative purposes and I am happy to change it if there is a better approach.
public class ApiResponseObject
{
[JsonProperty("paging")]
public Paging PagingInfo { get; set; }
[JsonProperty("data")]
public dynamic[] Data { get; set; }
}
And I would like to have the Data collection resolve to the appropriate type for the objects it contains. e.g.
string userJson = "{\"paging\":{\"page\":1},\"data\":[{\"id\":1,\"first_name\":\"Joe\",\"last_name\":\"Bloggs\"},{\"id\":2,\"first_name\":\"Jane\",\"last_name\":\"Doe\"}]}"; // json string would come from API
string foobarJson = "{\"paging\":{\"page\":1},\"data\":[{\"foo\":\"Lorem\",\"bar\":\"Ipsum\"},{\"foo\":\"Dolor\",\"bar\":\"Amet\"}]}";
var userResponse = JsonConvert.DeserializeObject<ApiResponseObject>(userJson);
var foobarResponse = JsonConvert.DeserializeObject<ApiResponseObject>(foobarJson);
The deserialization succeeds but the Data collection is of type JObject and cannot be cast into the correct type (User, FooBar).
I am trying to avoid having to write specific response object classes for each request if possible.
I will know what type of object I am expecting in the collection when I am requesting it so I could pass that type to the deserializer but I'm not clear on how to achieve that in this particular scenario.
Something like the below psuedo code would be ideal.
var userResponse = JsonConvert.DeserializeObject<ApiResponseObject<User>>(userJson);
Thanks for your help!
You can use the generic type T, like this :
public class ApiResponseObject<T>
{
[JsonProperty("paging")]
public Paging PagingInfo { get; set; }
[JsonProperty("data")]
public T[] Data { get; set; }
}

Base Class For JSON Data

I'm creating objects to store the JSON data I will be receiving and cannot figure out the right way to structure the objects. Basically, I can be receiving two different objects that only have differences in the body, so I wish to make a base class.
public class SampleBase
{
public string url { get; set; }
public string resource { get; set; }
public string Body body { get; set; }
}
This is an example of the base, with the Body object declared below
public abstract class Body{ }
I then have two separate files for the versions of the base object I can receive, with an example below:
public class SampleObject : SampleBase
{
public class Body
{
public string bodyproperty { get; set; }
}
}
I am doing this just to be efficient with the classes since they share some properties. The SampleBase class will never be called, instead incoming json will be deserialized into SampleObject. Is this best practice?
Edit: Going by this example, the JSON is received as
{
"url": "xxxxxxxxxx",
"resource": "xxxxxxx",
"body": {
"bodyproperty": "xxxx",
}
}
Your class structure can heavily depend on your choice of serializer. For example, the DataContractJsonSerializer can technically handle inherited classes, but it does it in somewhat of a clunky way. You need to define all the known inheritors of your base type on the base type.
Personally, I would use composition rather than inheritance in your case. Here's an example using the DataContractJsonSerializer:
[DataContract]
public class Wrapper<T> where T : Body
{
[DataMember(Name = "url")]
public string Url { get; set; }
[DataMember(Name = "resource")]
public string Resource { get; set; }
[DataMember(Name = "body")]
public string T Body { get; set; }
}
[DataContract]
public class Body
{
[DataMember(Name = "bodyproperty")]
public string BodyProperty { get; set; }
}
Then you'd use the class like any other generic.
Wrapper<Body> obj = new Wrapper<Body>();
Edit: Since this is a MVC application, you'll likely be working with the JavascriptSerializer. The DataContract and DataMember can be ignored but the structure of the classes is still relevant.
var serializer = new JavaScriptSerializer();
var data = serializer.Deserialize<Wrapper<Body>>(json);

WCF return Class with object field

I have a Wcf service that works as RestApi
[KnownType(typeof(myClass1))]
[KnownType(typeof(myClass2))]
[KnownType(typeof(myClassAndOther23typesOmmited))]
[DataContract]
public class ApiResult
{
[DataMember]
public bool Success { get; private set; }
[DataMember]
public object Result { get; private set; }
}
Field Result is problematic part, it cannot be serialized as it's an object type. So the question is, how to return proper ApiResult object.
Note
Althought there is KnownTypeAttribute, service throws SerializationException when i try to assign string[] to ApiResult Result field and return to client.
Upd
After trying ApiResult<T>
Service compiled successfully, and intellisense gives this
After few investigations, gathered that those weird type names are made to avoid collisions in service (it's simple hashcode of type which were achieved by GetHash() of literally object),
This is responce to nvoigt solution as couldnt insert image to comment
Why don't you create a generic ApiResult:
[DataContract]
public class ApiResult<T>
{
[DataMember]
public bool Success { get; internal set; }
[DataMember]
public T Result { get; internal set; }
}
That way, your method can actually return a typed value like ApiResult<myClass1> and you don't need any KnownTypes at all.

JSON Deserialize Error: Parameter cannot be null

Couldn't find an answer from the other Json Serialization issue questions, so maybe someone can help me:
I'm getting a JSON object from a REST api and attempting to Deserialize it to an object. Below is the JSON Object I receive:
{"id":"6wVcZ9ZF67ECUQ8xuIjFT2",
"userId":"83ca0ab5-3b7c-48fe-8019-000320081b00",
"authorizations":["employee","API","trainer","queueAdmin","supervisor","workflowAdmin","realtimeManager","forecastAnalyst","qualityEvaluator","contactCenterManager","teamLead","personnelAdmin","telephonyAdmin","qualityAdmin","businessAdmin","businessUser","accountAdmin","dialerAdmin","contentManagementUser","contentManagementAdmin","admin","api","scriptDesigner","agent","user"],
"primaryAuthorization":"employee",
"thirdPartyOrgName":"in",
"username":"somebody",
"selfUri":"https://blahblahblah.com/api/v1/auth/sessions/6wVcZ9ZF67ECUQ8xuIjFT2"}
And my object I'm attempting to DeSerialize to:
[Serializable]
public class Session : BaseRequest, ISession
{
public Session(string url) : base(url)
{
}
#region Members
[JsonProperty(PropertyName = "userId")]
public string UserId { get; set; }
[JsonProperty(PropertyName = "authorizations")]
public object[] Authorizations { get; set; }
[JsonProperty(PropertyName = "primaryAuthorization")]
public string PrimaryAuthorization { get; set; }
[JsonProperty(PropertyName = "thirdPartyOrgName")]
public string ThirdPartyOrgName { get; set; }
[JsonProperty(PropertyName = "username")]
public string Username { get; set; }
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "selfUri")]
public string SelfUri { get; set; }
#endregion
}
I simply make the web request and get the response stream using a stream reader and return the string. Pretty standard.
However, when I attempt to Deserialize into my Session object it always throws an error: Value Cannot be Null
var serializer = new JsonSerializer();
response = MakePostRequest(true);
var obj = serializer.Deserialize<Session>(new JsonTextReader(new StringReader(response)));
The response is the JSON string I get back from the web request and is exact to what I specified above.
I've done this before but normally I've been the one that designed the REST api. Not the case this time but I can't for the life of my figure out why this won't deserialize? I've specified the JSonProperty PropertyName to avoid issues with proper casing, is this not working right maybe? Any help is appreciated!
UDPATE
I think I found part of the problem. It is attempting to deserialize my base class which consists of :
public abstract class BaseRequest
{
protected BaseRequest(string apiUrl)
{
ApiUrl = apiUrl;
Request = (HttpWebRequest)WebRequest.Create(apiUrl);
}
public string ApiUrl { get; set; }
public string JsonPayload { get; set; }
public HttpWebRequest Request { get; private set; }
}
Is there any directive I can give to prevent it from doing so? Or will I need to refactor around this?
Below code works (using Json.Net):
var session = JsonConvert.DeserializeObject<Session>(json);
public class Session
{
public string Id { get; set; }
public string UserId { get; set; }
public List<string> Authorizations { get; set; }
public string PrimaryAuthorization { get; set; }
public string ThirdPartyOrgName { get; set; }
public string Username { get; set; }
public string SelfUri { get; set; }
}
EDIT
How should I tell it to ignore the base class?
var session = (Session)System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject(typeof(Session));
JsonConvert.PopulateObject(DATA, session);
But I don't think this is a nice way of doing it. Changing your design may be a better solution.
I've tested your code and it works fine, only change I made was removing the constructor, I take it that the serializer can't create an instance on the object for some reason, can you remove
public Session(string url) : base(url)
{
}
Your code works just fine for me but I haven't the BaseRequest source code so I made class with empty constructor.
IMO the exception is coming exactly from there. In the Session constructor the url parameter is null because your JSON object doesn't have url property. May be in the BaseRequest class you use this url param and you receive the Value Can't be Null error.
You can change just the name of parameter if this is the issue:
public Session(string selfUri ) : base(selfUri)
{
}
Check also if the 'response' variable is null. StringReader can throw this exception if you pass null to its constructor.

Categories

Resources