3

I have test class which contains some protected and private member:

    public class Doc_PrivateValues : Document
    {
        public int PublicIntProperty { get; set; }
        public int PublicIntField;

        protected int ProtectedIntProperty { get; set; }
        protected int ProtectedIntField;

        private int PrivateIntProperty { get; set; }
        private int PrivateIntField;

        public SimpleDocument PublicDocument;
        protected SimpleDocument ProtectedDocument;
        private SimpleDocument PrivateDocument;

        public SimpleStruct PublicStruct;
        protected SimpleStruct ProtectedStruct;
        private SimpleStruct PrivateStruct;
    }

I save this document into CosmosDB in very Simple Way:

        Microsoft.Azure.Documents.Document result = CosmosClient.CreateDocumentAsync( CosmosCollection.SelfLink, document ).Result.Resource;
        document.id = result.Id;

And the result in database:

{
    "PublicIntField": 2,
    "PublicDocument": {
        "StrVal": "seven",
        "IntVal": 7,
        "id": null
    },
    "PublicStruct": {
        "StrVal": "ten",
        "IntVal": 10
    },
    "PublicIntProperty": 1,
    "id": "58f18ccf-9e0c-41a6-85cd-a601f12a120a",
    "_rid": "mPlfANiOud4BAAAAAAAAAA==",
    "_self": "dbs/mPlfAA==/colls/mPlfANiOud4=/docs/mPlfANiOud4BAAAAAAAAAA==/",
    "_etag": "\"00000000-0000-0000-8b2a-d853688c01d4\"",
    "_attachments": "attachments/",
    "_ts": 1543856923
}

The Document is contains only the public members. How can I save the Non public members as well?

Thx!

György Gulyás
  • 1,290
  • 11
  • 37

1 Answers1

6

JSON.NET doesn't have access to non public properties, that's why it can't process them. It simply can't see them.

What you can do is to write your own ContractResolver which uses reflection to get the non public properties.

Then you can simply provide the JsonSerializerSettings on either the DocumentClient or the operation level.

The way to do that is described here: JSON.Net: Force serialization of all private fields and all fields in sub-classes

Nick Chapsas
  • 6,872
  • 1
  • 20
  • 29