Pergunta

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!

Foi útil?

Solução

You can tell the JsonSerializer to include public fields.

ServiceStack.Text.JsConfig.IncludePublicFields = true;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top