liubiao4123 / servicestack

Automatically exported from code.google.com/p/servicestack
0 stars 0 forks source link

JsonSerializer with Typed dictionnary #40

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
hi, seems to be impossibile to deserialize json when typed dictionary is used.  

        public class Item {
            public int type {get;set;}
            public int color {get;set;}
        }
        public class Basket {
            public Basket() {
                Items=new Dictionary<Program.Item, int>();
            }
            public Dictionary<Item, int> Items {get;set;}
        }

        public static void Main() {
            Basket b = new Basket();
            b.Items.Add(new Item(){type=1,color=2},10);
            b.Items.Add(new Item(){type=4,color=1},20);
            string str = TypeSerializer.SerializeToString<Basket>(b);
            Console.WriteLine(str);
            Basket b2 = TypeSerializer.DeserializeFromString<Basket>(str);
            Console.WriteLine(b2.SerializeToString());
        }

thanks a lot !

Original issue reported on code.google.com by jeanmari...@gmail.com on 4 Oct 2010 at 8:53

GoogleCodeExporter commented 8 years ago
This problem has to do with the key of a JSON object must always be a quoted 
string. And because the key in this case is actually a type, it didn't start 
with a quote. Anyway I've made some changes to encode these 'object keys' as 
strings so it should still be valid JSON and serialize properly now.

Although google code for some reason is refusing to let me upload the new 
binaries. So can you test with the binaries attached.

Cheers,
Demis

Original comment by demis.be...@gmail.com on 5 Oct 2010 at 1:17

Attachments:

GoogleCodeExporter commented 8 years ago
Updated ServiceStack.dll

Original comment by demis.be...@gmail.com on 5 Oct 2010 at 7:36

Attachments:

GoogleCodeExporter commented 8 years ago
Great ! 
works like a charm.

now lets try your lite orm :)

Original comment by jeanmari...@gmail.com on 5 Oct 2010 at 4:37

GoogleCodeExporter commented 8 years ago
Nice! Let me know how OrmLite goes :) 

Note: only SQLite and SQL Server is supported at the moment until there is 
demand to support other dbs or I have a project that needs it :) 

Original comment by demis.be...@gmail.com on 5 Oct 2010 at 4:43