Parse heterogeneous JSON objects - c#

I send JSON objects from a client to a server. In the server I need to do some action based on the object type. I could send a filed type in every request then try to parse JSON against base Message class:
public class Message
{
string type { get; set; }
}
...
JsonSerializer.Deserialize<Message>(data);
then read the type and deserialize again:
public class SpecificMessage : Message
{
public string command;
}
var message = JsonSerializer.Deserialize<SpecificMessage>(data);
But it requires the data to be parsed twice.
I thought about solution like adding message type in front of JSON message but then thought that maybe it's not worth to reinvent the wheel.

Related

What is the proper way to handle a generic object in a WEBAPI method?

The scenario is something like this:
I have this web-api which will handle a variety of payment gateways, and I want to have the same endpoint for all of those.
So, what I have in mind is to get some json data like this:
{
"OperationId":"N0004",
"Generic_Object_That_Will_Change_According_ToThe_GateWay":
{
"Sale_id":1000,
"CodUser":"1000040",
"Email":"teste#teste.com"
}
}
Or this, for some other payment gateway
{
"OperationId":"N044444",
"Generic_Object_That_Will_Change_According_ToThe_GateWay":
{
"Token":1000,
"UserSettings":{
id: "4563345",
name: "Average Joe"
}
}
}
What I want to do is to transform this "Generic_Object_That_Will_Change_According_ToThe_GateWay" in the specific object for each payment gateway (paypal, or some other), becase each one is completely different, but I don't want that to affect the way the client will call this API - I want it to be as flexible as possible, in a way that you just have to pass whatever data in this Generic_Object_That_Will_Change_According_ToThe_GateWay, and I will then cast it to the proper object and then call another endpoint(like an aggragate microservice design) passing this newly created object.
My idea so far, was creating some class with a generic property like this
public class Payment<Gateway>
{
public int OperationId{ get; set; }
public Gateway paymentGateWay{ get; set; }
}
And this property paymentGateWay could be typed according the available payment Gateways.
And then maybe I could get this data in the API method as Object, and do the necessary casts
[Route("api/payment")]
[HttpPost]
public string Compra(Object payment) {
But, to be honest, I don't know if I'm in the right way.
I already know that I can't have a generic method in a web-api endpoint - so what would be the correct way to get this data in my endpoint considering that a part of this json data is flexible/generic and may be cast to a few different objects.
To summarize, I want to handle json data that can be deserialized to a few different known objects, but I don't want to have a different method in my API to handle each one this possible data scenarios.
if you want a generic method in webapi you have to use JObject
something like the following
public void Post([FromBody] JObject testJObject)
{
//here you have to do some additional work in order to parse and get it working for generic entity
}
in addition to this, you can use the Schema validator against any received request and use the factory pattern in order to create the correct object
here an example
var json =
" {\"OperationId\":\"N0004\",\"Generic_Object_That_Will_Change_According_ToThe_GateWay\":{\"Sale_id\":1000,\"CodUser\":\"1000040\"}}";
JsonSchema paypalschema = new JsonSchema();
paypalschema.Type = JsonSchemaType.Object;
paypalschema.Properties = new Dictionary<string, JsonSchema>
{
{"OperationId", new JsonSchema {Type = JsonSchemaType.String}},
{
"Generic_Object_That_Will_Change_According_ToThe_GateWay",
new JsonSchema {Type = JsonSchemaType.Object,Properties = new Dictionary<string, JsonSchema>
{
{"Sale_id", new JsonSchema {Type = JsonSchemaType.Integer}},
{"CodUser", new JsonSchema {Type = JsonSchemaType.String}},
}}
}
};
JObject requestObject = JObject.Parse( json);
bool valid = requestObject.IsValid(paypalschema);
if (valid)
{
//create your GatewayObject here
}
//else check another gateway object
Consider using JObject or String as your input (And then converting to JObject.) Then you can do some type or data checking before casting. Here's an example shows how they use a pre-defined 'type' value provided, but in lieu of that, you can instead look in the JObject for the 'parts' of each unique provider's payload to determine which type to use.
You can have a generic controller to implement the method and instance-controllers, which inherit of the generic controller:
// I'll rename Gateway to TGateway according to the fact, that it is a generic Type parameter.
public class Payment<TGateway>
{
public int OperationId{ get; set; }
public TGateway paymentGateWay{ get; set; }
}
GenericController:
// Don't add a RouteAttribute to this Controller.
public class GenericController<TGateway>: ApiController
{
// The implementation of the method. No RouteAttribute.
[HttpPost]
public string Compra(Payment<TGateway> payment) {...}
}
InstanceController:
// No need to override the method. RouteAttribute.
[Route("api/payment/"+typeof(AGateway).Name)]
public class AGatewayController : GenericController<AGateway>
{}

ServiceStack Json deserializing incorrectely

I've got a RequestDto, let's say Class A Dto, it contains a self defined type property:
// C# code
public Class MyObject
{
public string A { get; set; }
public string B { get; set; }
}
public Class ADto
{
public List<MyObject> MO { get; set;}
}
When I am trying to send the Dto using Json, the Json object looks like this:
{"MO":[{"A":"String","B":"a"},{"A":"String","B":"b"}]}
but the object I am receiving will be null.
However if I change the Json string into:
{MO:[{A:"String",B:"a"},{A:"String",B:"b"}]}
I lose the quotation marks on the objects' names and it works.
The correct format of Json should include those quotation marks right?
Why is this happening?
ServiceStack does serializes and deserializes valid JSON which requires every property name to be quoted however you're saying that the text below works:
{MO:[{A:"String",B:"a"},{A:"String",B:"b"}]}
However this isn't valid JSON, it instead looks like ServiceStack's JSV Format
You haven't mentioned where you're sending the JSV Format or have provided the Raw HTTP Request (for us to work it out), but I'm assuming if you're using Postman (mentioned in your comments) than you're trying to send JSON in the ?QueryString which isn't allowed.
But ServiceStack does support sending complex object graphs on the QueryString using JSV.
Since you're sending a complex type you'd either POST the request as either JSON or www-form-urlencoded Form Data or if you want to pass it in the QueryString you need to convert it to JSV.
In future please include the Raw HTTP Request as this question lacks any context on where you're changing the JSON string, how you're trying to use it or what's actually being sent, where you're sending it to, etc - making it impossible to guess what the issue is.
Change your class to
public Class MyObject
{
public string Mobile { get; set; }
public string Name { get; set; }
}
public Class ADto
{
public List<MyObject> MO { get; set;}
}
Then your json should be
{MO:[{Mobile:"0556604",Name:"Peter"},{Mobile:"4565466",Name:"John"}]}

WCF Return a custom result with an object type

I'm trying return an object from my WCF function call, and if I specify the type in my ServiceModel, the result is sent successfully.
But if I send an object displayme a message like:
This operation is not supported in the wcf test client because it uses type
Code:
[DataContract]
public class ServiceModel
{
[DataMember]
public int status { get; set; }
[DataMember]
public string message { get; set; }
[DataMember]
public requestdVM result { get; set; }
//public object result { get; set; }
}
The problem is similar to: WCF service: Returning custom objects but as solution they change the type
In common there is no such possibility as using base common objects in WCF services.
Here is the example of WSDL type declaration:
<complexType name="TimePeriod">
<all>
<element name="StartTime" type="xsd:timeInstant"/>
<element name="EndTime" type="xsd:timeInstant"/>
</all>
</complexType>
This schema is visible for the clients and then .NET can understand that it can use his own object and transfer it.
In .NET this will be similar to
[DataContract]
public class TimePeriod
{
[DataMember]
public DateTime StartTime {get;set;}
[DataMember]
public DateTime EndTime {get;set;}
}
Java can understand WSDL and use own class and objects; Python can do it, PHP can do it, hope you got the SOA idea.
Now you want to send object that can be anything. It is not possible to do with contracts.
However if you can promise that only .NET is using in your project then there is a solution.
The idea is to serialize .NET object in binary format and give a type name of the object. Then on the client side deserialize it. Of course you cannot simply do it with sending object from .NET to, say, Python.
1) Small object sending in string XML format
[Serializable]
public class Data
{
public string TypeName {get;set;} // typeof(string), typeof(int), etc.
public string Xml {get;set;} // serialized object via XmlSerializer
}
Then after serialization and receiving object in the client it can be deserialized
Data objectData;// we got it from service
var serializer = new XmlSerializer(typeof(objectData.TypeName));
object result;
using (TextReader reader = new StringReader(objectData.Xml))
{
result = serializer.Deserialize(reader);
}
Also binary serialization can be used instead of XML, then there will be byte[] property.
2) Second solution will be using binary stream and write type name in the header of message, the approach is similar to the first point.
All depends on situation, but I recommend it for big files (> 1 MB).

handling JSON response that is not the same each time

i am working with json.net to deserialize json -> C# objects.
it works great in most cases but there are times where rather than getting an array i get object.
so my class (generated using http://json2csharp.com/ and modified to add property).
where i more than 1 arrival methods (such as pick up or ship) it works fine.
public class ArrivalMethods
{
[JsonProperty(PropertyName = "ArrivalMethod")]
public List<string> ArrivalMethod { get; set; }
}
but it breaks where there only 1 arrival method in the json response from my service because i believe json.net is expecting an object like below.
public class ArrivalMethods
{
[JsonProperty(PropertyName = "ArrivalMethod")]
public string ArrivalMethod { get; set; }
}
i am new to json etc. so i am not sure what i am doing wrong. but this throws exception.
Error converting value \"Ship\" to type 'System.Collections.Generic.List`1[System.String]'.
Path 'ProductDetail.SoftHardProductDetails.Fulfillment.
ArrivalMethods.ArrivalMethod', line 1, position 113."
here is where it breaks:
"ArrivalMethods":{
"ArrivalMethod":"Ship"
},
Your JSON should contain [] where there is a list. And if I'm understanding your answer correct, the first class example is the way you want to end up? If so you need the JSON on the bottom to mark ArrivalMethod as a list, wich its not now.
"ArrivalMethods":
{
"ArrivalMethod":["Ship"]
},
To be honest i get a little confused when there is a list with no plural ending.
If you can change the response format that would be the best. It really looks like XML semantics converted to JSON...
If you can't change the response format, you can try to use JsonConverterAttribute to create custom converters for those properties.

JSON RPC Serializing Object with SPECIFIC naming

Im using NewtonSoft linq 2 json to serialize objects from classes straight to a json string
The class object I'm using is very simple :
public class OverviewQuery
{
public string id { get; set; }
public string method { get; set; }
public string Params { get; set; }
public OverviewQuery(string sid, string smethod, string sparam)
{
this.id = sid;
this.method = smethod;
this.Params = sparam;
}
}
If I serialise this, I get the Json string :
"{\"id\":\"1\",\"method\":\"getStockItemDetails\",\"Params\":\"0000000002\"}"
The Oracle server I'm connecting to (through WebAPI's) requires me to use very very specific naming,
here it should be
"{\"id\":\"1\",\"method\":\"getStockItemDetails\",\"Params\":[\"0000000002\"]}"
Is there any way NewtonSoft implemented a way to achieve this formatting ?
Without correct formatting, the only way to send the information is through hardcoding everything..
What the serialiser is doing with your class seems straightforward.
Generally JSON-RPC services will require that the params value in the envelope be a JSON Array (for indexed parameters) or an Object (for named parameters).
Could you just change your class such that Params is an Array of String ?

Categories

Resources