aliencube/CloudEvents.NET

Deserializing of the application/json to ObjectEvent

tanwarsatya opened this issue · 4 comments

The example provided is for using the event from a .net client. Could you provide example how the event will be submitted via PostMan in body and received in .Net Controller. Looks like when trying to deseralize using newtonsoft the conversion of data is failing as it's trying to directly assign the string data to Data object.

{
"cloudEventsVersion" : "0.1",
"eventType" : "com.smarttrack.smartmonitoring.applicationdata",
"eventTypeVersion" : "1.0",
"source" : "/da9a1bcd-43f4-4704-9add-db615d978aa7/Demo1/GPS",
"eventID" : "3b8c3591-14c3-4ca6-9ad9-e314df3b54c9",
"eventTime" : "2015-02-02 09:58:30",
"contentType" : "application/json",
"data" : "{"latitude": "28.657236", "longitude": "77.141235", "speed": "0.1"}"
}

cloudevent = JsonConvert.DeserializeObject<ObjectEvent<LocationPayload>>(message, _jsonsettings);

public class LocationPayload 
    {
        [Required]
        [DataMember]
        [JsonProperty(PropertyName = "latitude")]
        public double latitude;

        [Required]
        [DataMember]
        [JsonProperty(PropertyName = "longitude")]
        public double longitude;


        [Required]
        [DataMember]
        [JsonProperty(PropertyName = "speed")]
        public double speed;

    }

Error converting value "{"latitude": "28.657236", "longitude": "77.141235", "speed": "0.1"}" to type 'SmartMonitoring.Module.Core.Models.Device.LocationPayload'. Path 'data', line 9, position 91.

Could not cast or convert from System.String to SmartMonitoring.Module.Core.Models.Device.LocationPayload.

@tanwarsatya Thanks for reporting this. Will have a look.

Thx, let me know if any help is required.

@tanwarsatya Sorry about the super-duper late response. I just had a look at this, but found there's an issue on your event payload.

The content type of your payload is application/json but the data itself is a stringified JSON object, which is literally a string. If the payload is like (assuming the values are all numerical ones):

{
  "cloudEventsVersion" : "0.1",
  "eventType" : "com.smarttrack.smartmonitoring.applicationdata",
  "eventTypeVersion" : "1.0",
  "source" : "/da9a1bcd-43f4-4704-9add-db615d978aa7/Demo1/GPS",
  "eventID" : "3b8c3591-14c3-4ca6-9ad9-e314df3b54c9",
  "eventTime" : "2015-02-02 09:58:30",
  "contentType" : "application/json",
  "data" : {
    "latitude": 28.657236,
    "longitude": 77.141235,
    "speed": 0.1
  }
}

I confirm that it works fine.

image

Hence, there's nothing this library can do, unless your payload has a correct format.

HTH

And I'm closing this issue.