Empty Object when deserializing ViewPort object with ServiceStack

48 views Asked by At

I'm having an issue since I migrated to the latest version of GoogleApi (by Vivet) After analyzing I have identified a problem with deserializing the ViewPort object (this object was changed ). This only happens with the chosen serializer (ServiceStack). Using Newtonsoft returns a populated object with its properties correctly populated as well.

Here is an example of what I mean :

var testSample :  "{\"SouthWest\":{\"Latitude\":62.8271148697085,\"Longitude\":0.4500822697084981},\"NorthEast\":{\"Latitude\":79.82981283029149,\"Longitude\":0.2527802302915022}}";

 var serviceStackResult = ServiceStack.Text.JsonSerializer.DeserializeFromString<ViewPort>(testSample); // {|}
              
 var newtonsoftResult = JsonConvert.DeserializeObject<ViewPort>(Value); //{49.8271148697085,0.8500822697084981|49.82981283029149,0.8527802302915022}

(this is the modification that is causing the error : https://github.com/vivet/GoogleApi/commit/13c3aca7bc2bcc3c03cba25f24c681638abc6a66) I've checked that the deserializer works with one of the properties on its own (Coordinate object)

I've also tried changing the names of the properties to pascalCase then camelCase fand also changing the name to the JsonProperty that was defined in the new commit linked above

Any help would be useful as I'm at a bit of a loss as to how to proceed forward.

Thanks !

1

There are 1 answers

0
Rouqanzhul On

Here is what I did to solve the problem. I wrote my own custom Deserializer to force it to work:

JsConfig<GoogleApi.Entities.Common.ViewPort>.DeSerializeFn = vp =>
        {
                var ne = new Regex("northeast:{(.*?)}");
                var sw = new Regex("southwest:{(.*?)}");
                var longitude = new Regex("longitude:(.*?)", RegexOptions.RightToLeft);
                var latitude = new Regex("latitude:(.*?)longitude");

                vp = vp.ToLower().Replace(" ", "").Replace("\"", "").Replace("address:null", "").Replace(",", "");

                if (ne.Match(vp).Groups.Count > 1 && sw.Match(vp).Groups.Count > 1)
                {
                    var NorthEast = ne.Match(vp).Groups[1].Value;
                    var SouthWest = sw.Match(vp).Groups[1].Value;

                    if (longitude.Match(NorthEast).Groups.Count > 1 && latitude.Match(NorthEast).Groups.Count > 1 && longitude.Match(SouthWest).Groups.Count > 1 && latitude.Match(SouthWest).Groups.Count > 1)
                    {
                        return new GoogleApi.Entities.Common.ViewPort(new Coordinate(Convert.ToDouble(latitude.Match(SouthWest).Groups[1].Value), Convert.ToDouble(longitude.Match(SouthWest).Groups[1].Value)), new Coordinate(Convert.ToDouble(latitude.Match(NorthEast).Groups[1].Value), Convert.ToDouble(longitude.Match(NorthEast).Groups[1].Value)));
                    }
                }
                return new GoogleApi.Entities.Common.ViewPort(new Coordinate(0,0), new Coordinate(0,0));
        };