package org.realityforge.replicant.shared.gwt_json;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;

/**
 * Constants used in building up the JSON response to gwt client.
 */
public final class TransportUtil
{
  public static final String ID = "id";
  public static final String LAST_CHANGE_SET_ID = "last_id";
  public static final String CHANGE_SET = "changeset";
  public static final String CHANGES = "changes";
  public static final String ENTITY_ID = ID;
  public static final String TYPE_ID = "type";
  public static final String DATA = "data";

  @Nonnull
  public static JSONObject toObject( final JSONValue value, final String message )
  {
    final JSONObject object = value.isObject();
    if ( null == object )
    {
      throw new IllegalStateException( message + " is not an object" );
    }
    return object;
  }

  @Nonnull
  public static Map<String, Serializable> toMap( final JSONObject value )
  {
    final HashMap<String, Serializable> result = new HashMap<String, Serializable>();
    for ( final String key : value.keySet() )
    {
      final JSONValue attributeValue = value.get( key );
      if ( null != attributeValue.isString() )
      {
        result.put( key, attributeValue.isString().stringValue() );
      }
      else if ( null != attributeValue.isNumber() )
      {
        result.put( key, (int) attributeValue.isNumber().doubleValue() );
      }
      else if ( null != attributeValue.isBoolean() )
      {
        result.put( key, attributeValue.isBoolean().booleanValue() );
      }
      else if ( null != attributeValue.isNull() )
      {
        result.put( key, null );
      }
      else
      {
        throw new IllegalStateException( "Unexpected value for " + key );
      }
    }
    return result;
  }

  @Nonnull
  public static JSONArray toArray( final JSONValue value, final String message )
  {
    final JSONArray result = value.isArray();
    if ( null == result )
    {
      throw new IllegalStateException( message + " is not an array" );
    }
    return result;
  }

  public static int toInteger( final JSONValue value, final String message )
  {
    final JSONNumber number = value.isNumber();
    if ( null == number )
    {
      throw new IllegalStateException( message + " is not a number" );
    }
    return (int) number.doubleValue();
  }

  public static double toDouble( final JSONValue value, final String message )
  {
    final JSONNumber number = value.isNumber();
    if ( null == number )
    {
      throw new IllegalStateException( message + " is not a number" );
    }
    return number.doubleValue();
  }

  @Nonnull
  public static String toString( final JSONValue value, final String message )
  {
    final JSONString result = value.isString();
    if ( null == result )
    {
      throw new IllegalStateException( message + " is not a string" );
    }
    return result.stringValue();
  }
}

@@ -50,347 +50,8 @@
 n;%0A%0A
-import com.google.gwt.json.client.JSONArray;%0Aimport com.google.gwt.json.client.JSONNumber;%0Aimport com.google.gwt.json.client.JSONObject;%0Aimport com.google.gwt.json.client.JSONString;%0Aimport com.google.gwt.json.client.JSONValue;%0Aimport java.io.Serializable;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport javax.annotation.Nonnull;%0A%0A
 /**%0A
@@ -500,2337 +500,7 @@
 ta%22;
-%0A%0A  @Nonnull%0A  public static JSONObject toObject( final JSONValue value, final String message )%0A  %7B%0A    final JSONObject object = value.isObject();%0A    if ( null == object )%0A    %7B%0A      throw new IllegalStateException( message + %22 is not an object%22 );%0A    %7D%0A    return object;%0A  %7D%0A%0A  @Nonnull%0A  public static Map%3CString, Serializable%3E toMap( final JSONObject value )%0A  %7B%0A    final HashMap%3CString, Serializable%3E result = new HashMap%3CString, Serializable%3E();%0A    for ( final String key : value.keySet() )%0A    %7B%0A      final JSONValue attributeValue = value.get( key );%0A      if ( null != attributeValue.isString() )%0A      %7B%0A        result.put( key, attributeValue.isString().stringValue() );%0A      %7D%0A      else if ( null != attributeValue.isNumber() )%0A      %7B%0A        result.put( key, (int) attributeValue.isNumber().doubleValue() );%0A      %7D%0A      else if ( null != attributeValue.isBoolean() )%0A      %7B%0A        result.put( key, attributeValue.isBoolean().booleanValue() );%0A      %7D%0A      else if ( null != attributeValue.isNull() )%0A      %7B%0A        result.put( key, null );%0A      %7D%0A      else%0A      %7B%0A        throw new IllegalStateException( %22Unexpected value for %22 + key );%0A      %7D%0A    %7D%0A    return result;%0A  %7D%0A%0A  @Nonnull%0A  public static JSONArray toArray( final JSONValue value, final String message )%0A  %7B%0A    final JSONArray result = value.isArray();%0A    if ( null == result )%0A    %7B%0A      throw new IllegalStateException( message + %22 is not an array%22 );%0A    %7D%0A    return result;%0A  %7D%0A%0A  public static int toInteger( final JSONValue value, final String message )%0A  %7B%0A    final JSONNumber number = value.isNumber();%0A    if ( null == number )%0A    %7B%0A      throw new IllegalStateException( message + %22 is not a number%22 );%0A    %7D%0A    return (int) number.doubleValue();%0A  %7D%0A%0A  public static double toDouble( final JSONValue value, final String message )%0A  %7B%0A    final JSONNumber number = value.isNumber();%0A    if ( null == number )%0A    %7B%0A      throw new IllegalStateException( message + %22 is not a number%22 );%0A    %7D%0A    return number.doubleValue();%0A  %7D%0A%0A  @Nonnull%0A  public static String toString( final JSONValue value, final String message )%0A  %7B%0A    final JSONString result = value.isString();%0A    if ( null == result )%0A    %7B%0A      throw new IllegalStateException( message + %22 is not a string%22 );%0A    %7D%0A    return result.stringValue();%0A  %7D
 %0A%7D%0A

Remove unused methods