3

I am having an issue of how to structure my geolocation object in my ASP.NET Core 3 application using the mongodb.driver

My object

public class Person
{
    [BsonId]
    public ObjectId Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Address { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string PhoneNumber { get; set; }
    public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location { get; set; }
}

I am just doing a straight insertone and passing the object. If I leave out the location it inserts find.

Here is the JSON object I am trying to pass to the api in a simple post.

{
"firstName": "Paul",
"lastName": "Staley",
"email": "[email protected]",
"address": "12345 E 1st Ave",
"state": "AA",
"zipCode": "11111",
"phoneNumber": "555-555-5555",
"location":  {

    "coordinates": [
        0.123321,
        0.3453455
    ]
}

}

The error I get.

System.NotSupportedException: Deserialization of reference types without parameterless constructor is not supported. Type 'MongoDB.Driver.GeoJsonObjectModel.GeoJsonPoint`1[MongoDB.Driver.GeoJsonObjectModel.GeoJson2DGeographicCoordinates]'

3
  • 2
    Follow this example learn.microsoft.com/en-us/dotnet/architecture/microservices/… Commented Mar 23, 2020 at 1:44
  • It looks like "location" was not properly constructed in the JSON string. You can check the proper formatting by serializing the result of something like this: var location = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(new GeoJson2DGeographicCoordinates(32.0, 91.0)); :: this will show you what is expected. Commented Mar 23, 2020 at 3:53
  • Nkosi The example you put up was the easiest way. Thank you. The object it wanted in json was large and convoluted and couldn't get it right. The private set was easy to implement. Commented Mar 28, 2020 at 16:55

1 Answer 1

1

Answer helped by Nksoi

Person Object

 public class Person
 {
    [BsonId]
    public ObjectId Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Address { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string PhoneNumber { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location
    { get; private set; }
    public void SetLocation(double lon, double lat) => SetPosition(lon, lat);


    private void SetPosition(double lon, double lat)
    {
        Latitude = lat;
        Longitude = lon;
        Location = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(
            new GeoJson2DGeographicCoordinates(lon, lat));
    }
}

Controller

 [HttpPost]
    public IActionResult CreatePerson(Person person)
    {
        person.SetLocation(person.Longitude, person.Latitude);
        _personRepository.CreatePerson(person);

        return Ok();
    }

JSON Object

 "firstName": "Paul",
 "lastName": "Staley",
 "email": "[email protected]",
 "address": "12345 E 1st Ave",
 "state": "AA",
 "zipCode": "11111",
 "phoneNumber": "555-555-5555",
 "latitude": 32.0,
 "longitude": 91.0
Sign up to request clarification or add additional context in comments.

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.