Example usage for com.google.gson JsonElement getAsJsonPrimitive

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

Introduction

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

Prototype

public JsonPrimitive getAsJsonPrimitive() 

Source Link

Document

convenience method to get this element as a JsonPrimitive .

Usage

From source file:edu.wustl.lookingglass.community.api.VersionNumberDeserializer.java

License:Open Source License

@Override
public edu.wustl.lookingglass.common.VersionNumber deserialize(com.google.gson.JsonElement json,
        java.lang.reflect.Type typeOfT, com.google.gson.JsonDeserializationContext context)
        throws com.google.gson.JsonParseException {
    try {//w w w.  j  a  v a  2 s .  c  o  m
        return new edu.wustl.lookingglass.common.VersionNumber(json.getAsJsonPrimitive().getAsString());
    } catch (edu.wustl.lookingglass.common.VersionNumber.MalformedVersionException e) {
        throw new com.google.gson.JsonParseException(e.getMessage());
    }
}

From source file:eu.vranckaert.worktime.web.json.model.DateTimeDeserializer.java

License:Apache License

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Long time = json.getAsJsonPrimitive().getAsLong();
    Date date = new Date(time);
    return date;//from   w ww  .ja  v a 2s  . co  m
}

From source file:ezbake.services.search.utils.BooleanSerializer.java

License:Apache License

@Override
public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    if (arg0.getAsJsonPrimitive().isBoolean()) {
        return arg0.getAsBoolean();
    }//from  w  w w. ja  va  2s.co m
    if (arg0.getAsJsonPrimitive().isString()) {
        return Boolean.parseBoolean(arg0.getAsString());
    } else {
        return arg0.getAsInt() == 1 ? true : false;
    }
}

From source file:fi.vm.sade.organisaatio.business.impl.OrganisaatioTarjonta.java

License:EUPL

private void initGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();

    // Jtetn oid pois sill KoulutusHakutulosV1RDTO sislt kaksi oid kentt
    gsonBuilder.setExclusionStrategies(new ExclusionStrategy() {
        @Override//  ww w.j  av  a  2  s.  c o m
        public boolean shouldSkipField(FieldAttributes fieldAttributes) {
            return "oid".equals(fieldAttributes.getName());
        }

        @Override
        public boolean shouldSkipClass(Class<?> arg0) {
            return false;
        }
    });

    // Rekisteridn adapteri, jolla hoidetaan date tyyppi long arvona
    gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });

    gson = gsonBuilder.create();
}

From source file:fr.inria.atlanmod.discoverer.JsonDiscoverer.java

License:Open Source License

/**
 * Maps json types into ecore types/* ww w .  j a v a  2 s. c  o m*/
 * 
 * @param id
 * @param value
 * @return
 */
private EClassifier mapType(String id, JsonElement value) {
    if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
        return EcorePackage.Literals.ESTRING;
    } else if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) {
        return EcorePackage.Literals.EINT;
    } else if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isBoolean()) {
        return EcorePackage.Literals.EBOOLEAN;
    } else if (value.isJsonArray()) {
        JsonArray arrayValue = value.getAsJsonArray();
        if (arrayValue.size() > 0) {
            EClassifier generalArrayType = mapType(digestId(id), arrayValue.get(0));
            for (int i = 1; i < arrayValue.size(); i++) {
                JsonElement arrayElement = arrayValue.get(i);
                EClassifier arrayType = mapType(digestId(id), arrayElement);
                if (generalArrayType != arrayType) {
                    LOGGER.finer(
                            "[mapType] Detected array multi-typed, using fallback type (String) for " + id);
                    return EcorePackage.Literals.ESTRING;
                }
            }
            return generalArrayType;
        }
    } else if (value.isJsonObject()) {
        return discoverMetaclass(digestId(id), value.getAsJsonObject());
    }
    LOGGER.finer("[mapType] Type not discovererd for " + id);
    return EcorePackage.Literals.ESTRING;
}

From source file:fr.inria.atlanmod.discoverer.JsonInjector.java

License:Open Source License

