1

I serialize this Kontejner class without problems.

public struct Dimenzije
{
    public double duzina, sirina, visina;
    public Dimenzije(double d, double s, double v)
    {
        duzina = d; sirina = s; visina = v;
    }
}

public class Kontejner
{

    [BsonId]
    public string Id { get; set; }

    public string Opis { get; set; }
    public Dimenzije Dimenzije { get; set; }

    [BsonElement]
    public double Zapremina
    {
        get
        {
            return Dimenzije.duzina * Dimenzije.sirina * Dimenzije.visina;
        }
    }

    public bool Cvrsti { get; set; }
    public bool Tecni { get; set; }
    public bool Rasuti { get; set; }
}

When I try to deserialize it, it can't deserialize the Dimenzije field.

I know this is a mapping issue, but I'm pressured into finding a quick answer, and I don't see it in MongoDB docs. Thanks in advance.

4
  • Is Dimenzije a class or struct? Commented Jun 3, 2014 at 20:49
  • It is a struct. Would changing it to class help? Commented Jun 3, 2014 at 20:51
  • I was thinking that. No harm in trying. There is not much info in MongoCSharp Driver documentation about struct data types. And i think this is indeed the glitch as to why you can't de-serialize it. Commented Jun 3, 2014 at 20:53
  • Actually, my bad, it does when done right. I'll post an answer. Commented Jun 3, 2014 at 20:59

1 Answer 1

1

I changed Dimenzije to be a class instead of struct, like this:

public class Dimenzije
{
    public Dimenzije() { }
    public Dimenzije(double d, double s, double v)
    {
        duzina = d; sirina = s; visina = v;
    }
    public double duzina{get; set;}
    public double sirina{get; set;}
    public double visina{get; set;}

}

It works as expected. Notice the presence of Dimenzije() constructor!

Sign up to request clarification or add additional context in comments.

4 Comments

Hope the pressure is released. Congrats. :)
Also, you don't need that constructor. If your constructor does nothing then you don't even need to write it. An empty constructor is created by default in C#.
Yeah, I know. The Dimenzije(double d, double s, double v) does something else. Cheers, mate :)
This does absolutely not reply to original question on how to deserialize a struct with MongoDB driver. Changing a struct into a class is changing the problem, not finding a solution.

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.