SerializeObject<T>(T item, .., ..)

Feb 13, 2011 at 7:34 PM
Edited Feb 14, 2011 at 10:49 PM

Hi,

I have a scenario where I have a baseclass e.g "ItemBase" which is extended by "ComplexItem". When I eserialize an item declared and instanced as ComplexItem I would like to pass ItemBase as T or as Type so that only members of the ItemBase are serialized. How can I achieve this?

E.g.

public class ItemBase
{
    public string Name {get;set;}

public class ComplexItem : ItemBase
{
    public Stream Source {get;set;}
}

public static class Dummy
{
public static string ToJsonOrEmptyString<T>(T item) where T : class
    {
        if (item == null)
            return string.Empty;
        
        return JsonConvert.SerializeObject(item, Formatting.None, SerializerSettings);
    }
}

When calling this with: 

var item = new ComplexItem { Name = "Test" };

var json = Dummy.ToJsonOrEmptyString<ItemBase>(item);

I want the members of ItemBase and not the members of ComplexItem to be serialized. How do I do this? Json.Net looks at the actual type of the item being passed.

//Daniel

Coordinator
Feb 16, 2011 at 7:47 AM

What about marking the base item with JSON attributes?

    [JsonObject(MemberSerialization.OptIn)]
    public class ItemBase
    {
      [JsonProperty]
      public string Name { get; set; }
    }
When you serialize the complex class only Name will be serialized.

Feb 16, 2011 at 7:48 AM

I want a clean entity.

//Daniel

On Feb 16, 2011 8:47 AM, "JamesNK" <notifications@codeplex.com> wrote:
> From: JamesNK
>
> What about marking the base item with JSON attributes? [JsonObject(MemberSerialization.OptIn)] public class ItemBase { [JsonProperty] public string Name { get; set; } }When you serialize the complex class only Name will be serialized.
>
>
Feb 16, 2011 at 8:10 AM

I have uploaded a patch for this.

//Daniel

On Feb 16, 2011 8:48 AM, "Daniel Wertheim" <daniel@wertheim.se> wrote:
> I want a clean entity.
>
> //Daniel
> On Feb 16, 2011 8:47 AM, "JamesNK" <notifications@codeplex.com> wrote:
>> From: JamesNK
>>
>> What about marking the base item with JSON attributes?
> [JsonObject(MemberSerialization.OptIn)] public class ItemBase {
> [JsonProperty] public string Name { get; set; } }When you serialize the
> complex class only Name will be serialized.
>>
>>
Coordinator
Feb 16, 2011 at 12:00 PM

I know. I don't want to add another SerializeObject like method.

If I was going to add this feature I'd be more likely to add it as an option to the serializer. Options are preferable because they can be set independently and combined together.

Feb 16, 2011 at 1:06 PM

Would the setting be the contract type or what?

//Daniel

On Feb 16, 2011 1:01 PM, "JamesNK" <notifications@codeplex.com> wrote:
> From: JamesNK
>
> I know. I don't want to add another SerializeObject like method.If I was going to add this feature I'd be more likely to add it as an option to the serializer. Options are preferable because they can be set independently and combined together.
>
>