protected Object digestValue(EAttribute eAttribute, JsonElement value) {
    if (eAttribute.getEType().equals(EcorePackage.Literals.ESTRING)) {
        if (value.isJsonArray() || value.isJsonObject())
            return ""; // TODO Improve discovery process to deal with this
        else//from  w w  w .  j  ava  2  s  . co  m
            return value.getAsJsonPrimitive().getAsString();
    } else if (eAttribute.getEType().equals(EcorePackage.Literals.EINT)) {
        return new Integer(value.getAsJsonPrimitive().getAsNumber().intValue());
    } else if (eAttribute.getEType().equals(EcorePackage.Literals.EBOOLEAN)) {
        return value.getAsJsonPrimitive().getAsBoolean() ? Boolean.TRUE : Boolean.FALSE;
    } else {
        return null;
    }
}

From source file:fr.inria.atlanmod.discoverer.JsonMultiInjector.java

License:Open Source License

protected Object digestValue(JsonElement value) {
    if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
        return value.getAsJsonPrimitive().getAsString();
    } else if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) {
        return new Integer(value.getAsJsonPrimitive().getAsNumber().intValue());
    } else if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isBoolean()) {
        return value.getAsJsonPrimitive().getAsBoolean() ? Boolean.TRUE : Boolean.FALSE;
    } else {/*from  www  . j  a v a  2 s  .  co  m*/
        return null;
    }
}

From source file:fr.zcraft.MultipleInventories.snaphots.ItemStackSnapshot.java

License:Open Source License

/**
 * From a JSON element, constructs a data structure representing
 * the same structure (recursively) using native types.
 *
 * <p>We had to re-implement this to ensure the generated structure to have the
 * right data type (instead of all numbers being doubles) and precision.</p>
 *
 * @param element The json element to be decoded.
 * @return A native data structure (either a {@link Map Map&lt;String, Object&gt;},
 * a {@link List List&lt;Object&gt;}, or a native type) representing the same
 * structure (recursively).//w  ww. j a  v a  2  s  .  c o  m
 *
 * @see #jsonToNative(JsonObject) Converts a json object to an explicit {@link Map}.
 * The JavaDoc also contains explainations on why this is needed.
 */
private static Object jsonToNative(final JsonElement element) {
    if (element.isJsonObject()) {
        return jsonToNative(element.getAsJsonObject());
    } else if (element.isJsonArray()) {
        final List<Object> list = new ArrayList<>();

        element.getAsJsonArray().forEach(listElement -> {
            final Object nativeValue = jsonToNative(listElement);

            if (nativeValue != null) {
                list.add(nativeValue);
            }
        });

        return list;
    } else if (element.isJsonPrimitive()) {
        final JsonPrimitive primitive = element.getAsJsonPrimitive();

        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isString()) {
            return primitive.getAsString();
        } else /* it's a number we yet have to find the type. */
        {
            final BigDecimal number = primitive.getAsBigDecimal();

            try {
                return number.byteValueExact();
            } catch (final ArithmeticException e1) {
                try {
                    return number.shortValueExact();
                } catch (final ArithmeticException e2) {
                    try {
                        return number.intValueExact();
                    } catch (final ArithmeticException e3) {
                        try {
                            return number.longValueExact();
                        } catch (final ArithmeticException e4) {
                            try {
                                return number.doubleValue();
                            } catch (final ArithmeticException | NumberFormatException e5) {
                                return number;
                            }
                        }
                    }
                }
            }
        }
    }

    // Else the element is null.
    return null;
}

From source file:gg.uhc.githubreleasechecker.deserialization.VersionDeserializer.java

License:Open Source License

@Override
public Version deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {//from w w w.jav  a  2 s .  co  m
        return Version.valueOf(json.getAsJsonPrimitive().getAsString());
    } catch (Exception ex) {
        return null;
    }
}

From source file:gov.nasa.jpf.jdart.summaries.json.ExpressionHandler.java

License:Open Source License

@Override
public Expression<?> deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    try {/*from  w w w . j  av  a 2  s  . c  o m*/
        String s = je.getAsJsonPrimitive().getAsString();
        if (s.startsWith("[V]")) {
            return ParserUtil.parseVariable(s.substring(3));
        } else if (s.startsWith("[A]")) {
            return ParserUtil.parseArithmetic(s.substring(3));
        }
        return ParserUtil.parseLogical(s.substring(3));
    } catch (RecognitionException ex) {
        throw new JsonParseException(ex);
    }
}