Local.settings.json file is actually container of application level configuration which is used while running functions locally.
It(configurations) has to be added in function Apps Application settings after function is deployed on Azure.
3. Add a new project to the solution to hold the class definition of the incoming message and outgoing message . (It can be done in same project also, but segregation based on purpose is always good)
Inorder.cs represents the incoming message
OrderXml.cs represents the outgoing message
4.
In the first project delete the function1.cs file as we won't be using it rather we will create new SingleObjectMapping.cs in which we will add code to map JSON object to XML message
First create a method named Run, but the method name can be any valid C# method name. Decorate it with Function Name [FunctionName("SingleObjectMapping")] - line 19 in below figure
The order of parameters in the function signature does not matter. For example, you can put the logger parameter before or after trigger or binding parameters and you can put trigger parameters before or after other bindings.
First we create an object of Outgoing message
OrderXml orderXml = new OrderXml(); - line 26 in above figure
Read the http req body as a stream in a string variable requestBody and deserialize it as a JSON - line 28,29 in above figure
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);ToEndAsync();
Now we have the incoming data deserialized in JSON, so the properties can be referenced and for mapping.
We create the orderXml object which we want to return -- line 30-33 in above figure
orderXml.OrderId = data.OrderID;
orderXml.CreatedDate = DateTime.Now.ToLongDateString();
orderXml.OrderStatus = data.status;
orderXml.Total = data.price;
Next we need to serialize above created object in XML, for that we make use of XmlSerializer and providing it typeof(OrderXml) to serialize as per OrderXml model -- line 35-38
StringBuilder stringBuilder = new System.Text.StringBuilder();
XmlSerializer serializer = new XmlSerializer(typeof(OrderXml));
StringWriter sw = new StringWriter(stringBuilder);
serializer.Serialize(sw, orderXml);
Last step, create response message and return it
string responseMessage = stringBuilder.ToString();
return new OkObjectResult(responseMessage);
That's it, our singleobjectmapping function is ready.
Now let's create another function, which will accept array of Json object and transform that into XML message.
Scenario - Mapping multiple Json object into XML
Here the incoming request will be array of order in Json format which needs to be transformed into message having multiple orders but in XML format.
Steps in Solution
1. Add support for holding multiple Json Object in Inorder.cs (highlighted in yellow)
2. Add support for holding multiple xml in OrderXml.cs ( highlighted in yellow)
3. Add new cs file with function for supporting the multiobject mapping
First, decorate the method with a functionname [FunctionName("MultiObjectMapping")]
In the method, create instance of Orders
Orders orders = new();
Read the stream of data received over http and deserialize the same against the Inorders (which we added in step 1), this will return Json array - store it in a variable.
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var jsons = JsonConvert.DeserializeObject<Inorders>(requestBody);
Here we need to create multiple OrderXml objects, same as number of Json object in the array i.e.for each Json object in array OrderXml object will be created and add those in Orders object (see in model definition in step 2 where we have defined Orders as list of OrderXml.
orders.Orderxmls = new List<OrderXml>(); //Initializing Orders object
foreach (var json in jsons.inorders)
{
OrderXml orderXml = new OrderXml(); //Initializing OrderXML object
orderXml.OrderId = json.orderId;
orderXml.CreatedDate = json.createdDate;
orderXml.OrderStatus = json.orderStatus;
orderXml.Total = json.total;
orderXml.Address = new List<Address>(); //Initializing Address object
foreach (var addr in json.addr)
{
Address address = new Address();
address.Type = addr.Type;
address.City = addr.City;
address.State = addr.State;
address.PostalCode = addr.PostalCode;
orderXml.Address.Add(address);
}
orders.Orderxmls.Add(orderXml); //Adding each orderXml in Orders
}
StringBuilder stringBuilder = new System.Text.StringBuilder();
XmlSerializer serializer = new XmlSerializer(typeof(Model.Orders));
StringWriter sw = new StringWriter(stringBuilder);
serializer.Serialize(sw, orders);
string responseMessage = stringBuilder.ToString();
return new OkObjectResult(responseMessage);
That's it, our second function - Multiobjectmapping is also ready.