I'm currently using the Newtonsoft.json
nuget package but I'd like to find a faster alternative. ServiceStack.Text
seems to parse it, but it's returning the JSON in a different format that I'm expecting. Is there anyway to change the format in which this returns the object to match what I'm expecting?
The problem is that after deserializing, response.fullName
returns "joey cool" as expected for Newtonsoft.json
but the ServiceStack.Text
will return a null
because the format is different.
Can I alter the outputting format of the ServiceStack.Text
so that it matches what I'm expecting? I want to call response.fullName
and get "joey cool".
Here is my code using the ServiceStack.Text
component:
T response = a_response.Content.FromJson<T>();
Here is my code using Newtonsoft.json
:
T response = JsonConvert.DeserializeObject<T>(a_response.Content);
Below is how the JSON in text looks as output:
{ "userId": "fc7b4c4e0b6660c7daf711b1d17e0039", "emailAddress": "[email protected]", "fullName": "joey cool", "accountType": "individual", "createdDate": 1440104822411, "phoneNumber": "15555555555", "timezone": "America/Los_Angeles", "photo": "https://stringify.s3.amazonaws.com/users/fc7b4c4e0b6660c7daf711b1d17e0039-profile.jpg", "name": "default", "type": "api" }
Below is how the debugger shows the Newtonsoft.json
object:
Below is how your ServiceStack.Text
JSON Deserializer shows the response object:
----EDIT---- Tried 4.0.62 from NuGet and it gives me an exception.
Message: The type initializer for 'ServiceStack.StringExtensions' threw an exception. Object reference not set to instance of an object, at ServiceStack.StringExtensions..cctor () [0x00017] in :0
-----EDIT-----
URL to a file containing the JSON class
Here's a video demonstrating the usage differences and the strange output
You can try using the latest v4 of ServiceStack.Text which is now free from v4.0.62+ and is available in all Xamarin platforms.
It's had a lot of improvements added to it since v3 so if this behavior was a result of a bug in v3, it's likely fixed now.
Edit:
This class you're trying to serialize is invalid, ServiceStack either serializes Dictionaries or POCO classes with public properties, it doesn't do both, e.g:
[JsonObject (MemberSerialization.OptIn)]
public class UserDataDict : Dictionary<string, object>
{
[JsonProperty]
public string userID { get; set; }
[JsonProperty]
public string emailAddress { get; set; }
[JsonProperty]
public string fullName { get; set; }
[JsonProperty]
public string accountType { get; set; }
[JsonProperty]
public string units { get; set; }
[JsonProperty]
public string unitsDistance { get; set; }
[JsonProperty]
public string newsletterSub { get; set; }
[JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
public string location { get; set; }
[JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
public string phoneNumber { get; set; }
[JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
public string address { get; set; }
[JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
public string photo { get; set; }
[JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
public string createdDate { get; set; }
[JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
public string verifyURL { get; set; }
[JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
public string timezone { get; set; }
[JsonProperty (NullValueHandling = NullValueHandling.Include)]
public APIManifestDict apiManifest { get; set; }
}
I would remove Inheriting the dictionary since it's much more interoperable to have your class contain a dictionary then inherit from one. Also your [JsonProperty]
attributes are JSON.NET specific and have no effect in other serializers so I'd rewrite your class to:
public class UserData
{
public string userID { get; set; }
public string emailAddress { get; set; }
public string fullName { get; set; }
public string accountType { get; set; }
public string units { get; set; }
public string unitsDistance { get; set; }
public string newsletterSub { get; set; }
public string location { get; set; }
public string phoneNumber { get; set; }
public string address { get; set; }
public string photo { get; set; }
public string createdDate { get; set; }
public string verifyURL { get; set; }
public string timezone { get; set; }
public APIManifestDict apiManifest { get; set; }
public Dictionary<string,string> Metadata { get; set; }
}
If you want to include nulls you can specify it with:
JsConfig.IncludeNullValues = true;
But I'd recommend against your App relying on the presence of nulls as your properties are naturally null if their not included in the JSON payload. Including nulls is more fragile, have to cater for multiple definitions of emptiness, inhibits versioning and just bloats the payload.