Web API Serialize/Deserialize Derived types - asp.net-web-api

I have a Web API that returns a list of objects, when the client passes Accept application/json I want my globally registered json formatter to include TypeNameHandling for the derived types during serialization. However this doesn't work and I can't see why this shouldn't work ?
My objects
public class BaseClass
{
public int Id { get; set; }
}
public class SubClass : BaseClass
{
public string SubClassProp { get; set; }
}
public class SubClassA : SubClass
{
public string SubClassAProp { get; set; }
}
public class SubClassB : SubClass
{
public string SubClassBProp { get; set; }
}
WebApiConfig
public static void Register(HttpConfiguration config)
{
var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.Formatting = Formatting.Indented;
settings.NullValueHandling = NullValueHandling.Ignore;
settings.TypeNameHandling = TypeNameHandling.Auto;
}
Web API Controller
public class MyController : ApiController
{
[HttpGet]
public async Task<IList<BaseClass>> GetClasses()
{
return new List<BaseClass>
{
new SubClassA
{
Id = 1,
SubClassProp = "SubClass",
SubClassAProp = "SubClassAProp"
},
new SubClassB
{
Id = 2,
SubClassProp = "SubClass",
SubClassBProp = "SubClassBProp"
}
};
}
}
Call from API Client in same solution
var client = new HttpClient() { BaseAddress = new Uri("uri goes here...")}
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var resp = await client.GetAsync("uri goes here..."));
var jsonContent = await resp.Content.ReadAsStringAsync();
var ListOfClasses = JsonConvert.DeserializeObject<IList<BaseClass>>(jsonContent, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
I'am expecting to get one element which is SubClassA and one that is SubClassB, but both is BaseClass ?
I also want it to be possible to Deserialize json to object in Post method.
And this should be possible for both json and xml

Related

Passing variables / arguments between repeated Activities in MassTransit Courier Routing Slips

I'm looking to integrate the MassTransit Courier Routing Slip features into an existing solution using Azure Functions and ServiceBusTriggers that synchronizes data between two systems, and has to use a SOAP HTTP client. However, I'm struggling to understand how arguments and variables passed between activities are prioritized. This is best explained via a poor mock example itinerary. My assumption was that variables override existing arguments, but I think that was an incorrect assumption.
public class SyncCustomerOrderConsumer : IConsumer<SyncCustomerOrderMessage>
{
public async Task Consume(ConsumeContext<SyncCustomerOrderMessage> context)
{
var slip = this.BuildRoutingSlip(context.Message);
await context.Execute(slip);
}
private RoutingSlip BuildRoutingSlip(SyncCustomerOrderMessage args)
{
var builder = new RoutingSlipBuilder(NewId.NextGuid());
builder.AddVariable("OrderItems", args.OrderItems);
builder.AddActivity(nameof(SyncCustomerActivity), GetActivityCustomer<SyncCustomerActivity, SyncCustomerArgs>() new {
args.Customer
});
builder.AddActivity(nameof(SyncCustomerActivity), GetActivityCustomer<SyncCustomerActivity, SyncCustomerArgs>() new {
args.ShippingAddress
});
builder.AddActivity(nameof(SyncOrderActivity), GetActivityCustomer<SyncOrderActivity, SyncOrderArgs>() new {
args.Order
});
foreach (var item in args.OrderItems)
{
builder.AddActivity(nameof(SyncOrderItemActivity), GetActivityCustomer<SyncOrderItemActivity, SyncOrderItemArgs>(), new
{
OrderItem = args.item
});
}
builder.AddActivity(nameof(SyncSourceActivity), GetActivityCustomer<SyncSourceActivity, SyncSourceArgs>());
return builder.Build();
}
}
public class SyncOrderItemActivity : IExecuteActivity<SyncOrderItemArgs>
{
private readonly IOrderItemWebserviceClient _client;
privater readonly IMapper _mapper;
public SyncOrderItemActivity(IOrderItemWebserviceClient client, IMapper mapper)
{
_client = client;
_mapper = mapper;
}
public async Task<ExecutionResult> Execute(ExecuteContext<SyncOrderItemArgs> context)
{
var args = context.Arguments;
var dto = _mapper.Map<OrderItemDto>(args);
if (args.OrderItem.External.IsNotSynced())
{
var response = await _client.AddAsync(dto);
args.OrderItem.ExternalId = response.Uuid;
args.OrderItem.LastSynced = response.LastUpdated;
}
else
{
var response = await _client.UpdateAsync(dto);
args.OrderItem.LastSynced = response.LastUpdated;
}
// replace the existing order items variable
int index = args.OrderItems.FindIndex(oi => oi.Id == args.OrderItem.Id);
if (index != 1)
args.OrderItems[index] = orderItem;
return context.CompletedWithVariables(new { OrderItem = args.OrderItem, OrderItems = args.OrderItems });
}
}
public class SyncOrderItemArgs
{
public OrderItem OrderItem { get; set; }
public List<OrderItem> OrderItems { get; set; }
}
public class SyncSourceActivity : IExecuteActivity<SyncSourceArgs>
{
private readonly IEventGridClient _client;
privater readonly IMapper _mapper;
public SyncSourceActivity(IEventGridClient client, IMapper mapper)
{
_client = client;
_mapper = mapper;
}
public async Task<ExecutionResult> Execute(ExecuteContext<SyncSourceArgs> context)
{
var args = context.Arguments;
// this is the original list, not the replaced list
foreach (var item in args.OrderItems)
{
await _client.PublishAsync(new OrderItemSyncedEvent { item });
}
return context.Completed();
}
}
public class SyncCustomerOrderMessage
{
public Customer Customer { get; set; }
public Order Order { get; set; }
public List<OrderItem> OrderItems { get; set; }
public Address ShippingAddress { get; set; }
}
The problem here is that the list of activities to deal with each OrderItem is defined as an argument and updated in each call to that SynOrderItemactivity. As the individual item is processed, it is supposed to replace the original item in the list and then pass the entire altered list as a variable into the next iteration of the same activity. However, the list is not the altered list, but the original one.
I guess my question is two-fold:
How do should you best design a routing slip that has a a list if the same activity, where some of the arguments have to be defined, but others are expected to come from the variable?
When it comes to arguments and variables, which ones take priority?
Unknown? I'm not even sure what this question is asking.
Arguments first, variables second. Explicitly specified arguments when adding the activity to the itinerary take precedence, missing arguments are pulled from variables if present, or left at the default/null value.

How to retrieve the serialized field name of an Elasticsearch Nest.Field

I want to get the field name of a field which is created with Infer.Field<MyDocument>(doc => doc.StringField1).
Example code:
using System;
using Nest;
using Xunit;
namespace Elasticsearch.Tests
{
public class MyDocument
{
public string StringField1 { get; set; }
}
public class SerializeField
{
[Fact]
public void TestFieldName()
{
var connectionSettings = new ConnectionSettings(new Uri("http://myesdomain.com:9200"));
var client = new ElasticClient(connectionSettings);
var stringField = Infer.Field<MyDocument>(doc => doc.StringField1);
// TODO: Code to get then name of stringField when serialized
}
}
}
Can I leaverage the client in order to serialize the name of the stringField as it would do in any request?
Fortunately I found the answer myself:
Short:
client.SourceSerializer.Serialize(stringField, ms);
Complete:
using System;
using System.IO;
using System.Text;
using Nest;
using Xunit;
namespace Elasticsearch.Tests
{
public class MyDocument
{
public string StringField1 { get; set; }
}
public class SerializeField
{
[Fact]
public void TestFieldName()
{
var connectionSettings = new ConnectionSettings(new Uri("http://myesdomain.com:9200"));
var client = new ElasticClient(connectionSettings);
var stringField = Infer.Field<MyDocument>(doc => doc.StringField1);
string fieldName;
using (var ms = new MemoryStream())
{
client.SourceSerializer.Serialize(stringField, ms);
ms.Position = 0;
using (var reader = new StreamReader(ms, Encoding.UTF8))
{
fieldName = reader.ReadToEnd();
}
}
Assert.Equal("\"stringField1\"", fieldName);
}
}
}
There is a better solution that uses a Nest.FieldResolver.
using System;
using Nest;
using Xunit;
namespace Elasticsearch.Tests
{
public class MyDocument
{
public string StringField1 { get; set; }
}
public class TestClass
{
[Fact]
public void TestFieldName()
{
var connectionSettings = new ConnectionSettings(new Uri("http://myesdomain.com:9200"));
var fieldResolver = new FieldResolver(connectionSettings);
var stringField = Infer.Field<MyDocument>(doc => doc.StringField1);
var fieldName = fieldResolver.Resolve(stringField);
Assert.Equal("stringField1", fieldName);
}
}
}

How to send complex objects in GET to WEB API 2

Let's say that you have the following code
public class MyClass {
public double Latitude {get; set;}
public double Longitude {get; set;}
}
public class Criteria
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public MyClass MyProp {get; set;}
}
[HttpGet]
public Criteria Get([FromUri] Criteria c)
{
return c;
}
I'd like to know if someone is aware of a library that could transform any object into query string that is understood by a WEB API 2 Controller.
Here is an example of what I'd like
SerializeToQueryString(new Criteria{StartDate=DateTime.Today, EndDate = DateTime.Today.AddDays(1), MyProp = new MyProp{Latitude=1, Longitude=3}});
=> "startDate=2015-10-13&endDate=2015-10-14&myProp.latitude=1&myProp.longitude=3"
A full example with httpClient might look like :
new HttpClient("http://localhost").GetAsync("/tmp?"+SerializeToQueryString(new Criteria{StartDate=DateTime.Today, EndDate = DateTime.Today.AddDays(1), MyProp = new MyProp{Latitude=1, Longitude=3}})).Result;
At the moment, I use a version (taken from a question I do not find again, maybe How do I serialize an object into query-string format? ...).
The problem is that it is not working for anything else than simple properties.
For example, calling ToString on a Date will not give something that is parseable by WEB API 2 controller...
private string SerializeToQueryString<T>(T aObject)
{
var query = HttpUtility.ParseQueryString(string.Empty);
var fields = typeof(T).GetProperties();
foreach (var field in fields)
{
string key = field.Name;
var value = field.GetValue(aObject);
if (value != null)
query[key] = value.ToString();
}
return query.ToString();
}
"Transform any object to a query string" seems to imply there's a standard format for this, and there just isn't. So you would need to pick one or roll your own. JSON seems like the obvious choice due to the availability of great libraries.
Since it seems no one has dealt with the problem before, here is the solution I use in my project :
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Web;
namespace App
{
public class QueryStringSerializer
{
public static string SerializeToQueryString(object aObject)
{
return SerializeToQueryString(aObject, "").ToString();
}
private static NameValueCollection SerializeToQueryString(object aObject, string prefix)
{
//!\ doing this to get back a HttpValueCollection which is an internal class
//we want a HttpValueCollection because toString on this class is what we want in the public method
//cf http://stackoverflow.com/a/17096289/1545567
var query = HttpUtility.ParseQueryString(String.Empty);
var fields = aObject.GetType().GetProperties();
foreach (var field in fields)
{
string key = string.IsNullOrEmpty(prefix) ? field.Name : prefix + "." + field.Name;
var value = field.GetValue(aObject);
if (value != null)
{
var propertyType = GetUnderlyingPropertyType(field.PropertyType);
if (IsSupportedType(propertyType))
{
query.Add(key, ToString(value));
}
else if (value is IEnumerable)
{
var enumerableValue = (IEnumerable) value;
foreach (var enumerableValueElement in enumerableValue)
{
if (IsSupportedType(GetUnderlyingPropertyType(enumerableValueElement.GetType())))
{
query.Add(key, ToString(enumerableValueElement));
}
else
{
//it seems that WEB API 2 Controllers are unable to deserialize collections of complex objects...
throw new Exception("can not use IEnumerable<T> where T is a class because it is not understood server side");
}
}
}
else
{
var subquery = SerializeToQueryString(value, key);
query.Add(subquery);
}
}
}
return query;
}
private static Type GetUnderlyingPropertyType(Type propType)
{
var nullablePropertyType = Nullable.GetUnderlyingType(propType);
return nullablePropertyType ?? propType;
}
private static bool IsSupportedType(Type propertyType)
{
return SUPPORTED_TYPES.Contains(propertyType) || propertyType.IsEnum;
}
private static readonly Type[] SUPPORTED_TYPES = new[]
{
typeof(DateTime),
typeof(string),
typeof(int),
typeof(long),
typeof(float),
typeof(double)
};
private static string ToString(object value)
{
if (value is DateTime)
{
var dateValue = (DateTime) value;
if (dateValue.Hour == 0 && dateValue.Minute == 0 && dateValue.Second == 0)
{
return dateValue.ToString("yyyy-MM-dd");
}
else
{
return dateValue.ToString("yyyy-MM-dd HH:mm:ss");
}
}
else if (value is float)
{
return ((float) value).ToString(CultureInfo.InvariantCulture);
}
else if (value is double)
{
return ((double)value).ToString(CultureInfo.InvariantCulture);
}
else /*int, long, string, ENUM*/
{
return value.ToString();
}
}
}
}
Here is the unit test to demonstrate :
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Framework.WebApi.Core.Tests
{
[TestClass]
public class QueryStringSerializerTest
{
public class EasyObject
{
public string MyString { get; set; }
public int? MyInt { get; set; }
public long? MyLong { get; set; }
public float? MyFloat { get; set; }
public double? MyDouble { get; set; }
}
[TestMethod]
public void TestEasyObject()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EasyObject(){MyString = "string", MyInt = 1, MyLong = 1L, MyFloat = 1.5F, MyDouble = 1.4});
Assert.IsTrue(queryString.Contains("MyString=string"));
Assert.IsTrue(queryString.Contains("MyInt=1"));
Assert.IsTrue(queryString.Contains("MyLong=1"));
Assert.IsTrue(queryString.Contains("MyFloat=1.5"));
Assert.IsTrue(queryString.Contains("MyDouble=1.4"));
}
[TestMethod]
public void TestEasyObjectNullable()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EasyObject() { });
Assert.IsTrue(queryString == "");
}
[TestMethod]
public void TestUrlEncoding()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EasyObject() { MyString = "&=/;+" });
Assert.IsTrue(queryString.Contains("MyString=%26%3d%2f%3b%2b"));
}
public class DateObject
{
public DateTime MyDate { get; set; }
}
[TestMethod]
public void TestDate()
{
var d = DateTime.ParseExact("2010-10-13", "yyyy-MM-dd", CultureInfo.InvariantCulture);
var queryString = QueryStringSerializer.SerializeToQueryString(new DateObject() { MyDate = d });
Assert.IsTrue(queryString.Contains("MyDate=2010-10-13"));
}
[TestMethod]
public void TestDateTime()
{
var d = DateTime.ParseExact("2010-10-13 20:00", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
var queryString = QueryStringSerializer.SerializeToQueryString(new DateObject() { MyDate = d });
Assert.IsTrue(queryString.Contains("MyDate=2010-10-13+20%3a00%3a00"));
}
public class InnerComplexObject
{
public double Lat { get; set; }
public double Lon { get; set; }
}
public class ComplexObject
{
public InnerComplexObject Inner { get; set; }
}
[TestMethod]
public void TestComplexObject()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new ComplexObject() { Inner = new InnerComplexObject() {Lat = 50, Lon = 2} });
Assert.IsTrue(queryString.Contains("Inner.Lat=50"));
Assert.IsTrue(queryString.Contains("Inner.Lon=2"));
}
public class EnumerableObject
{
public IEnumerable<int> InnerInts { get; set; }
}
[TestMethod]
public void TestEnumerableObject()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EnumerableObject() {
InnerInts = new[] { 1,2 }
});
Assert.IsTrue(queryString.Contains("InnerInts=1"));
Assert.IsTrue(queryString.Contains("InnerInts=2"));
}
public class ComplexEnumerableObject
{
public IEnumerable<InnerComplexObject> Inners { get; set; }
}
[TestMethod]
public void TestComplexEnumerableObject()
{
try
{
QueryStringSerializer.SerializeToQueryString(new ComplexEnumerableObject()
{
Inners = new[]
{
new InnerComplexObject() {Lat = 50, Lon = 2},
new InnerComplexObject() {Lat = 51, Lon = 3},
}
});
Assert.Fail("we should refuse something that will not be understand by the server");
}
catch (Exception e)
{
Assert.AreEqual("can not use IEnumerable<T> where T is a class because it is not understood server side", e.Message);
}
}
public enum TheEnum : int
{
One = 1,
Two = 2
}
public class EnumObject
{
public TheEnum? MyEnum { get; set; }
}
[TestMethod]
public void TestEnum()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EnumObject() { MyEnum = TheEnum.Two});
Assert.IsTrue(queryString.Contains("MyEnum=Two"));
}
}
}
I'd like to thank all the participants even if this is not something that you should usually do in a Q&A format :)

