Example usage for com.google.gson JsonElement isJsonNull

List of usage examples for com.google.gson JsonElement isJsonNull

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonNull.

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

From source file:com.continuuity.weave.internal.json.LocalFileCodec.java

License:Open Source License

@Override
public LocalFile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();

    String name = jsonObj.get("name").getAsString();
    URI uri = URI.create(jsonObj.get("uri").getAsString());
    long lastModified = jsonObj.get("lastModified").getAsLong();
    long size = jsonObj.get("size").getAsLong();
    boolean archive = jsonObj.get("archive").getAsBoolean();
    JsonElement pattern = jsonObj.get("pattern");

    return new DefaultLocalFile(name, uri, lastModified, size, archive,
            (pattern == null || pattern.isJsonNull()) ? null : pattern.getAsString());
}

From source file:com.crypticbit.diff.demo.swing.contacts.JsonJTreeNode.java

License:Apache License

/**
 * @param fieldName//from  www  . j  av  a  2  s .  c  o m
 *            - name of field if applicable or null
 * @param index
 *            - index of element in the array or -1 if not part of an array
 * @param jsonElement
 *            - element to represent
 */
public JsonJTreeNode(String fieldName, int index, JsonElement jsonElement) {
    this.index = index;
    this.fieldName = fieldName;
    if (jsonElement.isJsonArray()) {
        this.dataType = DataType.ARRAY;
        this.value = jsonElement.toString();
        populateChildren(jsonElement);
    } else if (jsonElement.isJsonObject()) {
        this.dataType = DataType.OBJECT;
        this.value = jsonElement.toString();
        populateChildren(jsonElement);
    } else if (jsonElement.isJsonPrimitive()) {
        this.dataType = DataType.VALUE;
        this.value = jsonElement.toString();
    } else if (jsonElement.isJsonNull()) {
        this.dataType = DataType.VALUE;
        this.value = jsonElement.toString();
    } else {
        throw new IllegalArgumentException("jsonElement is an unknown element type.");
    }

}

From source file:com.crypticbit.diff.demo.swing.JSONJTreeNode.java

License:Apache License

/**
 * @param fieldName/*from   w  w w  .  j  a va 2  s .  com*/
 *            - name of field if applicable or null
 * @param index
 *            - index of element in the array or -1 if not part of an array
 * @param jsonElement
 *            - element to represent
 */
public JSONJTreeNode(String fieldName, int index, JsonElement jsonElement) {
    this.index = index;
    this.fieldName = fieldName;
    if (jsonElement.isJsonArray()) {
        this.dataType = DataType.ARRAY;
        this.value = jsonElement.toString();
        populateChildren(jsonElement);
    } else if (jsonElement.isJsonObject()) {
        this.dataType = DataType.OBJECT;
        this.value = jsonElement.toString();
        populateChildren(jsonElement);
    } else if (jsonElement.isJsonPrimitive()) {
        this.dataType = DataType.VALUE;
        this.value = jsonElement.toString();
    } else if (jsonElement.isJsonNull()) {
        this.dataType = DataType.VALUE;
        this.value = jsonElement.toString();
    } else {
        throw new IllegalArgumentException("jsonElement is an unknown element type.");
    }

}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Reads and parses each field element.//from  w w  w .j  av a 2  s . co  m
 * @param fieldName
 *            the field name
 * @param elem
 *            a json element represents a record object
 * @return the field object created
 * @throws IOException
 */
