Why is there [JsonObject(MemberSerialization.OptIn)] on BaseObject?

452 views
Skip to first unread message

Andrei Alecu

unread,
Oct 3, 2011, 2:05:16 PM10/3/11
to S#arp Architecture
I've been trying to work with RavenDb and SharpArchitecture and by
default none of the properties in my entities were serialized.

I realized that if I add [JsonProperty] attributes to properties, they
start serializing.

After looking at the source, it appears that BaseObject has this
attribute applied to it:

[JsonObject(MemberSerialization.OptIn)]

And it also seems that the Id property in Entity also has
[JsonProperty] on it.

What is the point of the attribute on the class? I looked in the
source and couldn't find it.

Is it safe to remove?

Thanks.

geoffrey smith

unread,
Oct 3, 2011, 2:33:16 PM10/3/11
to sharp-arc...@googlegroups.com
If that attribute wasn't there, you'd have to do all sorts of nasty things (reflection, recompiling SharpArch), to serialize the attribute. I believe Id is the only property provided by the base object.


--
You received this message because you are subscribed to the Google Groups "S#arp Architecture" group.
To post to this group, send email to sharp-arc...@googlegroups.com.
To unsubscribe from this group, send email to sharp-architect...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sharp-architecture?hl=en.


Andrei Alecu

unread,
Oct 4, 2011, 7:14:40 AM10/4/11
to S#arp Architecture
What do you mean by serialize the "attribute"? What is Json
serialization even used for internally by Sharp Architecture? I
couldn't find any references other than a JsonNet result helper class
for MVC.

In my fork I removed the attribute and was able to use RavenDb because
things now serialize properly. Otherwise the entity would just get
serialized along with the Id property and nothing else inside of it.
Essentially resulting in blank documents.

There are various other issues in the RavenDb implementation which
I'll open an issue on github about.

Andrei Alecu

unread,
Oct 4, 2011, 7:59:53 AM10/4/11
to S#arp Architecture

Geoffrey Smith

unread,
Oct 4, 2011, 8:09:30 AM10/4/11
to sharp-arc...@googlegroups.com
Thanks for your contribution. I meant the property is marked like that because you need to explicitly mark properties to be serialized to json if your object inherits from entity. Or at least that's my recollection

Sent from my iPhone

On Oct 4, 2011, at 6:59 AM, Andrei Alecu <aand...@gmail.com> wrote:

> More details here:
>
> https://github.com/sharparchitecture/Sharp-Architecture/issues/29
>
> On Oct 4, 2:14 pm, Andrei Alecu <aandre...@gmail.com> wrote:
>> What do you mean by serialize the "attribute"? What is Json
>> serialization even used for internally by Sharp Architecture? I

>> couldn't find any referencesother than a JsonNet result helper class

Andrei Alecu

unread,
Oct 4, 2011, 9:38:18 AM10/4/11
to S#arp Architecture
That doesn't seem to be the case. I have added this test to my pull
request:

[Test]
public void EntitySerializesAsJsonProperly()
{
var object1 = new ObjectWithOneDomainSignatureProperty();
object1.SetIdTo(999);
object1.Age = 13;
object1.Name = "Foo";

var jsonSerializer = new JsonSerializer();

string jsonString;
using (var stringWriter = new StringWriter())
{
jsonSerializer.Serialize(stringWriter, object1);
jsonString = stringWriter.ToString();
}

using (var stringReader = new StringReader(jsonString))
using (var jsonReader = new JsonTextReader(stringReader))
{
var deserialized =
jsonSerializer.Deserialize<ObjectWithOneDomainSignatureProperty>(jsonReader);
Assert.IsNotNull(deserialized);
Assert.AreEqual(999, object1.Id);
Assert.AreEqual(13, object1.Age);
Assert.AreEqual("Foo", object1.Name);
}
}

You will actually see this test break *with* the attribute. It only
passes without it.


On Oct 4, 3:09 pm, Geoffrey Smith <smith.geoffre...@gmail.com> wrote:
> Thanks for your contribution. I meant the property is marked like that because you need to explicitly mark properties to be serialized to json if your object inherits from entity. Or at least that's my recollection
>
> Sent from my iPhone
>

Andrei Alecu

unread,
Oct 4, 2011, 9:43:50 AM10/4/11
to S#arp Architecture
Oops, "object1" should be "deserialized" on those last 3 asserts. My
bad. Still passes though.

Alec Whittington

unread,
Oct 4, 2011, 11:28:25 AM10/4/11
to sharp-arc...@googlegroups.com
Please see this: http://groups.google.com/group/sharp-architecture/browse_thread/thread/4f3c91823b349008/dd553cc2dc55c54a?lnk=gst&q=JsonObject

Not sure why that got in there, but it will be coming out. Asbjørn makes a really good point that people can blindly just serialize anything with that attribute on there. This can lead to security issues (they are a possibility) as it could expose things you are not wanting to expose.


With regards to RavenDB, the RTM version of 2.0 will not include support for it. It will either be moved to the contrib project or dropped all together. MongoDB will be used in it's place. The appeal of Raven was the fact you could embed it and thus use it in a shared environment. What we've come to find is that not a lot of our users are running in shared environments. I've also become a huge fan of MongoDB and use it on a daily basis. If RavenDB support means a lot to you, we do accept pull requests.

Alec Whittington
Owner - Webmaster / Developer

Andrei Alecu

unread,
Oct 4, 2011, 12:02:17 PM10/4/11
to S#arp Architecture
I'm actually trying to decide between Mongo and Raven, I like that
Raven has Lucene support, which is better for my project.

Regarding that thread, I think it's a bad practice to serialize
entities directly for public consumption. The good practice would be
to serialize view models instead (using something like automapper).


Either way, the attribute would probably hurt Mongo integration too,
since I believe it uses json as well (not sure if it uses the same
json serializer though)


On Oct 4, 6:28 pm, Alec Whittington <alec.whitting...@gmail.com>
wrote:
> Please see this:http://groups.google.com/group/sharp-architecture/browse_thread/threa...
>
> Not sure why that got in there, but it will be coming out. Asbjørn makes a
> really good point that people can blindly just serialize anything with that
> attribute on there. This can lead to security issues (they are a
> possibility) as it could expose things you are not wanting to expose.
>
> With regards to RavenDB, the RTM version of 2.0 will not include support for
> it. It will either be moved to the contrib project or dropped all together.
> MongoDB will be used in it's place. The appeal of Raven was the fact you
> could embed it and thus use it in a shared environment. What we've come to
> find is that not a lot of our users are running in shared environments. I've
> also become a huge fan of MongoDB and use it on a daily basis. If RavenDB
> support means a lot to you, we do accept pull requests.
>
> Alec Whittington
> Owner - Webmaster / Developerhttp://www.streetsource.com

Alec Whittington

unread,
Oct 4, 2011, 12:20:47 PM10/4/11
to sharp-arc...@googlegroups.com
I can say that it does not affect Mongo at all. Not sure what their driver is using for serialization, but it it seems to ignore it.

I've applied your pull request, so hopefully that will bring us all to a happy middle ground. 
Reply all
Reply to author
Forward
0 new messages