Pass data serialized with Json.net library to the View and bind it with knockout

I am trying to send some serialized data to the view and bind it to the knockout code. I am using json.net library for serialization because I want to pass the constants of an enum to the view ( and not the underlying integers.) I am not sure how my controller returning Json data should look like. Here is the sample code:
My view model that will be serialized:
public class FranchiseInfoViewModel
{
public string FolderName { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public LobbyTemplateOptions LobbyTemplate { get; set; }
public List<LobbyTemplateOptions> LobbyTemplates { get; set; }
public void Initialize()
{
FolderName = "Test";
LobbyTemplate = LobbyTemplateOptions.G;
LobbyTemplates = new List<LobbyTemplateOptions>
{
LobbyTemplateOptions.G,
LobbyTemplateOptions.H,
LobbyTemplateOptions.I
};
Enum:
[JsonConverter(typeof(StringEnumConverter))]
public enum LobbyTemplateOptions
{
G = 7,
H = 8,
I = 9
}
My knockout code:
$(function () {
omega.FranchiseInfo = (function () {
var FolderName = ko.observable();
var LobbyTemplates = ko.observableArray([]);
$.getJSON("FranchiseData", function (data) {
FolderName(data.FolderName);
for (var i = 0; i < data.LobbyTemplate.length; i++) {
LobbyTemplates.push(data.LobbyTemplate[i]);
}
});
return {
folderName: FolderName,
lobbyTemplates: LobbyTemplates
}
} ());
ko.applyBindings(omega.FranchiseInfo);
})
}
I am wondering how my controller that passes serialized Json data to the view should look like as I have not used json.net and I am relatively new to programming:
Controller passing the Json data to the view:
public JsonResult FranchiseData()
{
FranchiseInfoViewModel franchiseInfoViewModel = new FranchiseInfoViewModel();
franchiseInfoViewModel.MapFranchiseInfoToFranchiseInfoViewModel();
string json = JsonConvert.SerializeObject(franchiseInfoViewModel);
// this is how I do it with the default Json serializer
// return Json(franchiseInfoViewModel, JsonRequestBehavior.AllowGet);
}
I would be very gratefull if somebody can post a working example of my controller. Thank You!
You need to implement JsonNetResult.
public class JsonNetResult : ActionResult
{
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public object Data { get; set; }
public JsonSerializerSettings SerializerSettings { get; set; }
public Formatting Formatting { get; set; }
public JsonNetResult()
{
SerializerSettings = new JsonSerializerSettings();
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType)
? ContentType
: "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data != null)
{
JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting };
JsonSerializer serializer = JsonSerializer.Create(SerializerSettings);
serializer.Serialize(writer, Data);
writer.Flush();
}
}
}
To use it, in your case you need to rewrite controller method in this way:
public ActionResult FranchiseData()
{
FranchiseInfoViewModel franchiseInfoViewModel = new FranchiseInfoViewModel();
franchiseInfoViewModel.MapFranchiseInfoToFranchiseInfoViewModel();
JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.Formatting = Formatting.Indented;
jsonNetResult.Data = franchiseInfoViewModel;
return jsonNetResult;
}
(implementation of JsonNetResult above was taken this blog post
http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx )

