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

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

Introduction

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

Prototype

public JsonToken peek() throws IOException 

Source Link

Document

Returns the type of the next token without consuming it.

Usage

From source file:com.nridge.ds.solr.SolrConfigJSON.java

License:Open Source License

private String nextValueAsString(JsonReader aReader) throws IOException {
    String valueString = StringUtils.EMPTY;

    JsonToken jsonToken = aReader.peek();
    switch (jsonToken) {
    case BOOLEAN:
        valueString = StrUtl.booleanToString(aReader.nextBoolean());
        break;/*from   w  w w .jav  a 2 s .c o m*/
    case NUMBER:
    case STRING:
        valueString = Solr.cleanContent(aReader.nextString());
        break;
    default:
        aReader.skipValue();
        break;
    }

    return valueString;
}

From source file:com.nridge.ds.solr.SolrConfigJSON.java

License:Open Source License

private void addFieldToDocument(JsonReader aReader, Document aDocument, String aName) throws IOException {
    DataBag childBag;//  w  w  w .  ja  v  a  2 s  .com
    JsonToken jsonToken;
    boolean isArrayArray;
    Document childDocument;
    String jsonName, jsonValue;
    DataTextField dataTextField;

    DataBag dataBag = aDocument.getBag();
    if (isNextTokenAnArray(aReader)) {
        aReader.beginArray();
        dataTextField = new DataTextField(aName, Field.nameToTitle(aName));
        dataTextField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_TRUE);
        jsonToken = aReader.peek();
        if (jsonToken == JsonToken.BEGIN_ARRAY) {
            isArrayArray = true;
            aReader.beginArray();
            jsonToken = aReader.peek();
        } else
            isArrayArray = false;
        while (jsonToken != JsonToken.END_ARRAY) {
            jsonValue = nextValueAsString(aReader);
            dataTextField.addValue(jsonValue);
            jsonToken = aReader.peek();
        }
        aReader.endArray();
        if (isArrayArray)
            aReader.endArray();
        dataBag.add(dataTextField);
    } else if (isNextTokenAnObject(aReader)) {
        childDocument = new Document(aName);
        childDocument.setName(aName);
        childBag = childDocument.getBag();

        aReader.beginObject();
        jsonToken = aReader.peek();
        while (jsonToken != JsonToken.END_OBJECT) {
            jsonName = aReader.nextName();
            jsonValue = nextValueAsString(aReader);
            addBagTextField(childBag, jsonName, jsonValue);
            jsonToken = aReader.peek();
        }
        aReader.endObject();
        aDocument.addRelationship(aName, childDocument);
    } else {
        jsonValue = nextValueAsString(aReader);
        addBagTextField(dataBag, aName, jsonValue);
    }
}

From source file:com.nridge.ds.solr.SolrConfigJSON.java

License:Open Source License

private void addObjectToDocument(JsonReader aReader, Document aParentDocument, String aType, String aName)
        throws IOException {
    String jsonValue;//from   www. j  a  v a  2  s.c om
    JsonToken jsonToken;
    boolean isArrayArray;
    Document childDocument;

    DataBag dataBag = aParentDocument.getBag();
    if (isNextTokenAnArray(aReader)) {
        aReader.beginArray();
        jsonToken = aReader.peek();
        if (jsonToken == JsonToken.BEGIN_ARRAY) {
            isArrayArray = true;
            aReader.beginArray();
            jsonToken = aReader.peek();
        } else
            isArrayArray = false;
        while (jsonToken != JsonToken.END_ARRAY) {
            childDocument = new Document(aType);
            childDocument.setName(aName);
            aReader.beginObject();
            while (aReader.hasNext())
                addFieldToDocument(aReader, childDocument);
            aReader.endObject();
            aParentDocument.addRelationship(aType, childDocument);
            jsonToken = aReader.peek();
        }
        aReader.endArray();
        if (isArrayArray)
            aReader.endArray();
    } else if (isNextTokenAnObject(aReader)) {
        childDocument = new Document(aType);
        childDocument.setName(aName);
        aReader.beginObject();
        while (aReader.hasNext())
            addFieldToDocument(aReader, childDocument);
        aReader.endObject();
        aParentDocument.addRelationship(aType, childDocument);
    } else {
        jsonValue = nextValueAsString(aReader);
        addBagTextField(dataBag, aName, jsonValue);
    }
}

