Example usage for com.google.gson.stream JsonReader nextNull

List of usage examples for com.google.gson.stream JsonReader nextNull

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader nextNull.

Prototype

public void nextNull() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is a literal null.

Usage

From source file:com.miki.webapp.webservicerestful.MikiWsJsonTools.java

/**
 * Cette methode permet de renvoyer un map contenant les attributs(pass en parametre) et leurs valeurs
 * @param json les donnes en format json
 * @param listeAttribut une liste des attributs a chercher dans le Json
 * @return un Map// w  w w  .  j  av a2  s  .co m
 */
public Map<String, String> getObjetLectureJson(String json, List<String> listeAttribut) {
    try {
        if (!listeAttribut.isEmpty()) {
            final JsonReader reader = new JsonReader(new StringReader(json));
            Map<String, String> resultat = new HashMap<>();
            reader.beginObject();
            while (reader.hasNext()) {
                final String name = reader.nextName();
                int iter = 0;
                for (String attribut : listeAttribut) {
                    if (name.equals(attribut)) {
                        if (reader.peek() == JsonToken.NULL) {
                            reader.nextNull();
                            resultat.put(attribut, null);
                        } else {
                            resultat.put(attribut, reader.nextString());
                        }
                    } else {
                        iter++;
                    }
                }
                if (iter == listeAttribut.size()) {
                    reader.skipValue();
                }
            }
            reader.endObject();
            return resultat;
        } else {
            return null;
        }

    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Echec de l'opration");
        return null;
    }
}

From source file:com.nridge.core.io.gson.JSONDocument.java

License:Open Source License

/**
 * Parses an JSON stream and loads it into an internally managed
 * document instance./*from   w w w .  ja v  a2 s . c  o m*/
 *
 * @param aReader Json reader stream instance.
 *
 * @throws IOException I/O related exception.
 */
private void loadDocument(JsonReader aReader, Document aDocument) throws IOException {
    DataBag dataBag;
    JsonToken jsonToken;
    DataField dataField;
    String jsonName, jsonValue, jsonTitle;

    aReader.beginObject();

    jsonToken = aReader.peek();
    while (jsonToken == JsonToken.NAME) {
        jsonName = aReader.nextName();
        jsonTitle = Field.nameToTitle(jsonName);

        jsonToken = aReader.peek();
        switch (jsonToken) {
        case BOOLEAN:
            dataBag = aDocument.getBag();
            dataField = new DataField(jsonName, jsonTitle, aReader.nextBoolean());
            dataBag.add(dataField);
            break;
        case NUMBER:
            dataBag = aDocument.getBag();
            jsonValue = aReader.nextString();
            if (StringUtils.contains(jsonValue, StrUtl.CHAR_DOT))
                dataField = new DataField(jsonName, jsonTitle, Double.valueOf(jsonValue));
            else
                dataField = new DataField(jsonName, jsonTitle, Long.valueOf(jsonValue));
            dataBag.add(dataField);
            break;
        case STRING:
            dataBag = aDocument.getBag();
            jsonValue = aReader.nextString();
            Date dateValue = DatUtl.detectCreateDate(jsonValue);
            if (dateValue != null)
                dataField = new DataField(jsonName, jsonTitle, dateValue);
            else
                dataField = new DataField(Field.Type.Text, jsonName, jsonTitle, jsonValue);
            dataBag.add(dataField);
            break;
        case NULL:
            dataBag = aDocument.getBag();
            aReader.nextNull();
            dataField = new DataField(Field.Type.Text, jsonName, jsonTitle);
            dataBag.add(dataField);
            break;
        case BEGIN_ARRAY:
            aReader.beginArray();
            if (isNextTokenAnObject(aReader)) {
                Document childDocument = new Document(jsonTitle);
                childDocument.setName(jsonName);
                aDocument.addRelationship(jsonTitle, childDocument);
                loadDocumentArray(aReader, childDocument);
            } else {
                dataBag = aDocument.getBag();
                dataField = new DataField(Field.Type.Text, jsonName, jsonTitle);
                jsonToken = aReader.peek();
                while (jsonToken != JsonToken.END_ARRAY) {
                    jsonValue = aReader.nextString();
                    dataField.addValue(jsonValue);
                    jsonToken = aReader.peek();
                }
                dataBag.add(dataField);
            }
            aReader.endArray();
            break;
        case BEGIN_OBJECT:
            Document childDocument = new Document(jsonTitle);
            childDocument.setName(jsonName);
            aDocument.addRelationship(jsonTitle, childDocument);
            loadDocument(aReader, childDocument);
            break;
        default:
            aReader.skipValue();
            break;
        }
        jsonToken = aReader.peek();
    }

    aReader.endObject();
}

From source file:com.onfido.JSON.java

