Hello Chen,

from what I understand, you aim at sending a json representation and converting 
it into an Item object. How do you send the json representation? Are you using 
a Web form, an AJAX client, GWT?
In the first case, the json representation is wrapped in a field (I think you 
called it "json" from what I notice in your second mail), but the whole 
representation caught by the server is clearly a web form, so don't update the 
entity's content type.
In this case, you can provide your own converter that accepts Web forms (media 
type application/x-www-form-urlencoded) and transform them into an objects.
I send you such sample converter (not tested). You will also have to register 
it by adding a service descriptor (have a look at the src/META-INF directory in 
the xstream extension). I have to say that I'm not a huge fan of wrapping 
representations into a web form.

In the second case (with AJAX), you seem to use the xstream converter on server 
side (which leverages a patched version of xstream). You have to manually 
generate an entity compatible with the xstream form. It looks like:
{"<package>.Item": {<json representation of Item's class attributes>}}

I hope this will help you.

Best regards,
Thierry Boileau

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2580506
package xstream;

import java.io.IOException;
import java.util.List;

import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.engine.resource.VariantInfo;
import org.restlet.ext.xstream.XstreamConverter;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.representation.Variant;
import org.restlet.resource.UniformResource;

/**
 * Converter that leverages the XStream converter in order to handle
 * representations wrapped in a Web form.
 * 
 */
public class MyConverter extends XstreamConverter {

    private static final VariantInfo VARIANT_APPLICATION_WWW_FORM = new VariantInfo(
            MediaType.APPLICATION_WWW_FORM);

    @Override
    public List<Class<?>> getObjectClasses(Variant source) {
        // Able to generate objects from Web forms.
        List<Class<?>> result = null;
        if (VARIANT_APPLICATION_WWW_FORM.isCompatible(source)) {
            result = addObjectClass(result, Object.class);
        }
        return null;
    }

    @Override
    public <T> float score(Representation source, Class<T> target,
            UniformResource resource) {
        if (MediaType.APPLICATION_WWW_FORM.equals(source.getMediaType())) {
            return 0.7f;
        }

        return 0;
    }

    @Override
    public <T> T toObject(Representation source, Class<T> target,
            UniformResource resource) throws IOException {
        if (source == null) {
            return null;
        }
        if (MediaType.APPLICATION_WWW_FORM.equals(source.getMediaType())) {
            Form form = new Form(source);
            String str = form.getFirstValue("json");
            if (str != null) {
                return super.toObject(new StringRepresentation(str,
                        MediaType.APPLICATION_JSON), target, resource);
            }
        }
        return null;
    }

}

Reply via email to