Example usage for com.google.gwt.json.client JSONValue isNull

List of usage examples for com.google.gwt.json.client JSONValue isNull

Introduction

In this page you can find the example usage for com.google.gwt.json.client JSONValue isNull.

Prototype

public JSONNull isNull() 

Source Link

Document

Returns a non-null reference if this JSONValue is really a JSONNull.

Usage

From source file:com.hiramchirino.restygwt.client.AbstractJsonEncoderDecoder.java

License:Apache License

static public <Type> List<Type> toList(JSONValue value, AbstractJsonEncoderDecoder<Type> encoder) {
    if (value == null || value.isNull() != null) {
        return null;
    }//from  www  .  j a v a 2 s.  com
    JSONArray array = value.isArray();
    if (array == null) {
        throw new DecodingException("Expected a json array, but was given: " + value);
    }

    ArrayList<Type> rc = new ArrayList<Type>(array.size());
    int size = array.size();
    for (int i = 0; i < size; i++) {
        rc.add(encoder.decode(array.get(i)));
    }
    return rc;
}

From source file:com.hiramchirino.restygwt.client.AbstractJsonEncoderDecoder.java

License:Apache License

static public <Type> Set<Type> toSet(JSONValue value, AbstractJsonEncoderDecoder<Type> encoder) {
    if (value == null || value.isNull() != null) {
        return null;
    }//from w ww  . j  a va2 s  .co m
    JSONArray array = value.isArray();
    if (array == null) {
        throw new DecodingException("Expected a json array, but was given: " + value);
    }

    HashSet<Type> rc = new HashSet<Type>(array.size() * 2);
    int size = array.size();
    for (int i = 0; i < size; i++) {
        rc.add(encoder.decode(array.get(i)));
    }
    return rc;
}

From source file:com.hiramchirino.restygwt.client.AbstractJsonEncoderDecoder.java

License:Apache License

static public <Type> Map<String, Type> toMap(JSONValue value, AbstractJsonEncoderDecoder<Type> encoder,
        Style style) {// ww w . j av a2  s  . c  o  m
    if (value == null || value.isNull() != null) {
        return null;
    }

    switch (style) {
    case DEFAULT:
    case SIMPLE: {
        JSONObject object = value.isObject();
        if (object == null) {
            throw new DecodingException("Expected a json object, but was given: " + value);
        }

        HashMap<String, Type> rc = new HashMap<String, Type>(object.size() * 2);
        for (String key : object.keySet()) {
            rc.put(key, encoder.decode(object.get(key)));
        }
        return rc;
    }
    case JETTISON_NATURAL: {
        JSONObject object = value.isObject();
        if (object == null) {
            throw new DecodingException("Expected a json object, but was given: " + value);
        }
        value = object.get("entry");
        if (value == null) {
            throw new DecodingException("Expected an entry array not found");
        }
        JSONArray entries = value.isArray();
        if (entries == null) {
            throw new DecodingException("Expected an entry array, but was given: " + value);
        }

        HashMap<String, Type> rc = new HashMap<String, Type>(object.size() * 2);
        for (int i = 0; i < entries.size(); i++) {
            JSONObject entry = entries.get(i).isObject();
            if (entry == null)
                throw new DecodingException("Expected an entry object, but was given: " + value);
            JSONValue key = entry.get("key");
            if (key == null)
                throw new DecodingException("Expected an entry key field not found");
            JSONString k = key.isString();
            if (k == null)
                throw new DecodingException("Expected an entry key to be a string, but was given: " + value);

            rc.put(k.stringValue(), encoder.decode(entry.get("value")));
        }
        return rc;
    }
    default:
        throw new UnsupportedOperationException("The encoding style is not yet suppored: " + style.name());
    }
}

From source file:com.hunchee.haystack.client.utils.JsonConverter.java

License:Open Source License

protected static List<Object> decodeToList(JSONArray array) {
    List<Object> list = new ArrayList<Object>();
    for (int i = 0; i < array.size(); i++) {
        JSONValue v = array.get(i);
        if (v.isObject() != null) {
            list.add(decode(v.isObject()));
        } else if (v.isArray() != null) {
            list.add(decodeToList(v.isArray()));
        } else if (v.isNull() != null) {
            list.add(null);//from  w  ww .j  a v  a2s  .c o m
        } else if (v.isNumber() != null) {
            list.add(v.isNumber().doubleValue());
        } else if (v.isBoolean() != null) {
            list.add(v.isBoolean().booleanValue());
        } else if (v.isString() != null) {
            //                list.add(decodeValue(v.isString().stringValue()));
            list.add(v.isString().stringValue());
        }
    }

    return list;
}

From source file:com.jythonui.client.util.ParseJ.java

License:Apache License

