0

Here is my model:

namespace RESTalm
{
    [DataContract]
    [KnownType(typeof(Entity))]
    [KnownType(typeof(Field))]
    [KnownType(typeof(Value))]
    public class Entities
    {
        [DataMember(IsRequired = true)]
        public List<Entity> entities;

        [DataMember(IsRequired = true)]
        public int TotalResults;
    }

    [DataContract]
    [KnownType(typeof(Field))]
    [KnownType(typeof(Value))]
    public class Entity
    {
        [DataMember(IsRequired = true)]
        public Field[] Fields;

        [DataMember(IsRequired = true)]
        public String Type;
    }

    [DataContract]
    [KnownType(typeof(Value))]
    public class Field
    {
        [DataMember(IsRequired = true)]
        public String Name;

        [DataMember(IsRequired = true)]
        public Value[] values;
    }

    [DataContract]
    [KnownType(typeof(Value))]
    public class Value
    {
        [DataMember(IsRequired = true)]
        public String value;
    }    
}

Here is my program:

        private String toJSON(Object poco)
        {
            String json;
            DataContractJsonSerializer jsonParser = new DataContractJsonSerializer(poco.GetType());
            MemoryStream buffer = new MemoryStream();

            jsonParser.WriteObject(buffer, poco);
            StreamReader reader = new StreamReader(buffer);
            json = reader.ReadToEnd();
            reader.Close();
            buffer.Close();

            return json;
    }

When the object jsonParser initializes it doesn't seem to recognize my model at all. This leads to an empty MemoryStream(). Please help.

P.S. I have cut-out exception-handling in my program because it's distracting. Thanks. Also, for the moment the object poco is always assumed to be a type in my model.

3
  • And yes; they are all in the same name-space and have all the needed references. Commented Jun 27, 2017 at 10:45
  • 1
    It looks like you need to rewind the stream to the beginning by resetting its Position before you can read from it. Commented Jun 27, 2017 at 10:47
  • @dbc you genious, i love you. Commented Jun 27, 2017 at 10:52

1 Answer 1

1

You need to rewind the stream to the beginning by resetting its Position before you can read from it, for instance like so:

public static string ToJson<T>(T obj, DataContractJsonSerializer serializer = null)
{
    serializer = serializer ?? new DataContractJsonSerializer(obj == null ? typeof(T) : obj.GetType());
    using (var memory = new MemoryStream())
    {
        serializer.WriteObject(memory, obj);
        memory.Seek(0, SeekOrigin.Begin);
        using (var reader = new StreamReader(memory))
        {
            return reader.ReadToEnd();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.