License:Apache License

@Override
public OffsetDateTime read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case NULL:/*w w w.j  a v  a  2s .c o  m*/
        in.nextNull();
        return null;
    default:
        String date = in.nextString();
        if (date.endsWith("+0000")) {
            date = date.substring(0, date.length() - 5) + "Z";
        }

        return OffsetDateTime.parse(date, formatter);
    }
}

From source file:com.onfido.JSON.java

License:Apache License

@Override
public LocalDate read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case NULL://  w w w.ja  v  a 2  s .c  om
        in.nextNull();
        return null;
    default:
        String date = in.nextString();
        return LocalDate.parse(date, formatter);
    }
}

From source file:com.ouyangzn.github.json.DoubleAdapter.java

License:Apache License

@Override
public Number read(JsonReader jsonReader) throws IOException {
    if (jsonReader.peek() == JsonToken.NULL) {
        jsonReader.nextNull();
        return null;
    }//ww  w  . j  a  v  a2s  . com

    try {
        String value = jsonReader.nextString();
        if ("".equals(value)) {
            return null;
        }
        return Double.parseDouble(value);
    } catch (NumberFormatException e) {
        throw new JsonSyntaxException(e);
    }
}

From source file:com.ouyangzn.github.json.IntegerAdapter.java

License:Apache License

@Override
public Number read(JsonReader jsonReader) throws IOException {
    if (jsonReader.peek() == JsonToken.NULL) {
        jsonReader.nextNull();
        return null;
    }//from   www .  j a v  a2  s  . co  m

    try {
        String value = jsonReader.nextString();
        if ("".equals(value)) {
            return null;
        }
        return Integer.parseInt(value);
    } catch (NumberFormatException e) {
        throw new JsonSyntaxException(e);
    }
}

From source file:com.ouyangzn.github.json.LongAdapter.java

License:Apache License

@Override
public Number read(JsonReader jsonReader) throws IOException {
    if (jsonReader.peek() == JsonToken.NULL) {
        jsonReader.nextNull();
        return null;
    }/*from   w  ww.  j  av a 2  s  . co  m*/

    try {
        String value = jsonReader.nextString();
        if ("".equals(value)) {
            return null;
        }
        return Long.parseLong(value);
    } catch (NumberFormatException e) {
        throw new JsonSyntaxException(e);
    }
}

From source file:com.patloew.countries.util.CountryTypeAdapter.java

License:Apache License

@Override
public Country read(JsonReader in) throws IOException {
    try {//from   w  w  w.j ava2  s. c  o  m
        Country country = new Country();

        in.beginObject();

        while (in.hasNext()) {
            String name = in.nextName();

            switch (name) {
            case "alpha2Code": {
                // cannot be null, because it is the primary key
                country.alpha2Code = in.nextString();
                break;
            }
            case "alpha3Code": {
                country.alpha3Code = JsonUtils.readNullSafeString(in);
                break;
            }
            case "name": {
                country.name = JsonUtils.readNullSafeString(in);
                break;
            }
            case "nativeName": {
                country.nativeName = JsonUtils.readNullSafeString(in);
                break;
            }
            case "region": {
                country.region = JsonUtils.readNullSafeString(in);
                break;
            }
            case "capital": {
                country.capital = JsonUtils.readNullSafeString(in);
                break;
            }
            case "population": {
                country.population = JsonUtils.readNullSafeInteger(in);
                break;
            }
            case "latlng": {
                if (in.peek() == JsonToken.NULL) {
                    in.nextNull();
                } else {
                    in.beginArray();
                    if (in.hasNext()) {
                        country.lat = JsonUtils.readNullSafeFloat(in);
                    }
                    if (in.hasNext()) {
                        country.lng = JsonUtils.readNullSafeFloat(in);
                    }
                    in.endArray();
                }
                break;
            }
            case "currencies": {
                country.currencies = RealmStringListTypeAdapter.INSTANCE.read(in);
                break;
            }
            case "borders": {
                country.borders = RealmStringListTypeAdapter.INSTANCE.read(in);
                break;
            }
            case "languages": {
                country.languages = RealmStringListTypeAdapter.INSTANCE.read(in);
                break;
            }
            case "translations": {
                country.translations = RealmStringMapEntryListTypeAdapter.INSTANCE.read(in);
                break;
            }
            default:
                in.skipValue();
                break;
            }
        }

        in.endObject();

        return country;
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static String readNullSafeString(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    } else {//from   ww  w.  j  a va2  s . c  o  m
        return reader.nextString();
    }
}

From source file:com.patloew.countries.util.JsonUtils.java

License:Apache License

public static Long readNullSafeLong(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    } else {/*  www  . ja  v  a  2 s  .  com*/
        return reader.nextLong();
    }
}