Skip to main content

API JSON response for HOS Log can not be deserialized.

BGrah-1344
Original Poster

When I am trying to get HOS Log for specific date range but without restricting by driver the JSON response can not be deserialized due some HOS data contains driver=NotUserId (has to be Driver object with Id). Is there a way to eliminate this data without restricting by userSearch? I tried restrict by statuses = ON, OFF but even with this restriction it still returns data with driver=NotUserId. Thanks.

Join the conversation

You need to be logged in to reply to this post and participate in the Geotab Community discussions.

2 Replies

{ [...], "driver": "NoUserId" }

This is valid JSON and this is what is expected in this case. Are you using an SDK wrapper that's throwing an exception here? If so, which language and version? Again there is no problem in decoding the above JSON or any other response from any Geotab API call. Once decoded, you should handle these cases where you get device=NoDeviceId or driver=NoUserId instead of your expected device={id:'b1'} or driver={id:'b2'}.

BGrah-1344
Original Poster

Thank you for response.

I am not using SDK wrapper. This is about c# and DataContractJsonSerializer.

Code Example:

var request = WebRequest.Create(uri) as HttpWebRequest;

        request.Timeout = 30000;

        request.Method = "POST";

        request.ContentType = "application/json";

        var requestData = Encoding.ASCII.GetBytes(json);

        request.ContentLength = requestData.Length;

        var requestStream = request.GetRequestStream();

        requestStream.Write(requestData, 0, requestData.Length);

        requestStream.Close();

 

        // Get response  

        using (var response = request.GetResponse() as HttpWebResponse)

        {

          // Get the response stream  

          var stream = response?.GetResponseStream();

          if (stream != null)

          {

            return this.DeserializeResponse<T>(stream);

          }

        }

 

public GeoTabResponse<T> DeserializeResponse<T>(Stream stream)

    {

      GeoTabResponse<T> response = null;

      var ms = new MemoryStream();

      stream.CopyTo(ms);

      ms.Position = 0;

 

      var responseReader = new StreamReader(ms);

      var rawJson = responseReader.ReadToEnd();

 

      try

      {

        ms.Position = 0;

        var ser = new DataContractJsonSerializer(typeof(GeoTabResponse<T>));

        response = ser.ReadObject(ms) as GeoTabResponse<T>;And this is HOS class

 

[DataContract]

  public class GeoTabHOS

  {

    [DataMember(Name = "dateTime", EmitDefaultValue = false)]

    public string DateTime { get; set; }

 

    [DataMember(Name = "id", EmitDefaultValue = false)]

    public string Id { get; set; }

 

    [DataMember(Name = "driver", EmitDefaultValue = false)]

    public GeoTabUser Driver { get; set; }

...

  }

[DataContract]

  public class GeoTabUser

  {

    [DataMember(Name = "id", EmitDefaultValue = false)]

    public string Id { get; set; }

...

 

It works great if response does not contain driver "NotUserId".

 

Thanks,

 

Still have questions?