private Field readField(String fieldName, JsonElement fieldElem) throws IOException {

    Field field = null;

    if (!fieldElem.isJsonObject())
        return null;
    JsonObject obj = fieldElem.getAsJsonObject();

    FieldType type = FieldType.getEnum(obj.get("type").getAsString());
    JsonElement element = obj.get("value");

    Object object = null;
    String strVal = null;
    if (type == null || element == null)
        return null;

    if (!element.isJsonNull()) {
        switch (type) {
        case SINGLE_LINE_TEXT:
        case CALC:
        case MULTI_LINE_TEXT:
        case RICH_TEXT:
        case RADIO_BUTTON:
        case DROP_DOWN:
        case LINK:
        case STATUS:
        case RECORD_NUMBER:
        case NUMBER:
            object = element.getAsString();
            break;
        case __ID__:
        case __REVISION__:
            strVal = element.getAsString();
            try {
                object = Long.valueOf(strVal);
            } catch (NumberFormatException e) {
            }
            break;
        case DATE:
        case TIME:
        case DATETIME:
        case CREATED_TIME:
        case UPDATED_TIME:
            object = element.getAsString();
            break;
        case CHECK_BOX:
        case MULTI_SELECT:
        case CATEGORY:
            object = jsonToStringArray(element);
            break;
        case FILE:
            object = jsonToFileArray(element);
            break;
        case CREATOR:
        case MODIFIER:
            if (element.isJsonObject()) {
                Gson gson = new Gson();
                object = gson.fromJson(element, UserDto.class);
            }
            break;
        case USER_SELECT:
        case STATUS_ASSIGNEE:
            object = jsonToUserArray(element);
            break;
        case SUBTABLE:
            object = jsonToSubtable(element);
            break;
        }
    }
    field = new Field(fieldName, type, object);

    return field;
}

From source file:com.diversityarrays.dalclient.DalUtil.java

License:Open Source License

static public DalResponseRecord createFrom(String requestUrl, String tagName, JsonObject input) {
    DalResponseRecord result = new DalResponseRecord(requestUrl, tagName);
    for (Map.Entry<String, JsonElement> entry : input.entrySet()) {
        String key = entry.getKey();
        JsonElement value = entry.getValue();
        if (value == null || value.isJsonNull()) {
            result.rowdata.put(key, "");
        } else if (value.isJsonArray()) {
            JsonArray array = (JsonArray) value;
            int count = 0;
            for (JsonElement elt : array) {
                if (elt != null && elt.isJsonObject()) {
                    JsonObject child = (JsonObject) elt;
                    Map<String, String> childMap = asRowdata(child);
                    if (childMap != null) {
                        result.addNestedData(key, childMap);
                    }/*from   ww w .j  av a  2  s  . c o m*/
                } else {
                    result.warnings.add(String.format("unexpected value-type for '%s'[%d] :%s", //$NON-NLS-1$
                            key, count, elt == null ? "null" : elt.getClass().getName()));
                }
                ++count;
            }
        } else if (value.isJsonPrimitive()) {
            JsonPrimitive prim = (JsonPrimitive) value;
            result.rowdata.put(key, prim.getAsString());
        } else if (value.isJsonObject()) {
            // ?? perhaps
            Map<String, String> childMap = asRowdata((JsonObject) value);
            if (childMap != null) {
                result.addNestedData(key, childMap);
            }
        } else {
            result.warnings.add(String.format("unexpected value-type for '%s' :%s", //$NON-NLS-1$
                    key, value.getClass().getName()));
        }
    }
    return result;
}

From source file:com.diversityarrays.dalclient.DalUtil.java

License:Open Source License

/**
 * Convert the the input into a Map&lt;String,String&gt;.
 * @param input/*from w  w  w .  jav a2  s .  co  m*/
 * @return a Map&lt;String,String&gt;
 */
static private Map<String, String> asRowdata(JsonObject input) {
    Map<String, String> result = null;
    Set<Map.Entry<String, JsonElement>> entrySet = input.entrySet();
    if (entrySet != null && !entrySet.isEmpty()) {
        for (Map.Entry<String, JsonElement> entry : entrySet) {
            if (result == null) {
                result = new LinkedHashMap<>();
            }
            String key = entry.getKey();
            JsonElement value = entry.getValue();
            if (value == null || value.isJsonNull()) {
                result.put(key, "");
            } else {
                result.put(key, value.getAsString());
            }
        }
    }
    return result;
}

From source file:com.edgebox.eacds.net.CDPostResponse.java

License:Open Source License

public static CDPostResponse build(String json) {
    CDPostResponse rt = new CDPostResponse();
    JsonElement e = new JsonParser().parse(json);
    rt.json = e.getAsJsonObject();/*from w  w w. j a  va  2s  . com*/
    rt.success = rt.json.get("success").getAsBoolean();
    rt.message = rt.json.get("message").getAsString();
    rt.errorTrace = rt.json.get("errorTrace").getAsString();

    try {
        rt.data = rt.json.getAsJsonObject("data").getAsJsonObject();
    } catch (java.lang.ClassCastException ex) {
        rt.data = rt.json.get("data").getAsString();
    }

    JsonElement dt = rt.json.get("data");
    if (dt.isJsonNull()) {
        rt.data = null;
    } else if (dt.isJsonObject()) {
        rt.data = dt.getAsJsonObject();
    } else {
        rt.data = dt.getAsJsonPrimitive();
    }
    return rt;
}

