1

I am having a strange issue with ServiceStack (SS). The entity I pass to the method is always serialized to empty json string by SS. So s is always "{}". I debug and see that the entity is a hydrated instance with properties with values.

Any ideas why this is the case?

public virtual void Serialize<TEntity>(TEntity entity, Stream stream)
{
    // s is always {}
    var s = JsonSerializer.SerializeToString(entity);

    // rest is not important at this point...
    s = JsvFormatter.Format(s);
    using (var writer = new StreamWriter(stream))
    {
        writer.Write(s);
    }
}

I am editing the question show exactly what the passed in (VolumeCreated) entity is.

public class VolumeEvent : IEvent<VolumeID>
{
    public VolumeEvent(VolumeID identity)
    {
        Identity = identity;
    }

    #region Implementation of IEvent<out VolumeIdentity>

    public VolumeID Identity { get; private set; }

    #endregion
}

public class VolumeCreated : VolumeEvent
{
    public DateTime PublishDate { get; set; }
    public string Title { get; set; }

    public VolumeCreated(VolumeID identity, string title, DateTime publishDate)
        : base(identity)
    {
        Title = title;
        PublishDate = publishDate;
    }
}

before after

kind_robot
  • 2,383
  • 4
  • 31
  • 45

1 Answers1

4

ServiceStack serializes only serializes public properties.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • I edited the question and added the VolumeCreated entity. All properties are public. – kind_robot Aug 11 '12 at 19:05
  • It doesn't show what you're trying to serialize? i.e. VolumeEvent or VolumeCreated? Although inheritance in DTO's is a bad idea: http://stackoverflow.com/a/10759250/85785 - you can emit type info required for inheritance if the base class is abstract. Also if you want to deserialize you need a parameterless constructor. Also ServiceStack serializers lets you serialize directly to a stream - if that's what you were trying to do. – mythz Aug 11 '12 at 21:03
  • I was trying to serialize the VolumeCreated instance. These objects are not DTOs, they are event messages serialized to JSON. I understand and agree with your point to DTOs but this is different. I removed the inheritance and simply implemented the IEvent in VolumeCreated class along with public properties but still no luck. It always serializes to {} json string. – kind_robot Aug 11 '12 at 21:31
  • Can you provide a complete code example of this including test data that produces `{}` output? – mythz Aug 11 '12 at 21:48
  • I took a screen shot of the debug session and added to the question above. There were too much code between the action and this piece of code. I'll try to simplify the code and upload. – kind_robot Aug 12 '12 at 02:56
  • I can't identify the issue or repro it from a screenshot? Can't you make a small stand-alone example that exhibits this behaviour? You're still pointing to a generic method, and I still don't know if you're trying to serialize a base class that's not abstract? (i.e. it needs to be if you want inheritance on DTOs) – mythz Aug 12 '12 at 04:29
  • Update... I swapped the serializer to JSON.NET but same result. So tried this: the class being serialized had [DataContract] attribute one it. I removed it and then ServiceStack serialized correctly; so did JSON.NET. – kind_robot Aug 25 '12 at 04:12
  • [DataContract] makes the type opt-in for serializers. [IgnoreDataMember] on properties makes it opt-out - i.e. the standard DataContract rules. – mythz Aug 25 '12 at 04:15
  • I have found that if I use [DataContract] but do not specify [DataMember] then nothing is serialized; perhaps this has been fixed in later versions of ServiceStack.Text, but judging by this perhaps not. – theMayer Oct 12 '12 at 22:08
  • That is the expected behavior adding `[DataContract]` makes it opt-in – mythz Oct 12 '12 at 22:19