Im trying to deserialize data that Ive got over POST in JSON format but having some problem.
The error message is:
SerializationException: Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''. System.Runtime.Serialization.XmlObjectSerializerReadContext.HandleMemberNotFound(XmlReaderDelegator xmlReader, ExtensionDataObject extensionData, int memberIndex)
Controller where the serialization is happening:
public String RequestToken(string userData)
{
Contract.Ensures(Contract.Result<string>() != null);
UserModel deserializedUser = new UserModel();
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(userData));
ms.Position = 0;
DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType());
deserializedUser = ser.ReadObject(ms) as UserModel;
}
UserModel that is used as a contract:
using System;
using System.Runtime.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace WishareIntegrationApi.Entities
{
[DataContract]
public class UserModel
{
[BsonId]
[BsonRepresentation(BsonType.String)]
[DataMember]
public ObjectId _id { get; set; }
[DataMember]
public string displayName { get; set; }
[DataMember]
public string photoURL { get; set; }
[DataMember]
public string email { get; set; }
[DataMember]
public int registeredAt { get; set; }
}
}
And an example JSON i'm sending over post:
{"_id":"8kmXH1fzSrVS8PqNLMwyhRH4hBw1","displayName":"Michal Takáč","photoURL":"https://lh3.googleusercontent.com/-xa5oE48RffQ/AAAAAAAAAAI/AAAAAAAACDE/OLrtV5-VIvw/photo.jpg","email":"[email protected]"}
public String RequestToken(UserModel userData)
and MVC will handle deserialization – Shuman