private static ListOfRows toS(RowIndex rI, String s) {
    ListOfRows r = new ListOfRows();

    JSONObject jo = (JSONObject) JSONParser.parseStrict(s);
    JSONArray a = (JSONArray) jo.get(IConsts.JSONROWLIST);
    for (int i = 0; i < a.size(); i++) {
        RowContent ro = rI.constructRow();
        JSONObject val = (JSONObject) a.get(i);
        for (FieldItem fi : rI.getColList()) {
            JSONValue v = val.get(fi.getId());
            FieldValue vall = new FieldValue();
            if (v.isNull() == JSONNull.getInstance())
                vall.setValue(fi.getFieldType(), null, fi.getAfterDot());
            else/*from w ww  .  j  av a 2 s  . c o  m*/
                switch (fi.getFieldType()) {
                case BOOLEAN:
                    JSONBoolean b = (JSONBoolean) v;
                    vall.setValue(b.booleanValue());
                    break;
                case DATE:
                    break;
                case BIGDECIMAL:
                    JSONNumber jn = (JSONNumber) v;
                    vall.setValue(new BigDecimal(jn.doubleValue()), fi.getAfterDot());
                    break;
                case INT:
                    JSONNumber ji = (JSONNumber) v;
                    vall.setValue(new Double(ji.doubleValue()).intValue());
                    break;
                case LONG:
                    JSONNumber jl = (JSONNumber) v;
                    vall.setValue(new Double(jl.doubleValue()).longValue());
                    break;
                default:
                    JSONString js = (JSONString) v;
                    vall.setValue(js.stringValue());
                    break;
                }
            rI.setRowField(ro, fi.getId(), vall);
        }
        r.addRow(ro);
    }

    return r;
}

From source file:com.kk_electronic.kkportal.core.rpc.JsonEncoder.java

License:Open Source License

public <T> T validate(JSONValue result, T resultType, List<Class<?>> subtypes) throws UnableToDeserialize {
    if (!(result instanceof JSONValue))
        throw new UnableToDeserialize("result must not be null");
    if (subtypes == null || subtypes.size() == 0)
        throw new UnableToDeserialize("Subtypes must not be null or empty");
    if (result.isNull() != null)
        return null;
    JsonValue<T> jsonValue = find(subtypes.remove(0), resultType);
    if (jsonValue == null) {
        throw new UnableToDeserialize("Could not find deserializer for " + subtypes.get(0));
    }/*from w ww . j ava  2s . c o m*/
    return jsonValue.fromJson(result, subtypes, this);
}

From source file:com.kk_electronic.kkportal.core.rpc.jsonformat.JsonDate.java

License:Open Source License

@Override
public Date fromJson(JSONValue jsonValue, List<Class<?>> subtypes, FrameEncoder<JSONValue> encoder)
        throws UnableToDeserialize {
    if (jsonValue.isNull() != null)
        return null;
    if (jsonValue.isNumber() == null)
        throw new UnableToDeserialize("Expected Json Number");
    JSONNumber o = jsonValue.isNumber();
    return new Date((long) (o.doubleValue()));
}

From source file:com.kk_electronic.kkportal.core.rpc.jsonformat.JsonString.java

License:Open Source License

@Override
public String fromJson(JSONValue jsonValue, List<Class<?>> subtypes, FrameEncoder<JSONValue> encoder)
        throws UnableToDeserialize {
    if (jsonValue.isNull() != null)
        return null;
    JSONString jsonString = jsonValue.isString();
    if (jsonString == null)
        throw new UnableToDeserialize("Expected json String");
    return jsonString.stringValue();
}

From source file:com.kk_electronic.kkportal.core.rpc.jsonformat.JsonTimeEntry.java

License:Open Source License

@Override
public TimeEntry fromJson(JSONValue jsonValue, List<Class<?>> subtypes, FrameEncoder<JSONValue> encoder)
        throws UnableToDeserialize {
    if (jsonValue.isNull() != null)
        return null;
    if (jsonValue.isObject() == null)
        throw new UnableToDeserialize("Expected Json Object");
    JSONObject o = jsonValue.isObject();

    Long checkin = null;/*from   ww  w .  j a  va 2  s  .  c om*/
    Long checkout = null;
    Integer taskid = null;
    Integer id = null;
    checkin = encoder.validate(o.get("checkin"), checkin, new Class<?>[] { Long.class });
    checkout = encoder.validate(o.get("checkout"), checkout, new Class<?>[] { Long.class });
    taskid = encoder.validate(o.get("taskId"), taskid, new Class<?>[] { Integer.class });
    id = encoder.validate(o.get("id"), id, new Class<?>[] { Integer.class });
    TimeEntry t = new TimeEntry(checkin, checkout, taskid);
    t.setId(id);
    return t;
}

From source file:com.kk_electronic.kkportal.core.rpc.SimpleEncoder.java

License:Open Source License

private static Object convert(JSONValue y, ObjectHelper objectHelper) {
    if (y.isArray() != null)
        return convert(y.isArray(), objectHelper);
    if (y.isObject() != null)
        return convert(y.isObject(), objectHelper);
    if (y.isBoolean() != null)
        return y.isBoolean().booleanValue();
    if (y.isString() != null)
        return y.isString().stringValue();
    if (y.isNumber() != null)
        return y.isNumber().doubleValue();
    if (y.isNull() != null)
        return null;
    throw new RuntimeException("Could not convert");
}