From source file:com.facebook.buck.json.RawParser.java

License:Apache License

/**
 * @return One of: String, Boolean, Long, Number, List<Object>, Map<String, Object>.
 *///w  w w  . j  a  v  a 2  s .  c om
@Nullable
@VisibleForTesting
static Object toRawTypes(JsonElement json) {
    // Cases are ordered from most common to least common.
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            return interner.intern(primitive.getAsString());
        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isNumber()) {
            Number number = primitive.getAsNumber();
            // Number is likely an instance of class com.google.gson.internal.LazilyParsedNumber.
            if (number.longValue() == number.doubleValue()) {
                return number.longValue();
            } else {
                return number;
            }
        } else {
            throw new IllegalStateException("Unknown primitive type: " + primitive);
        }
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        List<Object> out = Lists.newArrayListWithCapacity(array.size());
        for (JsonElement item : array) {
            out.add(toRawTypes(item));
        }
        return out;
    } else if (json.isJsonObject()) {
        Map<String, Object> out = new LinkedHashMap<>(json.getAsJsonObject().entrySet().size());
        for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
            // On a large project, without invoking intern(), we have seen `buck targets` OOM. When this
            // happened, according to the .hprof file generated using -XX:+HeapDumpOnOutOfMemoryError,
            // 39.6% of the memory was spent on char[] objects while 14.5% was spent on Strings.
            // (Another 10.5% was spent on java.util.HashMap$Entry.) Introducing intern() stopped the
            // OOM from happening.
            out.put(interner.intern(entry.getKey()), toRawTypes(entry.getValue()));
        }
        return out;
    } else if (json.isJsonNull()) {
        return null;
    } else {
        throw new IllegalStateException("Unknown type: " + json);
    }
}

From source file:com.flipkart.android.proteus.builder.DataAndViewParsingLayoutBuilder.java

License:Apache License

@Override
protected ProteusView onUnknownViewEncountered(String type, ViewGroup parent, JsonObject source,
        JsonObject data, int index, Styles styles) {
    JsonElement element = null;
    if (layouts != null) {
        element = layouts.get(type);//from w w  w.ja  va 2 s.  c o m
    }
    if (element != null && !element.isJsonNull()) {
        JsonObject layout = element.getAsJsonObject();
        layout = Utils.mergeLayouts(layout, source);
        ProteusView view = build(parent, layout, data, index, styles);
        onViewBuiltFromViewProvider(view, type, parent, index);
        return view;
    }
    return super.onUnknownViewEncountered(type, parent, source, data, index, styles);
}

From source file:com.flipkart.android.proteus.builder.DataParsingLayoutBuilder.java

License:Apache License

@Override
protected ProteusViewManager createViewManager(LayoutHandler handler, View parent, JsonObject layout,
        JsonObject data, int index, Styles styles) {
    ProteusViewManagerImpl viewManager = new ProteusViewManagerImpl();
    DataContext dataContext, parentDataContext = null;
    JsonElement scope = layout.get(ProteusConstants.DATA_CONTEXT);

    if (parent instanceof ProteusView) {
        parentDataContext = ((ProteusView) parent).getViewManager().getDataContext();
    }//  ww  w. j a v  a2s.c o  m

    if (scope == null || scope.isJsonNull()) {
        if (parentDataContext != null) {
            dataContext = new DataContext(parentDataContext);
        } else {
            dataContext = new DataContext();
            dataContext.setData(data);
            dataContext.setIndex(index);
        }
    } else {
        if (parentDataContext != null) {
            dataContext = parentDataContext.createChildDataContext(scope.getAsJsonObject(), index);
        } else {
            dataContext = new DataContext();
            dataContext.setData(data);
            dataContext = dataContext.createChildDataContext(scope.getAsJsonObject(), index);
        }
    }

    viewManager.setLayout(layout);
    viewManager.setDataContext(dataContext);
    viewManager.setStyles(styles);
    viewManager.setLayoutBuilder(this);
    viewManager.setLayoutHandler(handler);

    return viewManager;
}