LitJSON / litjson

JSON library for the .Net framework
https://litjson.net/
Other
1.37k stars 404 forks source link

Can I ignore some certain properties whose modifier is PUBLIC when I serialize an object ? #85

Open BinGuoGuo opened 6 years ago

BinGuoGuo commented 6 years ago

For example , class 'Test' implement interface 'ITest' ,as you know, class 'Test' must has some PUBLIC properties what defined in the interface.But I don't want to serialize the property,so what should I do? Below is the example-code:

        interface ITest
        {
            int TestProperty { get; }
        }
        class Test : ITest
        {
            //The property what I want not to serialize
            public int TestProperty => 0;
            public int Other { get; set; } = 1;
        }

        . . .

        Test t = new Test();
        string result = LitJson.JsonMapper.ToJson(t);

I look forward to your reply :)

devlead commented 6 years ago

Not currently, one workaround is if you make interface members explicit that'll omit the interface property

    interface ITest
    {
        int TestProperty { get; }
    }
    class Test : ITest
    {
        //The property what I want not to serialize
        int ITest.TestProperty => 0;
        public int Other { get; set; } = 1;
    }

    Test t = new Test();
    string result = LitJson.JsonMapper.ToJson(t);

will result in

{"Other":1}
devlead commented 6 years ago

This does seem related to what's requested in #80

BinGuoGuo commented 6 years ago

Thank you for your reply,It's works!:)

BinGuoGuo commented 6 years ago

I've contributed a ideal of it.See here https://github.com/LitJSON/litjson/pull/86