001/*
002 * Copyright (c) 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo
011 */
012package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers;
013
014import java.io.IOException;
015
016import org.codehaus.jackson.JsonGenerator;
017import org.codehaus.jackson.JsonParser;
018import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
019
020/**
021 * Marshaller for the default ObjectCodec for the java String class instances.
022 *
023 * @author ogrisel
024 * @since 5.7
025 */
026public class StringMarshaller implements JsonMarshaller<String> {
027
028    @Override
029    public String getType() {
030        return "string";
031    }
032
033    @Override
034    public Class<String> getJavaType() {
035        return String.class;
036    }
037
038    @Override
039    public String read(JsonParser jp) throws IOException {
040        jp.nextToken();
041        jp.nextToken();
042        return jp.getText();
043    }
044
045    @Override
046    public void write(JsonGenerator jg, Object value) throws IOException {
047        // wrap as a complex object to pass through the string input micro
048        // parsing used for backward compatibility with OperationInput.
049        jg.writeStartObject();
050        jg.writeStringField("entity-type", getType());
051        jg.writeStringField("value", value.toString());
052        jg.writeEndObject();
053    }
054
055}