I have an object with private field that contains collection of POCOs. I want to be able to serialize it using the Newtonsoft.Json portable package. What is the appropriate way? In other words what should be the implementation of the contract resolver? Thanks.
Needs to be portable, so using binding flags isn't an option, unless a shim library is introduced.
As to concrete example, think of something like this:
[Serializable]
public class SimpleItemDto
{
public string Name { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
}
public interface ISimpleProvider
{
IEnumerable<SimpleItemDto> GetSimpleItems();
}
[Serializable]
class SimpleProviderBuilder : FakeBuilderBase<ISimpleProvider>
{
private readonly List<SimpleItemDto> _warehouseItemsStorage = new List<SimpleItemDto>();
private SimpleProviderBuilder()
{
}
public static SimpleProviderBuilder CreateBuilder()
{
return new SimpleProviderBuilder();
}
public void WithWarehouseItems(IEnumerable<SimpleItemDto> warehouseItems)
{
_warehouseItemsStorage.Clear();
_warehouseItemsStorage.AddRange(warehouseItems);
}
protected override IServiceCall<ISimpleProvider> CreateServiceCall(IHaveNoMethods<ISimpleProvider> serviceCallTemplate)
{
var setup = serviceCallTemplate
.AddMethodCallWithResult(t => t.GetSimpleItems(), r => r.Complete(GetSimpleItems));
return setup;
}
private IEnumerable<SimpleItemDto> GetSimpleItems()
{
return _warehouseItemsStorage;
}
}
Pay attention that the DTOs might get complex as well so the serialization has to be hierarchical.