null reference binding jqgrid to object that contains sub objects

Does anyone know why the following c# mvc.net code gives a null ref exception when databainding the list to the jqgrid?
This is a cutdown example to illustrate the problem.
If I change
DataField = "MyClass.Id"
to
DataField = "Id"
It binds just fine, but in my real code I am trying to bind to an object with 2 subobjects and am trying to display data from both on the same grid.
Any suggestions much appreciated.
Thanks
(I saw this post but it didn't get answered: jqgrid List of objects (with sub objects) as datasource)
internal class MyWrapper
{
public MyClass MyClass { get; set; }
public int Id { get; set; }
}
internal class MyClass
{
public int Id { get; set; }
}
public class TestController : Controller
{
public ActionResult Index()
{
return View(GetGrid());
}
public JsonResult SearchGridDataRequested()
{
return GetGrid().DataBind(GetModel().AsQueryable());
}
private JQGrid GetGrid()
{
return new JQGrid
{
ID = "MyGrid",
DataUrl = Url.Action("SearchGridDataRequested"),
Width = Unit.Pixel(700),
Columns = new List<JQGridColumn>
{
new JQGridColumn
{
DataField = "MyClass.Id",
PrimaryKey = true,
DataType = typeof (int)
}
}
};
}
private static IEnumerable<MyWrapper> GetModel()
{
return new List<MyWrapper>
{
new MyWrapper {Id = 1, MyClass = new MyClass {Id = 11}},
new MyWrapper {Id = 2, MyClass = new MyClass {Id = 12}},
new MyWrapper {Id = 3, MyClass = new MyClass {Id = 13}}
};
}
}

Resources