0

I need to extract data from mongodb, but this data differs in coordinates, I can create a base-sub class structure and POST it to mongodb, but the coordinates do not come in the GET operation.

    public  class Geometry
    {
        public string type { get; set; }
        
    }
    public class GeoPoly:Geometry
    {

        public  double[][][] coordinates { get; set; }
    }

    public class GeoMultipoly : Geometry
    {
        public  double[][][][] coordinates { get; set; }
    }

how can I do that

  • Should the serialization convention change and how should it change
  • Is the base-sub class structure suitable for this problem?

database entries:

    {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        [
                            51.263632,
                            60.962938
                        ],
                        [
                            30.884274,
                            20.065517
                        ],
                        [
                            68.832044,
                            14.362602
                        ],
                        [
                            51.263632,
                            60.962938
                        ]
                    ]
                ]
            ]
    },
{
    "type": "MultiPolygon",
    "coordinates": [
        [
            [
                [
                    43.182162,
                    64.042209
                ],
                [
                    14.721334,
                    22.358269
                ],
                [
                    51.263632,
                    17.738761
                ],
                [
                    43.182162,
                    64.042209
                ]
            ]
        ],
        [
            [
                [
                    55.831419,
                    51.446822
                ],
                [
                    65.66973,
                    20.065517
                ],
                [
                    97.64424,
                    37.509124
                ],
                [
                    55.831419,
                    51.446822
                ]
            ]
        ]
    ]
}
XLVII
  • 119
  • 9

2 Answers2

0

For this JSON right class to do is:

public class Rootobject
{
    public string type { get; set; }
    public float[][][][] coordinates { get; set; }
}

For GeoPoly and GeoMultipoly there practical the same. You can procesig this in two steps:

  1. Get the list od Rootobject (List)

switch(g.type)
{
 case "GeoPoly":
  result.Add(new GeoPoly 
  { 
    type = g.type;
    coordinates  = g.coordinates 
  
  });
  break;
 case "GeoMultipoly":
  result.Add(new GeoMultipoly
  { 
    type = g.type;
    coordinates  = g.coordinates 
  
  });
  break;
  default:
  continue;
}

At the end the result list will have a list of Geometry types in right type.

blogprogramisty.net
  • 1,714
  • 1
  • 18
  • 22
0

I am not sure is the best idea, but newtonsoft json.net support serialize and deserialize with $type.

The item will be saved in DB with it's full class identifier.

You can check here

An example:

// {
//   "$type": "Newtonsoft.Json.Samples.Stockholder, Newtonsoft.Json.Tests",
//   "FullName": "Steve Stockholder",
//   "Businesses": {
//     "$type": "System.Collections.Generic.List`1[[Newtonsoft.Json.Samples.Business, Newtonsoft.Json.Tests]], mscorlib",
//     "$values": [
//       {
//         "$type": "Newtonsoft.Json.Samples.Hotel, Newtonsoft.Json.Tests",
//         "Stars": 4,
//         "Name": "Hudson Hotel"
//       }
//     ]
//   }
// }

To use it check newtonsoft documentation here.

Example of usage:

Stockholder stockholder = new Stockholder
{
    FullName = "Steve Stockholder",
    Businesses = new List<Business>
    {
        new Hotel
        {
            Name = "Hudson Hotel",
            Stars = 4
        }
    }
};

string jsonTypeNameAll = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All
});

Ygalbel
  • 5,214
  • 1
  • 24
  • 32