문제

I'm trying to use ServiceStack.Redis and i notice that when i store an object with public members and try to get it later on i get null.

I checked and found that ServiceStack.Redis is using ServiceStack.JsonSerializer so I've tried to create a test case and i notice that ServiceStack.JsonSerializer not serializing public members:

        public class ClassA
        {
            public string Id;

            public string Id2 { get; set; }
        }


        static void Main(string[] args)
        {
            ClassA t = new ClassA();
            t.Id = Guid.NewGuid().ToString("n");
            t.Id2 = Guid.NewGuid().ToString("n");

            string a = JsonSerializer.SerializeToString<ClassA>(t);

            ClassA t2 = JsonSerializer.DeserializeFromString<ClassA>(a);

            Console.WriteLine("id " + t2.Id);
            Console.WriteLine("id2 " + t2.Id2);

            Console.ReadLine();
        }

As you can see (if you test that code) that Id (public member) is null and Id2 (public property) is the same string that we entered.

So what i'm doing wrong?

Thanks!

도움이 되었습니까?

해결책

You can tell the JsonSerializer to include public fields.

ServiceStack.Text.JsConfig.IncludePublicFields = true;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top