Arshad Badar Khan Arshad Badar Khan - 1 month ago 17
JSON Question

Json not serializing all object in Azure

I have POCO which is as simple as

public partial class Member
{

public int ID { get; set; }

[Required]
[StringLength(100)]
public string MemberId { get; set; }

public DateTime CreatedOn { get; set; }

[Required]
[StringLength(100)]
public string FirstName { get; set; }}


and a Add command which looks like this

public class AddMemberCommand : ICommand
{
public AddMemberCommand(Member member )
{
ID = Guid.NewGuid();
MemberData = member;
}


public Member MemberData { get; private set; }

public Guid ID { get; }


public string CommandName
{
get { return "AddMemberCommand"; }

}
}


which inherits from

public interface ICommand
{
/// <summary>
/// Gets the command identifier.
/// </summary>
Guid ID { get; }

string CommandName { get; }
}


Now I send this code to a method which initializes Newton Json's serializing setting class with some parameters to return an object. The serializer looks like this

public class JsonTextSerializer
{
private readonly JsonSerializer _serializer;

public JsonTextSerializer()
{
_serializer = JsonSerializer.Create(new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
});
}

public void Serialize(TextWriter writer, object graph)
{
var jsonWriter = new JsonTextWriter(writer);
jsonWriter.Formatting = Formatting.Indented;
_serializer.Serialize(jsonWriter, graph);
writer.Flush();
}

public object Deserialize(TextReader reader)
{
var jsonReader = new JsonTextReader(reader);

try
{
return this._serializer.Deserialize(jsonReader);
}
catch (JsonSerializationException e)
{
// Wrap in a standard .NET exception.
throw new SerializationException(e.Message, e);
}
}
}


The serializer is used to convert the command into a payload for the brokered message as shown below

private BrokeredMessage CreateMessage(POCOS.Member member)
{
var serializer = new JsonTextSerializer();
var command = new AddMemberCommand(member);
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
serializer.Serialize(writer, command);
stream.Position = 0;
BrokeredMessage message = new BrokeredMessage(stream, true);
return message;

}


and there is a another method which receives the method

private POCOS.Member GetPocoFromMessage(BrokeredMessage message)
{
ITextSerializer serializer = new JsonTextSerializer();
AddMemberCommand command;

using (var stream = message.GetBody<Stream>())
{
using (var reader = new StreamReader(stream))
{
var payload = serializer.Deserialize(reader);
command = payload as AddMemberCommand;
}
}

return command.MemberData;
}


The issue is on deserializing some properties ( ID, CommandName) are filled with value except for MemberData which is null.
I can read the stream (by doing a reader.ReadToEnd()) and see it was transferred over the wire but Json can't deserialize all its object
At one time I also thought it perhaps picks only fields in the Interface but that's not the case

Answer

Your MemberData property has a private setter. Since the serializer needs access to the property externally, this setter should be public.

Comments