0

we're sending HTTP request to Bing maps with address details to get back the address map point. The response of the HTTP request is read as stream async, this stream is then deserialized to Bing response format. When I deserialize, I'm getting this error: System.Runtime.Serialization.SerializationException : Error while deserializing the object of type Previseo.Workflows.BingResponse. Unexpected character '<'.

Your help is so appreciated.

I've used Fiddler to see the response of bings which sends it back in the form of JSON:https://i.stack.imgur.com/LPIeW.png

JSON returned as text:

{"authenticationResultCode":"ValidCredentials","brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png","copyright":"Copyright © 2019 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.","resourceSets":[{"estimatedTotal":1,"resources":[{"__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1","bbox":[46.175388336181641,5.1528801918029785,46.231620788574219,5.28734016418457],"name":"01000, Ain, France","point":{"type":"Point","coordinates":[46.203506469726563,5.2217612266540527]},"address":{"adminDistrict":"Auvergne-Rhone-Alpes","adminDistrict2":"Ain","countryRegion":"France","formattedAddress":"01000, Ain, France","locality":"Bourg-en-Bresse","postalCode":"01000"},"confidence":"High","entityType":"Postcode1","geocodePoints":[{"type":"Point","coordinates":[46.203506469726563,5.2217612266540527],"calculationMethod":"Rooftop","usageTypes":["Display"]}],"matchCodes":["Good"]}]}],"statusCode":200,"statusDescription":"OK","traceId":"e1464a431e5e46b9854053ed8ae76ba8|DU00000D78|7.7.0.0|Ref A: 865592F3CBB74AD6ADA8A71D8344F297 Ref B: DB3EDGE0817 Ref C: 2019-08-07T07:56:20Z"}
private MapPoint GetAddressMapPoint(string address, string ville)
{
    int idx;
    string postal, city, url;
    HttpClient client;
    HttpResponseMessage apires;
    DataContractJsonSerializer jsonSerializer;
    BingResponse bingResp = null;

    idx = ville.IndexOf(" ");
    postal = ville.Substring(0, idx);
    city = ville.Substring(idx + 1);
    url = BING_MAPS_BASE_ADDRESS + $"REST/v1/Locations/FR/{postal}/{city}/{address.Trim()}?key={BingMapsKey}";

    client = new HttpClient();
    apires = client.GetAsync(url).Result;
    jsonSerializer = new DataContractJsonSerializer(typeof(BingResponse));
    Stream stream = apires.Content.ReadAsStreamAsync().Result;                
    bingResp = (BingResponse)jsonSerializer.ReadObject(stream); //here is where I'm getting the error 'Unexpected caracter '<'

        if (bingResp.resourceSets.Count > 0)
        {
            if (bingResp.resourceSets[0].resources.Count > 0)
            {
                var point = bingResp.resourceSets[0].resources[0].point;                     
                return point;
            }
        }
    return null;
}

public class BingResponse
{
    [DataMember]
    public string authenticationResultCode { get; set; }
    [DataMember]
    public string brandLogoUri { get; set; }
    [DataMember]
    public string copyright { get; set; }
    [DataMember]
    public List<ResourceSet> resourceSets { get; set; }
    [DataMember]
    public int statusCode { get; set; }
    [DataMember]
    public string statusDescription { get; set; }
    [DataMember]
    public string traceId { get; set; }
}
Marwan
  • 57
  • 12
  • 3
    Have your tried reading the stream yourself as string and see what's in it? – nvoigt Aug 06 '19 at 16:28
  • 2
    Normally this error means you got XML instead of JSON. – nvoigt Aug 06 '19 at 16:31
  • I read and copied the row JSON as text into the question, may you check out if it's XML or where the problem is? @dbc – Marwan Aug 07 '19 at 08:11
  • @nvoigt how can I solve it if it's XML instead of JSON? – Marwan Aug 07 '19 at 08:13
  • 1
    There is not a single "character '<'" in what you think you pass to the serializer. Either it's unbelievably buggy (highly unlikely) or you are not passing it what you *think* you are passing it. Did you actually get that JSON from reading the stream yourself and printing it out somewhere? – nvoigt Aug 07 '19 at 08:25
  • Also, the JSON you posted does not match the class you have. Where did you get the class from? – nvoigt Aug 07 '19 at 08:26
  • I got the stream by reading it to a stream reader, then reading it to a string and printing the string. If you're talking about BingResponse class, this is where it comes from: https://learn.microsoft.com/en-us/bingmaps/rest-services/common-response-description @nvoigt – Marwan Aug 07 '19 at 08:34
  • @Marwan - Given that my answer did not work, what I would do is to separate deserialization from downloading, at least while testing. Download the entire result as a string, log the string, then deserialize it using, say, `DataContractJsonSerializerHelper.GetObject`(jsonString)` from [this answer](https://stackoverflow.com/a/34435623/3744182). Then update your question with the string received when you actually get an error. – dbc Aug 07 '19 at 18:33
  • It may just be that you need to check [`HttpResponseMessage.IsSuccessStatusCode`](https://learn.microsoft.com/en-us/uwp/api/windows.web.http.httpresponsemessage.issuccessstatuscode). – dbc Aug 07 '19 at 18:33
  • @Marwan - *Shall I need to I put my function GetAddressMapPoint in async mode?* This is a good idea in general. – dbc Aug 07 '19 at 18:33

0 Answers0