Example usage for com.google.gson GsonBuilder setFieldNamingStrategy

List of usage examples for com.google.gson GsonBuilder setFieldNamingStrategy

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder setFieldNamingStrategy.

Prototype

public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) 

Source Link

Document

Configures Gson to apply a specific naming policy strategy to an object's field during serialization and deserialization.

Usage

From source file:org.chililog.server.common.JsonTranslator.java

License:Apache License

/**
 * <p>//from w  ww.j  av a2  s . c o  m
 * Singleton constructor that creates our reusable GSON object.
 * </p>
 * 
 * <p>
 * If there are any errors, the JVM is terminated. Without valid application properties, we will fall over elsewhere
 * so might as well terminate here.
 * </p>
 */
private JsonTranslator() {
    try {
        GsonBuilder builder = new GsonBuilder();
        if (AppProperties.getInstance().getJsonPretty()) {
            builder.setPrettyPrinting();
        }
        builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        builder.setFieldNamingStrategy(new ChiliLogFieldNamingStrategy());
        builder.disableHtmlEscaping();
        _gson = builder.create();
    } catch (Exception e) {
        _logger.error("Error initializing JSON translator: " + e.getMessage(), e);
        System.exit(1);
    }
}

From source file:org.wso2.developerstudio.eclipse.gmf.esb.diagram.debugger.channel.messagefactory.impl.JsonGsonMessageFactory.java

License:Open Source License

@Override
public String createGetPropertiesCommand(GetPropertyCommand command) {

    GsonBuilder builder = new GsonBuilder();
    builder.setFieldNamingStrategy(new PojoToGsonCustomNamingStrategy());
    Gson propertyCommandMessage = builder.create();
    return propertyCommandMessage.toJson(command);
}

From source file:org.wso2.developerstudio.eclipse.gmf.esb.diagram.debugger.channel.messagefactory.impl.JsonGsonMessageFactory.java

License:Open Source License

@Override
public String createBreakpointCommand(AbstractESBDebugPointMessage debugPoint) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(ESBMediatorPosition.class, new MediatorPositionGsonSerializer());
    builder.setFieldNamingStrategy(new PojoToGsonCustomNamingStrategy());
    Gson debugPointMessage = builder.create();
    return debugPointMessage.toJson(debugPoint);
}

From source file:org.wso2.developerstudio.eclipse.gmf.esb.diagram.debugger.channel.messagefactory.impl.JsonGsonMessageFactory.java

License:Open Source License

@Override
public JsonObject createMediatorLocationJsonObj(AbstractESBDebugPointMessage debugPoint) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(ESBMediatorPosition.class, new MediatorPositionGsonSerializer());
    builder.setFieldNamingStrategy(new PojoToGsonCustomNamingStrategy());
    Gson debugPointMessage = builder.create();
    String jsonString = debugPointMessage.toJson(debugPoint);
    JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
    jsonObject.remove(COMMAND_ARGUMENT_LABEL);
    return jsonObject;
}

From source file:org.wso2.developerstudio.eclipse.gmf.esb.diagram.debugger.channel.messagefactory.impl.JsonGsonMessageFactory.java

License:Open Source License

@Override
public String createPropertyChangeCommand(PropertyChangeCommand propertyChangeCommand) {
    GsonBuilder builder = new GsonBuilder();
    builder.setFieldNamingStrategy(new PojoToGsonCustomNamingStrategy());
    Gson propertyChangeMessage = builder.create();
    return propertyChangeMessage.toJson(propertyChangeCommand);
}

From source file:org.wso2.developerstudio.eclipse.gmf.esb.diagram.debugger.debugpoint.impl.ESBDebugPoint.java

License:Open Source License

/**
 * This constructor will create a ESB debug point with related breakpoint
 * marker./*from  ww w  .j  av  a  2s. com*/
 * <p>
 * Attribute values will be converted to string value to add it to a marker attribute.
 * 
 * @param resource
 *            {@link IResource} instance of source file.
 * @param lineNumber
 *            consists the debug point position in the source view.
 * @param attributes
 *            will contain values related to the specific artifact type to
 *            uniquely define the mediator.
 * @param persistent
 * @throws CoreException
 * @see <a href=
 *      "http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fcore%2Fresources%2FIMarker.html">IMarker</a>
 * 
 */
public ESBDebugPoint(final IResource resource, final int lineNumber,
        final AbstractESBDebugPointMessage debugPoint, final boolean persistent) throws CoreException {
    IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
        @Override
        public void run(IProgressMonitor monitor) throws CoreException {
            IMarker marker = resource.createMarker(ESB_BREAKPOINT_MARKER_ID);
            setMarker(marker);
            setEnabled(true);
            ensureMarker().setAttribute(IBreakpoint.ENABLED, true);
            ensureMarker().setAttribute(IBreakpoint.PERSISTED, persistent);
            ensureMarker().setAttribute(IMarker.LINE_NUMBER, lineNumber);
            ensureMarker().setAttribute(IBreakpoint.ID, getModelIdentifier());
            ensureMarker().setAttribute(IMarker.LOCATION, convertDebugPointToString(debugPoint));

        }

        /**
         * This method converts the {@link AbstractESBDebugPointMessage} for
         * a <code>String</code> value using <a
         * href="https://sites.google.com/site/gson/gson-user-guide"
         * >Gson</a> library {@link GsonBuilder}
         * 
         * @param debugPoint
         * @return
         */
        private String convertDebugPointToString(AbstractESBDebugPointMessage debugPoint) {
            GsonBuilder builder = new GsonBuilder();
            builder.registerTypeAdapter(ESBMediatorPosition.class, new MediatorPositionGsonSerializer());
            builder.setFieldNamingStrategy(new PojoToGsonCustomNamingStrategy());
            Gson debugPointMessage = builder.create();
            return debugPointMessage.toJson(debugPoint).toString();
        }

    };
    run(getMarkerRule(resource), runnable);
}