Example usage for com.google.gson JsonObject getAsJsonPrimitive

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

Introduction

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

Prototype

public JsonPrimitive getAsJsonPrimitive(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonPrimitive element.

Usage

From source file:com.imaginea.betterdocs.BetterDocsAction.java

License:Apache License

private String getContentsForFile(String file) {
    String esFileQueryJson = getJsonForFileContent(file);
    String esFileResultJson = getESResultJson(esFileQueryJson, esURL + SOURCEFILE_SEARCH);

    JsonReader reader = new JsonReader(new StringReader(esFileResultJson));
    reader.setLenient(true);//from w w w . ja  v  a  2s.c o  m
    JsonElement jsonElement = new JsonParser().parse(reader);
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonObject hitsObject = jsonObject.getAsJsonObject(HITS);
    JsonArray hitsArray = hitsObject.getAsJsonArray(HITS);
    JsonObject hitObject = hitsArray.get(0).getAsJsonObject();
    JsonObject sourceObject = hitObject.getAsJsonObject(SOURCE);
    //Replacing \r as it's treated as bad end of line character
    String fileContent = sourceObject.getAsJsonPrimitive(FILE_CONTENT).getAsString().replaceAll("\r", "");
    return fileContent;
}

From source file:com.imaginea.betterdocs.BetterDocsAction.java

License:Apache License

private static Map<String, String> getFileTokens(String esResultJson) {
    Map<String, String> fileTokenMap = new HashMap<String, String>();
    JsonReader reader = new JsonReader(new StringReader(esResultJson));
    reader.setLenient(true);//from  ww w  . ja v a  2s  .  co m
    JsonElement jsonElement = new JsonParser().parse(reader);
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonObject hitsObject = jsonObject.getAsJsonObject(HITS);
    JsonArray hitsArray = hitsObject.getAsJsonArray(HITS);

    for (JsonElement hits : hitsArray) {
        JsonObject hitObject = hits.getAsJsonObject();
        JsonObject sourceObject = hitObject.getAsJsonObject(SOURCE);
        String fileName = sourceObject.getAsJsonPrimitive(FILE).getAsString();
        String tokens = sourceObject.get(TOKENS).toString();
        fileTokenMap.put(fileName, tokens);
    }
    return fileTokenMap;
}

From source file:com.imaginea.kodebeagle.base.util.ESUtils.java

License:Apache License

public final void fetchContentsAndUpdateMap(final List<String> fileNames) {
    String esFileQueryJson = jsonUtils.getJsonForFileContent(fileNames);
    String esFileResultJson;/*  ww  w.  j  ava  2 s  .  c  om*/
    esFileResultJson = getESResultJson(esFileQueryJson, windowObjects.getEsURL() + SOURCEFILE_SEARCH);
    JsonObject jsonElements = getJsonElements(esFileResultJson);
    if (jsonElements != null) {
        JsonArray hitsArray = getJsonHitsArray(jsonElements);
        for (JsonElement hits : hitsArray) {
            JsonObject hitObject = hits.getAsJsonObject();
            JsonObject sourceObject = hitObject.getAsJsonObject(SOURCE);
            //Replacing \r as it's treated as bad end of line character
            String fileContent = sourceObject.getAsJsonPrimitive(FILE_CONTENT).getAsString();
            String fileName = sourceObject.getAsJsonPrimitive(FILE_NAME).getAsString();
            if (fileName != null && !fileName.isEmpty() && fileContent != null && !fileContent.isEmpty()) {
                windowObjects.getFileNameContentsMap().put(fileName, fileContent.replaceAll("\r", ""));
            }
        }
    }
}

From source file:com.imaginea.kodebeagle.base.util.ESUtils.java

License:Apache License

public final Map<String, String> getFileTokens(final String esResultJson) {
    Map<String, String> fileTokenMap = new HashMap<String, String>();
    final JsonObject hitsObject = getJsonElements(esResultJson);
    if (hitsObject != null) {
        JsonArray hitsArray = getJsonHitsArray(hitsObject);
        resultCount = hitsArray.size();/*from w w w. jav a  2  s  .  co  m*/
        totalHitsCount = getTotalHits(hitsObject);
        for (JsonElement hits : hitsArray) {
            JsonObject hitObject = hits.getAsJsonObject();
            JsonObject sourceObject = hitObject.getAsJsonObject(SOURCE);
            String fileName = sourceObject.getAsJsonPrimitive(FILE).getAsString();
            String tokens = sourceObject.get(TOKENS).toString();
            fileTokenMap.put(fileName, tokens);
        }
    }
    return fileTokenMap;
}

From source file:com.imaginea.kodebeagle.base.util.JSONUtils.java

License:Apache License

public final List<Integer> getLineNumbers(final Collection<String> imports, final String tokens) {
    List<Integer> lineNumbers = new ArrayList<Integer>();
    JsonReader reader = new JsonReader(new StringReader(tokens));
    reader.setLenient(true);//from  w  ww  . java  2 s .  com
    JsonArray tokensArray = new JsonParser().parse(reader).getAsJsonArray();
    for (JsonElement token : tokensArray) {
        JsonObject jObject = token.getAsJsonObject();
        String importName = jObject.getAsJsonPrimitive(IMPORT_EXACT_NAME).getAsString();
        if (imports.contains(importName)) {
            JsonArray lineNumbersArray = jObject.getAsJsonArray(LINE_NUMBERS);
            for (JsonElement lineNumberInfo : lineNumbersArray) {
                lineNumbers.add(lineNumberInfo.getAsJsonObject().getAsJsonPrimitive(LINE_NUMBER).getAsInt());
            }
        }
    }
    return lineNumbers;
}

From source file:com.imaginea.kodebeagle.base.util.SearchUtils.java

License:Apache License

public final Map<String, String> getFileTokens(final String kbResultJson) {
    Map<String, String> fileTokenMap = new HashMap<String, String>();
    final JsonObject hitsObject = getJsonElements(kbResultJson);
    if (hitsObject != null) {
        JsonArray hitsArray = getJsonHitsArray(hitsObject);
        resultCount = hitsArray.size();//from   ww  w. j ava  2  s  . c om
        totalHitsCount = getTotalHits(hitsObject);
        for (JsonElement hits : hitsArray) {
            JsonObject hitObject = hits.getAsJsonObject();
            String fileName = hitObject.getAsJsonPrimitive(FILE_NAME).getAsString();
            String types = hitObject.getAsJsonArray(TYPES).toString();
            fileTokenMap.put(fileName, types);
        }
    }
    return fileTokenMap;
}

From source file:com.imaginea.kodebeagle.base.util.SearchUtils.java

License:Apache License

public final void updateRepoStarsMap(final String kbResultJson) {

    JsonArray hitsArray = getJsonHitsArray(getJsonElements(kbResultJson));
    hitsArray.toString();/* ww w . j  a v a2 s  . c  o  m*/
    for (JsonElement hit : hitsArray) {

        JsonObject hitObject = hit.getAsJsonObject();
        hitObject.toString();
        String score = hitObject.getAsJsonPrimitive(SCORE).getAsString();
        String fileName = hitObject.getAsJsonPrimitive(FILE_NAME).getAsString();
        String repoName = fileName.substring(0, fileName.indexOf(GITHUB_BLOB) - 1);
        windowObjects.getRepoStarsMap().put(repoName, score);

    }
}

From source file:com.imaginea.kodebeagle.base.util.SearchUtils.java

License:Apache License

public final List<Integer> getLineNumbers(final Collection<String> imports, final String tokens) {

    List<Integer> lineNumbers = new ArrayList<Integer>();
    JsonReader reader = new JsonReader(new StringReader(tokens));
    reader.setLenient(true);/*from   www .  java2 s  .  co  m*/
    JsonArray tokensArray = new JsonParser().parse(reader).getAsJsonArray();
    for (JsonElement token : tokensArray) {
        JsonObject jObject = token.getAsJsonObject();
        String typeName = jObject.getAsJsonPrimitive(TYPE_EXACT_NAME).getAsString();
        if (imports.contains(typeName)) {

            JsonArray propsArray = jObject.getAsJsonArray(PROPS);
            for (JsonElement propsInfo : propsArray) {

                JsonArray lineNumbersArray = propsInfo.getAsJsonObject().getAsJsonArray(LINES);
                for (JsonElement lineNumber : lineNumbersArray) {

                    lineNumbers.add(lineNumber.getAsJsonArray().get(0).getAsInt());

                }

            }
        }
    }
    return lineNumbers;
}

From source file:com.kaltura.client.types.ListResponse.java

License:Open Source License

@SuppressWarnings("unchecked")
public ListResponse(JsonObject jsonObject) throws APIException {
    if (jsonObject == null)
        return;//from   w w w . ja v  a 2 s.  co m

    Class<ObjectBase> cls = ObjectBase.class;
    JsonPrimitive objectTypeElement = jsonObject.getAsJsonPrimitive("objectType");
    if (objectTypeElement != null) {
        String objectType = objectTypeElement.getAsString().replaceAll("ListResponse$", "");
        cls = GsonParser.getObjectClass(objectType, cls);
    }

    // set members values:
    totalCount = GsonParser.parseInt(jsonObject.get("totalCount"));
    objects = (List<T>) GsonParser.parseArray(jsonObject.getAsJsonArray("objects"), cls);
}

From source file:com.kurento.demo.crowddetector.CrowdDetectorDemo.java

License:Open Source License

@Override
public ContentCommandResult onContentCommand(final WebRtcContentSession contentSession,
        ContentCommand contentCommand) throws Exception {

    if ("configureFilter".equalsIgnoreCase(contentCommand.getType())) {
        if (crowdDetector == null) {
            MediaPipelineFactory mpf;/*  ww w .jav a 2 s .c om*/
            mpf = contentSession.getMediaPipelineFactory();
            mediaPipeline = mpf.create();
            contentSession.releaseOnTerminate(mediaPipeline);
            webRtcEndpoint = mediaPipeline.newWebRtcEndpoint().build();

            // create PlayerEndpoint
            playerendpoint = mediaPipeline.newPlayerEndpoint("http://files.kurento.org/video/crowd_long.avi")
                    .build();

            JsonArray readedRois = gson.fromJson(contentCommand.getData(), JsonArray.class);

            List<RegionOfInterest> rois = newArrayList();
            for (int j = 0; j < readedRois.size(); j++) {

                JsonObject roi = (JsonObject) readedRois.get(j);

                JsonArray coordenates = (JsonArray) roi.get("coordenates");
                // create structure to configure crowddetector
                List<RelativePoint> points = new ArrayList<RelativePoint>();
                for (int i = 0; i < coordenates.size(); i++) {
                    JsonObject coordenate = (JsonObject) coordenates.get(i);

                    float x = coordenate.getAsJsonPrimitive("x").getAsFloat();
                    float y = coordenate.getAsJsonPrimitive("y").getAsFloat();

                    points.add(new RelativePoint(x, y));
                }

                RegionOfInterestConfig config = new RegionOfInterestConfig();

                config.setFluidityLevelMin(roi.getAsJsonPrimitive("fluidityLevelMin").getAsInt());
                config.setFluidityLevelMed(roi.getAsJsonPrimitive("fluidityLevelMed").getAsInt());
                config.setFluidityLevelMax(roi.getAsJsonPrimitive("fluidityLevelMax").getAsInt());
                config.setFluidityNumFramesToEvent(
                        roi.getAsJsonPrimitive("fluidityNumFramesToEvent").getAsInt());
                config.setOccupancyLevelMin(roi.getAsJsonPrimitive("occupancyLevelMin").getAsInt());
                config.setOccupancyLevelMed(roi.getAsJsonPrimitive("occupancyLevelMed").getAsInt());
                config.setOccupancyLevelMax(roi.getAsJsonPrimitive("occupancyLevelMax").getAsInt());
                config.setOccupancyNumFramesToEvent(
                        roi.getAsJsonPrimitive("occupancyNumFramesToEvent").getAsInt());

                if (roi.getAsJsonPrimitive("sendOpticalFlowEvent").getAsInt() == 0) {
                    config.setSendOpticalFlowEvent(false);
                } else {
                    config.setSendOpticalFlowEvent(true);
                }

                config.setOpticalFlowNumFramesToEvent(
                        roi.getAsJsonPrimitive("opticalFlowNumFramesToEvents").getAsInt());
                config.setOpticalFlowNumFramesToReset(
                        roi.getAsJsonPrimitive("opticalFlowNumFramesToReset").getAsInt());
                config.setOpticalFlowAngleOffset(roi.getAsJsonPrimitive("opticalFlowAngleOffset").getAsInt());

                getLogger().info(config.toString());
                rois.add(new RegionOfInterest(points, config, roi.getAsJsonPrimitive("id").getAsString()));
            }

            crowdDetector = mediaPipeline.newCrowdDetectorFilter(rois).build();

            // connect elements
            playerendpoint.connect(crowdDetector);
            crowdDetector.connect(webRtcEndpoint);
            playerendpoint.play();

            contentSession.publishEvent(new ContentEvent("startConn", "startConn"));
            // addEventListener to crowddetector
            crowdDetector
                    .addCrowdDetectorDirectionListener(new MediaEventListener<CrowdDetectorDirectionEvent>() {
                        @Override
                        public void onEvent(CrowdDetectorDirectionEvent event) {
                            String eventText = "Direction event detect in ROI " + event.getRoiID()
                                    + "with direction " + event.getDirectionAngle();
                            contentSession.publishEvent(new ContentEvent(event.getType(), eventText));
                        }
                    });

            crowdDetector
                    .addCrowdDetectorFluidityListener(new MediaEventListener<CrowdDetectorFluidityEvent>() {
                        @Override
                        public void onEvent(CrowdDetectorFluidityEvent event) {
                            String eventText = "Fluidity event detect in ROI " + event.getRoiID()
                                    + ". Fluidity level = " + event.getFluidityLevel()
                                    + " and fluidity percentage = " + event.getFluidityPercentage();
                            contentSession.publishEvent(new ContentEvent(event.getType(), eventText));
                        }
                    });

            crowdDetector
                    .addCrowdDetectorOccupancyListener(new MediaEventListener<CrowdDetectorOccupancyEvent>() {
                        @Override
                        public void onEvent(CrowdDetectorOccupancyEvent event) {
                            String eventText = "Occupancy event detect in ROI " + event.getRoiID()
                                    + ". Occupancy level = " + event.getOccupancyLevel()
                                    + " and occupancy percentage = " + event.getOccupancyPercentage();
                            contentSession.publishEvent(new ContentEvent(event.getType(), eventText));
                        }
                    });
        }
    }

    return new ContentCommandResult(contentCommand.getData());
}