Encoding lists

Jun 4, 2011 at 10:17 PM

I am having trouble trying to encode a list of objects where each object in the list also has a list in.  Keep getting an exception.

Object A:

public class ObjectA {

    List<ObjectB> Objects { get; set; }

}

public class ObjectB {

    public string Name { get; set; }

}

List<ObjectA> myObjects = new List<ObjectA>();

encoder.Encode(myObjects);

The exception says that the encoder does not know how to encode ObjectA.  I can't figure out how to configure the encoder.  All help is greatly appreciated!

Coordinator
Jun 5, 2011 at 10:58 AM

Let's see..
In essence you should create your encoder like this:
JsonEncoder<List<ObjectA>> encoder = Json.EncoderFor<List<ObjectA>>(config => config
    .MapType<ObjectA>(map => map
        .AllFields()
    )
    .MapType<ObjectB>(map => map
        .AllFields()
    )
);

- You could also call Json.EncodeType<List<ObjectA>> , this encodes your input on the fly.
- Also I've included an example application (in the .NET 4 project only), mayby this would help too.

Hope this helps!