All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sencha.gxt.data.shared.writer.JsonWriter Maven / Gradle / Ivy

/**
 * Ext GWT 3.0.0-rc - Ext for GWT
 * Copyright(c) 2007-2011, Sencha, Inc.
 * [email protected]
 *
 * http://sencha.com/license
 */
package com.sencha.gxt.data.shared.writer;

import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.shared.AutoBeanFactory;

/**
 * An AutoBeanWriter that writes an auto bean into Json.
 * 
 * @param  the starting data format for the model to be inputed
 */
public class JsonWriter extends AutoBeanWriter {

  /**
   * Creates a new JSON writer for auto beans.
   * 
   * @param factory the auto bean factory
   * @param clazz the target class
   */
  public JsonWriter(AutoBeanFactory factory, Class clazz) {
    super(factory, clazz);
  }

  public String write(M model) {
    if (model == null) {
      return "null";
    }
    AutoBean autobean = getAutoBean(model);
    if (autobean == null) {
      throw new RuntimeException(
          "Could not serialize "
              + model.getClass()
              + " using Autobeans, it appears to not be backed by an autobean. You may need to implement your own DataWriter.");
    }
    return AutoBeanCodex.encode(autobean).getPayload();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy