Now using RavenDb 3. Serializing anonymous type changed behaviour.

102 views
Skip to first unread message

Nick

unread,
Nov 17, 2015, 4:43:35 PM11/17/15
to RavenDB - 2nd generation document database

There is a stackoverflow question here because I first thought this was related to json.net 7.0.1, then realized the converted I was using is in RavenDb.

Let me copy the question here. You can answer on SO if you prefer.

So far I have always used this piece of code to serialize anonymous types:

    using Raven.Imports.Newtonsoft.Json;

    var anon = new { errors = new string[] { "Login error" } };
    var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
    var str = JsonConvert.SerializeObject(anon, Formatting.None, settings);

It was with json.net embedded inside RavenDb 2.x.

Recently I started a new project. It uses latest RavenDb 3.x.

Now when I serialize an anonymous type, I get other fields in the result string, for ex:

    {"<errors>i__Field":["Login error"],"errors":["Login error"]}

Thanks

Michael Yarichuk

unread,
Nov 17, 2015, 4:48:04 PM11/17/15
to RavenDB - 2nd generation document database
Do you have a failing test?

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Best regards,

 

Michael Yarichuk

RavenDB Core Team

Tel: 972-4-6227811

Fax:972-153-4-6227811

Email : michael....@hibernatingrhinos.com

 

RavenDB paving the way to "Data Made Simple" http://ravendb.net/  

Nick

unread,
Nov 17, 2015, 4:55:48 PM11/17/15
to RavenDB - 2nd generation document database
To see it in action, just write this (with Raven 3.0.3800):

using System;
using Raven.Imports.Newtonsoft.Json;
using Raven.Imports.Newtonsoft.Json.Serialization;

public class Program
{
    public static void Main()
    {

        var anon = new { errors = new string[] { "Login error" } };
        var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
        var str = JsonConvert.SerializeObject(anon, Formatting.None, settings);
        Console.WriteLine(str);

Oren Eini (Ayende Rahien)

unread,
Nov 18, 2015, 1:11:30 AM11/18/15
to ravendb
It is related to the default approach to serializing fields or properties.
You can set it in the JsonSeiralizerSettings.

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


On Tue, Nov 17, 2015 at 11:43 PM, Nick <cadi...@gmail.com> wrote:

--

Nick

unread,
Nov 18, 2015, 7:17:30 AM11/18/15
to RavenDB - 2nd generation document database
Thanks.
But quite strange you changed that between 2 and 3. Why this change? It was perfect before. Now it needs more code to serialize an anonymous object (which I do from a lot of places to return json objects from asp.net mvc actions) and it is different from core json.net.


> You can set it in the JsonSeiralizerSettings.

How?

Oren Eini (Ayende Rahien)

unread,
Nov 18, 2015, 10:32:42 AM11/18/15
to ravendb
We changed a whole bunch of stuff, in this case, IIRC, it was done to enable us to capture private auto properties.

You can select this via:

JsonContractResolver = new DefaultRavenContractResolver(shareCache: true)
            {
                DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance
            };

Nick

unread,
Nov 18, 2015, 10:43:07 AM11/18/15
to RavenDB - 2nd generation document database
ok, can do that. However DefaultMembersSearchFlags is now deprecated. This means one have to create a derived class of the resolver.
Reply all
Reply to author
Forward
0 new messages