From source file:com.nridge.ds.solr.SolrConfigJSON.java

License:Open Source License

private void populateSearchComponent(JsonReader aReader, Document aCfgDocument, String aDocType)
        throws IOException {
    DataBag scBag;/*from ww w .  j  a v  a 2  s .c om*/
    Document scDocument;
    JsonToken jsonToken;
    boolean isArrayArray;
    Document childDocument;
    String jsonName, jsonValue;

    Document searchComponentDocument = new Document(aDocType);
    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        scDocument = new Document(Solr.RESPONSE_CONFIG_SEARCH_COMPONENT);
        scDocument.setName(jsonName);
        scBag = scDocument.getBag();
        DataTextField operationField = new DataTextField(Solr.CONFIG_OPERATION_FIELD_NAME,
                Field.nameToTitle(Solr.CONFIG_OPERATION_FIELD_NAME));
        operationField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_FALSE);
        scBag.add(operationField);

        aReader.beginObject();
        while (aReader.hasNext()) {
            jsonName = aReader.nextName();
            if (isNextTokenAnArray(aReader)) {
                aReader.beginArray();
                jsonToken = aReader.peek();
                if (jsonToken == JsonToken.BEGIN_ARRAY) {
                    isArrayArray = true;
                    aReader.beginArray();
                    jsonToken = aReader.peek();
                } else
                    isArrayArray = false;
                while (jsonToken != JsonToken.END_ARRAY) {
                    childDocument = new Document(Field.nameToTitle(jsonName));
                    childDocument.setName(jsonName);
                    aReader.beginObject();
                    while (aReader.hasNext())
                        addFieldToDocument(aReader, childDocument);
                    aReader.endObject();
                    scDocument.addRelationship(Field.nameToTitle(jsonName), childDocument);
                    jsonToken = aReader.peek();
                }
                aReader.endArray();
                if (isArrayArray)
                    aReader.endArray();
            } else if (isNextTokenAnObject(aReader)) {
                childDocument = new Document(Field.nameToTitle(jsonName));
                childDocument.setName(jsonName);
                aReader.beginObject();
                while (aReader.hasNext())
                    addFieldToDocument(aReader, childDocument);
                aReader.endObject();
                scDocument.addRelationship(Field.nameToTitle(jsonName), childDocument);
            } else {
                jsonValue = nextValueAsString(aReader);
                addBagTextField(scBag, jsonName, jsonValue);
            }
        }
        aReader.endObject();
        assignSearchComponentType(scDocument);
        searchComponentDocument.addRelationship(scDocument.getType(), scDocument);
    }

    aCfgDocument.addRelationship(aDocType, searchComponentDocument);
}

From source file:com.nridge.ds.solr.SolrSchemaJSON.java

License:Open Source License

private String nextValueAsString(JsonReader aReader) throws IOException {
    String valueString = StringUtils.EMPTY;

    JsonToken jsonToken = aReader.peek();
    switch (jsonToken) {
    case BOOLEAN:
        valueString = StrUtl.booleanToString(aReader.nextBoolean());
        break;//from   w  w  w .  j  a v a  2  s  . co m
    case NUMBER:
    case STRING:
        valueString = aReader.nextString();
        break;
    default:
        aReader.skipValue();
        break;
    }

    return valueString;
}

From source file:com.onfido.JSON.java

License:Apache License

@Override
public OffsetDateTime read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case NULL:/*from  w  w  w .j a va2 s . c  om*/
        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:/*from w w w . j  a  v a2s .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();//from  w w w.ja  v a  2 s.c om
        return null;
    }

    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();//  w w w  .j a va  2 s .  c  o m
        return null;
    }

    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();//from   w  w w. j  a v  a2s .c o m
        return null;
    }

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