1

While serializing MapRoute object I got JSON data like this:

"{\"RouteName\":\"route1\",\"RouteWaypoints\":[{},{},{},{},{}] }"

RouteWayPoints list are not serialized properly.

using Android.Gms.Maps.Model;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace App3.Model
{
  public class Id
  {
     [JsonProperty(PropertyName = "$oid")]
     public string id { get; set; }
  }

  public class MapRoute
  {
     public Id _id { get; set; }
     public string RouteName { get; set; }
     public List<LatLng> RouteWaypoints { get; set; }
  }
}

Here LatLng is Android.Gms.Maps.Model.LatLng from the Google Maps API for Xamarin.Android.

Serialize command:

string json = JsonConvert.SerializeObject(mapRoute);

to see objects in my list ,click this

4
  • 1
    You say it isn't serialized correctly - why? Does it throw an exception or just not populate? May be because it's incorrectly serializing the base Java.Lang.Object - could be worth trying to implement a model class and serializing that. Commented Jul 21, 2017 at 14:24
  • 4
    @Hesen can you share your LatLng class definition? Commented Jul 21, 2017 at 15:07
  • 1
    That isn't the serialize command you are using, according to your screenshot. You are passing in some JsonSerializerSettings also. What are those settings? Also can you show the code where you set up the waypoints, so we can see what's in them? Commented Jul 21, 2017 at 16:07
  • 1
    @JesusAngulo LatLng is class of the library Android.Gms.Maps.Model i dont write it myself . It contain latitude and longitude of current location. Commented Jul 21, 2017 at 18:58

2 Answers 2

2

I believe Android.Gms.Maps.Model.LatLng is actually a Managed Callable Wrapper proxy for Google APIs for Android's LatLng. Possibly (I cannot test it myself) Json.NET's reflection mechanism is unable to successfully discover the members of such a wrapper type.

If so, the easiest workaround would be to introduce a Data Transfer Object, map the LatLng to the DTO, and serialize that. First, define your types as follows:

// Required for For System.Linq.Enumerable.Select<TSource,TResult>
// https://developer.xamarin.com/api/member/System.Linq.Enumerable.Select%7BTSource,TResult%7D/p/System.Collections.Generic.IEnumerable%7BTSource%7D/System.Func%7BTSource,TResult%7D/
using System.Linq;  

public class LatLngDTO
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }

    public static implicit operator LatLng(LatLngDTO latLng)
    {
        if (latLng == null)
            return null;
        return new LatLng(latLng.Latitude, latLng.Longitude);
    }

    public static implicit operator LatLngDTO(LatLng latLng)
    {
        if (latLng == null)
            return null;
        return new LatLngDTO { Latitude = latLng.Latitude, Longitude = latLng.Longitude };
    }
}

public class Id
{
    [JsonProperty(PropertyName = "$oid")]
    public string id { get; set; }
}

public class MapRoute
{
    public Id _id { get; set; }
    public string RouteName { get; set; }
    public List<LatLngDTO> RouteWaypoints { get; set; }
}

Then, construct your MapRoute as follows:

_mapRoute.RouteWaypoints = (_wayPoints == null ? null : _wayPoints.Select(l => (LatLngDTO)l).ToList());
Sign up to request clarification or add additional context in comments.

5 Comments

i tried your variant of solution first it throw me from debug mode without exception then i made some changes i really dont remember now it loops in public static implicit operator LatLngDTO(LatLng latLng) { if (latLng == null) return null; return new LatLng(latLng.Latitude, latLng.Longitude); } method. I stack there link
@HesenTagiyev - apologies, I answered without unit testing the code. Fixed.
@HesenTagiyev its a bit rude not marking this as the answer, considering your own answer is entirely based on this one
@Mick - mine initially had an error, since corrected. HesenTagiyev's answer corrects it also, albeit in a different way.
@dbc i especially i emphasize that i found it based your answer. And again thank you for answer , it solved 80% of problem .
0

i modified @dbc LatLngDTO class and write two static object returner methods like below:


using Android.Gms.Maps.Model;

     namespace App3.Model
     {
        public class LatLngDTO
        {
          public double Latitude { get; set; }
          public double Longitude { get; set; }

          public LatLngDTO(double lat,double lng)
          {
            Latitude = lat;
            Longitude = lng;
           }

           public static LatLng ToLatLng(LatLngDTO latLng)
           {
             if (latLng == null)
                return null;
             else
                return new LatLng(latLng.Latitude, latLng.Longitude);
            }

            public static LatLngDTO ToLatLngDTO(LatLng latLng)
            {
              if (latLng == null)
                return null;
              else
                return new LatLngDTO(latLng.Latitude, latLng.Longitude);
            }
        }

it solved my issue. And i serialized my model with list of LatLngDTO objects like below:

mapRoute.RouteWaypoints = (_wayPoints == null ? null : _wayPoints.Select(l => LatLngDTO.ToLatLngDTO(l)).ToList());

            string json = JsonConvert.SerializeObject(mapRoute);
            Json = json;
            http.PostHTTPData(urlString, json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.